diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index c09dbffde..86f110dff 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -40,7 +40,7 @@ jobs: shell: pwsh run: | zig build install test-toolchains test-codegen test-deps test-il2cpp test-mod test-sync test-spud ` - -Dtarget=x86_64-windows-msvc -Doptimize=ReleaseFast -Dtest-optimize=Debug ` + -Dtarget=x86_64-windows-msvc -Doptimize=ReleaseFast -Dstrip=true -Dtest-optimize=Debug ` "-Dstfc_source_state_id=git:${{ github.sha }}" ` "-Dstfc_base_commit=${{ github.sha }}" ` "-Dstfc_build_invocation_id=github:${{ github.run_id }}:${{ github.run_attempt }}:windows" ` @@ -70,7 +70,7 @@ jobs: STFC_WINDOWS_TOOLCHAIN_URL: ${{ vars.STFC_WINDOWS_TOOLCHAIN_URL }} run: | zig build fetch-toolchains - zig build install build-tests -Dtarget=x86_64-windows-msvc -Doptimize=ReleaseFast -Dtest-optimize=Debug + zig build install build-tests -Dtarget=x86_64-windows-msvc -Doptimize=ReleaseFast -Dstrip=true -Dtest-optimize=Debug python3 scripts/verify-windows-binary.py # The native fixtures need no SDK, so they run on Linux too, where they are cheapest. @@ -112,7 +112,7 @@ jobs: - name: Build and test run: | zig build install launcher test-toolchains test-codegen test-deps test-il2cpp test-mod ${{ matrix.tests }} \ - -Dtarget=${{ matrix.target }} -Doptimize=ReleaseFast -Dtest-optimize=Debug --summary all + -Dtarget=${{ matrix.target }} -Doptimize=ReleaseFast -Dstrip=true -Dtest-optimize=Debug --summary all - name: Verify portable linkage run: | scripts/check-mac-linkage.sh zig-out/macOSLauncher.app/Contents/MacOS/macOSLauncher \ diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 33539c5cf..5ecde0295 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -80,7 +80,8 @@ To reset build outputs, remove `.zig-cache` and `zig-out`. Keep `.cache/toolchai ## Choose build options -`Debug` is the default. Use `-Doptimize=ReleaseFast` for releases. +The default developer build is `ReleaseFast` with debug symbols (`releasedbg`). Use `-Doptimize=Debug` for an +unoptimized debug build. Production packages also pass `-Dstrip=true`. Windows release builds enable link-time optimization. Disable it with `-Dlto=false`. The Swift launcher compiles as one module and links after the native archives are ready. macOS `ReleaseFast` builds batch feature sources to share header parsing; sync sources compile separately. @@ -89,11 +90,12 @@ Zig does not support Mach-O LTO, so macOS builds use ordinary optimized compilat | Option | Effect | | --- | --- | +| `-Dstrip=true` | Strip debug symbols for a production package. | | `-Dmoddbg=true` | Define `_MODDBG`: enable debug logging by default, disable sync request timeouts, and add diagnostics. | | `-Dbg_image=` | Replace the embedded loading image. | | `-Duse_original_bg=true` | Keep the game's loading background. | | `-Dmacos-sdk=` | Use an explicit macOS SDK. | -| `-Dtest-optimize=Debug` | Keep test fixtures and their assertions in Debug while sharing dependencies built with `-Doptimize=ReleaseFast`. | +| `-Dtest-optimize=` | Override the test fixtures' `Debug` default; dependencies still use `-Doptimize`. | `[patches]` switches work in every build. For library-only checks, use `zig build il2cpp`, `zig build unity`, or `zig build mods` with the required target. diff --git a/build.zig b/build.zig index 17dbecdd3..4f9856bf5 100644 --- a/build.zig +++ b/build.zig @@ -36,7 +36,9 @@ pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{ .default_target = .{ .abi = if (builtin.os.tag == .windows) .msvc else null }, }); - const optimize = b.standardOptimizeOption(.{}); + // Local builds should behave like the shipped mod while retaining symbols for crash reports and debuggers. + const optimize = b.option(std.builtin.OptimizeMode, "optimize", "Prioritize performance, safety, or binary size") + orelse .ReleaseFast; const options = Options.parse(b); mod.addUniversal(b, optimize, options); const ctx = Ctx.init(b, target, optimize, options); diff --git a/scripts/mac-build-test-debug.sh b/scripts/mac-build-test-debug.sh index 86e9cb4d3..6491eb0ee 100755 --- a/scripts/mac-build-test-debug.sh +++ b/scripts/mac-build-test-debug.sh @@ -14,7 +14,7 @@ Actions: clean Remove .zig-cache and zig-out Options: - -m, --mode MODE debug (default), release, or releasedbg (release with the mod's -Dmoddbg diagnostics) + -m, --mode MODE releasedbg (default; optimized with symbols), debug, or release -a, --arch ARCH arm64, x86_64 or universal (default: this Mac's) -l, --launcher Run the launcher bundle instead of the loader -c, --clean Clean before building @@ -22,7 +22,7 @@ Options: EOF } -MODE=debug +MODE=releasedbg ARCH="$(uname -m)" ACTION=build CLEAN=false @@ -40,9 +40,9 @@ while (($#)); do done case "$MODE" in - debug) OPTIMIZE=Debug MODDBG=false ;; - release) OPTIMIZE=ReleaseFast MODDBG=false ;; - releasedbg) OPTIMIZE=ReleaseFast MODDBG=true ;; + debug) OPTIMIZE=Debug STRIP=false ;; + release) OPTIMIZE=ReleaseFast STRIP=true ;; + releasedbg) OPTIMIZE=ReleaseFast STRIP=false ;; *) echo "Invalid mode: $MODE (debug, release, releasedbg)" >&2 && exit 1 ;; esac @@ -70,7 +70,7 @@ build() { target=aarch64 fi # Keep in step with macos_min in zig/ctx.zig. - zig build install launcher -Dtarget="$target-macos.14.6" -Doptimize="$OPTIMIZE" -Dmoddbg="$MODDBG" \ + zig build install launcher -Dtarget="$target-macos.14.6" -Doptimize="$OPTIMIZE" -Dstrip="$STRIP" \ --prefix "$PREFIX" scripts/assemble-mac-app.sh "$APP" "$PREFIX" } diff --git a/scripts/package-mac-app.sh b/scripts/package-mac-app.sh index 98bf12433..bfc76f574 100755 --- a/scripts/package-mac-app.sh +++ b/scripts/package-mac-app.sh @@ -9,9 +9,9 @@ cd "$ROOT" MODE="${1:-release}" case "$MODE" in - debug) OPTIMIZE=Debug MODDBG=false ;; - release) OPTIMIZE=ReleaseFast MODDBG=false ;; - releasedbg) OPTIMIZE=ReleaseFast MODDBG=true ;; + debug) OPTIMIZE=Debug STRIP=false ;; + release) OPTIMIZE=ReleaseFast STRIP=true ;; + releasedbg) OPTIMIZE=ReleaseFast STRIP=false ;; *) echo "Unknown build mode: $MODE" >&2 exit 1 @@ -27,7 +27,7 @@ for ARCH in arm64 x86_64; do TARGET=aarch64 fi PREFIX="zig-out/$ARCH/$MODE" - zig build install launcher -Dtarget="$TARGET-macos.$MACOS_MIN" -Doptimize="$OPTIMIZE" -Dmoddbg="$MODDBG" \ + zig build install launcher -Dtarget="$TARGET-macos.$MACOS_MIN" -Doptimize="$OPTIMIZE" -Dstrip="$STRIP" \ --prefix "$PREFIX" PREFIXES+=("$PREFIX") done diff --git a/src/mod/mod.cc b/src/mod/mod.cc index f198ce910..947a6bd53 100644 --- a/src/mod/mod.cc +++ b/src/mod/mod.cc @@ -345,12 +345,6 @@ void InstallFeatures(const Config& config) int il2cpp_init_Hook(auto original, const char* domain_name) { -#if _WIN32 && !defined(NDEBUG) - AllocConsole(); - FILE* console; - freopen_s(&console, "CONOUT$", "w", stdout); -#endif - // Nothing here may throw into the game: a mod that cannot set itself up leaves the game running without it. // Keeping the settings pointer is safe here because only the frame tick reloads, and it is not installed yet. const Config* config = nullptr; diff --git a/zig/ctx.zig b/zig/ctx.zig index 51e466308..ff0f9c9f7 100644 --- a/zig/ctx.zig +++ b/zig/ctx.zig @@ -18,11 +18,13 @@ pub const macos_min = "14.6"; pub const Options = struct { /// null keeps the per-target default: on for Windows release builds, off elsewhere. lto: ?bool, + /// Strip debug information for production packages. Local release builds retain symbols by default. + strip: bool, moddbg: bool, use_original_bg: bool, /// A custom loading screen, relative to the working directory; empty for the built-in one. bg_image: []const u8, - /// Fixture optimization; null follows -Doptimize. + /// Fixture optimization; null keeps assertions and diagnostics in Debug. test_optimize: ?OptimizeMode, msvc_sysroot: ?[]const u8, macos_sdk: ?[]const u8, @@ -31,6 +33,7 @@ pub const Options = struct { pub fn parse(b: *Build) Options { return .{ .lto = b.option(bool, "lto", "Enable LTO (Windows release default; unsupported for macOS)"), + .strip = b.option(bool, "strip", "Strip debug symbols from production artifacts") orelse false, .moddbg = b.option(bool, "moddbg", "Enable diagnostic patch toggles (_MODDBG)") orelse false, .use_original_bg = b.option( bool, @@ -41,7 +44,7 @@ pub const Options = struct { .test_optimize = b.option( OptimizeMode, "test-optimize", - "Optimization mode for test fixtures (dependencies use -Doptimize)", + "Optimization mode for test fixtures (Debug by default; dependencies use -Doptimize)", ), .msvc_sysroot = b.option([]const u8, "msvc-sysroot", "Path to an extracted MSVC + Windows SDK toolchain"), .macos_sdk = b.option([]const u8, "macos-sdk", "Path to a MacOSX.sdk directory"), @@ -86,7 +89,10 @@ pub const Ctx = struct { const copy = ctx.b.allocator.create(Ctx) catch @panic("OOM"); copy.* = ctx.*; copy.optimize = optimize; - if (optimize == OptimizeMode.Debug) copy.lto = false; + if (optimize == OptimizeMode.Debug) { + copy.lto = false; + copy.options.strip = false; + } return copy; } @@ -121,9 +127,13 @@ pub const Ctx = struct { .link_libc = true, .link_libcpp = if (cpp and !ctx.isWindows()) true else null, .pic = true, + .strip = ctx.options.strip, // Zig turns UBSan on in Debug, but a trap inside a game detour takes the client down. .sanitize_c = .off, }); + // Zig's optimization mode does not define the conventional C/C++ release macro. Keep debug-only runtime + // behavior out of optimized builds even when they retain debug symbols. + if (!ctx.isDebug()) module.addCMacro("NDEBUG", "1"); if (ctx.isWindows()) { if (ctx.sysroot.msvc_root) |root| { diff --git a/zig/mod.zig b/zig/mod.zig index 0b8f5eefa..02f2ae980 100644 --- a/zig/mod.zig +++ b/zig/mod.zig @@ -139,7 +139,7 @@ fn addModSources(ctx: *const Ctx, module: *std.Build.Module, flags: []const []co /// marker; the RC's /D cuts a value at '=', so the whole marker cannot be passed as one define. fn identityDefines(ctx: *const Ctx) []const []const u8 { const b = ctx.b; - const mode = if (ctx.options.moddbg) "releasedbg" else if (ctx.isDebug()) "debug" else "release"; + const mode = if (ctx.isDebug()) "debug" else if (ctx.options.strip) "release" else "releasedbg"; var flags: std.ArrayList([]const u8) = .empty; flags.appendSlice(b.allocator, &.{ "/D", b.fmt("STFC_BUILD_MODE=\"{s}\"", .{mode}) }) catch @panic("OOM"); inline for (.{ @@ -226,6 +226,7 @@ pub fn addUniversal(b: *std.Build, optimize: std.builtin.OptimizeMode, options: "-Dtarget=" ++ arch ++ "-macos." ++ macos_min, b.fmt("-Doptimize={s}", .{@tagName(optimize)}), b.fmt("-Dlto={}", .{options.lto orelse false}), + b.fmt("-Dstrip={}", .{options.strip}), b.fmt("-Dmoddbg={}", .{options.moddbg}), b.fmt("-Duse_original_bg={}", .{options.use_original_bg}), }); diff --git a/zig/tests.zig b/zig/tests.zig index 1f07b0140..c8b30eac0 100644 --- a/zig/tests.zig +++ b/zig/tests.zig @@ -4,6 +4,7 @@ const builtin = @import("builtin"); const compat = @import("compat.zig"); const Ctx = @import("ctx.zig").Ctx; const Lib = @import("ctx.zig").Lib; +const OptimizeMode = @import("ctx.zig").OptimizeMode; const deps = @import("deps.zig"); /// The libraries the fixtures link. They are the production artifacts, built at -Doptimize. @@ -19,7 +20,7 @@ pub const Libs = struct { /// Register the fixture steps. Fixture code compiles at -Dtest-optimize, so a Debug fixture keeps its assertions /// while sharing release libraries with the shipped build. pub fn add(build_ctx: *const Ctx, libs: Libs) void { - const ctx = build_ctx.withOptimize(build_ctx.options.test_optimize orelse build_ctx.optimize); + const ctx = build_ctx.withOptimize(build_ctx.options.test_optimize orelse OptimizeMode.Debug); const b = ctx.b; const build_tests = b.step("build-tests", "Compile the tests without running them"); const test_il2cpp = b.step("test-il2cpp", "Build and run the IL2CPP boundary tests (native targets only)");