Skip to content

Commit 4543c19

Browse files
committed
macOS link + libco MSVC: drop librt on Apple, shim libco TLS
macOS link failed with "ld: library 'rt' not found": librt is glibc-only (clock_gettime etc. live in libSystem on macOS). The uv/libgit2/libmdbx consumer stubs linked rt in their non-Windows branch, which also caught macOS — gate rt on Linux only (NOT WIN32 AND NOT APPLE). libco Windows: /std:c17 did not make MSVC define __STDC__, so libco's thread_local/alignas stayed undefined under LIBCO_MP. Force-include a shim (/FI) that defines both via __declspec before settings.h, which libco honours through its #if !defined(...) guards.
1 parent 63f19f8 commit 4543c19

4 files changed

Lines changed: 33 additions & 13 deletions

File tree

build-tools/3rdparty/libco/_build.sh

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,22 @@ windows-x86_64)
101101
echo "windows-x86_64 requires MSVC cl on PATH (run vcvarsall x64)" >&2; exit 1; }
102102
CC=cl
103103
AR=lib
104-
# /std:c17 makes MSVC define __STDC__, which gates libco settings.h's
105-
# _MSC_VER branch that maps thread_local -> __declspec(thread) and
106-
# alignas -> __declspec(align(...)). Without it neither the C nor C++
107-
# branch is taken and `thread_local`/`alignas` stay undefined keywords,
108-
# so amd64.c fails to compile under LIBCO_MP.
109-
CFLAGS_BASE="/nologo /O2 /MD /std:c17 /DLIBCO_MP /D_CRT_SECURE_NO_WARNINGS"
104+
CFLAGS_BASE="/nologo /O2 /MD /DLIBCO_MP /D_CRT_SECURE_NO_WARNINGS"
110105
CFLAGS_EXTRA=""
106+
# Under MSVC C mode __STDC__ is undefined, so libco settings.h leaves
107+
# `thread_local` and `alignas` undefined (its mapping to __declspec(...)
108+
# is gated on __STDC__/_cplusplus). amd64.c then fails to compile under
109+
# LIBCO_MP. libco honours a user-provided definition (#if !defined(...)),
110+
# so force-include a shim that defines both before settings.h is reached.
111+
LIBCO_MSVC_SHIM="$WORK_DIR/picomesh-libco-msvc-shim.h"
112+
cat > "$LIBCO_MSVC_SHIM" <<'SHIM'
113+
#ifndef thread_local
114+
#define thread_local __declspec(thread)
115+
#endif
116+
#ifndef alignas
117+
#define alignas(bytes) __declspec(align(bytes))
118+
#endif
119+
SHIM
111120
;;
112121
*)
113122
echo "unknown TARGET_PLATFORM: $TARGET_PLATFORM" >&2
@@ -125,7 +134,8 @@ if [ "$TARGET_PLATFORM" = "windows-x86_64" ]; then
125134
_OBJ_W=$(cygpath -w "$WORK_DIR/libco.obj")
126135
_OUT_W=$(cygpath -w "$INSTALL_DIR/lib/libco.lib")
127136
_SRC_DIR_W=$(cygpath -w "$SRC_DIR")
128-
MSYS2_ARG_CONV_EXCL='*' $CC $CFLAGS "/I${_SRC_DIR_W}" /c "$_SRC_W" "/Fo${_OBJ_W}"
137+
_SHIM_W=$(cygpath -w "$LIBCO_MSVC_SHIM")
138+
MSYS2_ARG_CONV_EXCL='*' $CC $CFLAGS "/FI${_SHIM_W}" "/I${_SRC_DIR_W}" /c "$_SRC_W" "/Fo${_OBJ_W}"
129139
MSYS2_ARG_CONV_EXCL='*' $AR /nologo "/OUT:${_OUT_W}" "${_OBJ_W}"
130140
else
131141
$CC $CFLAGS -I"$SRC_DIR" -c "$SRC_DIR/libco.c" -o "$WORK_DIR/libco.o"

build-tools/picomesh/libs/libgit2.cmake

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ if(WIN32)
4040
target_link_libraries(libgit2 INTERFACE zlib-ng
4141
ws2_32 advapi32 rpcrt4 crypt32 ole32 secur32 winhttp)
4242
else()
43-
target_link_libraries(libgit2 INTERFACE zlib-ng Threads::Threads rt)
43+
target_link_libraries(libgit2 INTERFACE zlib-ng Threads::Threads)
44+
if(NOT APPLE)
45+
target_link_libraries(libgit2 INTERFACE rt) # glibc clock_gettime; not on macOS
46+
endif()
4447
endif()
4548

4649
message(STATUS "libgit2: prebuilt v${PICOMESH_3RDPARTY_libgit2_VERSION} (${_LG_LIB})")

build-tools/picomesh/libs/libmdbx.cmake

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ find_package(Threads REQUIRED)
3232
if(WIN32)
3333
target_link_libraries(libmdbx INTERFACE ntdll advapi32 user32 kernel32)
3434
else()
35-
target_link_libraries(libmdbx INTERFACE Threads::Threads rt)
35+
target_link_libraries(libmdbx INTERFACE Threads::Threads)
36+
if(NOT APPLE)
37+
target_link_libraries(libmdbx INTERFACE rt) # glibc clock_gettime; not on macOS
38+
endif()
3639
endif()
3740

3841
message(STATUS "libmdbx: prebuilt v${PICOMESH_3RDPARTY_libmdbx_VERSION} (${_LM_LIB})")

build-tools/picomesh/libs/uv.cmake

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,18 @@ set_target_properties(uv PROPERTIES
3030
IMPORTED_LOCATION "${_LIBUV_LIB}"
3131
INTERFACE_INCLUDE_DIRECTORIES "${_LIBUV_DIR}/include"
3232
)
33-
# libuv's transitive system deps differ by platform: POSIX needs libm/librt +
34-
# dl; native Windows needs the Win32 socket/process/debug libraries instead
35-
# (m/rt do not exist there).
33+
# libuv's transitive system deps differ by platform: native Windows needs the
34+
# Win32 socket/process/debug libraries; POSIX needs libm + dl. librt (clock_*)
35+
# is glibc-only — it does NOT exist on macOS (those live in libSystem), so add
36+
# it on Linux only.
3637
if(WIN32)
3738
target_link_libraries(uv INTERFACE
3839
ws2_32 iphlpapi psapi userenv dbghelp ole32 shell32 advapi32)
3940
else()
40-
target_link_libraries(uv INTERFACE Threads::Threads ${CMAKE_DL_LIBS} m rt)
41+
target_link_libraries(uv INTERFACE Threads::Threads ${CMAKE_DL_LIBS} m)
42+
if(NOT APPLE)
43+
target_link_libraries(uv INTERFACE rt)
44+
endif()
4145
endif()
4246

4347
message(STATUS "libuv: prebuilt @${PICOMESH_3RDPARTY_libuv_VERSION} (${_LIBUV_LIB})")

0 commit comments

Comments
 (0)