Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --version flag #61

Merged
merged 20 commits into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ jobs:

name: Recompiling ido ${{ matrix.ido }} for ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0
# fetch-tags: 'true'
AngheloAlf marked this conversation as resolved.
Show resolved Hide resolved

# Ubuntu
- name: Install dependencies (Ubuntu)
Expand Down
54 changes: 38 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ else
$(error Unsupported host OS for Makefile)
endif

# check if in a git repository
ifeq ($(shell git rev-parse --is-inside-work-tree >/dev/null 2>/dev/null; echo $$?),0)
PACKAGE_VERSION := $(shell LC_ALL=C git --git-dir .git describe --tags --always --dirty)
endif

# Get the current date and time in ISO 8601 format
DATETIME := $(shell date +'%F %T UTC%z')

$(info Package version $(PACKAGE_VERSION))
$(info Build date $(DATETIME))


RABBITIZER := tools/rabbitizer
RABBITIZER_LIB := $(RABBITIZER)/build/librabbitizerpp.a

Expand Down Expand Up @@ -110,6 +122,7 @@ LIBS := $(foreach lib,$(IDO_LIBS),$(BUILT_BIN)/$(lib))

RECOMP_ELF := $(BUILD_BASE)/recomp.elf
LIBC_IMPL := libc_impl
VERSION_INFO := version_info

TARGET_BINARIES := $(foreach binary,$(IDO_TC),$(BUILT_BIN)/$(binary))
O_FILES := $(foreach binary,$(IDO_TC),$(BUILD_DIR)/$(binary).o)
Expand Down Expand Up @@ -138,6 +151,8 @@ ifeq ($(DETECTED_OS),linux)
$(RECOMP_ELF): LDFLAGS += -Wl,-export-dynamic
endif

CFLAGS += -DPACKAGE_VERSION="\"$(PACKAGE_VERSION)\"" -DDATETIME="\"$(DATETIME)\""

%/$(LIBC_IMPL).o: WARNINGS += -Wno-unused-parameter -Wno-deprecated-declarations
%/$(LIBC_IMPL)_53.o: WARNINGS += -Wno-unused-parameter -Wno-deprecated-declarations

Expand Down Expand Up @@ -220,25 +235,19 @@ $(BUILT_BIN)/%: $(BUILD_DIR)/arm64-apple-macos11/% $(BUILD_DIR)/x86_64-apple-mac

### Built programs ###

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

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

$(BUILD_BASE)/7.1/arm64-apple-macos11/NCC: $(BUILD_BASE)/7.1/arm64-apple-macos11/cc
cp $^ $@

$(BUILD_BASE)/7.1/x86_64-apple-macos10.14/NCC: $(BUILD_BASE)/7.1/x86_64-apple-macos10.14/cc
cp $^ $@
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happened to these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

version_info.o depends on every other .o file to be built first, but we weren't creating NCC.c/NCC.o files because this file is just a renamed cc binary. Because of this make couldn't build the version_info.o file, so how NCC is handled, instead of copying and renaming a cc program, it copies the cc.c program and builds that instead

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, ok. Seems a bit wasteful to compile cc.c multiple times, maybe the .o can be copied instead. Or NCC can be filtered out from O_FILES.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding an empty rule for NCC.o still complains because the other rule already set the dependency of NCC.o on NCC.c
I'll try to filter it out from O_FILES


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

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

Expand All @@ -264,19 +273,21 @@ $(BUILD_DIR)/arm64-apple-macos11/$(LIBC_IMPL)_53.o: $(LIBC_IMPL).c
$(BUILD_DIR)/x86_64-apple-macos10.14/$(LIBC_IMPL)_53.o: $(LIBC_IMPL).c
$(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -DIDO53 $(WARNINGS) -target x86_64-apple-macos10.14 -o $@ $<

$(BUILD_DIR)/arm64-apple-macos11/$(VERSION_INFO).o: $(VERSION_INFO).c $(O_FILES) $(BUILD_DIR)/arm64-apple-macos11/$(LIBC_IMPL).o
$(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -D$(IDO_VERSION) $(WARNINGS) -target arm64-apple-macos11 -o $@ $<

$(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
$(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -D$(IDO_VERSION) $(WARNINGS) -target x86_64-apple-macos10.14 -o $@ $<
Comment on lines +285 to +289
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These shouldn't need to depend on other .o files

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Elliptic's intention here was to force a rebuild for the version_info.o files if any other file was touched, so the version is always up to date.
Do you have a better way to handle this?

Copy link
Contributor

@simonlindholm simonlindholm Sep 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right! Makes sense, worth a comment maybe


else
### Built programs ###

$(BUILT_BIN)/%: $(BUILD_DIR)/%.o $(BUILD_DIR)/$(LIBC_IMPL).o | $(ERR_STRS)
$(BUILT_BIN)/%: $(BUILD_DIR)/%.o $(BUILD_DIR)/$(LIBC_IMPL).o $(BUILD_DIR)/$(VERSION_INFO).o | $(ERR_STRS)
$(CC) $(CSTD) $(OPTFLAGS) $(CFLAGS) -o $@ $^ $(LDFLAGS)
$(STRIP) $@

# NCC 7.1 is just a renamed cc
$(BUILD_BASE)/7.1/out/NCC: $(BUILD_BASE)/7.1/out/cc
cp $^ $@

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

Expand All @@ -292,8 +303,19 @@ $(BUILD_DIR)/$(LIBC_IMPL).o: $(LIBC_IMPL).c

$(BUILD_DIR)/$(LIBC_IMPL)_53.o: $(LIBC_IMPL).c
$(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -DIDO53 $(WARNINGS) -o $@ $<

$(BUILD_DIR)/$(VERSION_INFO).o: $(VERSION_INFO).c $(O_FILES) $(BUILD_DIR)/$(LIBC_IMPL).o $(BUILD_DIR)/$(LIBC_IMPL)_53.o
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similarly

$(CC) -c $(CSTD) $(OPTFLAGS) $(CFLAGS) -D$(IDO_VERSION) $(WARNINGS) -o $@ $<
endif


ifeq ($(VERSION),7.1)
# NCC 7.1 is just a renamed cc
$(BUILD_DIR)/NCC.c: $(BUILD_DIR)/cc.c
cp $^ $@
endif


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

Expand Down
16 changes: 12 additions & 4 deletions libc_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
#define STRING(param) \
size_t param##_len = wrapper_strlen(mem, param##_addr); \
char param[param##_len + 1]; \
for (size_t i = 0; i <= param##_len; i++) { \
param[i] = MEM_S8(param##_addr + i); \
for (size_t __i = 0; __i <= param##_len; __i++) { \
param[__i] = MEM_S8(param##_addr + __i); \
}

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

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

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

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

Expand All @@ -432,12 +432,20 @@ void final_cleanup(uint8_t* mem) {
destroy_global_args();
}

void print_version_info(void);
const char* progname;

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

for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--version") == 0) {
print_version_info();
return 0;
}
}

init_redirect_paths();
#ifdef RUNTIME_PAGESIZE
g_Pagesize = sysconf(_SC_PAGESIZE);
Expand Down
70 changes: 70 additions & 0 deletions version_info.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Function that prints version info. This file should be compiled with the following defined:
* - PACKAGE_VERSION
* e.g. with
* ```
* PACKAGE_VERSION := git --git-dir .git describe --tags --dirty
* [...]
* -DPACKAGE_VERSION="\"`LC_ALL=C $(PACKAGE_VERSION)`\"" \
* ```
AngheloAlf marked this conversation as resolved.
Show resolved Hide resolved
* - DATETIME, e.g. with `-DDATETIME=$(date +'%F %T UTC%z')`
AngheloAlf marked this conversation as resolved.
Show resolved Hide resolved
*
* The code in this file is mostly taken from
* - CPython: https://github.com/python/cpython/, licensed under the PSF, available here: https://docs.python.org/3/license.html
* - The Ocarina of Time practice rom, gz: https://github.com/glankk/gz/
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>

#if defined(IDO53)
#define IDO_VERSION "IDO 5.3"
#elif defined(IDO71)
#define IDO_VERSION "IDO 7.1"
#else
#define IDO_VERSION ""
#endif

#ifndef COMPILER

// Note the __clang__ conditional has to come before the __GNUC__ one because
// clang pretends to be GCC.
#if defined(__clang__)
#define COMPILER "Clang " __clang_version__
#elif defined(__GNUC__)
#define COMPILER "GCC " __VERSION__
// Generic fallbacks.
#elif defined(__cplusplus)
#define COMPILER "C++"
#else
#define COMPILER "C"
#endif

#endif /* !COMPILER */

/* git */
#ifndef PACKAGE_VERSION
#define PACKAGE_VERSION "Unknown version"
#endif

/* Date and time */
#ifndef DATETIME
#define DATETIME "xxxx-xx-xx xx:xx:xx UTC+xxxx"
AngheloAlf marked this conversation as resolved.
Show resolved Hide resolved
#endif

extern char* progname;

void print_version_info(void) {
char* buf = malloc(strlen(progname));
AngheloAlf marked this conversation as resolved.
Show resolved Hide resolved
strcpy(buf, progname);
char* name = basename(buf);

printf("%s `%s` static recompilation, Decompals version\n", IDO_VERSION, name);
printf("Source: https://github.com/decompals/ido-static-recomp\n");
printf("Version: %s\n", PACKAGE_VERSION);
printf("Build date: %s\n", DATETIME);
printf("Compiler: %s\n", COMPILER);

free(buf);
}