|
| 1 | +BUILD ?= Release |
| 2 | +DESTDIR ?= /usr/local |
| 3 | +CROSS ?= none |
| 4 | + |
| 5 | +UNAME_S := $(shell uname -s) |
| 6 | +UNAME_M := $(shell uname -m) |
| 7 | +rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2)) |
| 8 | +CC_IS_CLANG := $(shell $(CC) --version | grep -q clang && echo true || echo false) |
| 9 | + |
| 10 | +PACKAGES := zlib |
| 11 | + |
| 12 | +ifeq ($(wildcard third_party/ELFIO/elfio/elfio.hpp),) |
| 13 | +HAS_SUBMODULES = false |
| 14 | +else |
| 15 | +HAS_SUBMODULES = true |
| 16 | +endif |
| 17 | + |
| 18 | +CXXFLAGS += -std=c++2b |
| 19 | +CPPFLAGS += `pkg-config --cflags $(PACKAGES)` |
| 20 | +CPPFLAGS += -I. |
| 21 | +CPPFLAGS += -Isrc |
| 22 | +CPPFLAGS += -Ithird_party |
| 23 | +CPPFLAGS += -Ithird_party/ELFIO |
| 24 | +CPPFLAGS += -Ithird_party/fmt/include/ |
| 25 | +CPPFLAGS += -Ithird_party/googletest/googletest/include |
| 26 | +CPPFLAGS += -Ithird_party/ucl -Ithird_party/ucl/include |
| 27 | +CPPFLAGS += -g |
| 28 | + |
| 29 | +CPPFLAGS_Release += -O3 |
| 30 | +CPPFLAGS_Debug += -O0 |
| 31 | +CPPFLAGS_Coverage += -O0 |
| 32 | +ifeq ($(CC_IS_CLANG),true) |
| 33 | + CPPFLAGS_Coverage += -fprofile-instr-generate -fcoverage-mapping |
| 34 | +else |
| 35 | + CPPFLAGS_Coverage += -fprofile-arcs -ftest-coverage |
| 36 | +endif |
| 37 | +CPPFLAGS_asan += -O1 -fsanitize=address -fno-omit-frame-pointer |
| 38 | +CPPFLAGS_ubsan += -O1 -fsanitize=undefined -fno-omit-frame-pointer |
| 39 | +CPPFLAGS_lto += -O3 -flto=auto -fno-fat-lto-objects -flto-partition=one |
| 40 | + |
| 41 | +ifeq ($(CC_IS_CLANG),true) |
| 42 | + CXXFLAGS += -fcoroutines-ts |
| 43 | +else |
| 44 | + CXXFLAGS += -fcoroutines |
| 45 | +endif |
| 46 | + |
| 47 | +ifeq ($(UNAME_S),Darwin) |
| 48 | + CPPFLAGS += -mmacosx-version-min=10.15 |
| 49 | + CPPFLAGS += -stdlib=libc++ |
| 50 | +endif |
| 51 | + |
| 52 | +LDFLAGS += `pkg-config --libs $(PACKAGES)` |
| 53 | + |
| 54 | +ifeq ($(UNAME_S),Darwin) |
| 55 | + LDFLAGS += -lc++ |
| 56 | + LDFLAGS += -mmacosx-version-min=10.15 |
| 57 | +else |
| 58 | + LDFLAGS += -lstdc++fs |
| 59 | +endif |
| 60 | + |
| 61 | +LDFLAGS += -g |
| 62 | + |
| 63 | +ifeq ($(CC_IS_CLANG),true) |
| 64 | + LDFLAGS_Coverage += -fprofile-instr-generate -fcoverage-mapping |
| 65 | +else |
| 66 | + LDFLAGS_Coverage += -fprofile-arcs -ftest-coverage |
| 67 | +endif |
| 68 | +LDFLAGS_asan += -fsanitize=address |
| 69 | +LDFLAGS_ubsan += -fsanitize=undefined |
| 70 | +LDFLAGS_lto += -O3 -flto=auto -flto-partition=one |
| 71 | + |
| 72 | +CPPFLAGS += $(CPPFLAGS_$(BUILD)) -pthread |
| 73 | +LDFLAGS += $(LDFLAGS_$(BUILD)) -pthread |
| 74 | + |
| 75 | +ifeq ($(CROSS),arm64) |
| 76 | + CPPFLAGS += -fPIC -Wl,-rpath-link,/opt/cross/sysroot/usr/lib/aarch64-linux-gnu -L/opt/cross/sysroot/usr/lib/aarch64-linux-gnu |
| 77 | + LDFLAGS += -fPIC -Wl,-rpath-link,/opt/cross/sysroot/usr/lib/aarch64-linux-gnu -L/opt/cross/sysroot/usr/lib/aarch64-linux-gnu |
| 78 | +endif |
| 79 | + |
| 80 | +LD := $(CXX) |
| 81 | + |
| 82 | +SRCS := $(call rwildcard,src/,*.cc) |
| 83 | +SRCS += third_party/fmt/src/os.cc third_party/fmt/src/format.cc |
| 84 | +SRCS += $(wildcard third_party/cueparser/*.c) |
| 85 | +SRCS += $(wildcard third_party/iec-60908b/*.c) |
| 86 | +SRCS += third_party/ucl/src/n2e_99.c third_party/ucl/src/alloc.c |
| 87 | + |
| 88 | +TOOLS = exe2elf exe2iso ps1-packer psyq-obj-parser |
| 89 | + |
| 90 | +############################################################################## |
| 91 | + |
| 92 | +OBJECTS += $(patsubst %.c,%.o,$(filter %.c,$(SRCS))) |
| 93 | +OBJECTS += $(patsubst %.cc,%.o,$(filter %.cc,$(SRCS))) |
| 94 | +OBJECTS += $(patsubst %.cpp,%.o,$(filter %.cpp,$(SRCS))) |
| 95 | +OBJECTS += $(patsubst %.mm,%.o,$(filter %.mm,$(SRCS))) |
| 96 | + |
| 97 | +TESTS_SRC := $(call rwildcard,tests/,*.cc) |
| 98 | +TESTS := $(patsubst %.cc,%,$(TESTS_SRC)) |
| 99 | + |
| 100 | +CP ?= cp |
| 101 | +MKDIRP ?= mkdir -p |
| 102 | + |
| 103 | +all: check_submodules dep tools |
| 104 | + |
| 105 | +ifeq ($(HAS_SUBMODULES),true) |
| 106 | +check_submodules: |
| 107 | + |
| 108 | +else |
| 109 | +check_submodules: |
| 110 | + @echo "You need to clone this repository recursively, in order to get its submodules." |
| 111 | + @false |
| 112 | +endif |
| 113 | + |
| 114 | +strip: all |
| 115 | + strip $(TOOLS) |
| 116 | + |
| 117 | +install: all strip |
| 118 | + $(MKDIRP) $(DESTDIR)/bin |
| 119 | + $(CP) $(TOOLS) $(DESTDIR)/bin |
| 120 | + |
| 121 | +%.o: %.c |
| 122 | + $(CC) -c -o $@ $< $(CPPFLAGS) $(EXTRA_CPPFLAGS) $(CFLAGS) |
| 123 | + |
| 124 | +%.o: %.cc |
| 125 | + $(CXX) -c -o $@ $< $(CPPFLAGS) $(EXTRA_CPPFLAGS) $(CXXFLAGS) |
| 126 | + |
| 127 | +%.o: %.cpp |
| 128 | + $(CXX) -c -o $@ $< $(CPPFLAGS) $(EXTRA_CPPFLAGS) $(CXXFLAGS) |
| 129 | + |
| 130 | +%.o: %.mm |
| 131 | + $(CC) -c -o $@ $< $(CPPFLAGS) $(EXTRA_CPPFLAGS) $(CFLAGS) |
| 132 | + |
| 133 | +%.dep: %.c |
| 134 | + $(CC) $(CPPFLAGS) $(EXTRA_CPPFLAGS) $(CFLAGS) -M -MT $(addsuffix .o, $(basename $@)) -MF $@ $< |
| 135 | + |
| 136 | +%.dep: %.cc |
| 137 | + $(CXX) $(CPPFLAGS) $(EXTRA_CPPFLAGS) $(CXXFLAGS) -M -MT $(addsuffix .o, $(basename $@)) -MF $@ $< |
| 138 | + |
| 139 | +%.dep: %.cpp |
| 140 | + $(CXX) $(CPPFLAGS) $(EXTRA_CPPFLAGS) $(CXXFLAGS) -M -MT $(addsuffix .o, $(basename $@)) -MF $@ $< |
| 141 | + |
| 142 | +clean: |
| 143 | + rm -f $(OBJECTS) $(TARGET) $(DEPS) gtest-all.o gtest_main.o |
| 144 | + |
| 145 | +gtest-all.o: $(wildcard third_party/googletest/googletest/src/*.cc) |
| 146 | + $(CXX) -O3 -g $(CXXFLAGS) -Ithird_party/googletest/googletest -Ithird_party/googletest/googletest/include -c third_party/googletest/googletest/src/gtest-all.cc |
| 147 | + |
| 148 | +gtest_main.o: third_party/googletest/googletest/src/gtest_main.cc |
| 149 | + $(CXX) -O3 -g $(CXXFLAGS) -Ithird_party/googletest/googletest -Ithird_party/googletest/googletest/include -c third_party/googletest/googletest/src/gtest_main.cc |
| 150 | + |
| 151 | +gitclean: |
| 152 | + git clean -f -d -x |
| 153 | + git submodule foreach --recursive git clean -f -d -x |
| 154 | + |
| 155 | +tests: $(foreach t,$(TESTS),$(t).o) $(NONMAIN_OBJECTS) gtest-all.o gtest_main.o |
| 156 | + $(LD) -o tests $(NONMAIN_OBJECTS) gtest-all.o gtest_main.o $(foreach t,$(TESTS),$(t).o) -Ithird_party/googletest/googletest/include $(LDFLAGS) |
| 157 | + |
| 158 | +runtests: tests |
| 159 | + ./tests |
| 160 | + |
| 161 | +define TOOLDEF |
| 162 | +$(1): $(OBJECTS) tools/$(1)/$(1).o |
| 163 | + $(LD) -o $(1) $(CPPFLAGS) $(CXXFLAGS) $(OBJECTS) tools/$(1)/$(1).o -static $(LDFLAGS) |
| 164 | + |
| 165 | +endef |
| 166 | + |
| 167 | +$(foreach tool,$(TOOLS),$(eval $(call TOOLDEF,$(tool)))) |
| 168 | + |
| 169 | +tools: $(TOOLS) |
| 170 | + |
| 171 | +.PHONY: all dep clean gitclean runtests install strip tools |
| 172 | + |
| 173 | +DEPS += $(patsubst %.c,%.dep,$(filter %.c,$(SRCS))) |
| 174 | +DEPS := $(patsubst %.cc,%.dep,$(filter %.cc,$(SRCS))) |
| 175 | +DEPS += $(patsubst %.cpp,%.dep,$(filter %.cpp,$(SRCS))) |
| 176 | + |
| 177 | +dep: $(DEPS) |
| 178 | + |
| 179 | +ifneq ($(MAKECMDGOALS), clean) |
| 180 | +ifneq ($(MAKECMDGOALS), gitclean) |
| 181 | +ifeq ($(HAS_SUBMODULES), true) |
| 182 | +-include $(DEPS) |
| 183 | +endif |
| 184 | +endif |
| 185 | +endif |
0 commit comments