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