Skip to content

Commit 44b9f65

Browse files
committed
build: libbacktrace: fix windows msvc integration
libbacktrace was enabled for Windows/MSVC through a custom CMake path, but that path diverged from upstream configure.ac assumptions and broke Windows builds after the b9e4006 update. Fix the Windows build wiring to match upstream behavior: - select unwind backend by compiler: - backtrace.c for toolchains with _Unwind_* support - nounwind.c for MSVC cl.exe - generate a local unistd.h compatibility shim in libbacktrace_config for MSVC (open/read/close/lseek/getpid) - correct generated backtrace-supported.h values on Windows: - set BACKTRACE_SUPPORTED from selected unwind backend - set BACKTRACE_SUPPORTS_THREADS=0 for MSVC config - set BACKTRACE_SUPPORTS_DATA=0 for PE/COFF This keeps libbacktrace enabled on Windows while fixing the MSVC build failure and aligning metadata/macros with upstream semantics. Signed-off-by: Eduardo Silva <eduardo@chronosphere.io>
1 parent 000e158 commit 44b9f65

File tree

1 file changed

+62
-7
lines changed

1 file changed

+62
-7
lines changed

cmake/libbacktrace.cmake

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ if(NOT LIBBACKTRACE_FOUND)
2828
# autoconf/configure doesn't work with MSVC, so we build directly
2929
message(STATUS "libbacktrace: building directly with CMake for Windows/MSVC")
3030

31+
# Match configure.ac behavior for unwind support:
32+
# - GCC/clang with libunwind API -> backtrace.c
33+
# - MSVC cl.exe -> nounwind.c
34+
set(LIBBACKTRACE_BACKTRACE_SOURCE ${LIBBACKTRACE_SOURCE_DIR}/backtrace.c)
35+
set(LIBBACKTRACE_BACKTRACE_SUPPORTED 1)
36+
if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
37+
set(LIBBACKTRACE_BACKTRACE_SOURCE ${LIBBACKTRACE_SOURCE_DIR}/nounwind.c)
38+
set(LIBBACKTRACE_BACKTRACE_SUPPORTED 0)
39+
endif()
40+
3141
# Core source files (always needed)
3242
set(LIBBACKTRACE_CORE_SOURCES
3343
${LIBBACKTRACE_SOURCE_DIR}/atomic.c
@@ -45,7 +55,7 @@ if(NOT LIBBACKTRACE_FOUND)
4555
# - View: read.c (file reading, not mmap for Windows)
4656
# - Alloc: alloc.c (standard alloc, not mmap for Windows)
4757
set(LIBBACKTRACE_PLATFORM_SOURCES
48-
${LIBBACKTRACE_SOURCE_DIR}/backtrace.c
58+
${LIBBACKTRACE_BACKTRACE_SOURCE}
4959
${LIBBACKTRACE_SOURCE_DIR}/pecoff.c
5060
${LIBBACKTRACE_SOURCE_DIR}/read.c
5161
${LIBBACKTRACE_SOURCE_DIR}/alloc.c
@@ -121,6 +131,51 @@ if(NOT LIBBACKTRACE_FOUND)
121131
"#endif /* CONFIG_H */\n"
122132
)
123133

134+
# Create unistd.h compatibility wrapper for MSVC.
135+
# libbacktrace sources include <unistd.h> directly, but MSVC does not
136+
# provide that header.
137+
set(LIBBACKTRACE_UNISTD_H "${LIBBACKTRACE_CONFIG_DIR}/unistd.h")
138+
file(WRITE ${LIBBACKTRACE_UNISTD_H}
139+
"/* unistd.h compatibility for Windows/MSVC - generated by CMake */\n"
140+
"#ifndef LIBBACKTRACE_UNISTD_H\n"
141+
"#define LIBBACKTRACE_UNISTD_H\n"
142+
"\n"
143+
"#include <io.h>\n"
144+
"#include <process.h>\n"
145+
"#include <sys/types.h>\n"
146+
"\n"
147+
"#ifndef _SSIZE_T_DEFINED\n"
148+
"#ifdef _WIN64\n"
149+
"typedef __int64 ssize_t;\n"
150+
"#else\n"
151+
"typedef int ssize_t;\n"
152+
"#endif\n"
153+
"#define _SSIZE_T_DEFINED\n"
154+
"#endif\n"
155+
"\n"
156+
"#ifndef read\n"
157+
"#define read _read\n"
158+
"#endif\n"
159+
"\n"
160+
"#ifndef close\n"
161+
"#define close _close\n"
162+
"#endif\n"
163+
"\n"
164+
"#ifndef open\n"
165+
"#define open _open\n"
166+
"#endif\n"
167+
"\n"
168+
"#ifndef lseek\n"
169+
"#define lseek _lseeki64\n"
170+
"#endif\n"
171+
"\n"
172+
"#ifndef getpid\n"
173+
"#define getpid _getpid\n"
174+
"#endif\n"
175+
"\n"
176+
"#endif /* LIBBACKTRACE_UNISTD_H */\n"
177+
)
178+
124179
# Create backtrace-supported.h for Windows (normally generated by autoconf)
125180
set(LIBBACKTRACE_SUPPORTED_H "${LIBBACKTRACE_CONFIG_DIR}/backtrace-supported.h")
126181
file(WRITE ${LIBBACKTRACE_SUPPORTED_H}
@@ -131,8 +186,8 @@ if(NOT LIBBACKTRACE_FOUND)
131186
" should work, 0 if it will not. Libraries may #include this to make\n"
132187
" other arrangements. */\n"
133188
"\n"
134-
"/* Windows with PE/COFF format supports backtrace */\n"
135-
"#define BACKTRACE_SUPPORTED 1\n"
189+
"/* Stack unwind availability depends on the compiler runtime. */\n"
190+
"#define BACKTRACE_SUPPORTED ${LIBBACKTRACE_BACKTRACE_SUPPORTED}\n"
136191
"\n"
137192
"/* BACKTRACE_USES_MALLOC will be #define'd as 1 if the backtrace\n"
138193
" library will call malloc as it works, 0 if it will call mmap\n"
@@ -141,13 +196,13 @@ if(NOT LIBBACKTRACE_FOUND)
141196
"\n"
142197
"/* BACKTRACE_SUPPORTS_THREADS will be #define'd as 1 if the backtrace\n"
143198
" library is configured with threading support, 0 if not. */\n"
144-
"/* Windows supports threads via Windows API */\n"
145-
"#define BACKTRACE_SUPPORTS_THREADS 1\n"
199+
"/* __sync/__atomic are not available under MSVC cl.exe. */\n"
200+
"#define BACKTRACE_SUPPORTS_THREADS 0\n"
146201
"\n"
147202
"/* BACKTRACE_SUPPORTS_DATA will be #defined'd as 1 if the backtrace_syminfo\n"
148203
" will work for variables. It will always work for functions. */\n"
149-
"/* Windows PE/COFF format supports data symbols */\n"
150-
"#define BACKTRACE_SUPPORTS_DATA 1\n"
204+
"/* configure.ac sets this to 'no' for PE/COFF. */\n"
205+
"#define BACKTRACE_SUPPORTS_DATA 0\n"
151206
)
152207

153208
# Build libbacktrace as a static library

0 commit comments

Comments
 (0)