Makefile 7.6 KB

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