Makefile 5.47 KB
Newer Older
1 2
TARGET?=

3
CARGO?=cargo
4
CARGOFLAGS=
5
RUSTCFLAGS=
6

Jeremy Soller's avatar
Jeremy Soller committed
7
BUILD=target
8
ifneq ($(TARGET),)
Jeremy Soller's avatar
Jeremy Soller committed
9
	BUILD="target/$(TARGET)"
10
	CARGOFLAGS="--target=$(TARGET)"
Jeremy Soller's avatar
Jeremy Soller committed
11 12 13
endif

ifeq ($(TARGET),aarch64-unknown-linux-gnu)
14
	export CC=aarch64-linux-gnu-gcc
Jeremy Soller's avatar
Jeremy Soller committed
15 16
endif

17 18 19 20
ifeq ($(TARGET),aarch64-unknown-redox)
	export CC=aarch64-unknown-redox-gcc
endif

Jeremy Soller's avatar
Jeremy Soller committed
21
ifeq ($(TARGET),x86_64-unknown-redox)
22
	export CC=x86_64-unknown-redox-gcc
23 24
endif

25
SRC=\
Jeremy Soller's avatar
Jeremy Soller committed
26
	Cargo.* \
27 28 29 30 31
	src/* \
	src/*/* \
	src/*/*/* \
	src/*/*/*/*

32
.PHONY: all clean fmt headers install install-headers libs test
Jeremy Soller's avatar
Jeremy Soller committed
33

34
all: | headers libs
Jeremy Soller's avatar
Jeremy Soller committed
35 36

clean:
Jeremy Soller's avatar
Jeremy Soller committed
37
	$(CARGO) clean
38
	$(MAKE) -C tests clean
39
	rm -rf sysroot
Jeremy Soller's avatar
Jeremy Soller committed
40

41
check:
Jeremy Soller's avatar
Jeremy Soller committed
42
	$(CARGO) check
43

Jeremy Soller's avatar
Jeremy Soller committed
44 45 46
fmt:
	./fmt.sh

47 48 49
headers: $(BUILD)/include

install-headers: headers
Jeremy Soller's avatar
Jeremy Soller committed
50 51
	mkdir -pv "$(DESTDIR)/include"
	cp -rv "include"/* "$(DESTDIR)/include"
Jeremy Soller's avatar
Jeremy Soller committed
52
	cp -rv "$(BUILD)/include"/* "$(DESTDIR)/include"
53 54
	cp -v "openlibm/include"/*.h "$(DESTDIR)/include"
	cp -v "openlibm/src"/*.h "$(DESTDIR)/include"
55
	cp -v "pthreads-emb/"*.h "$(DESTDIR)/include"
56

57 58 59 60 61 62 63
libs: \
	$(BUILD)/release/libc.a \
	$(BUILD)/release/libc.so \
	$(BUILD)/release/crt0.o \
	$(BUILD)/release/crti.o \
	$(BUILD)/release/crtn.o \
	$(BUILD)/release/ld_so
64 65

install-libs: libs
66
	mkdir -pv "$(DESTDIR)/lib"
67
	cp -v "$(BUILD)/release/libc.a" "$(DESTDIR)/lib"
68
	cp -v "$(BUILD)/release/libc.so" "$(DESTDIR)/lib"
69
	cp -v "$(BUILD)/release/crt0.o" "$(DESTDIR)/lib"
jD91mZM2's avatar
jD91mZM2 committed
70 71
	cp -v "$(BUILD)/release/crti.o" "$(DESTDIR)/lib"
	cp -v "$(BUILD)/release/crtn.o" "$(DESTDIR)/lib"
Jeremy Soller's avatar
Jeremy Soller committed
72
	cp -v "$(BUILD)/release/ld_so" "$(DESTDIR)/lib/ld64.so.1"
jD91mZM2's avatar
jD91mZM2 committed
73
	cp -v "$(BUILD)/openlibm/libopenlibm.a" "$(DESTDIR)/lib/libm.a"
74
	cp -v "$(BUILD)/pthreads-emb/libpthread.a" "$(DESTDIR)/lib/libpthread.a"
Jeremy Soller's avatar
Jeremy Soller committed
75

76 77
install: install-headers install-libs

78
sysroot: all
79
	rm -rf $@
80 81
	rm -rf $@.partial
	mkdir -p $@.partial
82
	$(MAKE) install DESTDIR=$@.partial
83 84 85
	mv $@.partial $@
	touch $@

jD91mZM2's avatar
jD91mZM2 committed
86
test: sysroot
87
	$(MAKE) -C tests run
Jeremy Soller's avatar
Jeremy Soller committed
88

Jeremy Soller's avatar
Jeremy Soller committed
89 90 91
# Debug targets

$(BUILD)/debug/libc.a: $(BUILD)/debug/librelibc.a $(BUILD)/pthreads-emb/libpthread.a $(BUILD)/openlibm/libopenlibm.a
92 93 94 95 96 97 98 99
	echo "create $@" > "$@.mri"
	for lib in $^; do\
		echo "addlib $$lib" >> "$@.mri"; \
	done
	echo "save" >> "$@.mri"
	echo "end" >> "$@.mri"
	ar -M < "$@.mri"

100
$(BUILD)/debug/libc.so: $(BUILD)/debug/librelibc.a $(BUILD)/pthreads-emb/libpthread.a $(BUILD)/openlibm/libopenlibm.a
101
	$(CC) -nostdlib -shared -Wl,--whole-archive $^ -Wl,--no-whole-archive -o $@
102

103
$(BUILD)/debug/librelibc.a: $(SRC)
104
	CARGO_INCREMENTAL=0 $(CARGO) rustc $(CARGOFLAGS) -- --emit link=$@ $(RUSTCFLAGS)
105
	touch $@
106

107
$(BUILD)/debug/crt0.o: $(SRC)
108
	CARGO_INCREMENTAL=0 $(CARGO) rustc --manifest-path src/crt0/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
109
	touch $@
Jeremy Soller's avatar
Jeremy Soller committed
110

111
$(BUILD)/debug/crti.o: $(SRC)
112
	CARGO_INCREMENTAL=0 $(CARGO) rustc --manifest-path src/crti/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
113 114 115
	touch $@

$(BUILD)/debug/crtn.o: $(SRC)
116
	CARGO_INCREMENTAL=0 $(CARGO) rustc --manifest-path src/crtn/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
117 118
	touch $@

Jeremy Soller's avatar
Jeremy Soller committed
119 120 121 122 123
$(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
124
	$(LD) --gc-sections $^ -o $@
Jeremy Soller's avatar
Jeremy Soller committed
125 126 127 128 129 130 131 132 133 134 135 136

# Release targets

$(BUILD)/release/libc.a: $(BUILD)/release/librelibc.a $(BUILD)/pthreads-emb/libpthread.a $(BUILD)/openlibm/libopenlibm.a
	echo "create $@" > "$@.mri"
	for lib in $^; do\
		echo "addlib $$lib" >> "$@.mri"; \
	done
	echo "save" >> "$@.mri"
	echo "end" >> "$@.mri"
	ar -M < "$@.mri"

137
$(BUILD)/release/libc.so: $(BUILD)/release/librelibc.a $(BUILD)/pthreads-emb/libpthread.a $(BUILD)/openlibm/libopenlibm.a
Jeremy Soller's avatar
Jeremy Soller committed
138 139
	$(CC) -nostdlib -shared -Wl,--whole-archive $^ -Wl,--no-whole-archive -o $@

140
$(BUILD)/release/librelibc.a: $(SRC)
141
	CARGO_INCREMENTAL=0 $(CARGO) rustc --release $(CARGOFLAGS) -- --emit link=$@ $(RUSTCFLAGS)
142
	touch $@
Jeremy Soller's avatar
Jeremy Soller committed
143

144
$(BUILD)/release/crt0.o: $(SRC)
145
	CARGO_INCREMENTAL=0 $(CARGO) rustc --release --manifest-path src/crt0/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
146
	touch $@
Jeremy Soller's avatar
Jeremy Soller committed
147

jD91mZM2's avatar
jD91mZM2 committed
148
$(BUILD)/release/crti.o: $(SRC)
149
	CARGO_INCREMENTAL=0 $(CARGO) rustc --release --manifest-path src/crti/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
jD91mZM2's avatar
jD91mZM2 committed
150 151 152
	touch $@

$(BUILD)/release/crtn.o: $(SRC)
153
	CARGO_INCREMENTAL=0 $(CARGO) rustc --release --manifest-path src/crtn/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
jD91mZM2's avatar
jD91mZM2 committed
154 155
	touch $@

Jeremy Soller's avatar
Jeremy Soller committed
156 157 158 159 160
$(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
161
	$(LD) --gc-sections $^ -o $@
Jeremy Soller's avatar
Jeremy Soller committed
162 163 164

# Other targets

165 166 167 168 169
$(BUILD)/include: $(SRC)
	rm -rf $@ $@.partial
	mkdir -p $@.partial
	./include.sh $@.partial
	mv $@.partial $@
170
	touch $@
171

172 173
$(BUILD)/openlibm: openlibm
	rm -rf $@ $@.partial
174
	mkdir -p $(BUILD)
175 176
	cp -r $< $@.partial
	mv $@.partial $@
177
	touch $@
Jeremy Soller's avatar
Jeremy Soller committed
178

Jeremy Soller's avatar
Jeremy Soller committed
179
$(BUILD)/openlibm/libopenlibm.a: $(BUILD)/openlibm $(BUILD)/include
180
	$(MAKE) CC=$(CC) CPPFLAGS="-fno-stack-protector -I$(shell pwd)/include -I $(shell pwd)/$(BUILD)/include" -C $< libopenlibm.a
Jeremy Soller's avatar
Jeremy Soller committed
181 182 183 184 185 186 187 188 189

$(BUILD)/pthreads-emb: pthreads-emb
	rm -rf $@ $@.partial
	mkdir -p $(BUILD)
	cp -r $< $@.partial
	mv $@.partial $@
	touch $@

$(BUILD)/pthreads-emb/libpthread.a: $(BUILD)/pthreads-emb $(BUILD)/include
190
	$(MAKE) CC=$(CC) CFLAGS="-fno-stack-protector -I$(shell pwd)/include -I $(shell pwd)/$(BUILD)/include" -C $< libpthread.a