Makefile 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. # TARGET?=$(shell rustc -Z unstable-options --print target-spec-json | grep llvm-target | cut -d '"' -f4)
  2. export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
  3. export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
  4. TARGET=x86_64-unknown-dragonos
  5. CARGO?=cargo
  6. CARGO_TEST?=$(CARGO)
  7. CARGO_COMMON_FLAGS=-Z build-std=core,alloc,compiler_builtins
  8. CARGOFLAGS?=$(CARGO_COMMON_FLAGS)
  9. RUSTCFLAGS?=
  10. export OBJCOPY?=objcopy
  11. BUILD="target/$(TARGET)"
  12. CARGOFLAGS+="--target=$(TARGET)"
  13. ifeq ($(TARGET),aarch64-unknown-linux-gnu)
  14. export CC=aarch64-linux-gnu-gcc
  15. export LD=aarch64-linux-gnu-ld
  16. export AR=aarch64-linux-gnu-ar
  17. export OBJCOPY=aarch64-linux-gnu-objcopy
  18. endif
  19. ifeq ($(TARGET),aarch64-unknown-redox)
  20. export CC=aarch64-unknown-redox-gcc
  21. export LD=aarch64-unknown-redox-ld
  22. export AR=aarch64-unknown-redox-ar
  23. export OBJCOPY=aarch64-unknown-redox-objcopy
  24. endif
  25. ifeq ($(TARGET),x86_64-unknown-linux-gnu)
  26. export CC=x86_64-linux-gnu-gcc
  27. export LD=x86_64-linux-gnu-ld
  28. export AR=x86_64-linux-gnu-ar
  29. export OBJCOPY=x86_64-linux-gnu-objcopy
  30. endif
  31. ifeq ($(TARGET),i686-unknown-redox)
  32. export CC=i686-unknown-redox-gcc
  33. export LD=i686-unknown-redox-ld
  34. export AR=i686-unknown-redox-ar
  35. export OBJCOPY=i686-unknown-redox-objcopy
  36. endif
  37. ifeq ($(TARGET),x86_64-unknown-redox)
  38. export CC=x86_64-unknown-redox-gcc
  39. export LD=x86_64-unknown-redox-ld
  40. export AR=x86_64-unknown-redox-ar
  41. export OBJCOPY=x86_64-unknown-redox-objcopy
  42. endif
  43. ifeq ($(TARGET),x86_64-unknown-dragonos)
  44. # 如果存在x86_64-dragonos-gcc,就用它,否则用gcc
  45. ifeq ($(shell which x86_64-dragonos-gcc),)
  46. export CC=gcc
  47. # export LD=ld
  48. export LD=ld
  49. export AR=ar
  50. # export AR=x86_64-dragonos-ar
  51. export OBJCOPY=objcopy
  52. # export OBJCOPY=x86_64-dragonos-objcopy
  53. else
  54. export CC=x86_64-dragonos-gcc
  55. export LD=x86_64-dragonos-ld
  56. export AR=x86_64-dragonos-ar
  57. export OBJCOPY=x86_64-dragonos-objcopy
  58. endif
  59. endif
  60. SRC=\
  61. Cargo.* \
  62. $(shell find src -type f)
  63. BUILTINS_VERSION=0.1.70
  64. .PHONY: all clean fmt install install-headers libs submodules test
  65. all: | libs
  66. clean:
  67. $(CARGO) clean
  68. check:
  69. $(CARGO) -Z build-std=core,alloc,compiler_builtins check --workspace --message-format=json --target target.json
  70. fmt:
  71. $(CARGO) fmt
  72. fmt-check:
  73. $(CARGO) fmt --check
  74. install-headers: libs
  75. mkdir -pv "$(DESTDIR)/include"
  76. cp -rv "include"/* "$(DESTDIR)/include"
  77. cp -rv "target/include"/* "$(DESTDIR)/include"
  78. cp -v "openlibm/include"/*.h "$(DESTDIR)/include"
  79. cp -v "openlibm/src"/*.h "$(DESTDIR)/include"
  80. cp -v "pthreads-emb/"*.h "$(DESTDIR)/include"
  81. # $(BUILD)/release/libc.so
  82. libs:
  83. @cargo -Z build-std=core,alloc,compiler_builtins build --target target.json --release
  84. #$(BUILD)/release/ld_so
  85. install-libs: libs
  86. mkdir -pv "$(DESTDIR)/lib"
  87. cp -v "$(BUILD)/release/libc.a" "$(DESTDIR)/lib"
  88. # cp -v "$(BUILD)/release/libc.so" "$(DESTDIR)/lib"
  89. # ln -frsv "$(DESTDIR)/lib/libc.so" "$(DESTDIR)/lib/libc.so.6"
  90. cp -v "$(BUILD)/release/crt0.o" "$(DESTDIR)/lib"
  91. ln -frsv "$(DESTDIR)/lib/crt0.o" "$(DESTDIR)/lib/crt1.o"
  92. cp -v "$(BUILD)/release/crti.o" "$(DESTDIR)/lib"
  93. cp -v "$(BUILD)/release/crtn.o" "$(DESTDIR)/lib"
  94. # cp -v "$(BUILD)/release/ld_so" "$(DESTDIR)/lib/ld64.so.1"
  95. cp -v "$(BUILD)/openlibm/libopenlibm.a" "$(DESTDIR)/lib/libm.a"
  96. cp -v "$(BUILD)/pthreads-emb/libpthread.a" "$(DESTDIR)/lib/libpthread.a"
  97. # Empty libraries for dl and rt
  98. $(AR) -rcs "$(DESTDIR)/lib/libdl.a"
  99. $(AR) -rcs "$(DESTDIR)/lib/librt.a"
  100. install: install-headers install-libs
  101. submodules:
  102. git submodule sync
  103. git submodule update --init --recursive
  104. sysroot: all
  105. rm -rf $@
  106. rm -rf [email protected]
  107. mkdir -p [email protected]
  108. $(MAKE) install [email protected]
  109. mv [email protected] $@
  110. touch $@
  111. test: sysroot
  112. # TODO: Fix SIGILL when running cargo test
  113. # $(CARGO_TEST) test
  114. $(MAKE) -C tests verify
  115. # Debug targets
  116. $(BUILD)/debug/libc.a: $(BUILD)/debug/libdrsstd.a $(BUILD)/pthreads-emb/libpthread.a $(BUILD)/openlibm/libopenlibm.a
  117. echo "create $@" > "[email protected]"
  118. for lib in $^; do\
  119. echo "addlib $$lib" >> "[email protected]"; \
  120. done
  121. echo "save" >> "[email protected]"
  122. echo "end" >> "[email protected]"
  123. $(AR) -M < "[email protected]"
  124. $(BUILD)/debug/libc.so: $(BUILD)/debug/libdrsstd.a $(BUILD)/pthreads-emb/libpthread.a $(BUILD)/openlibm/libopenlibm.a
  125. $(CC) -nostdlib -shared -Wl,--allow-multiple-definition -Wl,--whole-archive $^ -Wl,--no-whole-archive -Wl,-soname,libc.so.6 -o $@
  126. $(BUILD)/debug/libdrsstd.a: $(SRC)
  127. CARGO_INCREMENTAL=0 $(CARGO) rustc $(CARGOFLAGS) -- --emit link=$@ $(RUSTCFLAGS)
  128. ./renamesyms.sh $@ $(BUILD)/debug/deps/
  129. touch $@
  130. $(BUILD)/debug/crt0.o: $(SRC)
  131. CARGO_INCREMENTAL=0 $(CARGO) rustc --manifest-path src/crt0/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
  132. touch $@
  133. $(BUILD)/debug/crti.o: $(SRC)
  134. CARGO_INCREMENTAL=0 $(CARGO) rustc --manifest-path src/crti/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
  135. touch $@
  136. $(BUILD)/debug/crtn.o: $(SRC)
  137. CARGO_INCREMENTAL=0 $(CARGO) rustc --manifest-path src/crtn/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
  138. touch $@
  139. $(BUILD)/debug/ld_so.o: $(SRC)
  140. CARGO_INCREMENTAL=0 $(CARGO) rustc --manifest-path src/ld_so/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
  141. touch $@
  142. $(BUILD)/debug/ld_so: $(BUILD)/debug/ld_so.o $(BUILD)/debug/crti.o $(BUILD)/debug/libc.a $(BUILD)/debug/crtn.o
  143. $(LD) --no-relax -T src/ld_so/ld_script/$(TARGET).ld --allow-multiple-definition --gc-sections --gc-keep-exported $^ -o $@
  144. # Release targets
  145. $(BUILD)/release/libc.a: $(BUILD)/release/libdrsstd.a $(BUILD)/pthreads-emb/libpthread.a $(BUILD)/openlibm/libopenlibm.a
  146. echo "create $@" > "[email protected]"
  147. for lib in $^; do\
  148. echo "addlib $$lib" >> "[email protected]"; \
  149. done
  150. echo "save" >> "[email protected]"
  151. echo "end" >> "[email protected]"
  152. $(AR) -M < "[email protected]"
  153. $(BUILD)/release/libc.so: $(BUILD)/release/libdrsstd.a $(BUILD)/pthreads-emb/libpthread.a $(BUILD)/openlibm/libopenlibm.a
  154. $(CC) -nostdlib -shared -Wl,--allow-multiple-definition -Wl,--whole-archive $^ -Wl,--no-whole-archive -Wl,-soname,libc.so.6 -o $@
  155. $(BUILD)/release/libdrsstd.a: $(SRC)
  156. CARGO_INCREMENTAL=0 $(CARGO) rustc --release $(CARGOFLAGS) -- --emit link=$@ $(RUSTCFLAGS)
  157. # TODO: Better to only allow a certain whitelisted set of symbols? Perhaps
  158. # use some cbindgen hook, specify them manually, or grep for #[no_mangle].
  159. ./renamesyms.sh $@ $(BUILD)/release/deps/
  160. touch $@
  161. $(BUILD)/release/crt0.o: $(SRC)
  162. CARGO_INCREMENTAL=0 $(CARGO) rustc --release --manifest-path src/crt0/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
  163. touch $@
  164. $(BUILD)/release/crti.o: $(SRC)
  165. CARGO_INCREMENTAL=0 $(CARGO) rustc --release --manifest-path src/crti/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
  166. touch $@
  167. $(BUILD)/release/crtn.o: $(SRC)
  168. CARGO_INCREMENTAL=0 $(CARGO) rustc --release --manifest-path src/crtn/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
  169. touch $@
  170. $(BUILD)/release/ld_so.o: $(SRC)
  171. CARGO_INCREMENTAL=0 $(CARGO) rustc --release --manifest-path src/ld_so/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
  172. touch $@
  173. $(BUILD)/release/ld_so: $(BUILD)/release/ld_so.o $(BUILD)/release/crti.o $(BUILD)/release/libc.a $(BUILD)/release/crtn.o
  174. $(LD) --no-relax -T src/ld_so/ld_script/$(TARGET).ld --allow-multiple-definition --gc-sections --gc-keep-exported $^ -o $@
  175. # Other targets
  176. $(BUILD)/openlibm: openlibm
  177. rm -rf $@ [email protected]
  178. mkdir -p $(BUILD)
  179. cp -r $< [email protected]
  180. mv [email protected] $@
  181. touch $@
  182. $(BUILD)/openlibm/libopenlibm.a: $(BUILD)/openlibm $(BUILD)/release/libdrsstd.a
  183. $(MAKE) AR=$(AR) CC=$(CC) LD=$(LD) CPPFLAGS="-fno-stack-protector -I $(shell pwd)/include -I $(shell pwd)/target/include" -C $< libopenlibm.a
  184. $(BUILD)/pthreads-emb: pthreads-emb
  185. rm -rf $@ [email protected]
  186. mkdir -p $(BUILD)
  187. cp -r $< [email protected]
  188. mv [email protected] $@
  189. touch $@
  190. $(BUILD)/pthreads-emb/libpthread.a: $(BUILD)/pthreads-emb $(BUILD)/release/libdrsstd.a
  191. $(MAKE) AR=$(AR) CC=$(CC) LD=$(LD) CFLAGS="-fno-stack-protector -I $(shell pwd)/include -I $(shell pwd)/target/include" -C $< libpthread.a