From 47ee733afac58140eb436be1d5d770a26ba1dd5c Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jackpot51@gmail.com>
Date: Mon, 26 Mar 2018 19:12:20 -0600
Subject: [PATCH] Complete Makefile

---
 .travis.yml    |  4 +---
 Makefile       | 27 ++++++++++++++++++++-------
 README.md      |  2 +-
 ci.sh          |  9 +++++++++
 test.sh        |  7 -------
 tests/Makefile | 16 +++++++++-------
 6 files changed, 40 insertions(+), 25 deletions(-)
 create mode 100755 ci.sh
 delete mode 100755 test.sh

diff --git a/.travis.yml b/.travis.yml
index 6fd8524c..5fd93bf0 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -10,8 +10,6 @@ before_script:
   - rustup component add rustfmt-preview
   - if [ -n "$TARGET" ]; then rustup target add $TARGET; fi
 script:
-  - ./fmt.sh -- --write-mode=diff
-  - cargo build $([ -n "$TARGET" ] && echo --target="$TARGET")
-  - if [ -z "$TARGET" ]; then ./test.sh; fi
+  - ./ci.sh
 notifications:
   email: false
diff --git a/Makefile b/Makefile
index ab51c740..afb4bca8 100644
--- a/Makefile
+++ b/Makefile
@@ -1,15 +1,22 @@
 TARGET?=
 
-BUILD=target/debug
+BUILD=target
 ifneq ($(TARGET),)
-	BUILD=target/$(TARGET)/debug
+	BUILD="target/$(TARGET)"
 	CARGOFLAGS+="--target=$(TARGET)"
-	CC=$(TARGET)-gcc
+endif
+
+ifeq ($(TARGET),aarch64-unknown-linux-gnu)
+	CC="aarch64-linux-gnu-gcc"
+endif
+
+ifeq ($(TARGET),x86_64-unknown-redox)
+	CC="x86_64-unknown-redox-gcc"
 endif
 
 .PHONY: all clean fmt test
 
-all: $(BUILD)/libc.a $(BUILD)/libcrt0.a $(BUILD)/openlibm/libopenlibm.a
+all: $(BUILD)/debug/libc.a $(BUILD)/debug/libcrt0.a $(BUILD)/openlibm/libopenlibm.a
 
 clean:
 	cargo clean
@@ -21,16 +28,22 @@ fmt:
 test: all
 	make -C tests run
 
-$(BUILD)/libc.a:
+$(BUILD)/debug/libc.a: src/* src/*/* src/*/*/* src/*/*/*/*
 	cargo build $(CARGOFLAGS)
 
-$(BUILD)/libcrt0.a:
+$(BUILD)/debug/libcrt0.a: $(BUILD)/debug/libc.a
 	cargo build --manifest-path src/crt0/Cargo.toml $(CARGOFLAGS)
 
+$(BUILD)/release/libc.a: src/* src/*/* src/*/*/* src/*/*/*/*
+	cargo build --release $(CARGOFLAGS)
+
+$(BUILD)/release/libcrt0.a: $(BUILD)/release/libc.a
+	cargo build --release --manifest-path src/crt0/Cargo.toml $(CARGOFLAGS)
+
 $(BUILD)/openlibm: openlibm
 	rm -rf $@ $@.partial
 	cp -r $< $@.partial
 	mv $@.partial $@
 
 $(BUILD)/openlibm/libopenlibm.a: $(BUILD)/openlibm
-	CC=$(CC) CFLAGS=-fno-stack-protector make -C $< libopenlibm.a
+	make CC=$(CC) CFLAGS=-fno-stack-protector -C $< libopenlibm.a
diff --git a/README.md b/README.md
index 59d85250..b1838dd9 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,7 @@ relibc is a portable POSIX C standard library written in Rust. It is under heavy
 The motivation for this project is twofold: Reduce issues the redox crew was having with newlib, and create a safer alternative to a C standard library written in C. It is mainly designed to be used under redox, as an alternative to newlib, but it also supports linux syscalls via the [sc](https://crates.io/crates/sc) crate.
 
 ## Contributing
-Just search for any invocation of the `unimplemented` macro, and hop in! The ci server checks builds for linux and redox, checks formatting (via rustfmt), and runs the test suite. Run `ci.sh` locally to check that your changes will pass travis. Use `fmt.sh` to format your code and `test.sh` to run the C test suite.
+Just search for any invocation of the `unimplemented` macro, and hop in! The ci server checks builds for linux and redox, checks formatting (via rustfmt), and runs the test suite. Run `ci.sh` locally to check that your changes will pass travis. Use `fmt.sh` to format your code and `make test` to run the C test suite.
 
 ## Supported OSes
 
diff --git a/ci.sh b/ci.sh
new file mode 100755
index 00000000..1a5fee26
--- /dev/null
+++ b/ci.sh
@@ -0,0 +1,9 @@
+#!/bin/bash
+set -ex
+
+./fmt.sh -- --write-mode=diff
+make
+if [ -z "$TARGET" ]
+then
+    make test
+fi
diff --git a/test.sh b/test.sh
deleted file mode 100755
index 6b306341..00000000
--- a/test.sh
+++ /dev/null
@@ -1,7 +0,0 @@
-#!/bin/bash
-set -ex
-
-make
-
-make -C tests clean
-make -C tests run
diff --git a/tests/Makefile b/tests/Makefile
index 3f68491a..efc09a03 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -87,18 +87,20 @@ verify: $(EXPECT_BINS)
 		diff -u "gen/$${bin}.stderr" "expected/$${bin}.stderr" || exit $$?; \
 	done
 
-GCCHEAD=\
+CFLAGS=\
 	-nostdinc \
 	-nostdlib \
 	-I ../include \
 	-I ../target/include \
-	-I ../openlibm/include \
-	-I ../openlibm/src \
+	-I ../target/openlibm/include \
+	-I ../target/openlibm/src \
+
+HEADLIBS=\
 	../target/debug/libcrt0.a
 
-GCCTAIL=\
+TAILLIBS=\
 	../target/debug/libc.a \
-	../openlibm/libopenlibm.a
+	../target/openlibm/libopenlibm.a
 
-%: %.c
-	gcc -fno-stack-protector -Wall $(GCCHEAD) "$<" $(GCCTAIL) -o "$@"
+%: %.c $(HEADLIBS) $(TAILLIBS)
+	gcc -fno-stack-protector -Wall $(CFLAGS) $(HEADLIBS) "$<" $(TAILLIBS) -o "$@"
-- 
GitLab