Zelda64Recomp/Makefile

147 lines
4.4 KiB
Makefile

CONFIG ?= Debug
LIB ?= 0
ifeq ($(CONFIG),Debug)
BUILD_DIR := build/Debug
FUNC_OPTFLAGS := -Og -g -fno-strict-aliasing
OPTFLAGS := -Og -g -fno-strict-aliasing
# Static C runtime linking
LIBS := -Wl,/nodefaultlib:libcmt -Wl,/nodefaultlib:ucrt -Wl,/nodefaultlib:libucrt -llibcmtd -llibvcruntimed -llibucrtd
# Dynamic
# LIBS := -Wl,/nodefaultlib:libcmt -Wl,/nodefaultlib:ucrt -Wl,/nodefaultlib:libucrt -lmsvcrtd -lvcruntimed -lucrtd
else ifeq ($(CONFIG),Release)
BUILD_DIR := build/Release
FUNC_OPTFLAGS := -O2 -g -fno-strict-aliasing
OPTFLAGS := -O2 -g -fno-strict-aliasing
else
$(error "Invalid build configuration: $(CONFIG)")
endif
ifeq ($(OS),Windows_NT)
DYN_EXT := .dll
LIB_EXT := .lib
EXE_EXT := .exe
AR := clang++
ARFLAGS := $(OPTFLAGS) -fuse-ld=llvm-lib -o
# Static C runtime linking
LIBS := -Wl,/nodefaultlib:libcmt -Wl,/nodefaultlib:ucrt -Wl,/nodefaultlib:libucrt -llibcmt -llibvcruntime -llibucrt
# Dynamic
# LIBS := -Wl,/nodefaultlib:libcmt -Wl,/nodefaultlib:ucrt -Wl,/nodefaultlib:libucrt -lmsvcrt -lvcruntime -lucrt
LIB_DIR ?= C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\lib\x64
UCRT_DIR ?= C:\Program Files (x86)\Windows Kits\10\lib\10.0.22000.0\ucrt\x64;
SDK_DIR ?= C:\Program Files (x86)\Windows Kits\10\lib\10.0.22000.0\um\x64
define mkdir
mkdir $(subst /,\\,$(1))
endef
define rmdir
rmdir /S /Q $(subst /,\\,$(1))
endef
else
DYN_EXT := .so
LIB_EXT := .a
EXE_EXT :=
LIB_PRE := lib
AR := ar
ARFLAGS := rcs
define mkdir
mkdir -p $(1)
endef
define rmdir
rm -rf $(1)
endef
endif
SRC_DIRS := portultra src rsp
# Add the main folder if not building a library
ifneq ($(LIB),1)
SRC_DIRS += src/main
endif
FUNCS_DIR := RecompiledFuncs
FUNCS_LIB := $(BUILD_DIR)/$(LIB_PRE)RecompiledFuncs$(LIB_EXT)
FUNCS_C_SRCS := $(wildcard $(FUNCS_DIR)/*.c)
FUNCS_CXX_SRCS := $(wildcard $(FUNCS_DIR)/*.cpp)
FUNC_BUILD_DIR := $(BUILD_DIR)/RecompiledFuncs
FUNCS_C_OBJS := $(addprefix $(BUILD_DIR)/,$(FUNCS_C_SRCS:.c=.o))
FUNCS_CXX_OBJS := $(addprefix $(BUILD_DIR)/,$(FUNCS_CXX_SRCS:.cpp=.o))
ALL_FUNC_OBJS := $(FUNCS_C_OBJS) $(FUNCS_CXX_OBJS)
BUILD_SRC_DIRS := $(addprefix $(BUILD_DIR)/,$(SRC_DIRS))
C_SRCS := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.c))
CXX_SRCS := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.cpp))
C_OBJS := $(addprefix $(BUILD_DIR)/,$(C_SRCS:.c=.o))
CXX_OBJS := $(addprefix $(BUILD_DIR)/,$(CXX_SRCS:.cpp=.o))
ALL_OBJS := $(C_OBJS) $(CXX_OBJS)
CC := clang
CXX := clang++
LD := clang++
FUNC_CFLAGS := $(FUNC_OPTFLAGS) -c -Wno-unused-but-set-variable
FUNC_CXXFLAGS := $(FUNC_OPTFLAGS) -std=c++20 -c
FUNC_CPPFLAGS := -Iinclude
WARNFLAGS := -Wall -Wextra -Wpedantic -Wno-gnu-anonymous-struct
CFLAGS := -ffunction-sections -fdata-sections -march=nehalem $(OPTFLAGS) $(WARNFLAGS) -c
CXXFLAGS := -ffunction-sections -fdata-sections -march=nehalem $(OPTFLAGS) $(WARNFLAGS) -std=c++20 -c
CPPFLAGS := -Iinclude -Ithirdparty
ifeq ($(OS),Windows_NT)
LDFLAGS := -v -Wl,/OPT:REF $(OPTFLAGS) $(LIBS) -L"$(LIB_DIR:;=)" -L"$(UCRT_DIR:;=)" -L"$(SDK_DIR:;=)" lib/RT64/$(CONFIG)/RT64.lib
else
LDFLAGS := $(OPTFLAGS) -L$(BUILD_DIR) -lRecompiledFuncs -L. -lrt64 -lSDL2 -lX11 -Wl,--gc-sections
FUNC_CFLAGS += -ffunction-sections -fdata-sections
FUNC_CXXFLAGS += -ffunction-sections -fdata-sections
EXTRA_DEPS := librt64.a
endif
ifeq ($(LIB),1)
TARGET := $(BUILD_DIR)/MMRecomp$(DYN_EXT)
LDFLAGS += -shared
else
TARGET := $(BUILD_DIR)/MMRecomp$(EXE_EXT)
endif
default: $(TARGET)
clean:
$(call rmdir,$(BUILD_DIR))
cleanfuncs:
$(FUNCS_CXX_OBJS) : $(BUILD_DIR)/%.o : %.cpp | $(FUNC_BUILD_DIR)
@$(CXX) $(FUNC_CXXFLAGS) $(FUNC_CPPFLAGS) $^ -o $@
$(FUNCS_C_OBJS) : $(BUILD_DIR)/%.o : %.c | $(FUNC_BUILD_DIR)
@$(CC) $(FUNC_CFLAGS) $(FUNC_CPPFLAGS) $^ -o $@
$(FUNCS_LIB): $(ALL_FUNC_OBJS) | $(BUILD_DIR)
$(AR) $(ARFLAGS) $@ $(FUNC_BUILD_DIR)/*.o
$(CXX_OBJS) : $(BUILD_DIR)/%.o : %.cpp | $(BUILD_SRC_DIRS)
$(CXX) -MMD -MF $(@:.o=.d) $(CXXFLAGS) $(CPPFLAGS) $< -o $@
$(C_OBJS) : $(BUILD_DIR)/%.o : %.c | $(BUILD_SRC_DIRS)
$(CC) -MMD -MF $(@:.o=.d) $(CFLAGS) $(CPPFLAGS) $< -o $@
$(TARGET): $(FUNCS_LIB) $(ALL_OBJS) $(EXTRA_DEPS) | $(BUILD_SRC_DIRS)
$(LD) -o $@ $^ $(LDFLAGS)
$(BUILD_SRC_DIRS) $(FUNC_BUILD_DIR) $(BUILD_DIR):
$(call mkdir,$@)
-include $(ALL_OBJS:.o=.d)
MAKEFLAGS += --no-builtin-rules
.SUFFIXES:
.PHONY: default clean cleanfuncs
print-% : ; $(info $* is a $(flavor $*) variable set to [$($*)]) @true