Makefile 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. CLOSURE_DIR=closure-compiler
  2. CLOSURE=$(CLOSURE_DIR)/compiler.jar
  3. NASM_TEST_DIR=./tests/nasm
  4. INSTRUCTION_TABLES=src/rust/gen/jit.rs src/rust/gen/jit0f.rs \
  5. src/rust/gen/interpreter.rs src/rust/gen/interpreter0f.rs \
  6. src/rust/gen/analyzer.rs src/rust/gen/analyzer0f.rs \
  7. # Only the dependencies common to both generate_{jit,interpreter}.js
  8. GEN_DEPENDENCIES=$(filter-out gen/generate_interpreter.js gen/generate_jit.js gen/generate_analyzer.js, $(wildcard gen/*.js))
  9. JIT_DEPENDENCIES=$(GEN_DEPENDENCIES) gen/generate_jit.js
  10. INTERPRETER_DEPENDENCIES=$(GEN_DEPENDENCIES) gen/generate_interpreter.js
  11. ANALYZER_DEPENDENCIES=$(GEN_DEPENDENCIES) gen/generate_analyzer.js
  12. STRIP_DEBUG_FLAG=
  13. ifeq ($(STRIP_DEBUG),true)
  14. STRIP_DEBUG_FLAG=--v86-strip-debug
  15. endif
  16. WASM_OPT ?= false
  17. default: build/v86-debug.wasm
  18. all: build/v86_all.js build/libv86.js build/v86.wasm
  19. all-debug: build/libv86-debug.js build/v86-debug.wasm
  20. browser: build/v86_all.js
  21. # Used for nodejs builds and in order to profile code.
  22. # `debug` gives identifiers a readable name, make sure it doesn't have any side effects.
  23. CLOSURE_READABLE=--formatting PRETTY_PRINT --debug
  24. CLOSURE_SOURCE_MAP=\
  25. --source_map_format V3\
  26. --create_source_map '%outname%.map'
  27. CLOSURE_FLAGS=\
  28. --generate_exports\
  29. --externs src/externs.js\
  30. --warning_level VERBOSE\
  31. --jscomp_error accessControls\
  32. --jscomp_error checkRegExp\
  33. --jscomp_error checkTypes\
  34. --jscomp_error checkVars\
  35. --jscomp_error conformanceViolations\
  36. --jscomp_error const\
  37. --jscomp_error constantProperty\
  38. --jscomp_error deprecated\
  39. --jscomp_error deprecatedAnnotations\
  40. --jscomp_error duplicateMessage\
  41. --jscomp_error es5Strict\
  42. --jscomp_error externsValidation\
  43. --jscomp_error globalThis\
  44. --jscomp_error invalidCasts\
  45. --jscomp_error misplacedTypeAnnotation\
  46. --jscomp_error missingProperties\
  47. --jscomp_error missingReturn\
  48. --jscomp_error msgDescriptions\
  49. --jscomp_error nonStandardJsDocs\
  50. --jscomp_error suspiciousCode\
  51. --jscomp_error strictModuleDepCheck\
  52. --jscomp_error typeInvalidation\
  53. --jscomp_error undefinedVars\
  54. --jscomp_error unknownDefines\
  55. --jscomp_error visibility\
  56. --use_types_for_optimization\
  57. --summary_detail_level 3\
  58. --language_in ECMASCRIPT_2017\
  59. --language_out ECMASCRIPT_2017
  60. CARGO_FLAGS_SAFE=\
  61. --target wasm32-unknown-unknown \
  62. -- \
  63. -C linker=tools/rust-lld-wrapper \
  64. -C link-args="--import-table --global-base=4096 $(STRIP_DEBUG_FLAG)" \
  65. -C link-args="build/softfloat.o" \
  66. -C link-args="build/zstddeclib.o" \
  67. --verbose
  68. CARGO_FLAGS=$(CARGO_FLAGS_SAFE) -C target-feature=+bulk-memory
  69. CORE_FILES=const.js config.js io.js main.js lib.js buffer.js ide.js pci.js floppy.js \
  70. memory.js dma.js pit.js vga.js ps2.js pic.js rtc.js uart.js hpet.js \
  71. acpi.js apic.js ioapic.js \
  72. state.js ne2k.js sb16.js virtio.js bus.js log.js \
  73. cpu.js debug.js \
  74. elf.js kernel.js
  75. LIB_FILES=9p.js filesystem.js jor1k.js marshall.js utf8.js
  76. BROWSER_FILES=screen.js keyboard.js mouse.js speaker.js serial.js \
  77. network.js starter.js worker_bus.js dummy_screen.js \
  78. print_stats.js filestorage.js
  79. RUST_FILES=$(shell find src/rust/ -name '*.rs') \
  80. src/rust/gen/interpreter.rs src/rust/gen/interpreter0f.rs \
  81. src/rust/gen/jit.rs src/rust/gen/jit0f.rs \
  82. src/rust/gen/analyzer.rs src/rust/gen/analyzer0f.rs
  83. CORE_FILES:=$(addprefix src/,$(CORE_FILES))
  84. LIB_FILES:=$(addprefix lib/,$(LIB_FILES))
  85. BROWSER_FILES:=$(addprefix src/browser/,$(BROWSER_FILES))
  86. build/v86_all.js: $(CLOSURE) src/*.js src/browser/*.js lib/*.js
  87. mkdir -p build
  88. -ls -lh build/v86_all.js
  89. java -jar $(CLOSURE) \
  90. --js_output_file build/v86_all.js\
  91. --define=DEBUG=false\
  92. $(CLOSURE_SOURCE_MAP)\
  93. $(CLOSURE_FLAGS)\
  94. --compilation_level ADVANCED\
  95. --js $(CORE_FILES)\
  96. --js $(LIB_FILES)\
  97. --js $(BROWSER_FILES)\
  98. --js src/browser/main.js
  99. ls -lh build/v86_all.js
  100. build/v86_all_debug.js: $(CLOSURE) src/*.js src/browser/*.js lib/*.js
  101. mkdir -p build
  102. java -jar $(CLOSURE) \
  103. --js_output_file build/v86_all_debug.js\
  104. --define=DEBUG=true\
  105. $(CLOSURE_SOURCE_MAP)\
  106. $(CLOSURE_FLAGS)\
  107. --compilation_level ADVANCED\
  108. --js $(CORE_FILES)\
  109. --js $(LIB_FILES)\
  110. --js $(BROWSER_FILES)\
  111. --js src/browser/main.js
  112. build/libv86.js: $(CLOSURE) src/*.js lib/*.js src/browser/*.js
  113. mkdir -p build
  114. -ls -lh build/libv86.js
  115. java -jar $(CLOSURE) \
  116. --js_output_file build/libv86.js\
  117. --define=DEBUG=false\
  118. $(CLOSURE_FLAGS)\
  119. --compilation_level SIMPLE\
  120. --jscomp_off=missingProperties\
  121. --output_wrapper ';(function(){%output%}).call(this);'\
  122. --js $(CORE_FILES)\
  123. --js $(BROWSER_FILES)\
  124. --js $(LIB_FILES)
  125. ls -lh build/libv86.js
  126. build/libv86-debug.js: $(CLOSURE) src/*.js lib/*.js src/browser/*.js
  127. mkdir -p build
  128. java -jar $(CLOSURE) \
  129. --js_output_file build/libv86-debug.js\
  130. --define=DEBUG=true\
  131. $(CLOSURE_FLAGS)\
  132. $(CLOSURE_READABLE)\
  133. --compilation_level SIMPLE\
  134. --jscomp_off=missingProperties\
  135. --output_wrapper ';(function(){%output%}).call(this);'\
  136. --js $(CORE_FILES)\
  137. --js $(BROWSER_FILES)\
  138. --js $(LIB_FILES)
  139. src/rust/gen/jit.rs: $(JIT_DEPENDENCIES)
  140. ./gen/generate_jit.js --output-dir build/ --table jit
  141. src/rust/gen/jit0f.rs: $(JIT_DEPENDENCIES)
  142. ./gen/generate_jit.js --output-dir build/ --table jit0f
  143. src/rust/gen/interpreter.rs: $(INTERPRETER_DEPENDENCIES)
  144. ./gen/generate_interpreter.js --output-dir build/ --table interpreter
  145. src/rust/gen/interpreter0f.rs: $(INTERPRETER_DEPENDENCIES)
  146. ./gen/generate_interpreter.js --output-dir build/ --table interpreter0f
  147. src/rust/gen/analyzer.rs: $(ANALYZER_DEPENDENCIES)
  148. ./gen/generate_analyzer.js --output-dir build/ --table analyzer
  149. src/rust/gen/analyzer0f.rs: $(ANALYZER_DEPENDENCIES)
  150. ./gen/generate_analyzer.js --output-dir build/ --table analyzer0f
  151. build/v86.wasm: $(RUST_FILES) build/softfloat.o build/zstddeclib.o Cargo.toml
  152. mkdir -p build/
  153. -BLOCK_SIZE=K ls -l build/v86.wasm
  154. cargo rustc --release $(CARGO_FLAGS)
  155. cp build/wasm32-unknown-unknown/release/v86.wasm build/v86.wasm
  156. -$(WASM_OPT) && wasm-opt -O2 --strip-debug build/v86.wasm -o build/v86.wasm
  157. BLOCK_SIZE=K ls -l build/v86.wasm
  158. build/v86-debug.wasm: $(RUST_FILES) build/softfloat.o build/zstddeclib.o Cargo.toml
  159. mkdir -p build/
  160. -BLOCK_SIZE=K ls -l build/v86-debug.wasm
  161. cargo rustc $(CARGO_FLAGS)
  162. cp build/wasm32-unknown-unknown/debug/v86.wasm build/v86-debug.wasm
  163. BLOCK_SIZE=K ls -l build/v86-debug.wasm
  164. build/v86-fallback.wasm: $(RUST_FILES) build/softfloat.o build/zstddeclib.o Cargo.toml
  165. mkdir -p build/
  166. cargo rustc --release $(CARGO_FLAGS_SAFE)
  167. cp build/wasm32-unknown-unknown/release/v86.wasm build/v86-fallback.wasm || true
  168. debug-with-profiler: $(RUST_FILES) build/softfloat.o build/zstddeclib.o Cargo.toml
  169. mkdir -p build/
  170. cargo rustc --features profiler $(CARGO_FLAGS)
  171. cp build/wasm32-unknown-unknown/debug/v86.wasm build/v86-debug.wasm || true
  172. with-profiler: $(RUST_FILES) build/softfloat.o build/zstddeclib.o Cargo.toml
  173. mkdir -p build/
  174. cargo rustc --release --features profiler $(CARGO_FLAGS)
  175. cp build/wasm32-unknown-unknown/release/v86.wasm build/v86.wasm || true
  176. build/softfloat.o: lib/softfloat/softfloat.c
  177. mkdir -p build
  178. clang -c -Wall \
  179. --target=wasm32 -O3 -flto -nostdlib -fvisibility=hidden -ffunction-sections -fdata-sections \
  180. -DSOFTFLOAT_FAST_INT64 -DINLINE_LEVEL=5 -DSOFTFLOAT_FAST_DIV32TO16 -DSOFTFLOAT_FAST_DIV64TO32 \
  181. -o build/softfloat.o \
  182. lib/softfloat/softfloat.c
  183. build/zstddeclib.o: lib/zstd/zstddeclib.c
  184. mkdir -p build
  185. clang -c -Wall \
  186. --target=wasm32 -O3 -flto -nostdlib -fvisibility=hidden -ffunction-sections -fdata-sections \
  187. -DZSTDLIB_VISIBILITY="" \
  188. -o build/zstddeclib.o \
  189. lib/zstd/zstddeclib.c
  190. clean:
  191. -rm build/libv86.js
  192. -rm build/libv86-debug.js
  193. -rm build/v86_all.js
  194. -rm build/v86.wasm
  195. -rm build/v86-debug.wasm
  196. -rm $(INSTRUCTION_TABLES)
  197. -rm build/*.map
  198. -rm build/*.wast
  199. -rm build/*.o
  200. $(MAKE) -C $(NASM_TEST_DIR) clean
  201. run:
  202. python3 -m http.server 2> /dev/null
  203. update_version:
  204. set -e ;\
  205. COMMIT=`git log --format="%h" -n 1` ;\
  206. DATE=`git log --date="format:%b %e, %Y %H:%m" --format="%cd" -n 1` ;\
  207. SEARCH='<code>Version: <a href="https://github.com/copy/v86/commits/[a-f0-9]\+">[a-f0-9]\+</a> ([^(]\+)</code>' ;\
  208. REPLACE='<code>Version: <a href="https://github.com/copy/v86/commits/'$$COMMIT'">'$$COMMIT'</a> ('$$DATE')</code>' ;\
  209. sed -i "s@$$SEARCH@$$REPLACE@g" index.html ;\
  210. grep $$COMMIT index.html
  211. $(CLOSURE):
  212. mkdir -p $(CLOSURE_DIR)
  213. # don't upgrade until https://github.com/google/closure-compiler/issues/3972 is fixed
  214. wget -nv -O $(CLOSURE) https://repo1.maven.org/maven2/com/google/javascript/closure-compiler/v20210601/closure-compiler-v20210601.jar
  215. build/integration-test-fs/fs.json:
  216. mkdir -p build/integration-test-fs/flat
  217. cp images/buildroot-bzimage.bin build/integration-test-fs/bzImage
  218. touch build/integration-test-fs/initrd
  219. cd build/integration-test-fs && tar cfv fs.tar bzImage initrd
  220. ./tools/fs2json.py build/integration-test-fs/fs.tar --out build/integration-test-fs/fs.json
  221. ./tools/copy-to-sha256.py build/integration-test-fs/fs.tar build/integration-test-fs/flat
  222. rm build/integration-test-fs/fs.tar build/integration-test-fs/bzImage build/integration-test-fs/initrd
  223. tests: all-debug build/integration-test-fs/fs.json
  224. ./tests/full/run.js
  225. tests-release: build/libv86.js build/v86.wasm build/integration-test-fs/fs.json
  226. TEST_RELEASE_BUILD=1 ./tests/full/run.js
  227. nasmtests: all-debug
  228. $(MAKE) -C $(NASM_TEST_DIR) all
  229. $(NASM_TEST_DIR)/gen_fixtures.js
  230. $(NASM_TEST_DIR)/run.js
  231. nasmtests-force-jit: all-debug
  232. $(MAKE) -C $(NASM_TEST_DIR) all
  233. $(NASM_TEST_DIR)/gen_fixtures.js
  234. $(NASM_TEST_DIR)/run.js --force-jit
  235. jitpagingtests: all-debug
  236. $(MAKE) -C tests/jit-paging test-jit
  237. ./tests/jit-paging/run.js
  238. qemutests: all-debug
  239. $(MAKE) -C tests/qemu test-i386
  240. ./tests/qemu/run.js > build/qemu-test-result
  241. ./tests/qemu/run-qemu.js > build/qemu-test-reference
  242. diff build/qemu-test-result build/qemu-test-reference
  243. qemutests-release: build/libv86.js build/v86.wasm
  244. $(MAKE) -C tests/qemu test-i386
  245. TEST_RELEASE_BUILD=1 time ./tests/qemu/run.js > build/qemu-test-result
  246. ./tests/qemu/run-qemu.js > build/qemu-test-reference
  247. diff build/qemu-test-result build/qemu-test-reference
  248. kvm-unit-test: all-debug
  249. (cd tests/kvm-unit-tests && ./configure && make x86/realmode.flat)
  250. tests/kvm-unit-tests/run.js tests/kvm-unit-tests/x86/realmode.flat
  251. kvm-unit-test-release: build/libv86.js build/v86.wasm
  252. (cd tests/kvm-unit-tests && ./configure && make x86/realmode.flat)
  253. TEST_RELEASE_BUILD=1 tests/kvm-unit-tests/run.js tests/kvm-unit-tests/x86/realmode.flat
  254. expect-tests: all-debug build/libwabt.js
  255. make -C tests/expect/tests
  256. ./tests/expect/run.js
  257. devices-test: all-debug
  258. ./tests/devices/virtio_9p.js
  259. rust-test: $(RUST_FILES)
  260. env RUSTFLAGS="-D warnings" RUST_BACKTRACE=full RUST_TEST_THREADS=1 cargo test -- --nocapture
  261. ./tests/rust/verify-wasmgen-dummy-output.js
  262. rust-test-intensive:
  263. QUICKCHECK_TESTS=100000000 make rust-test
  264. api-tests: all-debug
  265. ./tests/api/clean-shutdown.js
  266. ./tests/api/state.js
  267. ./tests/api/reset.js
  268. all-tests: jshint kvm-unit-test qemutests qemutests-release jitpagingtests api-tests nasmtests nasmtests-force-jit tests expect-tests
  269. # Skipping:
  270. # - devices-test (hangs)
  271. jshint:
  272. jshint --config=./.jshint.json src tests gen lib
  273. rustfmt: $(RUST_FILES)
  274. cargo fmt --all -- --check
  275. build/capstone-x86.min.js:
  276. mkdir -p build
  277. wget -nv -P build https://github.com/AlexAltea/capstone.js/releases/download/v3.0.5-rc1/capstone-x86.min.js
  278. build/libwabt.js:
  279. mkdir -p build
  280. wget -nv -P build https://github.com/WebAssembly/wabt/archive/1.0.6.zip
  281. unzip -j -d build/ build/1.0.6.zip wabt-1.0.6/demo/libwabt.js
  282. rm build/1.0.6.zip
  283. build/xterm.js:
  284. curl https://cdn.jsdelivr.net/npm/xterm@4.9.0/lib/xterm.js > build/xterm.js
  285. curl https://cdn.jsdelivr.net/npm/xterm@4.9.0/lib/xterm.js.map > build/xterm.js.map
  286. curl https://cdn.jsdelivr.net/npm/xterm@4.9.0/css/xterm.css > build/xterm.css