From fe1febfc4b68c0494790d915c6247d98ed2205e9 Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Sun, 30 Jan 2022 02:18:57 -0500 Subject: Update build system to use Makefile `make` to compile the compiler `make XXX.out` to assemble/link `XXX.nasm` into an executable `make test` to run all tests `make tests/XXX` to run `tests/XXX.sh` test file `./run.sh ` to build, compile, run and show exit code --- .gitignore | 4 +++- Makefile | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ assemble.sh | 25 ------------------------- compile.sh | 9 --------- run.sh | 17 ++++++++++++----- test.sh | 12 ------------ tests/common.sh | 25 ++++--------------------- 7 files changed, 75 insertions(+), 73 deletions(-) create mode 100644 Makefile delete mode 100755 assemble.sh delete mode 100755 compile.sh delete mode 100755 test.sh diff --git a/.gitignore b/.gitignore index 735abfa..5690676 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,6 @@ cupcc *.nasm *.o -*.out \ No newline at end of file +*.out + +build \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..97c1baf --- /dev/null +++ b/Makefile @@ -0,0 +1,56 @@ +# No separable compilation here, the program is too small to bother + +CC = gcc +CFLAGS = -Wall -Wextra -Werror -ggdb3 +SRCS = $(wildcard src/*.c) + +.PHONY: compile test clean + + +build/cupcc: build FORCE + $(CC) $(CFLAGS) -o $@ $(SRCS) + +compile: build/cupcc + +FORCE: + +UNAME_S := $(shell uname -s) +ifeq ($(UNAME_S),Linux) + +%.o: %.nasm FORCE + nasm -felf64 $< -o $@ + +%.out: %.o FORCE + ld -o $@ $< + +endif +ifeq ($(UNAME_S),Darwin) + +%.o: %.nasm FORCE + nasm -fmacho64 $< -o $@ + +%.out: %.o FORCE + ld $< -o $@ -lSystem + +endif + +build: + @mkdir -p build + +clean: + rm -rf build + +test: compile + @for f in $(shell ls tests | grep -v "common"); \ + do echo "Running $${f}"; \ + ./tests/$${f}; \ + done + +tests/%.sh: compile + @echo "Running $@" + @$@ + +tests/%: compile + @echo "Running $@.sh" + @$@.sh + diff --git a/assemble.sh b/assemble.sh deleted file mode 100755 index ed9b46e..0000000 --- a/assemble.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/bash - -# NOTE: Only for macOS (intel) - -if [ -z "$1" ] -then - echo "Usage: $0 " - exit 1 -fi - - -case "$(uname -s)" in - Darwin) - set -xe - nasm -f macho64 -o $1.o $1 - ld -lSystem $1.o - set +xe - ;; - Linux) - set -xe - nasm -f elf64 -o $1.o $1 - ld $1.o - set +xe - ;; -esac \ No newline at end of file diff --git a/compile.sh b/compile.sh deleted file mode 100755 index fa24474..0000000 --- a/compile.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -CC=gcc -CFLAGS="-Wall -Wextra -Werror -ggdb3" -SRCS=src/*.c - -set -xe - -$CC $CFLAGS $SRCS -o cupcc diff --git a/run.sh b/run.sh index 6038d91..19360df 100755 --- a/run.sh +++ b/run.sh @@ -1,18 +1,25 @@ #!/bin/bash +# This script does the following: +# 1. Builds the project +# 2. Compiles selected file +# 3. Assembles executable from compiled asm +# 4. Runs the executable +# 5. Echoes the output of the executable + if [ -z "$1" ] then - echo "Usage: $0 " + echo "Usage: $0 " exit 1 fi set -xe -./compile.sh -./cupcc "$@" -./assemble.sh output.nasm +make +build/cupcc "$@" +make build/output.out set +e -./a.out +build/output.out echo "Exit status: $?" \ No newline at end of file diff --git a/test.sh b/test.sh deleted file mode 100755 index 091a2bb..0000000 --- a/test.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash - -set -e -./compile.sh - -for i in `ls tests/*.sh | grep -v common.sh` -do - echo "Running $i" - bash $i -done - -set +e \ No newline at end of file diff --git a/tests/common.sh b/tests/common.sh index f863826..575443c 100644 --- a/tests/common.sh +++ b/tests/common.sh @@ -1,28 +1,11 @@ #!/bin/bash -function assemble() { - case "$(uname -s)" in - Darwin) - set -e - nasm -f macho64 -o output.nasm.o output.nasm - ld -lSystem output.nasm.o - set +e - ;; - Linux) - set -e - nasm -f elf64 -o output.nasm.o output.nasm - ld output.nasm.o - set +e - ;; - esac -} - function assert_exit_status() { - ./cupcc -c "$1" - assemble + build/cupcc -c "$1" -o build/test.nasm + make build/test.out -s set +e - ./a.out + build/test.out res=$? set -e if [ $res -ne $2 ] @@ -45,7 +28,7 @@ function assert_exit_status_stdin() { function assert_compile_failure_stdin() { code=$(/dev/null 2>&1 + build/cupcc -c "$code" -o build/test.nasm >/dev/null 2>&1 res=$? set -e if [ $res -eq 0 ] -- cgit v1.2.3