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