From 1d6aa605fb9bf30e15791d432f08d61b2bafd3bd Mon Sep 17 00:00:00 2001 From: theverygaming Date: Wed, 22 Feb 2023 09:59:59 +0100 Subject: [PATCH] better Makefile --- .gitignore | 2 ++ Makefile | 55 ++++++++++++++++++++++++++++++++++-------------------- README.md | 2 +- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index fa0df25..66bf841 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ **/.vscode/ +*.o + fox32.rom fox32rom.h diff --git a/Makefile b/Makefile index 4d796b7..71155b2 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,27 @@ +TARGET = linux +ifeq ($(TARGET),linux) +# default is used for CC SDL2_CONFIG = sdl2-config -CFLAGS = -g -Ofast -std=c99 -Wall -Wextra `$(SDL2_CONFIG) --cflags --libs` -CC_WIN = x86_64-w64-mingw32-gcc -CFLAGS_WIN = -g -Ofast -std=c99 -Wall -Wextra -lmingw32 -lSDL2main -lSDL2 -CC_WASM = emcc -CFLAGS_WASM = -O3 -std=c99 -Wall -Wextra -s TOTAL_MEMORY=70057984 -sALLOW_MEMORY_GROWTH=1 -sUSE_SDL=2 --preload-file fox32os.img -TARGET=fox32 -TARGET_WIN=fox32.exe -TARGET_WASM=fox32.html +CFLAGS += -g -Ofast -std=c99 -Wall -Wextra `$(SDL2_CONFIG) --cflags` +LDFLAGS += `$(SDL2_CONFIG) --libs` +else +ifeq ($(TARGET),mingw) +CC = x86_64-w64-mingw32-gcc +CFLAGS += -g -Ofast -std=c99 -Wall -Wextra +LDFLAGS += -lmingw32 -lSDL2main -lSDL2 +TARGET_FILE_EXTENSION = .exe +else +ifeq ($(TARGET),wasm) +CC = emcc +CFLAGS += -O3 -std=c99 -Wall -Wextra +LDFLAGS += -s TOTAL_MEMORY=70057984 -sALLOW_MEMORY_GROWTH=1 -sUSE_SDL=2 --preload-file fox32os.img +TARGET_EXTRADEPS = fox32os.img +TARGET_FILE_EXTENSION = .html +else +$(error unknown TARGET) +endif +endif +endif CFILES = src/main.c \ src/bus.c \ @@ -19,23 +34,23 @@ CFILES = src/main.c \ src/screen.c \ src/serial.c +OBJS = $(addsuffix .o, $(basename $(CFILES))) + +.PHONY: all +all: fox32$(TARGET_FILE_EXTENSION) + FOX32ROM_IN = fox32.rom FOX32ROM_OUT = fox32rom.h -$(TARGET): $(CFILES) $(FOX32ROM_IN) +$(FOX32ROM_OUT): $(FOX32ROM_IN) xxd -i $(FOX32ROM_IN) $(FOX32ROM_OUT) - sed -i -e 's/fox32_rom/fox32rom/' fox32rom.h - $(CC) -o $@ $(filter %.c, $^) $(CFLAGS) + sed -i -e 's/fox32_rom/fox32rom/' $(FOX32ROM_OUT) -$(TARGET_WIN): $(CFILES) - xxd -i $(FOX32ROM_IN) $(FOX32ROM_OUT) - sed -i -e 's/fox32_rom/fox32rom/' fox32rom.h - $(CC_WIN) -o $@ $(filter %.c, $^) $(CFLAGS_WIN) +fox32$(TARGET_FILE_EXTENSION): $(TARGET_EXTRADEPS) $(OBJS) + $(CC) -o $@ $(OBJS) $(LDFLAGS) -$(TARGET_WASM): $(CFILES) $(FOX32ROM_IN) - xxd -i $(FOX32ROM_IN) $(FOX32ROM_OUT) - sed -i -e 's/fox32_rom/fox32rom/' fox32rom.h - $(CC_WASM) -o $@ $(filter %.c, $^) $(CFLAGS_WASM) +%.o: %.c $(FOX32ROM_OUT) + $(CC) -o $@ -c $< $(CFLAGS) clean: - rm -rf $(TARGET) $(TARGET_WIN) $(TARGET_WASM) + rm -rf fox32 fox32.exe fox32.wasm fox32.html fox32.data fox32.js fox32rom.h $(OBJS) diff --git a/README.md b/README.md index 11698eb..66b4e45 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Prebuilt Linux binaries of the latest commit are also available on the [GitHub A ### Building -Download the latest release or commit of [**fox32rom**](https://github.com/fox32-arch/fox32rom), and place the downloaded `fox32.rom` file into the root directory of this repo. Then simply run `make`. The resulting binary will be saved as `fox32`. +Download the latest release or commit of [**fox32rom**](https://github.com/fox32-arch/fox32rom), and place the downloaded `fox32.rom` file into the root directory of this repo. Then simply run `make`. The resulting binary will be saved as `fox32`. Optionally you may build for a different target with `make TARGET=`, see the Makefile for details. ### Usage