Refactor makefile, make it parallelizable

These changes make it possible to run with -jN, if the user has
configured it as such (through e.g. MAKEFLAGS).

It also simplifies quite a few things, and makes running `make` without
arguments just work out of the box.
This commit is contained in:
mid-kid
2024-11-06 18:58:42 +01:00
parent d8040bf93d
commit 55fefc18bf

146
Makefile
View File

@@ -1,12 +1,12 @@
.PHONY: configure rom check target format clean setup_release setup_debug release debug
.PHONY: all release debug check rom target format clean distclean setup_release setup_debug configure
SHELL := /bin/bash
POKEPLATINUM_BUILD ?= build
BUILD ?= build
TOOLS := tools
MESON ?= meson
NINJA ?= ninja
ROOT_INI := $(POKEPLATINUM_BUILD)/root.ini
DOT_MWCONFIG := $(POKEPLATINUM_BUILD)/.mwconfig
ROOT_INI := $(BUILD)/root.ini
DOT_MWCONFIG := $(BUILD)/.mwconfig
WINELOADER ?= wine
UNAME_R := $(shell uname -r)
@@ -23,41 +23,22 @@ else
WSL_ACCESSING_WINDOWS := 1
endif
WRAP_CONFIG := 0
ifneq (,$(findstring Linux,$(UNAME_S)))
ifeq (0,$(WSL_ACCESSING_WINDOWS))
NATIVE := native.ini
CROSS := cross_unix.ini
WRAP_CONFIG := 0
else
NATIVE := native_unix.ini
CROSS := cross_unix.ini
WRAP_CONFIG := 0
endif
else
ifneq (,$(findstring Darwin,$(UNAME_S)))
NATIVE := native_macos.ini
CROSS := cross_unix.ini
WRAP_CONFIG := 0
else
NATIVE := native.ini
CROSS := cross.ini
WRAP_CONFIG := 1
endif
endif
ifneq (,$(findstring native_,$(NATIVE)))
WRAP_WINE := $(shell which $(WINELOADER))
WRAP_PATH_UNX := $(CWD)
WRAP_PATH_WIN := $(shell $(WRAP_WINE) winepath -w $(WRAP_PATH_UNX))
WRAP_PATH_BUILD_UNX := $(POKEPLATINUM_BUILD)
WRAP_PATH_BUILD_WIN := $(shell $(WRAP_WINE) winepath -w $(WRAP_PATH_BUILD_UNX))
else
ifeq (0,$(WSL_ACCESSING_WINDOWS))
WRAP_WINE :=
WRAP_PATH_UNX := $(CWD)
WRAP_PATH_WIN := $(shell wslpath -w $(WRAP_PATH_UNX))
WRAP_PATH_BUILD_UNX := $(POKEPLATINUM_BUILD)
WRAP_PATH_BUILD_WIN := $(shell wslpath -w $(WRAP_PATH_BUILD_UNX))
endif
endif
@@ -65,68 +46,81 @@ WRAP := $(TOOLS)/cw
WRAP_BUILD := $(WRAP)/build
MWRAP := $(WRAP)/mwrap
ROOT := $(CWD)
NINJA_STATUS := "[%p %f/%t] "
export NINJA_STATUS := [%p %f/%t]
all: release check
release: setup_release rom
release: setup_release .WAIT rom
debug: setup_debug rom
@NINJA_STATUS=$(NINJA_STATUS) $(MESON) compile -C $(POKEPLATINUM_BUILD) "debug.nef" "overlay.map"
debug: setup_debug .WAIT rom
$(MESON) compile -C $(BUILD) "debug.nef" "overlay.map"
setup_release:
@$(MESON) configure build "-Dgdb_debugging=false"
check: rom
$(MESON) test -C $(BUILD)
setup_debug:
@$(MESON) configure build "-Dgdb_debugging=true"
rom: $(BUILD)/build.ninja
$(MESON) compile -C $(BUILD) "pokeplatinum.us.nds"
rom:
@NINJA_STATUS=$(NINJA_STATUS) $(MESON) compile -C $(POKEPLATINUM_BUILD) "pokeplatinum.us.nds"
target: $(BUILD)/build.ninja
$(MESON) compile -C $(BUILD) $(MESON_TARGET)
check:
@NINJA_STATUS=$(NINJA_STATUS) $(MESON) test -C $(POKEPLATINUM_BUILD)
format: $(BUILD)/build.ninja
$(MESON) compile -C $(BUILD) clang-format
target:
@NINJA_STATUS=$(NINJA_STATUS) $(MESON) compile -C $(POKEPLATINUM_BUILD) $(MESON_TARGET)
clean: $(BUILD)/build.ninja
$(MESON) compile -C $(BUILD) --clean
distclean:
rm -rf $(BUILD) $(MWRAP)
setup_release: $(BUILD)/build.ninja
$(MESON) configure build -Dgdb_debugging=false
setup_debug: $(BUILD)/build.ninja
$(MESON) configure build -Dgdb_debugging=true
configure: $(BUILD)/build.ninja
$(BUILD)/build.ninja: $(ROOT_INI) $(DOT_MWCONFIG) | $(BUILD)
MWCONFIG=$(abspath $(DOT_MWCONFIG)) $(MESON) setup \
--wrap-mode=nopromote \
--native-file=meson/$(NATIVE) \
--native-file=$(ROOT_INI) \
--cross-file=meson/$(CROSS) \
--cross-file=$(ROOT_INI) \
-- $(BUILD)
$(ROOT_INI): | $(BUILD)
echo "[constants]" > $@
echo "root = '$$PWD'" >> $@
$(DOT_MWCONFIG): $(MWRAP) | $(BUILD)
ifneq (,$(filter native_%.ini,$(NATIVE)))
WINE="$$(command -v $(WINELOADER))"; \
BUILD="$(BUILD)"; \
MWCONFIG=$(abspath $(DOT_MWCONFIG)) $(MWRAP) -conf \
-wine "$$WINE" \
-path_unx "$$PWD" \
-path_win "$$("$$WINE" winepath -w "$$PWD")" \
-path_build_unx "$$BUILD" \
-path_build_win "$$("$$WINE" winepath -w "$$BUILD")"
else ifeq (0,$(WSL_ACCESSING_WINDOWS))
BUILD="$(BUILD)"; \
MWCONFIG=$(abspath $(DOT_MWCONFIG)) $(MWRAP) -conf \
-path_unx "$$PWD" \
-path_win "$$(wslpath -w "$$PWD")" \
-path_build_unx "$$BUILD" \
-path_build_win "$$(wslpath -w "$$PWD")"
else
touch $(DOT_MWCONFIG)
endif
$(BUILD):
mkdir -p -- $(BUILD)
$(MWRAP):
rm -rf $(MWRAP) $(WRAP_BUILD)
$(MESON) setup "$(WRAP_BUILD)" "$(WRAP)"
$(MESON) compile -C "$(WRAP_BUILD)"
install -m755 "$(WRAP_BUILD)/$(@F)" $@
$(MESON) setup $(WRAP_BUILD) $(WRAP)
$(MESON) compile -C $(WRAP_BUILD)
install -m755 $(WRAP_BUILD)/$(@F) $@
rm -rf $(WRAP_BUILD)
$(POKEPLATINUM_BUILD):
mkdir -p -- $(POKEPLATINUM_BUILD)
$(ROOT_INI): | $(POKEPLATINUM_BUILD)
echo "[constants]" > $@
echo "root = '$(ROOT)'" >> $@
$(DOT_MWCONFIG): | $(POKEPLATINUM_BUILD)
touch $(DOT_MWCONFIG)
configure: $(MWRAP) $(ROOT_INI) $(DOT_MWCONFIG)
ifeq (0,$(WRAP_CONFIG))
MWCONFIG=$(realpath -- $(DOT_MWCONFIG)) $(MWRAP) -conf \
-wine "$(WRAP_WINE)" \
-path_unx "$(WRAP_PATH_UNX)" \
-path_win "$(WRAP_PATH_WIN)" \
-path_build_unx "$(WRAP_PATH_BUILD_UNX)" \
-path_build_win "$(WRAP_PATH_BUILD_WIN)"
endif
MWCONFIG=$(realpath -- $(DOT_MWCONFIG)) $(MESON) setup \
--wrap-mode=nopromote \
--native-file=meson/$(NATIVE) \
--native-file=$(ROOT_INI) \
--cross-file=meson/$(CROSS) \
--cross-file=$(ROOT_INI) \
-- $(POKEPLATINUM_BUILD)
format:
@$(NINJA) -C $(POKEPLATINUM_BUILD) clang-format
clean:
@$(MESON) compile -C $(POKEPLATINUM_BUILD) --clean