Makefile 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. export CC=gcc
  43. # export LD=ld
  44. export LD=ld
  45. export AR=ar
  46. # export AR=x86_64-dragonos-ar
  47. export OBJCOPY=objcopy
  48. # export OBJCOPY=x86_64-dragonos-objcopy
  49. endif
  50. SRC=\
  51. Cargo.* \
  52. $(shell find src -type f)
  53. BUILTINS_VERSION=0.1.70
  54. .PHONY: all clean fmt install install-headers libs submodules test
  55. all: | libs
  56. clean:
  57. $(CARGO) clean
  58. $(MAKE) -C tests clean
  59. rm -rf sysroot
  60. check:
  61. $(CARGO) check
  62. fmt:
  63. ./fmt.sh
  64. install-headers: libs
  65. mkdir -pv "$(DESTDIR)/include"
  66. cp -rv "include"/* "$(DESTDIR)/include"
  67. cp -rv "target/include"/* "$(DESTDIR)/include"
  68. cp -v "openlibm/include"/*.h "$(DESTDIR)/include"
  69. cp -v "openlibm/src"/*.h "$(DESTDIR)/include"
  70. cp -v "pthreads-emb/"*.h "$(DESTDIR)/include"
  71. # $(BUILD)/release/libc.so
  72. libs: \
  73. $(BUILD)/release/libc.a \
  74. $(BUILD)/release/crt0.o \
  75. $(BUILD)/release/crti.o \
  76. $(BUILD)/release/crtn.o
  77. #$(BUILD)/release/ld_so
  78. install-libs: libs
  79. mkdir -pv "$(DESTDIR)/lib"
  80. cp -v "$(BUILD)/release/libc.a" "$(DESTDIR)/lib"
  81. # cp -v "$(BUILD)/release/libc.so" "$(DESTDIR)/lib"
  82. # ln -frsv "$(DESTDIR)/lib/libc.so" "$(DESTDIR)/lib/libc.so.6"
  83. cp -v "$(BUILD)/release/crt0.o" "$(DESTDIR)/lib"
  84. ln -frsv "$(DESTDIR)/lib/crt0.o" "$(DESTDIR)/lib/crt1.o"
  85. cp -v "$(BUILD)/release/crti.o" "$(DESTDIR)/lib"
  86. cp -v "$(BUILD)/release/crtn.o" "$(DESTDIR)/lib"
  87. # cp -v "$(BUILD)/release/ld_so" "$(DESTDIR)/lib/ld64.so.1"
  88. cp -v "$(BUILD)/openlibm/libopenlibm.a" "$(DESTDIR)/lib/libm.a"
  89. cp -v "$(BUILD)/pthreads-emb/libpthread.a" "$(DESTDIR)/lib/libpthread.a"
  90. # Empty libraries for dl and rt
  91. $(AR) -rcs "$(DESTDIR)/lib/libdl.a"
  92. $(AR) -rcs "$(DESTDIR)/lib/librt.a"
  93. install: install-headers install-libs
  94. submodules:
  95. git submodule sync
  96. git submodule update --init --recursive
  97. sysroot: all
  98. rm -rf $@
  99. rm -rf [email protected]
  100. mkdir -p [email protected]
  101. $(MAKE) install [email protected]
  102. mv [email protected] $@
  103. touch $@
  104. test: sysroot
  105. # TODO: Fix SIGILL when running cargo test
  106. # $(CARGO_TEST) test
  107. $(MAKE) -C tests verify
  108. # Debug targets
  109. $(BUILD)/debug/libc.a: $(BUILD)/debug/librelibc.a $(BUILD)/pthreads-emb/libpthread.a $(BUILD)/openlibm/libopenlibm.a
  110. echo "create $@" > "[email protected]"
  111. for lib in $^; do\
  112. echo "addlib $$lib" >> "[email protected]"; \
  113. done
  114. echo "save" >> "[email protected]"
  115. echo "end" >> "[email protected]"
  116. $(AR) -M < "[email protected]"
  117. $(BUILD)/debug/libc.so: $(BUILD)/debug/librelibc.a $(BUILD)/pthreads-emb/libpthread.a $(BUILD)/openlibm/libopenlibm.a
  118. $(CC) -nostdlib -shared -Wl,--allow-multiple-definition -Wl,--whole-archive $^ -Wl,--no-whole-archive -Wl,-soname,libc.so.6 -o $@
  119. $(BUILD)/debug/librelibc.a: $(SRC)
  120. CARGO_INCREMENTAL=0 $(CARGO) rustc $(CARGOFLAGS) -- --emit link=$@ $(RUSTCFLAGS)
  121. ./renamesyms.sh $@ $(BUILD)/debug/deps/
  122. touch $@
  123. $(BUILD)/debug/crt0.o: $(SRC)
  124. CARGO_INCREMENTAL=0 $(CARGO) rustc --manifest-path src/crt0/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
  125. touch $@
  126. $(BUILD)/debug/crti.o: $(SRC)
  127. CARGO_INCREMENTAL=0 $(CARGO) rustc --manifest-path src/crti/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
  128. touch $@
  129. $(BUILD)/debug/crtn.o: $(SRC)
  130. CARGO_INCREMENTAL=0 $(CARGO) rustc --manifest-path src/crtn/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
  131. touch $@
  132. $(BUILD)/debug/ld_so.o: $(SRC)
  133. CARGO_INCREMENTAL=0 $(CARGO) rustc --manifest-path src/ld_so/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
  134. touch $@
  135. $(BUILD)/debug/ld_so: $(BUILD)/debug/ld_so.o $(BUILD)/debug/crti.o $(BUILD)/debug/libc.a $(BUILD)/debug/crtn.o
  136. $(LD) --no-relax -T src/ld_so/ld_script/$(TARGET).ld --allow-multiple-definition --gc-sections --gc-keep-exported $^ -o $@
  137. # Release targets
  138. $(BUILD)/release/libc.a: $(BUILD)/release/librelibc.a $(BUILD)/pthreads-emb/libpthread.a $(BUILD)/openlibm/libopenlibm.a
  139. echo "create $@" > "[email protected]"
  140. for lib in $^; do\
  141. echo "addlib $$lib" >> "[email protected]"; \
  142. done
  143. echo "save" >> "[email protected]"
  144. echo "end" >> "[email protected]"
  145. $(AR) -M < "[email protected]"
  146. $(BUILD)/release/libc.so: $(BUILD)/release/librelibc.a $(BUILD)/pthreads-emb/libpthread.a $(BUILD)/openlibm/libopenlibm.a
  147. $(CC) -nostdlib -shared -Wl,--allow-multiple-definition -Wl,--whole-archive $^ -Wl,--no-whole-archive -Wl,-soname,libc.so.6 -o $@
  148. $(BUILD)/release/librelibc.a: $(SRC)
  149. CARGO_INCREMENTAL=0 $(CARGO) rustc --release $(CARGOFLAGS) -- --emit link=$@ $(RUSTCFLAGS)
  150. # TODO: Better to only allow a certain whitelisted set of symbols? Perhaps
  151. # use some cbindgen hook, specify them manually, or grep for #[no_mangle].
  152. ./renamesyms.sh $@ $(BUILD)/release/deps/
  153. touch $@
  154. $(BUILD)/release/crt0.o: $(SRC)
  155. CARGO_INCREMENTAL=0 $(CARGO) rustc --release --manifest-path src/crt0/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
  156. touch $@
  157. $(BUILD)/release/crti.o: $(SRC)
  158. CARGO_INCREMENTAL=0 $(CARGO) rustc --release --manifest-path src/crti/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
  159. touch $@
  160. $(BUILD)/release/crtn.o: $(SRC)
  161. CARGO_INCREMENTAL=0 $(CARGO) rustc --release --manifest-path src/crtn/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
  162. touch $@
  163. $(BUILD)/release/ld_so.o: $(SRC)
  164. CARGO_INCREMENTAL=0 $(CARGO) rustc --release --manifest-path src/ld_so/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
  165. touch $@
  166. $(BUILD)/release/ld_so: $(BUILD)/release/ld_so.o $(BUILD)/release/crti.o $(BUILD)/release/libc.a $(BUILD)/release/crtn.o
  167. $(LD) --no-relax -T src/ld_so/ld_script/$(TARGET).ld --allow-multiple-definition --gc-sections --gc-keep-exported $^ -o $@
  168. # Other targets
  169. $(BUILD)/openlibm: openlibm
  170. rm -rf $@ [email protected]
  171. mkdir -p $(BUILD)
  172. cp -r $< [email protected]
  173. mv [email protected] $@
  174. touch $@
  175. $(BUILD)/openlibm/libopenlibm.a: $(BUILD)/openlibm $(BUILD)/release/librelibc.a
  176. $(MAKE) AR=$(AR) CC=$(CC) LD=$(LD) CPPFLAGS="-fno-stack-protector -I $(shell pwd)/include -I $(shell pwd)/target/include" -C $< libopenlibm.a
  177. $(BUILD)/pthreads-emb: pthreads-emb
  178. rm -rf $@ [email protected]
  179. mkdir -p $(BUILD)
  180. cp -r $< [email protected]
  181. mv [email protected] $@
  182. touch $@
  183. $(BUILD)/pthreads-emb/libpthread.a: $(BUILD)/pthreads-emb $(BUILD)/release/librelibc.a
  184. $(MAKE) AR=$(AR) CC=$(CC) LD=$(LD) CFLAGS="-fno-stack-protector -I $(shell pwd)/include -I $(shell pwd)/target/include" -C $< libpthread.a