Skip to content

Commit 39c2b06

Browse files
author
Rye
committed
Clean up cmake linker flags and introduce support for Sanitizers under linux
Fix test failing on clang-19
1 parent 8b7245c commit 39c2b06

File tree

12 files changed

+68
-92
lines changed

12 files changed

+68
-92
lines changed

indra/cmake/00-Common.cmake

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
include_guard()
1616

1717
include(Variables)
18-
include(Linker)
1918

2019
# We go to some trouble to set LL_BUILD to the set of relevant compiler flags.
2120
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{LL_BUILD}")
@@ -70,9 +69,12 @@ if (WINDOWS)
7069
# http://www.cmake.org/pipermail/cmake/2009-September/032143.html
7170
string(REPLACE "/Zm1000" " " CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
7271

73-
add_link_options(/LARGEADDRESSAWARE
74-
/NODEFAULTLIB:LIBCMT
75-
/IGNORE:4099)
72+
add_link_options(
73+
/LARGEADDRESSAWARE
74+
/NODEFAULTLIB:LIBCMT
75+
/IGNORE:4099
76+
/MANIFEST:NO
77+
)
7678

7779
add_compile_definitions(
7880
WIN32_LEAN_AND_MEAN
@@ -120,7 +122,6 @@ endif (WINDOWS)
120122
if (LINUX)
121123
set( CMAKE_BUILD_WITH_INSTALL_RPATH TRUE )
122124
set( CMAKE_INSTALL_RPATH $ORIGIN $ORIGIN/../lib )
123-
set(CMAKE_EXE_LINKER_FLAGS "-Wl,--exclude-libs,ALL")
124125

125126
find_program(CCACHE_EXE ccache)
126127
if(CCACHE_EXE AND NOT DISABLE_CCACHE)
@@ -139,76 +140,92 @@ if (LINUX)
139140
LL_IGNORE_SIGCHLD
140141
)
141142

142-
if( ENABLE_ASAN )
143-
add_compile_options(-U_FORTIFY_SOURCE
144-
-fsanitize=address
145-
--param asan-stack=0
146-
)
147-
add_link_options(-fsanitize=address)
148-
else()
149-
add_compile_definitions( _FORTIFY_SOURCE=2 )
150-
endif()
143+
if (USE_ASAN)
144+
add_compile_options(-fsanitize=address -fsanitize-recover=address)
145+
add_link_options(-fsanitize=address -fsanitize-recover=address)
146+
elseif (USE_LEAKSAN)
147+
add_compile_options(-fsanitize=leak)
148+
add_link_options(-fsanitize=leak)
149+
elseif (USE_UBSAN)
150+
add_compile_options(-fsanitize=undefined -fno-sanitize=vptr)
151+
add_link_options(-fsanitize=undefined -fno-sanitize=vptr)
152+
elseif (USE_THREAD_SAN)
153+
add_compile_options(-fsanitize=thread)
154+
add_link_options(-fsanitize=thread)
155+
else ()
156+
add_compile_definitions(_FORTIFY_SOURCE=2)
157+
endif ()
151158

152159
add_compile_options(
153160
-fexceptions
154161
-fno-math-errno
155162
-fno-strict-aliasing
163+
-fno-omit-frame-pointer
156164
-fsigned-char
157165
-msse2
158166
-mfpmath=sse
159167
-pthread
160168
-fvisibility=hidden
169+
-fstack-protector
161170
)
162171

163172
add_link_options(
164-
-Wl,--no-keep-memory
165-
-Wl,--build-id
166-
-Wl,--no-undefined
173+
"LINKER:--build-id"
174+
"LINKER:--as-needed"
175+
"LINKER:-z,relro"
176+
"LINKER:-z,now"
167177
)
168178

169-
# this stops us requiring a really recent glibc at runtime
170-
add_compile_options(-fno-stack-protector)
179+
if(LINK_WITH_MOLD)
180+
find_program(MOLD_BIN mold)
181+
if(MOLD_BIN)
182+
message(STATUS "Mold linker found: ${MOLD_BIN}. Enabling mold as active linker.")
183+
add_link_options(-fuse-ld=mold)
184+
else()
185+
set(LINK_WITH_MOLD OFF)
186+
message(STATUS "Mold linker not found. Using default linker.")
187+
endif()
188+
endif()
171189

172-
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
173-
# ND: clang is a bit more picky than GCC, the latter seems to auto include -lstdc++ and -lm. The former not so and thus fails to link
174-
add_link_options(
175-
-lstdc++
176-
-lm
177-
)
190+
if(NOT LINK_WITH_MOLD)
191+
# Use LLD for proper clang support under Linux
192+
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
193+
add_link_options(-fuse-ld=lld) # Use LLD for proper clang support
194+
endif()
178195
endif()
196+
179197
endif (LINUX)
180198

181199
if (DARWIN)
182200
# Warnings should be fatal -- thanks, Nicky Perian, for spotting reversed default
183201
set(CLANG_DISABLE_FATAL_WARNINGS OFF)
184202
set(CMAKE_CXX_LINK_FLAGS "-Wl,-headerpad_max_install_names,-search_paths_first")
185203
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_CXX_LINK_FLAGS}")
186-
set(DARWIN_extra_cstar_flags "-Wno-deprecated-declarations")
187204
# Ensure that CMAKE_CXX_FLAGS has the correct -g debug information format --
188205
# see Variables.cmake.
189206
string(REPLACE "-gdwarf-2" "-g${CMAKE_XCODE_ATTRIBUTE_DEBUG_INFORMATION_FORMAT}"
190207
CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
191-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${DARWIN_extra_cstar_flags}")
192-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${DARWIN_extra_cstar_flags}")
193208
# NOTE: it's critical that the optimization flag is put in front.
194209
# NOTE: it's critical to have both CXX_FLAGS and C_FLAGS covered.
195210
## Really?? On developer machines too?
196211
##set(ENABLE_SIGNING TRUE)
197212
##set(SIGNING_IDENTITY "Developer ID Application: Linden Research, Inc.")
198-
199-
# required for clang-15/xcode-15 since our boost package still uses deprecated std::unary_function/binary_function
200-
# see https://developer.apple.com/documentation/xcode-release-notes/xcode-15-release-notes#C++-Standard-Library
201-
add_compile_definitions(_LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION)
202213
endif(DARWIN)
203214

204215
if(LINUX OR DARWIN)
205-
add_compile_options(-Wall -Wno-sign-compare -Wno-trigraphs -Wno-reorder -Wno-unused-but-set-variable -Wno-unused-variable -Wno-unused-local-typedef)
216+
add_compile_options(-Wall -Wno-sign-compare -Wno-trigraphs -Wno-reorder -Wno-unused-but-set-variable -Wno-unused-variable)
217+
218+
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
219+
# libstdc++ headers contain deprecated declarations that fail on clang
220+
# macOS currently has many deprecated calls
221+
add_compile_options(-Wno-unused-local-typedef -Wno-deprecated-declarations)
222+
endif()
206223

207224
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
208-
add_compile_options(-Wno-stringop-truncation -Wno-parentheses -Wno-c++20-compat)
225+
add_compile_options(-Wno-stringop-truncation -Wno-parentheses)
209226
endif()
210227

211-
if (NOT GCC_DISABLE_FATAL_WARNINGS)
228+
if (NOT GCC_DISABLE_FATAL_WARNINGS AND NOT CLANG_DISABLE_FATAL_WARNINGS)
212229
add_compile_options(-Werror)
213230
endif ()
214231

indra/cmake/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ set(cmake_SOURCE_FILES
3434
JPEG.cmake
3535
JPEGEncoderBasic.cmake
3636
LibVLCPlugin.cmake
37-
Linker.cmake
3837
Linking.cmake
3938
LLAddBuildTest.cmake
4039
LLAppearance.cmake

indra/cmake/LLAddBuildTest.cmake

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,7 @@ FUNCTION(LL_ADD_INTEGRATION_TEST
222222
# The following was copied to llcorehttp/CMakeLists.txt's texture_load target.
223223
# Any changes made here should be replicated there.
224224
if (WINDOWS)
225-
set_target_properties(INTEGRATION_TEST_${testname}
226-
PROPERTIES
227-
LINK_FLAGS "/debug /NODEFAULTLIB:LIBCMT /SUBSYSTEM:CONSOLE"
228-
)
225+
target_link_options(INTEGRATION_TEST_${testname} PRIVATE /debug /NODEFAULTLIB:LIBCMT /SUBSYSTEM:CONSOLE)
229226
endif ()
230227

231228
if (DARWIN)

indra/cmake/Linker.cmake

Lines changed: 0 additions & 11 deletions
This file was deleted.

indra/linux_crash_logger/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ list(APPEND linux_crash_logger_SOURCE_FILES
4949
${linux_crash_logger_HEADER_FILES}
5050
)
5151

52-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--as-needed")
53-
5452
add_executable(linux-crash-logger ${linux_crash_logger_SOURCE_FILES})
5553

5654
# llcommon uses `clock_gettime' which is provided by librt on linux.

indra/llmath/tests/llquaternion_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,9 @@ namespace tut
349349
ensure(
350350
"2. LLVector4 operator*(const LLVector4 &a, const LLQuaternion &rot) failed",
351351
is_approx_equal(-58153.5390f, result.mV[0]) &&
352-
(183787.8125f == result.mV[1]) &&
353-
(116864.164063f == result.mV[2]) &&
354-
(78.099998f == result.mV[3]));
352+
is_approx_equal(183787.8125f, result.mV[1]) &&
353+
is_approx_equal(116864.164063f, result.mV[2]) &&
354+
is_approx_equal(78.099998f, result.mV[3]));
355355
}
356356

357357
//test case for LLVector3 operator*(const LLVector3 &a, const LLQuaternion &rot) fn.

indra/llprimitive/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,6 @@ if (LL_TESTS)
8181
llgltfmaterial.cpp
8282
)
8383

84-
set_property(SOURCE llprimitive.cpp PROPERTY LL_TEST_ADDITIONAL_LIBRARIES llmessage)
84+
set_property(SOURCE llprimitive.cpp PROPERTY LL_TEST_ADDITIONAL_LIBRARIES llmessage llfilesystem)
8585
LL_ADD_PROJECT_UNIT_TESTS(llprimitive "${llprimitive_TEST_SOURCE_FILES}")
8686
endif (LL_TESTS)

indra/media_plugins/cef/CMakeLists.txt

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,8 @@ if (LINUX)
5050
)
5151

5252
list(APPEND media_plugin_cef_SOURCE_FILES ${LINUX_VOLUME_CATCHER})
53-
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--build-id -Wl,-rpath,'$ORIGIN:$ORIGIN/../../lib'")
54-
list(APPEND media_plugin_cef_LINK_LIBRARIES llwindow )
5553
elseif (DARWIN)
5654
list(APPEND media_plugin_cef_SOURCE_FILES volume_catcher_null.cpp)
57-
find_library(CORESERVICES_LIBRARY CoreServices)
58-
find_library(AUDIOUNIT_LIBRARY AudioUnit)
59-
set( media_plugin_cef_LINK_LIBRARIES
60-
${CORESERVICES_LIBRARY} # for Component Manager calls
61-
${AUDIOUNIT_LIBRARY} # for AudioUnit calls
62-
)
6355
elseif (WINDOWS)
6456
list(APPEND media_plugin_cef_SOURCE_FILES windows_volume_catcher.cpp)
6557
endif (LINUX)
@@ -81,15 +73,15 @@ target_link_libraries(media_plugin_cef
8173
ll::glib_headers
8274
)
8375

84-
if (WINDOWS)
85-
set_target_properties(
86-
media_plugin_cef
87-
PROPERTIES
88-
LINK_FLAGS "/MANIFEST:NO /NODEFAULTLIB:LIBCMT /IGNORE:4099"
76+
if (DARWIN)
77+
find_library(CORESERVICES_LIBRARY CoreServices)
78+
find_library(AUDIOUNIT_LIBRARY AudioUnit)
79+
80+
target_link_libraries(media_plugin_cef
81+
${CORESERVICES_LIBRARY} # for Component Manager calls
82+
${AUDIOUNIT_LIBRARY} # for AudioUnit calls
8983
)
90-
endif (WINDOWS)
9184

92-
if (DARWIN)
9385
# Don't prepend 'lib' to the executable name, and don't embed a full path in the library's install name
9486
set_target_properties(
9587
media_plugin_cef
@@ -107,5 +99,6 @@ if (DARWIN)
10799
VERBATIM
108100
COMMENT "Fixing path to CEF Framework"
109101
)
110-
111-
endif (DARWIN)
102+
elseif (LINUX)
103+
target_link_options(media_plugin_cef PRIVATE "LINKER:--build-id" "LINKER:-rpath,'$ORIGIN:$ORIGIN/../../lib'")
104+
endif ()

indra/media_plugins/example/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@ add_library(media_plugin_example
2424

2525
target_link_libraries(media_plugin_example media_plugin_base )
2626

27-
if (WINDOWS)
28-
set_target_properties(
29-
media_plugin_example
30-
PROPERTIES
31-
LINK_FLAGS "/MANIFEST:NO /NODEFAULTLIB:LIBCMT"
32-
)
33-
endif (WINDOWS)
34-
3527
if (DARWIN)
3628
# Don't prepend 'lib' to the executable name, and don't embed a full path in the library's install name
3729
set_target_properties(

indra/media_plugins/libvlc/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,6 @@ target_link_libraries(media_plugin_libvlc
2828
ll::libvlc
2929
)
3030

31-
if (WINDOWS)
32-
set_target_properties(
33-
media_plugin_libvlc
34-
PROPERTIES
35-
LINK_FLAGS "/MANIFEST:NO /NODEFAULTLIB:LIBCMT"
36-
)
37-
endif (WINDOWS)
38-
3931
if (DARWIN)
4032
# Don't prepend 'lib' to the executable name, and don't embed a full path in the library's install name
4133
set_target_properties(

0 commit comments

Comments
 (0)