Skip to content

Commit 48b3c00

Browse files
AngheloAlfEllipticEllipsissimonlindholmhensldm
authored
Add --version flag (#61)
* Add version information file. Info is printed when `--version` is passed * Fix Mac (?) * Use malloc,strcpy instead of badly-supported strdup * --always * fix issue because of merge * fix mac * fix shadow warning * Set date when building * Use actions/checkout v4 * fetch-tags * Use fetch-depth since fetch-tags didn't work * remove brackets * Update version_info.c Co-authored-by: Simon Lindholm <[email protected]> * Update version_info.c Co-authored-by: Simon Lindholm <[email protected]> * Update version_info.c Co-authored-by: Simon Lindholm <[email protected]> * Update version_info.c Co-authored-by: Simon Lindholm <[email protected]> * review * filter out NCC * Update .github/workflows/build.yml Co-authored-by: Derek Hensley <[email protected]> --------- Co-authored-by: EllipticEllipsis <[email protected]> Co-authored-by: Simon Lindholm <[email protected]> Co-authored-by: Derek Hensley <[email protected]>
1 parent c4d24ce commit 48b3c00

File tree

4 files changed

+114
-12
lines changed

4 files changed

+114
-12
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ jobs:
1313

1414
name: Recompiling ido ${{ matrix.ido }} for ${{ matrix.os }}
1515
steps:
16-
- uses: actions/checkout@v3
16+
- uses: actions/checkout@v4
17+
with:
18+
fetch-depth: 0
1719

1820
# Ubuntu
1921
- name: Install dependencies (Ubuntu)

Makefile

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ else
5454
$(error Unsupported host OS for Makefile)
5555
endif
5656

57+
# check if in a git repository
58+
ifeq ($(shell git rev-parse --is-inside-work-tree >/dev/null 2>/dev/null; echo $$?),0)
59+
PACKAGE_VERSION := $(shell LC_ALL=C git --git-dir .git describe --tags --always --dirty)
60+
endif
61+
62+
# Get the current date and time in ISO 8601 format
63+
DATETIME := $(shell date +'%F %T UTC%z')
64+
65+
$(info Package version $(PACKAGE_VERSION))
66+
$(info Build date $(DATETIME))
67+
68+
5769
RABBITIZER := tools/rabbitizer
5870
RABBITIZER_LIB := $(RABBITIZER)/build/librabbitizerpp.a
5971

@@ -110,9 +122,11 @@ LIBS := $(foreach lib,$(IDO_LIBS),$(BUILT_BIN)/$(lib))
110122

111123
RECOMP_ELF := $(BUILD_BASE)/recomp.elf
112124
LIBC_IMPL := libc_impl
125+
VERSION_INFO := version_info
113126

114127
TARGET_BINARIES := $(foreach binary,$(IDO_TC),$(BUILT_BIN)/$(binary))
115-
O_FILES := $(foreach binary,$(IDO_TC),$(BUILD_DIR)/$(binary).o)
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)
116130
C_FILES := $(O_FILES:.o=.c)
117131

118132
# Automatic dependency files
@@ -138,6 +152,8 @@ ifeq ($(DETECTED_OS),linux)
138152
$(RECOMP_ELF): LDFLAGS += -Wl,-export-dynamic
139153
endif
140154

155+
CFLAGS += -DPACKAGE_VERSION="\"$(PACKAGE_VERSION)\"" -DDATETIME="\"$(DATETIME)\""
156+
141157
%/$(LIBC_IMPL).o: WARNINGS += -Wno-unused-parameter -Wno-deprecated-declarations
142158
%/$(LIBC_IMPL)_53.o: WARNINGS += -Wno-unused-parameter -Wno-deprecated-declarations
143159

@@ -220,25 +236,26 @@ $(BUILT_BIN)/%: $(BUILD_DIR)/arm64-apple-macos11/% $(BUILD_DIR)/x86_64-apple-mac
220236

221237
### Built programs ###
222238

223-
$(BUILD_DIR)/arm64-apple-macos11/%: $(BUILD_DIR)/arm64-apple-macos11/%.o $(BUILD_DIR)/arm64-apple-macos11/$(LIBC_IMPL).o | $(ERR_STRS)
239+
$(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)
224240
$(CC) $(CSTD) $(OPTFLAGS) $(CFLAGS) -target arm64-apple-macos11 -o $@ $^ $(LDFLAGS)
225241
$(STRIP) $@
226242

227-
$(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)
243+
$(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)
228244
$(CC) $(CSTD) $(OPTFLAGS) $(CFLAGS) -target x86_64-apple-macos10.14 -o $@ $^ $(LDFLAGS)
229245
$(STRIP) $@
230246

247+
# NCC 7.1 is just a renamed cc
231248
$(BUILD_BASE)/7.1/arm64-apple-macos11/NCC: $(BUILD_BASE)/7.1/arm64-apple-macos11/cc
232249
cp $^ $@
233250

234251
$(BUILD_BASE)/7.1/x86_64-apple-macos10.14/NCC: $(BUILD_BASE)/7.1/x86_64-apple-macos10.14/cc
235252
cp $^ $@
236253

237-
$(BUILD_DIR)/arm64-apple-macos11/edgcpfe: $(BUILD_DIR)/arm64-apple-macos11/edgcpfe.o $(BUILD_DIR)/arm64-apple-macos11/$(LIBC_IMPL)_53.o | $(ERR_STRS)
254+
$(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)
238255
$(CC) $(CSTD) $(OPTFLAGS) $(CFLAGS) -target arm64-apple-macos11 -o $@ $^ $(LDFLAGS)
239256
$(STRIP) $@
240257

241-
$(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 | $(ERR_STRS)
258+
$(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)
242259
$(CC) $(CSTD) $(OPTFLAGS) $(CFLAGS) -target x86_64-apple-macos10.14 -o $@ $^ $(LDFLAGS)
243260
$(STRIP) $@
244261

@@ -264,10 +281,17 @@ $(BUILD_DIR)/arm64-apple-macos11/$(LIBC_IMPL)_53.o: $(LIBC_IMPL).c
264281
$(BUILD_DIR)/x86_64-apple-macos10.14/$(LIBC_IMPL)_53.o: $(LIBC_IMPL).c
265282
$(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -DIDO53 $(WARNINGS) -target x86_64-apple-macos10.14 -o $@ $<
266283

284+
# $(VERSION_INFO).o is set to depend on every other .o file to ensure the version information is always up to date
285+
$(BUILD_DIR)/arm64-apple-macos11/$(VERSION_INFO).o: $(VERSION_INFO).c $(O_FILES) $(BUILD_DIR)/arm64-apple-macos11/$(LIBC_IMPL).o
286+
$(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -D$(IDO_VERSION) $(WARNINGS) -target arm64-apple-macos11 -o $@ $<
287+
288+
$(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
289+
$(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -D$(IDO_VERSION) $(WARNINGS) -target x86_64-apple-macos10.14 -o $@ $<
290+
267291
else
268292
### Built programs ###
269293

270-
$(BUILT_BIN)/%: $(BUILD_DIR)/%.o $(BUILD_DIR)/$(LIBC_IMPL).o | $(ERR_STRS)
294+
$(BUILT_BIN)/%: $(BUILD_DIR)/%.o $(BUILD_DIR)/$(LIBC_IMPL).o $(BUILD_DIR)/$(VERSION_INFO).o | $(ERR_STRS)
271295
$(CC) $(CSTD) $(OPTFLAGS) $(CFLAGS) -o $@ $^ $(LDFLAGS)
272296
$(STRIP) $@
273297

@@ -276,7 +300,7 @@ $(BUILD_BASE)/7.1/out/NCC: $(BUILD_BASE)/7.1/out/cc
276300
cp $^ $@
277301

278302
# 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
279-
$(BUILT_BIN)/edgcpfe: $(BUILD_DIR)/edgcpfe.o $(BUILD_DIR)/$(LIBC_IMPL)_53.o | $(ERR_STRS)
303+
$(BUILT_BIN)/edgcpfe: $(BUILD_DIR)/edgcpfe.o $(BUILD_DIR)/$(LIBC_IMPL)_53.o $(BUILD_DIR)/$(VERSION_INFO).o | $(ERR_STRS)
280304
$(CC) $(CSTD) $(OPTFLAGS) $(CFLAGS) -o $@ $^ $(LDFLAGS)
281305
$(STRIP) $@
282306

@@ -292,6 +316,10 @@ $(BUILD_DIR)/$(LIBC_IMPL).o: $(LIBC_IMPL).c
292316

293317
$(BUILD_DIR)/$(LIBC_IMPL)_53.o: $(LIBC_IMPL).c
294318
$(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -DIDO53 $(WARNINGS) -o $@ $<
319+
320+
# $(VERSION_INFO).o is set to depend on every other .o file to ensure the version information is always up to date
321+
$(BUILD_DIR)/$(VERSION_INFO).o: $(VERSION_INFO).c $(O_FILES) $(BUILD_DIR)/$(LIBC_IMPL).o $(BUILD_DIR)/$(LIBC_IMPL)_53.o
322+
$(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -D$(IDO_VERSION) $(WARNINGS) -o $@ $<
295323
endif
296324

297325
# Remove built-in rules, to improve performance

libc_impl.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
#define STRING(param) \
4343
size_t param##_len = wrapper_strlen(mem, param##_addr); \
4444
char param[param##_len + 1]; \
45-
for (size_t i = 0; i <= param##_len; i++) { \
46-
param[i] = MEM_S8(param##_addr + i); \
45+
for (size_t __i = 0; __i <= param##_len; __i++) { \
46+
param[__i] = MEM_S8(param##_addr + __i); \
4747
}
4848

4949
#if !defined(IDO53) && !defined(IDO71) && !defined(IDO72)
@@ -399,7 +399,7 @@ static void destroy_global_args(void) {
399399
static char** make_argv_from_mem(uint8_t* mem, int argc, uint32_t argv_addr) {
400400
char** argv = malloc((argc + 1) * sizeof(char*));
401401

402-
for (uint32_t i = 0; i < argc; i++) {
402+
for (int i = 0; i < argc; i++) {
403403
uint32_t str_addr = MEM_U32(argv_addr + i * sizeof(uint32_t));
404404
uint32_t len = wrapper_strlen(mem, str_addr) + 1;
405405

@@ -417,7 +417,7 @@ static char** make_argv_from_mem(uint8_t* mem, int argc, uint32_t argv_addr) {
417417
}
418418

419419
static void free_argv(int argc, char** argv) {
420-
for (uint32_t i = 0; i < argc; i++) {
420+
for (int i = 0; i < argc; i++) {
421421
free(argv[i]);
422422
}
423423

@@ -432,12 +432,20 @@ void final_cleanup(uint8_t* mem) {
432432
destroy_global_args();
433433
}
434434

435+
void print_version_info(void);
435436
const char* progname;
436437

437438
int main(int argc, char* argv[]) {
438439
int ret;
439440
progname = argv[0];
440441

442+
for (int i = 1; i < argc; i++) {
443+
if (strcmp(argv[i], "--version") == 0) {
444+
print_version_info();
445+
return 0;
446+
}
447+
}
448+
441449
init_redirect_paths();
442450
#ifdef RUNTIME_PAGESIZE
443451
g_Pagesize = sysconf(_SC_PAGESIZE);

version_info.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Function that prints version info. This file should be compiled with the following defined:
3+
* - PACKAGE_VERSION, e.g. with `-DPACKAGE_VERSION="\"$(LC_ALL=C git --git-dir .git describe --tags --dirty)\""`
4+
* - DATETIME, e.g. with `-DDATETIME="\"$(date +'%F %T UTC%z')\""`
5+
*
6+
* The code in this file is mostly taken from
7+
* - CPython: https://github.com/python/cpython/, licensed under the PSF, available here: https://docs.python.org/3/license.html
8+
* - The Ocarina of Time practice rom, gz: https://github.com/glankk/gz/
9+
*/
10+
#include <stdio.h>
11+
#include <stdlib.h>
12+
#include <string.h>
13+
#include <libgen.h>
14+
15+
#if defined(IDO53)
16+
#define IDO_VERSION "IDO 5.3"
17+
#elif defined(IDO71)
18+
#define IDO_VERSION "IDO 7.1"
19+
#else
20+
#define IDO_VERSION ""
21+
#endif
22+
23+
#ifndef COMPILER
24+
25+
// Note the __clang__ conditional has to come before the __GNUC__ one because
26+
// clang pretends to be GCC.
27+
#if defined(__clang__)
28+
#define COMPILER "Clang " __clang_version__
29+
#elif defined(__GNUC__)
30+
#define COMPILER "GCC " __VERSION__
31+
// Generic fallbacks.
32+
#elif defined(__cplusplus)
33+
#define COMPILER "C++"
34+
#else
35+
#define COMPILER "C"
36+
#endif
37+
38+
#endif /* !COMPILER */
39+
40+
/* git */
41+
#ifndef PACKAGE_VERSION
42+
#define PACKAGE_VERSION "Unknown version"
43+
#endif
44+
45+
/* Date and time */
46+
#ifndef DATETIME
47+
#define DATETIME "Unknown date"
48+
#endif
49+
50+
extern char* progname;
51+
52+
void print_version_info(void) {
53+
char* buf = malloc(strlen(progname) + 1);
54+
strcpy(buf, progname);
55+
char* name = basename(buf);
56+
57+
printf("%s `%s` static recompilation, Decompals version\n", IDO_VERSION, name);
58+
printf("Source: https://github.com/decompals/ido-static-recomp\n");
59+
printf("Version: %s\n", PACKAGE_VERSION);
60+
printf("Build date: %s\n", DATETIME);
61+
printf("Compiler: %s\n", COMPILER);
62+
63+
free(buf);
64+
}

0 commit comments

Comments
 (0)