Skip to content

Commit 1f0f9f2

Browse files
authored
Reduce Makefile duplication (#71)
* Reduce Makefile duplication * Really remove built-in rules
1 parent 0ce6f66 commit 1f0f9f2

File tree

2 files changed

+60
-124
lines changed

2 files changed

+60
-124
lines changed

Makefile

+60-107
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,12 @@ BUILT_BIN := $(BUILD_DIR)/out
116116
IRIX_BASE ?= ido
117117
IRIX_USR_DIR ?= $(IRIX_BASE)/$(VERSION)/usr
118118

119-
# -- Location of the irix tool chain error messages
120-
ERR_STRS := $(BUILT_BIN)/err.english.cc
121-
LIBS := $(foreach lib,$(IDO_LIBS),$(BUILT_BIN)/$(lib))
119+
# -- IRIX toolchain error messages and libraries for linking
120+
RUNTIME_DEPS := $(BUILT_BIN)/err.english.cc $(foreach lib,$(IDO_LIBS),$(BUILT_BIN)/$(lib))
122121

123122
RECOMP_ELF := $(BUILD_BASE)/recomp.elf
124-
LIBC_IMPL := libc_impl
125-
VERSION_INFO := version_info
126123

127124
TARGET_BINARIES := $(foreach binary,$(IDO_TC),$(BUILT_BIN)/$(binary))
128-
# NCC is filtered out since it isn't an actual program, but a symlink to cc
129-
O_FILES := $(foreach binary,$(filter-out NCC, $(IDO_TC)),$(BUILD_DIR)/$(binary).o)
130-
C_FILES := $(O_FILES:.o=.c)
131-
132-
# Automatic dependency files
133-
DEP_FILES := $(O_FILES:.o=.d)
134125

135126
# create build directories
136127
$(shell mkdir -p $(BUILT_BIN))
@@ -152,14 +143,20 @@ ifeq ($(DETECTED_OS),linux)
152143
$(RECOMP_ELF): LDFLAGS += -Wl,-export-dynamic
153144
endif
154145

155-
CFLAGS += -DPACKAGE_VERSION="\"$(PACKAGE_VERSION)\"" -DDATETIME="\"$(DATETIME)\""
146+
LIBC_WARNINGS := $(WARNINGS) -Wno-unused-parameter -Wno-deprecated-declarations
156147

157-
%/$(LIBC_IMPL).o: WARNINGS += -Wno-unused-parameter -Wno-deprecated-declarations
158-
%/$(LIBC_IMPL)_53.o: WARNINGS += -Wno-unused-parameter -Wno-deprecated-declarations
148+
LIBC_IMPLS := libc_impl_53 libc_impl_71
149+
LIBC_IMPL := libc_impl_$(subst .,,$(VERSION))
150+
151+
%/libc_impl_53.o: CFLAGS += -DIDO53
152+
%/libc_impl_71.o: CFLAGS += -DIDO71
153+
154+
# edgcpfe 7.1 uses libc 5.3
155+
%/7.1/out/edgcpfe:: LIBC_IMPL := libc_impl_53
159156

160157
#### Main Targets ###
161158

162-
all: $(TARGET_BINARIES) $(ERR_STRS) $(LIBS)
159+
all: $(TARGET_BINARIES) $(RUNTIME_DEPS)
163160

164161
# Build the recompiler binary on a separate step.
165162
# Currently this is needed to avoid Windows and Linux ARM CIs from dying.
@@ -174,10 +171,10 @@ distclean:
174171
$(RM) -r $(BUILD_BASE)
175172
$(MAKE) -C $(RABBITIZER) distclean
176173

177-
c_files: $(C_FILES)
178-
174+
# Build only the C files (dependencies set below)
175+
c_files:
179176

180-
.PHONY: all clean distclean setup
177+
.PHONY: all setup clean distclean c_files
181178
.DEFAULT_GOAL := all
182179
# Prevent removing intermediate files
183180
.SECONDARY:
@@ -221,116 +218,72 @@ $(BUILT_BIN)/%.so.1: $(IRIX_USR_DIR)/lib/%.so.1
221218
cp $^ $@
222219

223220

224-
ifeq ($(TARGET),universal)
225-
MACOS_FAT_TARGETS ?= arm64-apple-macos11 x86_64-apple-macos10.14
226-
227-
FAT_FOLDERS := $(foreach target,$(MACOS_FAT_TARGETS),$(BUILD_DIR)/$(target))
228-
229-
# create build directories
230-
$(shell mkdir -p $(FAT_FOLDERS))
231-
232-
# TODO: simplify
233-
FAT_BINARIES := $(foreach binary,$(IDO_TC),$(BUILT_BIN)/arm64-apple-macos11/$(binary)) \
234-
$(foreach binary,$(IDO_TC),$(BUILT_BIN)/x86_64-apple-macos10.14/$(binary))
235-
236-
### Fat ###
237-
238-
$(BUILT_BIN)/%: $(BUILD_DIR)/arm64-apple-macos11/% $(BUILD_DIR)/x86_64-apple-macos10.14/% | $(ERR_STRS)
239-
lipo -create -output $@ $^
240-
241-
242-
### Built programs ###
243-
244-
$(BUILD_DIR)/arm64-apple-macos11/%: $(BUILD_DIR)/arm64-apple-macos11/%.o $(BUILD_DIR)/arm64-apple-macos11/$(LIBC_IMPL).o $(BUILD_DIR)/arm64-apple-macos11/$(VERSION_INFO).o | $(ERR_STRS)
245-
$(CC) $(CSTD) $(OPTFLAGS) $(CFLAGS) -target arm64-apple-macos11 -o $@ $^ $(LDFLAGS)
246-
$(STRIP) $@
247-
248-
$(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 $(BUILD_DIR)/x86_64-apple-macos10.14/$(VERSION_INFO).o | $(ERR_STRS)
249-
$(CC) $(CSTD) $(OPTFLAGS) $(CFLAGS) -target x86_64-apple-macos10.14 -o $@ $^ $(LDFLAGS)
250-
$(STRIP) $@
251-
252221
# NCC 7.1 is just a renamed cc
253-
$(BUILD_BASE)/7.1/arm64-apple-macos11/NCC: $(BUILD_BASE)/7.1/arm64-apple-macos11/cc
254-
cp $^ $@
255-
256-
$(BUILD_BASE)/7.1/x86_64-apple-macos10.14/NCC: $(BUILD_BASE)/7.1/x86_64-apple-macos10.14/cc
222+
$(BUILD_BASE)/7.1/out/NCC: $(BUILD_BASE)/7.1/out/cc
257223
cp $^ $@
258224

259-
$(BUILD_DIR)/arm64-apple-macos11/edgcpfe: $(BUILD_DIR)/arm64-apple-macos11/edgcpfe.o $(BUILD_DIR)/arm64-apple-macos11/$(LIBC_IMPL)_53.o $(BUILD_DIR)/arm64-apple-macos11/$(VERSION_INFO).o | $(ERR_STRS)
260-
$(CC) $(CSTD) $(OPTFLAGS) $(CFLAGS) -target arm64-apple-macos11 -o $@ $^ $(LDFLAGS)
261-
$(STRIP) $@
262225

263-
$(BUILD_DIR)/x86_64-apple-macos10.14/edgcpfe: $(BUILD_DIR)/x86_64-apple-macos10.14/edgcpfe.o $(BUILD_DIR)/x86_64-apple-macos10.14/$(LIBC_IMPL)_53.o $(BUILD_DIR)/x86_64-apple-macos10.14/$(VERSION_INFO).o | $(ERR_STRS)
264-
$(CC) $(CSTD) $(OPTFLAGS) $(CFLAGS) -target x86_64-apple-macos10.14 -o $@ $^ $(LDFLAGS)
265-
$(STRIP) $@
226+
# Template to compile libc_impl and output binaries
227+
# $(1): target name (or "default" if it's the only target)
228+
# $(2): target flags
229+
define compile_template
266230

231+
ifeq ($(1),default)
232+
TARGET_DIR-$(1) := $(BUILD_DIR)
233+
else
234+
TARGET_DIR-$(1) := $(BUILD_DIR)/$(1)
235+
endif
267236

268-
### Intermediary steps ###
269-
270-
$(BUILD_DIR)/arm64-apple-macos11/%.o: $(BUILD_DIR)/%.c
271-
$(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -target arm64-apple-macos11 -o $@ $<
272-
273-
$(BUILD_DIR)/x86_64-apple-macos10.14/%.o: $(BUILD_DIR)/%.c
274-
$(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -target x86_64-apple-macos10.14 -o $@ $<
275-
276-
277-
$(BUILD_DIR)/arm64-apple-macos11/$(LIBC_IMPL).o: $(LIBC_IMPL).c
278-
$(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -D$(IDO_VERSION) $(WARNINGS) -target arm64-apple-macos11 -o $@ $<
279-
280-
$(BUILD_DIR)/x86_64-apple-macos10.14/$(LIBC_IMPL).o: $(LIBC_IMPL).c
281-
$(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -D$(IDO_VERSION) $(WARNINGS) -target x86_64-apple-macos10.14 -o $@ $<
282-
283-
$(BUILD_DIR)/arm64-apple-macos11/$(LIBC_IMPL)_53.o: $(LIBC_IMPL).c
284-
$(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -DIDO53 $(WARNINGS) -target arm64-apple-macos11 -o $@ $<
285-
286-
$(BUILD_DIR)/x86_64-apple-macos10.14/$(LIBC_IMPL)_53.o: $(LIBC_IMPL).c
287-
$(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -DIDO53 $(WARNINGS) -target x86_64-apple-macos10.14 -o $@ $<
288-
289-
# $(VERSION_INFO).o is set to depend on every other .o file to ensure the version information is always up to date
290-
$(BUILD_DIR)/arm64-apple-macos11/$(VERSION_INFO).o: $(VERSION_INFO).c $(O_FILES) $(BUILD_DIR)/arm64-apple-macos11/$(LIBC_IMPL).o
291-
$(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -D$(IDO_VERSION) $(WARNINGS) -target arm64-apple-macos11 -o $@ $<
237+
TARGET_BIN-$(1) := $$(TARGET_DIR-$(1))/out
238+
TARGET_FLAGS-$(1) := $(2)
292239

293-
$(BUILD_DIR)/x86_64-apple-macos10.14/$(VERSION_INFO).o: $(VERSION_INFO).c $(O_FILES) $(BUILD_DIR)/x86_64-apple-macos10.14/$(LIBC_IMPL).o
294-
$(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -D$(IDO_VERSION) $(WARNINGS) -target x86_64-apple-macos10.14 -o $@ $<
240+
# NCC is filtered out since it isn't an actual program, but a symlink to cc
241+
LIBC_IMPL_O_FILES-$(1) := $(foreach libc_impl,$(LIBC_IMPLS),$$(TARGET_DIR-$(1))/$(libc_impl).o)
242+
PROGRAM_O_FILES-$(1) := $(foreach binary,$(filter-out NCC,$(IDO_TC)),$$(TARGET_DIR-$(1))/$(binary).o)
243+
PROGRAM_C_FILES-$(1) := $$(PROGRAM_O_FILES-$(1):.o=.c)
244+
O_FILES-$(1) := $$(LIBC_IMPL_O_FILES-$(1)) $$(PROGRAM_O_FILES-$(1))
295245

296-
else
297-
### Built programs ###
246+
# create build directories
247+
$$(shell mkdir -p $$(TARGET_BIN-$(1)))
298248

299-
$(BUILT_BIN)/%: $(BUILD_DIR)/%.o $(BUILD_DIR)/$(LIBC_IMPL).o $(BUILD_DIR)/$(VERSION_INFO).o | $(ERR_STRS)
300-
$(CC) $(CSTD) $(OPTFLAGS) $(CFLAGS) -o $@ $^ $(LDFLAGS)
301-
$(STRIP) $@
249+
c_files: $$(PROGRAM_C_FILES-$(1))
302250

303-
# NCC 7.1 is just a renamed cc
304-
$(BUILD_BASE)/7.1/out/NCC: $(BUILD_BASE)/7.1/out/cc
305-
cp $^ $@
251+
$$(TARGET_BIN-$(1))/%: $$(TARGET_DIR-$(1))/%.o $$(TARGET_DIR-$(1))/version_info.o $$(LIBC_IMPL_O_FILES-$(1)) | $$(RUNTIME_DEPS)
252+
$$(CC) $$(CSTD) $$(OPTFLAGS) $$(CFLAGS) $$(TARGET_FLAGS-$(1)) -o $$@ $$< $$(TARGET_DIR-$(1))/version_info.o $$(TARGET_DIR-$(1))/$$(LIBC_IMPL).o $$(LDFLAGS)
253+
$$(STRIP) $$@
306254

307-
# edgcpfe 7.1 uses libc 5.3, so we need to hack a way to link a libc_impl file with the 5.3 stuff
308-
$(BUILT_BIN)/edgcpfe: $(BUILD_DIR)/edgcpfe.o $(BUILD_DIR)/$(LIBC_IMPL)_53.o $(BUILD_DIR)/$(VERSION_INFO).o | $(ERR_STRS)
309-
$(CC) $(CSTD) $(OPTFLAGS) $(CFLAGS) -o $@ $^ $(LDFLAGS)
310-
$(STRIP) $@
255+
$$(TARGET_DIR-$(1))/%.o: $$(BUILD_DIR)/%.c
256+
$$(CC) -c $$(CSTD) $$(OPTFLAGS) $$(CFLAGS) $$(TARGET_FLAGS-$(1)) -o $$@ $$<
311257

258+
$$(TARGET_DIR-$(1))/libc_impl_%.o: libc_impl.c
259+
$$(CC) -c $$(CSTD) $$(OPTFLAGS) $$(CFLAGS) $$(LIBC_WARNINGS) $$(TARGET_FLAGS-$(1)) -o $$@ $$<
312260

313-
### Intermediary steps ###
261+
# Rebuild version info if the recomp binary or libc_impl are updated
262+
$$(TARGET_DIR-$(1))/version_info.o: version_info.c $$(RECOMP_ELF) $$(LIBC_IMPL_O_FILES-$(1))
263+
$$(CC) -c $$(CSTD) $$(OPTFLAGS) $$(CFLAGS) -D$$(IDO_VERSION) -DPACKAGE_VERSION="\"$$(PACKAGE_VERSION)\"" -DDATETIME="\"$$(DATETIME)\"" $$(TARGET_FLAGS-$(1)) -o $$@ $$<
314264

315-
$(BUILD_DIR)/%.o: $(BUILD_DIR)/%.c
316-
$(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -o $@ $<
265+
# Automatic dependency files
266+
-include $$(O_FILES-$(1):.o=.d)
317267

268+
endef
318269

319-
$(BUILD_DIR)/$(LIBC_IMPL).o: $(LIBC_IMPL).c
320-
$(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -D$(IDO_VERSION) $(WARNINGS) -o $@ $<
321270

322-
$(BUILD_DIR)/$(LIBC_IMPL)_53.o: $(LIBC_IMPL).c
323-
$(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -DIDO53 $(WARNINGS) -o $@ $<
271+
ifeq ($(TARGET),universal)
272+
# Build universal binaries on macOS
273+
$(eval $(call compile_template,arm64-apple-macos11,-target arm64-apple-macos11))
274+
$(eval $(call compile_template,x86_64-apple-macos10.14,-target x86_64-apple-macos10.14))
324275

325-
# $(VERSION_INFO).o is set to depend on every other .o file to ensure the version information is always up to date
326-
$(BUILD_DIR)/$(VERSION_INFO).o: $(VERSION_INFO).c $(O_FILES) $(BUILD_DIR)/$(LIBC_IMPL).o $(BUILD_DIR)/$(LIBC_IMPL)_53.o
327-
$(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -D$(IDO_VERSION) $(WARNINGS) -o $@ $<
276+
$(BUILT_BIN)/%: $(BUILD_DIR)/arm64-apple-macos11/out/% $(BUILD_DIR)/x86_64-apple-macos10.14/out/%
277+
lipo -create -output $@ $^
278+
else
279+
# Normal build
280+
$(eval $(call compile_template,default,))
328281
endif
329282

330-
# Remove built-in rules, to improve performance
331-
MAKEFLAGS += --no-builtin-rules
332283

333-
-include $(DEP_FILES)
284+
# Remove built-in rules
285+
MAKEFLAGS += --no-builtin-rules
286+
.SUFFIXES:
334287

335288
# --- Debugging
336289
# run `make print-VARIABLE` to debug that variable

README.md

-17
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,3 @@ Add `RELEASE=1` to build release builds with optimizations and stripped binaries
4747

4848
By default, make build script create native binaries on macOS. This was done to minimize the time to build the recompiled suite.
4949
In order to create "fat," universal ARM and x86_64, pass `TARGET=universal` to `gmake`.
50-
51-
### Manual Building
52-
53-
Example for compiling `as1` in a Linux environment:
54-
55-
```bash
56-
make -C tools/rabbitizer
57-
g++ -Itools/rabbitizer/include -Itools/rabbitizer/cplusplus/include recomp.cpp -o recomp.elf -g -Ltools/rabbitizer/build -lrabbitizerpp
58-
./recomp.elf ido/7.1/usr/lib/as1 > as1_c.c
59-
gcc libc_impl.c as1_c.c -o as1 -g -fno-strict-aliasing -lm -DIDO71
60-
```
61-
62-
Use the same approach for `cc`, `cfe`, `uopt`, `ugen`, `as1` (and `copt` if you need that).
63-
64-
Use `-DIDO53` instead of `-DIDO71` if the program you are trying to recompile was compiled with IDO 5.3 rather than IDO 7.1.
65-
66-
To compile `ugen` for IDO 5.3, add `--conservative` when invoking `./recomp.elf`. This mimics UB present in `ugen53`. That program reads uninitialized stack memory and its result depends on that stack memory.

0 commit comments

Comments
 (0)