Skip to content

Commit ea87017

Browse files
authored
Merge pull request #128 from tstenner/cmakelink
CMake reorganization
2 parents 20e7a11 + 3773381 commit ea87017

File tree

3 files changed

+72
-53
lines changed

3 files changed

+72
-53
lines changed

CMakeLists.txt

Lines changed: 69 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@ project (liblsl
77
)
88

99
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
10+
set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
1011
if(NOT MSVC_VERSION VERSION_LESS 1700)
1112
set(CMAKE_CXX_STANDARD 14)
1213
endif()
14+
# generate a compilation database (compile_commands.json) for clang tooling
1315
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
14-
set(LSL_LIB_TYPE SHARED)
1516

1617
option(LSL_DEBUGLOG "Enable (lots of) additional debug messages" OFF)
1718
option(LSL_UNIXFOLDERS "Use the unix folder layout for install targets" On)
1819
option(LSL_FORCE_FANCY_LIBNAME "Add library name decorations (32/64/-debug)" OFF)
1920
option(LSL_BUILD_EXAMPLES "Build example programs in examples/" OFF)
2021
option(LSL_BUILD_STATIC "Build LSL as static library." OFF)
2122
option(LSL_LEGACY_CPP_ABI "Build legacy C++ ABI into lsl-static" OFF)
23+
option(LSL_OPTIMIZATIONS "Enable some more compiler optimizations" ON)
2224
option(LSL_UNITTESTS "Build LSL library unit tests" OFF)
2325
option(LSL_BUNDLED_PUGIXML "Use the bundled pugixml by default" ON)
2426

@@ -28,6 +30,8 @@ set(LSL_WINVER "0x0601" CACHE STRING
2830
if(LSL_BUILD_STATIC)
2931
set(LSL_LIB_TYPE STATIC)
3032
else()
33+
set(LSL_LIB_TYPE SHARED)
34+
# shared libraries require relocatable symbols so we enable them by default
3135
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
3236
endif()
3337

@@ -106,7 +110,7 @@ if(LSL_BUNDLED_PUGIXML)
106110
message(STATUS "Using bundled pugixml")
107111
target_sources(lslobj PRIVATE thirdparty/pugixml/pugixml.cpp)
108112
target_include_directories(lslobj SYSTEM PUBLIC
109-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/pugixml>)
113+
${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/pugixml)
110114
else()
111115
message(STATUS "Using system pugixml")
112116
find_package(pugixml REQUIRED)
@@ -138,90 +142,104 @@ else()
138142
set(lslgitrevision "unknown")
139143
set(lslgitbranch "unknown")
140144
endif()
145+
146+
# generate a version information string that can be retrieved with the exported
147+
# lsl_library_info() function
141148
set(LSL_VERSION_INFO "git:${lslgitrevision}/branch:${lslgitbranch}/build:${CMAKE_BUILD_TYPE}/compiler:${CMAKE_CXX_COMPILER_ID}-${CMAKE_CXX_COMPILER_VERSION}")
149+
set_source_files_properties("src/buildinfo.cpp"
150+
PROPERTIES COMPILE_DEFINITIONS
151+
LSL_LIBRARY_INFO_STR="${LSL_VERSION_INFO}/link:${LSL_LIB_TYPE}"
152+
)
142153
set_source_files_properties("thirdparty/loguru/loguru.cpp"
143154
PROPERTIES COMPILE_DEFINITIONS LOGURU_STACKTRACES=$<BOOL:${LSL_DEBUGLOG}>)
144155

145-
## create the lslboost target
156+
find_package(Threads REQUIRED)
157+
158+
# create the lslboost target
146159
add_library(lslboost OBJECT
147-
lslboost/asio_objects.cpp
148160
lslboost/serialization_objects.cpp
149161
)
150-
151-
if(UNIX)
152-
include(CheckSymbolExists)
153-
# look for clock_gettime, if not we need to link against librt
154-
check_symbol_exists(clock_gettime time.h HAS_GETTIME)
155-
if(NOT HAS_GETTIME)
156-
set(NEEDS_LIBRT ON CACHE INTERNAL "Link to librt")
157-
endif()
158-
endif()
159-
160-
find_package(Threads REQUIRED)
161-
target_link_libraries(lslboost
162-
PUBLIC
163-
Threads::Threads
164-
PRIVATE
165-
$<$<PLATFORM_ID:Windows>:bcrypt>
166-
$<$<PLATFORM_ID:Windows>:mswsock>
167-
$<$<PLATFORM_ID:Windows>:ws2_32>
168-
)
162+
target_link_libraries(lslboost PUBLIC Threads::Threads)
169163
target_compile_features(lslboost PUBLIC cxx_std_11 cxx_lambda_init_captures)
170164

171165
target_compile_definitions(lslboost
172166
PUBLIC
173167
BOOST_ALL_NO_LIB
174168
BOOST_ASIO_STANDALONE
175-
BOOST_ASIO_SEPARATE_COMPILATION
176169
BOOST_THREAD_DONT_PROVIDE_INTERRUPTIONS
177-
$<$<PLATFORM_ID:Windows>:_WIN32_WINNT=${LSL_WINVER}>
178-
PRIVATE
179-
$<$<PLATFORM_ID:Windows>:BOOST_THREAD_BUILD_DLL>
180170
)
181171
target_include_directories(lslboost SYSTEM PUBLIC
182-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/lslboost>)
172+
${CMAKE_CURRENT_SOURCE_DIR}/lslboost)
183173

184-
185-
target_link_libraries(lslobj
186-
PRIVATE
187-
lslboost
188-
$<$<PLATFORM_ID:Windows>:iphlpapi>
189-
$<$<PLATFORM_ID:Windows>:winmm>
190-
PUBLIC
191-
$<$<AND:$<BOOL:${LSL_DEBUGLOG}>,$<PLATFORM_ID:Linux>>:dl>
192-
$<$<BOOL:${NEEDS_LIBRT}>:rt>
193-
)
174+
# target configuration for the internal lslobj target
175+
target_link_libraries(lslobj PRIVATE lslboost)
194176
target_include_directories(lslobj
195177
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
196178
)
197179
target_include_directories(lslobj SYSTEM PUBLIC
198180
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/thirdparty/loguru>
199181
)
200-
target_compile_definitions(lslobj
201-
PRIVATE LIBLSL_EXPORTS $<$<PLATFORM_ID:Windows>:_CRT_SECURE_NO_WARNINGS>
202-
LOGURU_DEBUG_LOGGING=$<BOOL:${LSL_DEBUGLOG}>
203-
INTERFACE LSLNOAUTOLINK # don't use #pragma(lib) in CMake builds
182+
target_compile_definitions(lslobj PRIVATE
183+
LIBLSL_EXPORTS
184+
LOGURU_DEBUG_LOGGING=$<BOOL:${LSL_DEBUGLOG}>
204185
)
205186

187+
# platform specific configuration
188+
if(UNIX)
189+
include(CheckSymbolExists)
190+
# check that clock_gettime is present in the stdlib, link against librt otherwise
191+
check_symbol_exists(clock_gettime time.h HAS_GETTIME)
192+
if(NOT HAS_GETTIME)
193+
target_link_libraries(lslobj PRIVATE rt)
194+
endif()
195+
if(LSL_DEBUGLOG AND NOT APPLE)
196+
target_link_libraries(lslobj PRIVATE dl)
197+
endif()
198+
elseif(WIN32)
199+
target_link_libraries(lslobj PRIVATE iphlpapi winmm)
200+
target_link_libraries(lslboost PRIVATE bcrypt mswsock ws2_32)
201+
target_compile_definitions(lslobj
202+
PRIVATE _CRT_SECURE_NO_WARNINGS
203+
PUBLIC LSLNOAUTOLINK # don't use #pragma(lib) in CMake builds
204+
)
205+
target_compile_definitions(lslboost
206+
PUBLIC _WIN32_WINNT=${LSL_WINVER}
207+
PRIVATE BOOST_THREAD_BUILD_DLL
208+
)
209+
endif()
210+
211+
# the "real" liblsl library. It contains one source with the version info
212+
# string because some generators require it. The remaining source code is
213+
# built in the lslobj target and later linked into this library
206214
add_library(lsl ${LSL_LIB_TYPE} src/buildinfo.cpp)
207215

208216
# defines for LSL_CPP_API export header (shared: dllimport/dllexport)
209217
target_compile_definitions(lsl PUBLIC
210218
$<IF:$<BOOL:${LSL_BUILD_STATIC}>,LIBLSL_STATIC,LIBLSL_EXPORTS>
219+
$<$<CXX_COMPILER_ID:MSVC>:LSLNOAUTOLINK> # don't use #pragma(lib) in CMake builds
211220
)
212-
213-
set_source_files_properties("src/buildinfo.cpp"
214-
PROPERTIES COMPILE_DEFINITIONS
215-
LSL_LIBRARY_INFO_STR="${LSL_VERSION_INFO}/link:${LSL_LIB_TYPE}"
221+
target_link_libraries(lsl PRIVATE lslobj lslboost)
222+
target_include_directories(lsl INTERFACE
223+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
224+
$<INSTALL_INTERFACE:include>
216225
)
217-
218-
target_link_libraries(lsl
219-
PUBLIC lslobj
220-
PRIVATE lslboost)
221226
set_target_properties(lsl PROPERTIES
222227
VERSION ${liblsl_VERSION_MAJOR}.${liblsl_VERSION_MINOR}.${liblsl_VERSION_PATCH}
223228
)
224229

230+
# enable some additional expensive compiler optimizations
231+
if(LSL_OPTIMIZATIONS)
232+
# enable LTO (https://en.wikipedia.org/wiki/Interprocedural_optimization
233+
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)
234+
else()
235+
# build one object file for Asio instead of once every time an Asio function is called. See
236+
# https://think-async.com/Asio/asio-1.18.2/doc/asio/using.html#asio.using.optional_separate_compilation
237+
target_sources(lslboost PRIVATE lslboost/asio_objects.cpp)
238+
target_compile_definitions(lslboost PUBLIC BOOST_ASIO_SEPARATE_COMPILATION)
239+
endif()
240+
241+
242+
225243
if(LSL_FORCE_FANCY_LIBNAME)
226244
math(EXPR lslplatform "8 * ${CMAKE_SIZEOF_VOID_P}")
227245
set_target_properties(lsl PROPERTIES
@@ -246,7 +264,7 @@ write_basic_package_version_file(
246264
COMPATIBILITY AnyNewerVersion
247265
)
248266

249-
install(TARGETS lsl lslobj lslboost
267+
install(TARGETS lsl
250268
EXPORT LSLTargets
251269
COMPONENT liblsl
252270
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}

examples/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.5)
1+
cmake_minimum_required(VERSION 3.12)
22
project(lslexamples
33
LANGUAGES C CXX
44
VERSION 0.2.0)

testing/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ add_executable(lsl_test_exported
2525
test_ext_streaminfo.cpp
2626
test_ext_time.cpp
2727
)
28-
target_link_libraries(lsl_test_exported PRIVATE lsl catch_main)
28+
29+
target_link_libraries(lsl_test_exported PRIVATE lsl catch_main Threads::Threads)
2930

3031
find_package(Threads REQUIRED)
3132

0 commit comments

Comments
 (0)