From 0685260454c7e9542e35b69a2d2c739a804d17ea Mon Sep 17 00:00:00 2001 From: DylanDoesProgramming <120761889+DylanDoesProgramming664@users.noreply.github.com> Date: Fri, 12 Dec 2025 16:53:01 -0500 Subject: [PATCH] add `--linux-libc=host` option and set default to `host` (#2636) * added `--linux-libc=host` and set default to `default_linux` from `src/build/builder.c` * updated `resources/project_schema.json` for `linux-libc` host build option * Update how libc is set. --------- Co-authored-by: Christoffer Lerno --- resources/project_schema.json | 5 +++-- src/build/build.h | 4 +++- src/build/build_internal.h | 3 ++- src/build/build_options.c | 2 +- src/build/builder.c | 1 + src/build/project.c | 8 ++++---- src/compiler/linker.c | 20 +++++++++----------- 7 files changed, 23 insertions(+), 20 deletions(-) diff --git a/resources/project_schema.json b/resources/project_schema.json index 5709b03f9..f2a26e68c 100644 --- a/resources/project_schema.json +++ b/resources/project_schema.json @@ -204,9 +204,10 @@ "description": "Set the libc to use for Linux.", "enum": [ "gnu", - "musl" + "musl", + "host" ], - "default": "gnu" + "default": "host" }, "x86cpu": { "type": "string", diff --git a/src/build/build.h b/src/build/build.h index 9dc026f8a..95dd59648 100644 --- a/src/build/build.h +++ b/src/build/build.h @@ -108,6 +108,7 @@ typedef enum LINUX_LIBC_NOT_SET = -1, LINUX_LIBC_GNU = 0, LINUX_LIBC_MUSL = 1, + LINUX_LIBC_HOST = 2, } LinuxLibc; typedef enum @@ -897,7 +898,7 @@ static BuildTarget default_build_target = { .feature.panic_level = PANIC_NOT_SET, .win.crt_linking = WIN_CRT_DEFAULT, .win.def = NULL, - .linuxpaths.libc = LINUX_LIBC_GNU, + .linuxpaths.libc = LINUX_LIBC_NOT_SET, .switchrange_max_size = DEFAULT_SWITCHRANGE_MAX_SIZE, .switchjump_max_size = DEFAULT_SWITCH_JUMP_MAX_SIZE, .quiet = false, @@ -913,6 +914,7 @@ extern const int manifest_default_keys_count; extern const char *manifest_target_keys[][2]; extern const int manifest_target_keys_count; extern const char *arch_os_target[ARCH_OS_TARGET_LAST + 1]; +extern LinuxLibc default_libc; BuildOptions parse_arguments(int argc, const char *argv[]); ArchOsTarget arch_os_target_from_string(const char *target); diff --git a/src/build/build_internal.h b/src/build/build_internal.h index fc324dee1..923dbd87c 100644 --- a/src/build/build_internal.h +++ b/src/build/build_internal.h @@ -50,9 +50,10 @@ static const char *optsizes[3] = { [SIZE_OPTIMIZATION_TINY] = "tiny", }; -static const char *linuxlibc[2] = { +static const char *linuxlibc[3] = { [LINUX_LIBC_GNU] = "gnu", [LINUX_LIBC_MUSL] = "musl", + [LINUX_LIBC_HOST] = "host", }; static const char *linker_kind[3] = { diff --git a/src/build/build_options.c b/src/build/build_options.c index a070f24fb..2b84887f6 100644 --- a/src/build/build_options.c +++ b/src/build/build_options.c @@ -217,7 +217,7 @@ static void usage(bool full) print_opt("--macos-min-version ", "Set the minimum MacOS version to compile for."); print_opt("--macos-sdk-version ", "Set the MacOS SDK compiled for."); PRINTF(""); - print_opt("--linux-libc=", "Set the libc to use on Linux, defaults to gnu."); + print_opt("--linux-libc=", "Set the libc to use on Linux, defaults to host."); print_opt("--linux-crt ", "Set the directory to use for finding crt1.o and related files."); print_opt("--linux-crtbegin ", "Set the directory to use for finding crtbegin.o and related files."); PRINTF(""); diff --git a/src/build/builder.c b/src/build/builder.c index 8caa281a1..c629aa401 100644 --- a/src/build/builder.c +++ b/src/build/builder.c @@ -540,6 +540,7 @@ static void update_build_target_from_options(BuildTarget *target, BuildOptions * target->emit_asm = options->emit_asm; target->print_stats = options->verbosity_level >= 2; + if (target->linuxpaths.libc == LINUX_LIBC_NOT_SET) target->linuxpaths.libc = default_libc; target->benchmarking = options->benchmarking; target->testing = options->testing; target->silent = options->verbosity_level < 0; diff --git a/src/build/project.c b/src/build/project.c index 4422bdbb6..fcae5f9c5 100644 --- a/src/build/project.c +++ b/src/build/project.c @@ -32,7 +32,7 @@ const char *project_default_keys[][2] = { {"linker-search-paths", "Linker search paths."}, {"linux-crt", "Set the directory to use for finding crt1.o and related files."}, {"linux-crtbegin", "Set the directory to use for finding crtbegin.o and related files."}, - {"linux-libc", "Set the libc to use for Linux. Valid options are 'gnu' and 'musl', default is 'gnu'"}, + {"linux-libc", "Set the libc to use for Linux. Valid options are 'host', 'gnu' and 'musl', default is 'host'"}, {"loop-vectorize", "Force enable/disable loop auto-vectorization."}, {"macos-min-version", "Set the minimum MacOS version to compile for."}, {"macos-sdk-version", "Set the MacOS SDK compiled for." }, @@ -118,7 +118,7 @@ const char* project_target_keys[][2] = { {"linker-search-paths-override", "Linker search paths for this target, overriding global settings."}, {"linux-crt", "Set the directory to use for finding crt1.o and related files."}, {"linux-crtbegin", "Set the directory to use for finding crtbegin.o and related files."}, - {"linux-libc", "Set the libc to use for Linux. Valid options are 'gnu' and 'musl', default is 'gnu'"}, + {"linux-libc", "Set the libc to use for Linux. Valid options are 'host', 'gnu' and 'musl', default is 'host'"}, {"loop-vectorize", "Force enable/disable loop auto-vectorization."}, {"macos-min-version", "Set the minimum MacOS version to compile for."}, {"macos-sdk-version", "Set the MacOS SDK compiled for." }, @@ -488,7 +488,7 @@ static void load_into_build_target(BuildParseContext context, JSONObject *json, target->linuxpaths.crtbegin = get_string(context, json, "linux-crtbegin", target->linuxpaths.crtbegin); // linux-libc - LinuxLibc linux_libc = GET_SETTING(LinuxLibc, "linux-libc", linuxlibc, "`gnu` or `musl`."); + LinuxLibc linux_libc = GET_SETTING(LinuxLibc, "linux-libc", linuxlibc, "`gnu`, `musl` or `host`."); if (linux_libc > -1) target->linuxpaths.libc = linux_libc; // version @@ -503,7 +503,7 @@ static void load_into_build_target(BuildParseContext context, JSONObject *json, // testfn target->testfn = get_string(context, json, "testfn", target->testfn); - // testfn + // benchfn target->benchfn = get_string(context, json, "benchfn", target->benchfn); // link-libc diff --git a/src/compiler/linker.c b/src/compiler/linker.c index 9b64b9117..5525405e2 100644 --- a/src/compiler/linker.c +++ b/src/compiler/linker.c @@ -402,12 +402,10 @@ static const char *find_linux_crt_begin(void) static const char *find_linux_ld(void) { - INFO_LOG("Environment Type ID: %d", compiler.platform.environment_type); - switch (compiler.platform.environment_type) + if (compiler.platform.environment_type == ENV_TYPE_ANDROID) return "--dynamic-linker=/system/ld-android.so"; + switch (compiler.build.linuxpaths.libc) { - case ENV_TYPE_MUSL: - case ENV_TYPE_MUSLEABI: - case ENV_TYPE_MUSLEABIHF: + case LINUX_LIBC_MUSL: switch (compiler.platform.arch) { case ARCH_TYPE_ARM: return "--dynamic-linker=/lib/ld-musl-arm.so.1"; @@ -428,10 +426,9 @@ static const char *find_linux_ld(void) } UNREACHABLE; break; - case ENV_TYPE_ANDROID: - return "--dynamic-linker=/system/ld-android.so"; - default: - switch (compiler.platform.arch) { + case LINUX_LIBC_GNU: + switch (compiler.platform.arch) + { case ARCH_TYPE_ARM: return "--dynamic-linker=/lib/ld-linux.so.3"; case ARCH_TYPE_AARCH64: return "--dynamic-linker=/lib/ld-linux-aarch64.so.1"; case ARCH_TYPE_MIPS: return "--dynamic-linker=/lib/ld-linux-mipsn8.so.1"; @@ -443,11 +440,12 @@ static const char *find_linux_ld(void) case ARCH_TYPE_SPARCV9: return "--dynamic-linker=/lib/ld-linux.so.2"; case ARCH_TYPE_X86: return "--dynamic-linker=/lib64/ld-linux.so.2"; case ARCH_TYPE_X86_64: return "--dynamic-linker=/lib64/ld-linux-x86-64.so.2"; - default: return "--dynamic-linkrt=/lib/ld-linux-unknown.so.2"; // another placeholder until we have all of them + default: return "--dynamic-linker=/lib/ld-linux-unknown.so.2"; // another placeholder until we have all of them } + FALLTHROUGH; + default: UNREACHABLE; } - UNREACHABLE; } static void linker_setup_linux(const char ***args_ref, Linker linker_type, bool is_dylib)