From 7ccf7807b3f12428adc5d9ca0ede4e3f4ec6dbbc Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 7 Jul 2019 17:06:09 -0400 Subject: ability to target any glibc version --- src/target.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src/target.cpp') diff --git a/src/target.cpp b/src/target.cpp index f646b33e22..eace930940 100644 --- a/src/target.cpp +++ b/src/target.cpp @@ -10,6 +10,8 @@ #include "target.hpp" #include "util.hpp" #include "os.hpp" +#include "compiler.hpp" +#include "glibc.hpp" #include @@ -466,6 +468,29 @@ const char *target_abi_name(ZigLLVM_EnvironmentType abi) { return ZigLLVMGetEnvironmentTypeName(abi); } +Error target_parse_glibc_version(ZigGLibCVersion *glibc_ver, const char *text) { + glibc_ver->major = 2; + glibc_ver->minor = 0; + glibc_ver->patch = 0; + SplitIterator it = memSplit(str(text), str("GLIBC_.")); + { + Optional> opt_component = SplitIterator_next(&it); + if (!opt_component.is_some) return ErrorUnknownABI; + glibc_ver->major = strtoul(buf_ptr(buf_create_from_slice(opt_component.value)), nullptr, 10); + } + { + Optional> opt_component = SplitIterator_next(&it); + if (!opt_component.is_some) return ErrorNone; + glibc_ver->minor = strtoul(buf_ptr(buf_create_from_slice(opt_component.value)), nullptr, 10); + } + { + Optional> opt_component = SplitIterator_next(&it); + if (!opt_component.is_some) return ErrorNone; + glibc_ver->patch = strtoul(buf_ptr(buf_create_from_slice(opt_component.value)), nullptr, 10); + } + return ErrorNone; +} + void get_native_target(ZigTarget *target) { ZigLLVM_OSType os_type; ZigLLVM_ObjectFormatType oformat; // ignored; based on arch/os @@ -481,6 +506,17 @@ void get_native_target(ZigTarget *target) { if (target->abi == ZigLLVM_UnknownEnvironment) { target->abi = target_default_abi(target->arch, target->os); } + target->glibc_version = nullptr; +#ifdef ZIG_OS_LINUX + if (target_is_glibc(target)) { + target->glibc_version = allocate(1); + Error err; + if ((err = glibc_detect_native_version(target->glibc_version))) { + // Use a default version. + *target->glibc_version = {2, 17, 0}; + } + } +#endif } Error target_parse_archsub(ZigLLVM_ArchType *out_arch, ZigLLVM_SubArchType *out_sub, -- cgit v1.2.3 From f04782785f66879db1f31dd6620cd31161c4da08 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 7 Jul 2019 19:24:44 -0400 Subject: fix not setting a target glibc version on non-linux --- src/target.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/target.cpp') diff --git a/src/target.cpp b/src/target.cpp index eace930940..8240023305 100644 --- a/src/target.cpp +++ b/src/target.cpp @@ -507,16 +507,16 @@ void get_native_target(ZigTarget *target) { target->abi = target_default_abi(target->arch, target->os); } target->glibc_version = nullptr; -#ifdef ZIG_OS_LINUX if (target_is_glibc(target)) { target->glibc_version = allocate(1); + *target->glibc_version = {2, 17, 0}; +#ifdef ZIG_OS_LINUX Error err; if ((err = glibc_detect_native_version(target->glibc_version))) { - // Use a default version. - *target->glibc_version = {2, 17, 0}; + // Fall back to the default version. } - } #endif + } } Error target_parse_archsub(ZigLLVM_ArchType *out_arch, ZigLLVM_SubArchType *out_sub, -- cgit v1.2.3 From 8692c6fc0da831421855c27008dc046dcf59fca7 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 8 Jul 2019 02:10:26 -0400 Subject: zero initialize target Fixes glibc_version being set to garbage. I've made this mistake before so this is an attempt to prevent future bugs. Zig doesn't have zero-initialization, so are we being a hypocrite by using this C feature? No, because C doesn't have the feature that forces you to initialize all fields. That would have prevented this bug every single time. --- src/target.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'src/target.cpp') diff --git a/src/target.cpp b/src/target.cpp index 8240023305..5cbf8c4de1 100644 --- a/src/target.cpp +++ b/src/target.cpp @@ -492,6 +492,9 @@ Error target_parse_glibc_version(ZigGLibCVersion *glibc_ver, const char *text) { } void get_native_target(ZigTarget *target) { + // first zero initialize + *target = {}; + ZigLLVM_OSType os_type; ZigLLVM_ObjectFormatType oformat; // ignored; based on arch/os ZigLLVMGetNativeTarget( @@ -506,7 +509,6 @@ void get_native_target(ZigTarget *target) { if (target->abi == ZigLLVM_UnknownEnvironment) { target->abi = target_default_abi(target->arch, target->os); } - target->glibc_version = nullptr; if (target_is_glibc(target)) { target->glibc_version = allocate(1); *target->glibc_version = {2, 17, 0}; @@ -703,6 +705,10 @@ Error target_parse_abi(ZigLLVM_EnvironmentType *out_abi, const char *abi_ptr, si Error target_parse_triple(ZigTarget *target, const char *triple) { Error err; + + // first initialize all to zero + *target = {}; + SplitIterator it = memSplit(str(triple), str("-")); Optional> opt_archsub = SplitIterator_next(&it); @@ -732,9 +738,6 @@ Error target_parse_triple(ZigTarget *target, const char *triple) { } else { target->abi = target_default_abi(target->arch, target->os); } - - target->vendor = ZigLLVM_UnknownVendor; - target->is_native = false; return ErrorNone; } -- cgit v1.2.3