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