From 275880151762e684c1170dbe323387dcc6fe1d17 Mon Sep 17 00:00:00 2001 From: Jake Petroules Date: Wed, 19 Feb 2025 22:13:02 -0800 Subject: [PATCH 1/2] Use C to force the POSIX (not GNU) overload of strerror_r to be selected This allows building TSC for Android without needing to pass custom flags via -Xcc. --- Sources/TSCBasic/CMakeLists.txt | 2 -- Sources/TSCBasic/misc.swift | 3 ++- Sources/TSCclibc/CMakeLists.txt | 2 +- Sources/TSCclibc/include/module.modulemap | 1 + Sources/TSCclibc/include/strerror.h | 5 +++++ Sources/TSCclibc/strerror.c | 9 +++++++++ 6 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 Sources/TSCclibc/include/strerror.h create mode 100644 Sources/TSCclibc/strerror.c diff --git a/Sources/TSCBasic/CMakeLists.txt b/Sources/TSCBasic/CMakeLists.txt index a1f5bcc2..d8b85eaa 100644 --- a/Sources/TSCBasic/CMakeLists.txt +++ b/Sources/TSCBasic/CMakeLists.txt @@ -52,8 +52,6 @@ add_library(TSCBasic misc.swift) target_compile_options(TSCBasic PUBLIC - # Don't use GNU strerror_r on Android. - "$<$:SHELL:-Xcc -U_GNU_SOURCE>" # Ignore secure function warnings on Windows. "$<$:SHELL:-Xcc -D_CRT_SECURE_NO_WARNINGS>") target_link_libraries(TSCBasic PRIVATE diff --git a/Sources/TSCBasic/misc.swift b/Sources/TSCBasic/misc.swift index 58a302de..d85dd096 100644 --- a/Sources/TSCBasic/misc.swift +++ b/Sources/TSCBasic/misc.swift @@ -8,6 +8,7 @@ See http://swift.org/CONTRIBUTORS.txt for Swift project authors */ +@_implementationOnly import TSCclibc import TSCLibc import Foundation #if os(Windows) @@ -327,7 +328,7 @@ extension SystemError: CustomStringConvertible { var cap = 64 while cap <= 16 * 1024 { var buf = [Int8](repeating: 0, count: cap) - let err = TSCLibc.strerror_r(errno, &buf, buf.count) + let err = TSCclibc.tsc_strerror_r(errno, &buf, buf.count) if err == EINVAL { return "Unknown error \(errno)" } diff --git a/Sources/TSCclibc/CMakeLists.txt b/Sources/TSCclibc/CMakeLists.txt index bbeb1911..d3c1e78e 100644 --- a/Sources/TSCclibc/CMakeLists.txt +++ b/Sources/TSCclibc/CMakeLists.txt @@ -7,7 +7,7 @@ # See http://swift.org/CONTRIBUTORS.txt for Swift project authors add_library(TSCclibc STATIC - libc.c process.c) + libc.c process.c strerror.c) target_include_directories(TSCclibc PUBLIC include) target_compile_definitions(TSCclibc PRIVATE diff --git a/Sources/TSCclibc/include/module.modulemap b/Sources/TSCclibc/include/module.modulemap index 82bddc8e..e3ee9bc8 100644 --- a/Sources/TSCclibc/include/module.modulemap +++ b/Sources/TSCclibc/include/module.modulemap @@ -2,5 +2,6 @@ module TSCclibc { header "TSCclibc.h" header "indexstore_functions.h" header "process.h" + header "strerror.h" export * } diff --git a/Sources/TSCclibc/include/strerror.h b/Sources/TSCclibc/include/strerror.h new file mode 100644 index 00000000..ca03e2c5 --- /dev/null +++ b/Sources/TSCclibc/include/strerror.h @@ -0,0 +1,5 @@ +#include + +#ifndef _WIN32 +extern int tsc_strerror_r(int errnum, char *buf, size_t buflen); +#endif diff --git a/Sources/TSCclibc/strerror.c b/Sources/TSCclibc/strerror.c new file mode 100644 index 00000000..9dc082ad --- /dev/null +++ b/Sources/TSCclibc/strerror.c @@ -0,0 +1,9 @@ +#undef _GNU_SOURCE +#include "strerror.h" +#include + +#ifndef _WIN32 +int tsc_strerror_r(int errnum, char *buf, size_t buflen) { + return strerror_r(errnum, buf, buflen); +} +#endif From d1ade27962bdbae92cc20f733d0c9aedba12449f Mon Sep 17 00:00:00 2001 From: Finagolfin Date: Wed, 19 Mar 2025 23:51:32 +0530 Subject: [PATCH 2/2] Make sure TSCclibc is built position-independent, because it is failing to build on Amazon Linux 2 otherwise --- Sources/TSCclibc/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/TSCclibc/CMakeLists.txt b/Sources/TSCclibc/CMakeLists.txt index d3c1e78e..e28860c2 100644 --- a/Sources/TSCclibc/CMakeLists.txt +++ b/Sources/TSCclibc/CMakeLists.txt @@ -12,6 +12,7 @@ target_include_directories(TSCclibc PUBLIC include) target_compile_definitions(TSCclibc PRIVATE "$<$:_GNU_SOURCE>") +set_target_properties(TSCclibc PROPERTIES POSITION_INDEPENDENT_CODE YES) if(NOT BUILD_SHARED_LIBS) install(TARGETS TSCclibc