Skip to content

Commit dea9dbe

Browse files
committed
build: attempt to fix macOS build errors
Signed-off-by: Ronald Caesar <github43132@proton.me>
1 parent 04b5af2 commit dea9dbe

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

CMakeLists.txt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,59 @@ endforeach()
8989

9090
message(STATUS "All submodules verified successfully")
9191

92+
93+
if(APPLE AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
94+
# 1. Attempt to detect Homebrew prefix automatically
95+
find_program(BREW_PROG brew)
96+
if(BREW_PROG)
97+
execute_process(
98+
COMMAND ${BREW_PROG} --prefix
99+
OUTPUT_VARIABLE BREW_PREFIX
100+
OUTPUT_STRIP_TRAILING_WHITESPACE
101+
)
102+
else()
103+
# Fallback: Standard Architecture Paths
104+
if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "arm64")
105+
set(BREW_PREFIX "/opt/homebrew")
106+
else()
107+
set(BREW_PREFIX "/usr/local")
108+
endif()
109+
endif()
110+
111+
# 2. Define LLVM Path
112+
set(LLVM_PATH "${BREW_PREFIX}/opt/llvm")
113+
114+
if(EXISTS "${LLVM_PATH}")
115+
message(STATUS "Found Homebrew LLVM: ${LLVM_PATH}")
116+
message(STATUS "Patching Linker Flags for libc++ compatibility")
117+
118+
# 3. Add Library Paths
119+
# We use link_directories to ensure these are searched BEFORE system paths
120+
link_directories("${LLVM_PATH}/lib")
121+
122+
# 4. Inject Linker Flags
123+
# -L: Force linker to look in Homebrew LLVM lib first
124+
# -Wl,-rpath: Bake the path into the binary so it runs without setting DYLD_LIBRARY_PATH
125+
# -lunwind: Explicitly link unwinder (often required for C++ exceptions/RTTI in newer LLVM)
126+
set(MACOS_LINKER_FLAGS "-L${LLVM_PATH}/lib -Wl,-rpath,${LLVM_PATH}/lib -stdlib=libc++")
127+
128+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${MACOS_LINKER_FLAGS}")
129+
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${MACOS_LINKER_FLAGS}")
130+
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${MACOS_LINKER_FLAGS}")
131+
132+
# 5. Inject Compiler Flags
133+
# Ensure headers match the library
134+
include_directories(SYSTEM "${LLVM_PATH}/include")
135+
add_compile_options(-stdlib=libc++)
136+
137+
# [Fix] Suppress C++20 char8_t warning in GTest/System headers if desired
138+
add_compile_options(-Wno-character-conversion)
139+
140+
else()
141+
message(WARNING "Homebrew LLVM not found at ${LLVM_PATH}. Build may fail with linker errors.")
142+
endif()
143+
endif()
144+
92145
#-----------------------------
93146
# ---- Target Definitions ----
94147
#-----------------------------

0 commit comments

Comments
 (0)