Skip to content

Commit 57f5e48

Browse files
committed
Cleanup CMake redundancies and use FILE_SET for headers -- only solution that installed non-flat header tree by transitive dependencies.
1 parent a51c463 commit 57f5e48

File tree

7 files changed

+37
-83
lines changed

7 files changed

+37
-83
lines changed

CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required (VERSION 3.16)
1+
cmake_minimum_required (VERSION 3.23)
22
project (liblsl
33
VERSION 1.16.2
44
LANGUAGES C CXX
@@ -17,7 +17,6 @@ include(cmake/SourceFiles.cmake) #
1717
include(cmake/TargetObjLib.cmake) #
1818
include(cmake/TargetLib.cmake) #
1919
include(cmake/Installation.cmake) #
20-
include(cmake/LSLCMake.cmake)
2120
include(cmake/TargetOther.cmake)
2221

2322
if(LSL_UNITTESTS)

cmake/Dependencies.cmake

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ endif()
1919
add_library(lslboost INTERFACE)
2020
if(LSL_BUNDLED_BOOST)
2121
message(STATUS "Using bundled header-only Boost")
22-
target_include_directories(lslboost SYSTEM INTERFACE
23-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/lslboost>)
22+
target_include_directories(lslboost
23+
SYSTEM INTERFACE
24+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/lslboost>
25+
)
2426
else()
2527
message(STATUS "Using system Boost")
2628
find_package(Boost REQUIRED)
27-
target_compile_definitions(lslboost INTERFACE
28-
lslboost=boost # allows the LSL code base to work with the original Boost namespace/headers
29-
)
29+
# Map `lslboost` namespace, which LSL code base uses, to system `boost` namespace/headers.
30+
target_compile_definitions(lslboost INTERFACE lslboost=boost)
3031
target_link_libraries(lslboost INTERFACE Boost::boost Boost::disable_autolinking)
3132
endif()
3233
target_compile_definitions(lslboost INTERFACE BOOST_ALL_NO_LIB)

cmake/Installation.cmake

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,20 @@ write_basic_package_version_file(
1919

2020
# Define installation targets
2121
set(LSLTargets lsl)
22-
if(LSL_BUILD_STATIC)
23-
list(APPEND LSLTargets lslobj lslboost)
24-
endif()
2522

2623
# Install the targets and store configuration information.
2724
install(TARGETS ${LSLTargets}
28-
EXPORT LSLTargets # generates a CMake package config; TODO: Why the same name as the list of targets?
25+
EXPORT LSLTargets
2926
COMPONENT liblsl
3027
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
3128
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
3229
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
33-
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
34-
# If we use CMake 3.23 FILE_SET, replace INCLUDES line with: FILE_SET HEADERS DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
35-
)
36-
37-
# TODO: What does this do? Why do we need LSLTargets.cmake in the build dir?
38-
export(EXPORT LSLTargets
39-
FILE "${CMAKE_CURRENT_BINARY_DIR}/LSLTargets.cmake"
40-
NAMESPACE LSL::
30+
FILE_SET HEADERS DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
4131
)
4232

4333
# Generate the LSLConfig.cmake file and mark it for installation
4434
install(EXPORT LSLTargets
45-
FILE LSLTargets.cmake # TODO: I think we can use this to generate LSLConfig.cmake, no?
35+
FILE LSLConfig.cmake
4636
COMPONENT liblsl
4737
NAMESPACE "LSL::"
4838
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/LSL
@@ -53,28 +43,10 @@ install(EXPORT LSLTargets
5343
# INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/lsl)
5444
# If we use this method, then we need a corresponding install(FILES ...) command to install the generated file.
5545

56-
# Copy hardcoded CMake files to the build directory.
57-
# TODO: Why bother with this copy? Is it not enough to install (into cmake/LSL)?
58-
configure_file(cmake/LSLCMake.cmake "${CMAKE_CURRENT_BINARY_DIR}/LSLCMake.cmake" COPYONLY)
59-
# TODO: Why bother with this copy? Is it not enough to install (into cmake/LSL)?
60-
# TODO: Why use hardcoded files? We can generate the LSLConfig.cmake.
61-
configure_file(cmake/LSLConfig.cmake "${CMAKE_CURRENT_BINARY_DIR}/LSLConfig.cmake" COPYONLY)
62-
63-
# Install the public headers.
64-
# TODO: Verify that this is necessary, given that we already installed the INCLUDES above.
65-
# TODO: Verify if it is still necessary to install the headers if we use FILE_SET.
66-
install(DIRECTORY include/
67-
COMPONENT liblsl
68-
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
69-
)
70-
7146
# Install the version file and the helper CMake script.
7247
install(
7348
FILES
74-
# TODO: Keep this. But why does the configure_file(... COPYONLY) above exist?
7549
cmake/LSLCMake.cmake
76-
# TODO: Next line shouldn't be necessary if install(EXPORT...) uses LSLConfig.cmake instead of LSLTargets.cmake
77-
${CMAKE_CURRENT_BINARY_DIR}/LSLConfig.cmake
7850
${CMAKE_CURRENT_BINARY_DIR}/LSLConfigVersion.cmake
7951
COMPONENT liblsl
8052
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/LSL

cmake/LSLConfig.cmake

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

cmake/TargetLib.cmake

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set_source_files_properties("src/buildinfo.cpp"
66
LSL_LIBRARY_INFO_STR="${LSL_VERSION_INFO}/link:${LSL_LIB_TYPE}"
77
)
88
add_library(lsl ${LSL_LIB_TYPE} src/buildinfo.cpp)
9+
add_library(LSL::lsl ALIAS lsl)
910

1011
# Configure main library
1112

@@ -23,19 +24,19 @@ if(LSL_FORCE_FANCY_LIBNAME)
2324
)
2425
endif()
2526

26-
# Includes. TODO: Can we not inherit these from lslobj?
27-
target_include_directories(lsl
28-
INTERFACE
29-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
30-
$<INSTALL_INTERFACE:include>
31-
)
27+
# Link dependencies. The only dependency is lslobj, which contains the bulk of the library code and linkages.
28+
# Note: We link PRIVATE because lslobj exposes extra symbols that are not part of the public API
29+
# but are used by the internal tests.
30+
target_link_libraries(lsl PRIVATE lslobj)
3231

33-
# Link dependencies. The biggest dependency is lslobj, which contains the bulk of the library code.
34-
target_link_libraries(lsl
35-
PRIVATE
36-
lslobj # TODO: If this is public, does that improve inheritance of includes and compile definitions?
37-
lslboost # TODO: Shouldn't be needed -- lslobj already links it
38-
${PUGIXML_LIBRARIES}
32+
# Set the include directories for the lsl target.
33+
# Note: We had to link lslobj as a PRIVATE dependency, therefore we must manually expose the include directories
34+
get_target_property(LSLOBJ_HEADERS lslobj HEADER_SET)
35+
target_sources(lsl
36+
INTERFACE
37+
FILE_SET HEADERS
38+
BASE_DIRS include
39+
FILES ${LSLOBJ_HEADERS}
3940
)
4041

4142
# Set compile definitions for lsl

cmake/TargetObjLib.cmake

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
11
# Create object library so all files are only compiled once
22
add_library(lslobj OBJECT
33
${lslsources}
4-
${lslheaders}
4+
# ${lslheaders} # Headers are added later using FILE_SET
55
)
66

77
# Set the includes/headers for the lslobj target
8-
# Note: We cannot use the PUBLIC_HEADER property of the target, because
9-
# it flattens the include directories.
10-
# We could use the new FILE_SET feature that comes with CMake 3.23.
11-
# This is how it would look. Note that HEADERS is a special set name and implies its type.
12-
#target_sources(lslobj
13-
# PUBLIC
14-
# FILE_SET HEADERS
15-
# BASE_DIRS include
16-
# FILES ${lslheaders}
17-
#)
18-
# We settle on the older and more common target_include_directories PUBLIC approach.
19-
# If we used the FILET_SET approach then we would remove the PUBLIC includes below.
20-
target_include_directories(lslobj
21-
PUBLIC
22-
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
23-
$<INSTALL_INTERFACE:include>
8+
# Note: We cannot use the PUBLIC_HEADER property of the target,
9+
# because it flattens the include directories.
10+
# Note: IME, this approach is less error prone than target_include_directories
11+
target_sources(lslobj
2412
INTERFACE
25-
# Propagate include directories to consumers
26-
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/src> # for unit tests
13+
FILE_SET HEADERS # special set name; implies TYPE.
14+
BASE_DIRS include
15+
FILES ${lslheaders}
2716
)
2817

2918
# Link system libs
19+
# (boost might be bundled or system)
3020
target_link_libraries(lslobj PRIVATE lslboost Threads::Threads)
3121
if(MINGW)
3222
target_link_libraries(lslobj PRIVATE bcrypt)
@@ -61,17 +51,13 @@ if(WIN32)
6151
PUBLIC
6252
_WIN32_WINNT=${LSL_WINVER}
6353
)
64-
if(BUILD_SHARED_LIBS)
65-
# set_target_properties(lslobj
66-
# PROPERTIES
67-
# WINDOWS_EXPORT_ALL_SYMBOLS ON
68-
# )
69-
endif(BUILD_SHARED_LIBS)
7054
endif(WIN32)
7155

7256
# Link in 3rd party dependencies
7357
# - loguru and asio header-only
7458
target_include_directories(lslobj
59+
# Note: We use `SYSTEM` to suppress warnings from 3rd party headers and put these at the end of the include path.
60+
# Note: We use `PUBLIC` because 'internal tests' import individual source files and link lslobj.
7561
SYSTEM PUBLIC
7662
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/loguru>
7763
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/asio>
@@ -88,6 +74,7 @@ if(NOT LSL_OPTIMIZATIONS)
8874
endif()
8975

9076
# - pugixml
77+
# Note: We use `PUBLIC` because 'internal tests' import individual source files and link lslobj.
9178
if(LSL_BUNDLED_PUGIXML)
9279
target_sources(lslobj PRIVATE thirdparty/pugixml/pugixml.cpp)
9380
target_include_directories(lslobj

cmake/TargetOther.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
include(cmake/LSLCMake.cmake) # Needed for `installLSLApp`
2+
13
# Build utilities
24
add_executable(lslver testing/lslver.c)
35
target_link_libraries(lslver PRIVATE lsl)

0 commit comments

Comments
 (0)