Makefile (6954B)
1 # Build options can be changed by modifying the makefile or by building with 'make SETTING=value'. 2 # It is also possible to override the settings in Defaults in a file called .make_options as 'SETTING=value'. 3 4 -include .make_options 5 6 #### Defaults #### 7 8 # if WERROR is 1, pass -Werror to CC, so warnings would be treated as errors 9 WERROR ?= 0 10 # if RELEASE is 1 strip binaries as well as enable optimizations 11 RELEASE ?= 1 12 # On Mac, set this to `universal` to build universal (x86+ARM) binaries 13 TARGET ?= native 14 # Set to 1 to build with sanitization enabled 15 # N.B. cannot be used for `make setup` at the moment due to recomp.cpp not respecting it 16 ASAN ?= 0 17 18 # IDO 5.3 only for Super Mario 64 19 IDO_VERSION := IDO53 20 IDO_TC := cc acpp as0 as1 cfe copt ugen ujoin uld umerge uopt usplit 21 22 23 # -- determine the host environment and target 24 # | Host | Targets | 25 # |-------|-------------------| 26 # | macOS | native, universal | 27 # | linux | native | 28 # | win | native | 29 30 UNAME_S := $(shell uname -s) 31 UNAME_P := $(shell uname -p) 32 33 MAKE := make 34 ifeq ($(OS),Windows_NT) 35 DETECTED_OS := windows 36 else ifeq ($(UNAME_S),Linux) 37 DETECTED_OS := linux 38 else ifeq ($(UNAME_S),Darwin) 39 DETECTED_OS := macos 40 MAKE := gmake 41 CPPFLAGS += -xc++ 42 else 43 $(error Unsupported host OS for Makefile) 44 endif 45 46 RABBITIZER := tools/rabbitizer 47 RABBITIZER_LIB := $(RABBITIZER)/build/librabbitizerpp.a 48 49 CC := gcc 50 CXX := g++ 51 STRIP := strip 52 53 CSTD ?= -std=c11 54 CFLAGS ?= -MMD -fno-strict-aliasing -I. 55 CXXSTD ?= -std=c++17 56 CXXFLAGS ?= -MMD 57 WARNINGS ?= -Wall -Wextra 58 LDFLAGS ?= -lm 59 RECOMP_FLAGS ?= 60 61 ifneq ($(WERROR),0) 62 WARNINGS += -Werror 63 endif 64 65 ifeq ($(RELEASE),1) 66 OPTFLAGS ?= -Os 67 else 68 OPTFLAGS ?= -Og -g3 69 STRIP := @: 70 endif 71 72 ifneq ($(ASAN),0) 73 CFLAGS += -fsanitize=address -fsanitize=pointer-compare -fsanitize=pointer-subtract -fsanitize=undefined -fno-sanitize-recover=all 74 CXXFLAGS += -fsanitize=address -fsanitize=pointer-compare -fsanitize=pointer-subtract -fsanitize=undefined -fno-sanitize-recover=all 75 endif 76 77 78 ifeq ($(DETECTED_OS),windows) 79 CXXFLAGS += -static 80 endif 81 82 # -- Build Directories 83 # designed to work with Make 3.81 (macOS/last GPL-2 version) 84 # https://ismail.badawi.io/blog/automatic-directory-creation-in-make/ 85 BUILD_BASE ?= build 86 BUILD_DIR := $(BUILD_BASE) 87 BUILT_BIN := $(BUILD_DIR)/out 88 89 90 # -- Location of original IDO binaries in SM64 repo 91 IRIX_USR_DIR ?= ../ido5.3_compiler/usr 92 93 # -- Location of the irix tool chain error messages 94 ERR_STRS := $(BUILT_BIN)/err.english.cc 95 96 RECOMP_ELF := $(BUILD_BASE)/recomp.elf 97 LIBC_IMPL_O := libc_impl.o 98 99 TARGET_BINARIES := $(foreach binary,$(IDO_TC),$(BUILT_BIN)/$(binary)) 100 O_FILES := $(foreach binary,$(IDO_TC),$(BUILD_DIR)/$(binary).o) 101 C_FILES := $(O_FILES:.o=.c) 102 103 # Automatic dependency files 104 DEP_FILES := $(O_FILES:.o=.d) 105 106 # create build directories 107 $(shell mkdir -p $(BUILT_BIN)) 108 109 # per-file flags 110 # 5.3 ugen relies on UB stack reads 111 # to emulate, pass the conservative flag to `recomp` 112 $(BUILD_BASE)/ugen.c: RECOMP_FLAGS := --conservative 113 114 $(RECOMP_ELF): CXXFLAGS += -I$(RABBITIZER)/include -I$(RABBITIZER)/cplusplus/include 115 $(RECOMP_ELF): LDFLAGS += -L$(RABBITIZER)/build -lrabbitizerpp 116 117 ifneq ($(DETECTED_OS),windows) 118 # For traceback 119 $(RECOMP_ELF): LDFLAGS += -ldl 120 endif 121 ifeq ($(DETECTED_OS),linux) 122 # For traceback 123 $(RECOMP_ELF): LDFLAGS += -Wl,-export-dynamic 124 endif 125 126 # Too many warnings, disable everything for now... 127 $(RECOMP_ELF): WARNINGS += -Wpedantic -Wno-shadow -Wno-unused-variable -Wno-unused-but-set-variable -Wno-unused-parameter -Wno-implicit-fallthrough 128 %/$(LIBC_IMPL_O): CFLAGS += -D$(IDO_VERSION) 129 # TODO: fix warnings 130 %/$(LIBC_IMPL_O): WARNINGS += -Wno-unused-parameter -Wno-unused-variable -Wno-unused-but-set-variable -Wno-sign-compare -Wno-deprecated-declarations 131 132 #### Main Targets ### 133 134 all: $(TARGET_BINARIES) $(ERR_STRS) 135 136 setup: 137 $(MAKE) -C $(RABBITIZER) static CC=$(CC) CXX=$(CXX) DEBUG=$(DEBUG) 138 $(MAKE) $(RECOMP_ELF) 139 140 clean: 141 $(RM) -r $(BUILD_DIR) 142 143 distclean: 144 $(RM) -r $(BUILD_BASE) 145 $(MAKE) -C $(RABBITIZER) distclean 146 147 c_files: $(C_FILES) 148 149 150 .PHONY: all clean distclean setup 151 .DEFAULT_GOAL := all 152 # Prevent removing intermediate files 153 .SECONDARY: 154 155 156 #### Various Recipes #### 157 158 $(BUILD_BASE)/%.elf: %.cpp 159 $(CXX) $(CXXSTD) $(OPTFLAGS) $(CXXFLAGS) $(WARNINGS) -o $@ $^ $(LDFLAGS) 160 161 162 $(BUILD_DIR)/%.c: $(IRIX_USR_DIR)/lib/% 163 $(RECOMP_ELF) $(RECOMP_FLAGS) $< > $@ || ($(RM) -f $@ && false) 164 165 # cc and strip are special and are stored in the `bin` folder instead of the `lib` one 166 $(BUILD_DIR)/%.c: $(IRIX_USR_DIR)/bin/% 167 $(RECOMP_ELF) $(RECOMP_FLAGS) $< > $@ || ($(RM) -f $@ && false) 168 169 170 $(BUILT_BIN)/%.cc: $(IRIX_USR_DIR)/lib/%.cc 171 cp $^ $@ 172 173 174 ifeq ($(TARGET),universal) 175 MACOS_FAT_TARGETS ?= arm64-apple-macos11 x86_64-apple-macos10.14 176 177 FAT_FOLDERS := $(foreach target,$(MACOS_FAT_TARGETS),$(BUILD_DIR)/$(target)) 178 179 # create build directories 180 $(shell mkdir -p $(FAT_FOLDERS)) 181 182 # TODO: simplify 183 FAT_BINARIES := $(foreach binary,$(IDO_TC),$(BUILT_BIN)/arm64-apple-macos11/$(binary)) \ 184 $(foreach binary,$(IDO_TC),$(BUILT_BIN)/x86_64-apple-macos10.14/$(binary)) 185 186 $(BUILT_BIN)/%: $(BUILD_DIR)/arm64-apple-macos11/% $(BUILD_DIR)/x86_64-apple-macos10.14/% | $(ERR_STRS) 187 lipo -create -output $@ $^ 188 189 190 $(BUILD_DIR)/arm64-apple-macos11/%: $(BUILD_DIR)/arm64-apple-macos11/%.o $(BUILD_DIR)/arm64-apple-macos11/$(LIBC_IMPL_O) | $(ERR_STRS) 191 $(CC) $(CSTD) $(OPTFLAGS) $(CFLAGS) -target arm64-apple-macos11 -o $@ $^ $(LDFLAGS) 192 $(STRIP) $@ 193 194 $(BUILD_DIR)/x86_64-apple-macos10.14/%: $(BUILD_DIR)/x86_64-apple-macos10.14/%.o $(BUILD_DIR)/x86_64-apple-macos10.14/$(LIBC_IMPL_O) | $(ERR_STRS) 195 $(CC) $(CSTD) $(OPTFLAGS) $(CFLAGS) -target x86_64-apple-macos10.14 -o $@ $^ $(LDFLAGS) 196 $(STRIP) $@ 197 198 $(BUILD_DIR)/arm64-apple-macos11/%.o: $(BUILD_DIR)/%.c 199 $(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -target arm64-apple-macos11 -o $@ $< 200 201 $(BUILD_DIR)/x86_64-apple-macos10.14/%.o: $(BUILD_DIR)/%.c 202 $(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -target x86_64-apple-macos10.14 -o $@ $< 203 204 205 $(BUILD_DIR)/arm64-apple-macos11/$(LIBC_IMPL_O): libc_impl.c 206 $(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) $(WARNINGS) -target arm64-apple-macos11 -o $@ $< 207 208 $(BUILD_DIR)/x86_64-apple-macos10.14/$(LIBC_IMPL_O): libc_impl.c 209 $(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) $(WARNINGS) -target x86_64-apple-macos10.14 -o $@ $< 210 211 else 212 $(BUILT_BIN)/%: $(BUILD_DIR)/%.o $(BUILD_DIR)/$(LIBC_IMPL_O) | $(ERR_STRS) 213 $(CC) $(CSTD) $(OPTFLAGS) $(CFLAGS) -o $@ $^ $(LDFLAGS) 214 $(STRIP) $@ 215 216 $(BUILD_DIR)/%.o: $(BUILD_DIR)/%.c 217 $(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -o $@ $< 218 219 220 $(BUILD_DIR)/$(LIBC_IMPL_O): libc_impl.c 221 $(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) $(WARNINGS) -o $@ $< 222 endif 223 224 # Remove built-in rules, to improve performance 225 MAKEFLAGS += --no-builtin-rules 226 227 -include $(DEP_FILES) 228 229 # --- Debugging 230 # run `make print-VARIABLE` to debug that variable 231 print-% : ; $(info $* is a $(flavor $*) variable set to [$($*)]) @true