Makefile 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 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 lib.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. --output_wrapper ';(function(){%output%}).call(this);'\
  121. --js $(CORE_FILES)\
  122. --js $(BROWSER_FILES)\
  123. --js $(LIB_FILES)
  124. ls -lh build/libv86.js
  125. build/libv86-debug.js: $(CLOSURE) src/*.js lib/*.js src/browser/*.js
  126. mkdir -p build
  127. java -jar $(CLOSURE) \
  128. --js_output_file build/libv86-debug.js\
  129. --define=DEBUG=true\
  130. $(CLOSURE_FLAGS)\
  131. $(CLOSURE_READABLE)\
  132. --compilation_level SIMPLE\
  133. --output_wrapper ';(function(){%output%}).call(this);'\
  134. --js $(CORE_FILES)\
  135. --js $(BROWSER_FILES)\
  136. --js $(LIB_FILES)
  137. src/rust/gen/jit.rs: $(JIT_DEPENDENCIES)
  138. ./gen/generate_jit.js --output-dir build/ --table jit
  139. src/rust/gen/jit0f.rs: $(JIT_DEPENDENCIES)
  140. ./gen/generate_jit.js --output-dir build/ --table jit0f
  141. src/rust/gen/interpreter.rs: $(INTERPRETER_DEPENDENCIES)
  142. ./gen/generate_interpreter.js --output-dir build/ --table interpreter
  143. src/rust/gen/interpreter0f.rs: $(INTERPRETER_DEPENDENCIES)
  144. ./gen/generate_interpreter.js --output-dir build/ --table interpreter0f
  145. src/rust/gen/analyzer.rs: $(ANALYZER_DEPENDENCIES)
  146. ./gen/generate_analyzer.js --output-dir build/ --table analyzer
  147. src/rust/gen/analyzer0f.rs: $(ANALYZER_DEPENDENCIES)
  148. ./gen/generate_analyzer.js --output-dir build/ --table analyzer0f
  149. build/v86.wasm: $(RUST_FILES) build/softfloat.o build/zstddeclib.o Cargo.toml
  150. mkdir -p build/
  151. -BLOCK_SIZE=K ls -l build/v86.wasm
  152. cargo rustc --release $(CARGO_FLAGS)
  153. mv build/wasm32-unknown-unknown/release/v86.wasm build/v86.wasm
  154. -$(WASM_OPT) && wasm-opt -O2 --strip-debug build/v86.wasm -o build/v86.wasm
  155. BLOCK_SIZE=K ls -l build/v86.wasm
  156. build/v86-debug.wasm: $(RUST_FILES) build/softfloat.o build/zstddeclib.o Cargo.toml
  157. mkdir -p build/
  158. -BLOCK_SIZE=K ls -l build/v86-debug.wasm
  159. cargo rustc $(CARGO_FLAGS)
  160. mv build/wasm32-unknown-unknown/debug/v86.wasm build/v86-debug.wasm
  161. BLOCK_SIZE=K ls -l build/v86-debug.wasm
  162. build/v86-fallback.wasm: $(RUST_FILES) build/softfloat.o build/zstddeclib.o Cargo.toml
  163. mkdir -p build/
  164. cargo rustc --release $(CARGO_FLAGS_SAFE)
  165. mv build/wasm32-unknown-unknown/release/v86.wasm build/v86-fallback.wasm || true
  166. debug-with-profiler: $(RUST_FILES) build/softfloat.o build/zstddeclib.o Cargo.toml
  167. mkdir -p build/
  168. cargo rustc --features profiler $(CARGO_FLAGS)
  169. mv build/wasm32-unknown-unknown/debug/v86.wasm build/v86-debug.wasm || true
  170. with-profiler: $(RUST_FILES) build/softfloat.o build/zstddeclib.o Cargo.toml
  171. mkdir -p build/
  172. cargo rustc --release --features profiler $(CARGO_FLAGS)
  173. mv build/wasm32-unknown-unknown/release/v86.wasm build/v86.wasm || true
  174. build/softfloat.o: lib/softfloat/softfloat.c
  175. mkdir -p build
  176. clang -c -Wall \
  177. --target=wasm32 -O3 -flto -nostdlib -fvisibility=hidden -ffunction-sections -fdata-sections \
  178. -DSOFTFLOAT_FAST_INT64 -DINLINE_LEVEL=5 -DSOFTFLOAT_FAST_DIV32TO16 -DSOFTFLOAT_FAST_DIV64TO32 \
  179. -o build/softfloat.o \
  180. lib/softfloat/softfloat.c
  181. build/zstddeclib.o: lib/zstd/zstddeclib.c
  182. mkdir -p build
  183. clang -c -Wall \
  184. --target=wasm32 -O3 -flto -nostdlib -fvisibility=hidden -ffunction-sections -fdata-sections \
  185. -DZSTDLIB_VISIBILITY="" \
  186. -o build/zstddeclib.o \
  187. lib/zstd/zstddeclib.c
  188. clean:
  189. -rm build/libv86.js
  190. -rm build/libv86-debug.js
  191. -rm build/v86_all.js
  192. -rm build/v86.wasm
  193. -rm build/v86-debug.wasm
  194. -rm $(INSTRUCTION_TABLES)
  195. -rm build/*.map
  196. -rm build/*.wast
  197. -rm build/*.o
  198. $(MAKE) -C $(NASM_TEST_DIR) clean
  199. run:
  200. python3 -m http.server 2> /dev/null
  201. update_version:
  202. set -e ;\
  203. COMMIT=`git log --format="%h" -n 1` ;\
  204. DATE=`git log --date="format:%b %e, %Y %H:%m" --format="%cd" -n 1` ;\
  205. SEARCH='<code>Version: <a href="https://github.com/copy/v86/commits/[a-f0-9]\+">[a-f0-9]\+</a> ([^(]\+)</code>' ;\
  206. REPLACE='<code>Version: <a href="https://github.com/copy/v86/commits/'$$COMMIT'">'$$COMMIT'</a> ('$$DATE')</code>' ;\
  207. sed -i "s@$$SEARCH@$$REPLACE@g" index.html ;\
  208. grep $$COMMIT index.html
  209. $(CLOSURE):
  210. mkdir -p $(CLOSURE_DIR)
  211. wget -nv -O $(CLOSURE) https://repo1.maven.org/maven2/com/google/javascript/closure-compiler/v20220104/closure-compiler-v20220104.jar
  212. build/integration-test-fs/fs.json:
  213. mkdir -p build/integration-test-fs/flat
  214. cp images/buildroot-bzimage.bin build/integration-test-fs/bzImage
  215. touch build/integration-test-fs/initrd
  216. cd build/integration-test-fs && tar cfv fs.tar bzImage initrd
  217. ./tools/fs2json.py build/integration-test-fs/fs.tar --out build/integration-test-fs/fs.json
  218. ./tools/copy-to-sha256.py build/integration-test-fs/fs.tar build/integration-test-fs/flat
  219. rm build/integration-test-fs/fs.tar build/integration-test-fs/bzImage build/integration-test-fs/initrd
  220. tests: all-debug build/integration-test-fs/fs.json
  221. ./tests/full/run.js
  222. tests-release: build/libv86.js build/v86.wasm build/integration-test-fs/fs.json
  223. TEST_RELEASE_BUILD=1 ./tests/full/run.js
  224. nasmtests: all-debug
  225. $(MAKE) -C $(NASM_TEST_DIR) all
  226. $(NASM_TEST_DIR)/gen_fixtures.js
  227. $(NASM_TEST_DIR)/run.js
  228. nasmtests-force-jit: all-debug
  229. $(MAKE) -C $(NASM_TEST_DIR) all
  230. $(NASM_TEST_DIR)/gen_fixtures.js
  231. $(NASM_TEST_DIR)/run.js --force-jit
  232. jitpagingtests: all-debug
  233. $(MAKE) -C tests/jit-paging test-jit
  234. ./tests/jit-paging/run.js
  235. qemutests: all-debug
  236. $(MAKE) -C tests/qemu test-i386
  237. ./tests/qemu/run.js > build/qemu-test-result
  238. ./tests/qemu/run-qemu.js > build/qemu-test-reference
  239. diff build/qemu-test-result build/qemu-test-reference
  240. qemutests-release: build/libv86.js build/v86.wasm
  241. $(MAKE) -C tests/qemu test-i386
  242. TEST_RELEASE_BUILD=1 time ./tests/qemu/run.js > build/qemu-test-result
  243. ./tests/qemu/run-qemu.js > build/qemu-test-reference
  244. diff build/qemu-test-result build/qemu-test-reference
  245. kvm-unit-test: all-debug
  246. (cd tests/kvm-unit-tests && ./configure && make x86/realmode.flat)
  247. tests/kvm-unit-tests/run.js tests/kvm-unit-tests/x86/realmode.flat
  248. kvm-unit-test-release: build/libv86.js build/v86.wasm
  249. (cd tests/kvm-unit-tests && ./configure && make x86/realmode.flat)
  250. TEST_RELEASE_BUILD=1 tests/kvm-unit-tests/run.js tests/kvm-unit-tests/x86/realmode.flat
  251. expect-tests: all-debug build/libwabt.js
  252. make -C tests/expect/tests
  253. ./tests/expect/run.js
  254. devices-test: all-debug
  255. ./tests/devices/virtio_9p.js
  256. rust-test: $(RUST_FILES)
  257. env RUSTFLAGS="-D warnings" RUST_BACKTRACE=full RUST_TEST_THREADS=1 cargo test -- --nocapture
  258. ./tests/rust/verify-wasmgen-dummy-output.js
  259. rust-test-intensive:
  260. QUICKCHECK_TESTS=100000000 make rust-test
  261. api-tests: all-debug
  262. ./tests/api/clean-shutdown.js
  263. ./tests/api/state.js
  264. ./tests/api/reset.js
  265. all-tests: jshint kvm-unit-test qemutests qemutests-release jitpagingtests api-tests nasmtests nasmtests-force-jit tests expect-tests
  266. # Skipping:
  267. # - devices-test (hangs)
  268. jshint:
  269. jshint --config=./.jshint.json src tests gen lib
  270. rustfmt: $(RUST_FILES)
  271. cargo fmt --all -- --check
  272. build/capstone-x86.min.js:
  273. mkdir -p build
  274. wget -nv -P build https://github.com/AlexAltea/capstone.js/releases/download/v3.0.5-rc1/capstone-x86.min.js
  275. build/libwabt.js:
  276. mkdir -p build
  277. wget -nv -P build https://github.com/WebAssembly/wabt/archive/1.0.6.zip
  278. unzip -j -d build/ build/1.0.6.zip wabt-1.0.6/demo/libwabt.js
  279. rm build/1.0.6.zip
  280. build/xterm.js:
  281. curl https://cdn.jsdelivr.net/npm/xterm@4.9.0/lib/xterm.js > build/xterm.js
  282. curl https://cdn.jsdelivr.net/npm/xterm@4.9.0/lib/xterm.js.map > build/xterm.js.map
  283. curl https://cdn.jsdelivr.net/npm/xterm@4.9.0/css/xterm.css > build/xterm.css