Skip to content

Commit 8c94034

Browse files
committed
Fix build of headers-only libraries
1 parent 6ea0610 commit 8c94034

File tree

4 files changed

+86
-40
lines changed

4 files changed

+86
-40
lines changed

cmakemodules/DeclareMRPTLib.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,6 @@ macro(internal_define_mrpt_lib name headers_only )
437437
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT Libraries
438438
)
439439

440-
441440
# Collect .pdb debug files for optional installation:
442441
if (MSVC)
443442
set(PDB_FILE

modules/mrpt_common/CMakeLists.txt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,21 @@ endif()
109109
# -----------------------------------------------------------------------------
110110
# common
111111
# -----------------------------------------------------------------------------
112-
set(CUSTOM_CMAKE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
113-
114112
# Install files common to both, ROS and non-ROS builds:
115113
install(
116114
FILES
117-
${CUSTOM_CMAKE_DIR}/mrpt_cmake_functions.cmake
118-
${CUSTOM_CMAKE_DIR}/mrpt-xxx-config.cmake.in
115+
${CMAKE_CURRENT_SOURCE_DIR}/cmake/mrpt_cmake_functions.cmake
116+
${CMAKE_CURRENT_SOURCE_DIR}/cmake/mrpt-xxx-config.cmake.in
119117
DESTINATION
120118
share/${PROJECT_NAME}/cmake
121119
)
122-
120+
install(
121+
DIRECTORY
122+
${CMAKE_CURRENT_SOURCE_DIR}/common_headers
123+
${CMAKE_CURRENT_SOURCE_DIR}/common_sources
124+
DESTINATION
125+
share/${PROJECT_NAME}/
126+
)
123127

124128
# -----------------------------------------------------------------------------
125129
# ROS1

modules/mrpt_common/cmake/mrpt_cmake_functions.cmake

Lines changed: 76 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ include(CMakePackageConfigHelpers)
1313
find_package(GTest QUIET)
1414

1515
# This file defines utility CMake functions to ensure uniform settings all
16-
# accross MRPT modules, programs, and tests.
16+
# across MRPT modules, programs, and tests.
17+
#
1718
# Usage:
18-
# include(mrpt_cmake_functions)
19+
#
20+
# find_package(mrpt_common REQUIRED) # this includes mrpt_cmake_functions.cmake
1921
#
2022

2123
# ccache:
@@ -160,7 +162,7 @@ endfunction()
160162
#
161163
# Set defaults for each MRPT cmake target
162164
# -----------------------------------------------------------------------------
163-
function(mrpt_set_target_build_options TARGETNAME)
165+
function(mrpt_set_target_build_options TARGETNAME HEADERS_ONLY_LIBRARY)
164166
# Build for C++17
165167
mrpt_set_target_cxx17(${TARGETNAME})
166168

@@ -219,34 +221,50 @@ endfunction()
219221
#
220222
# Define a consistent install behavior for cmake-based library project:
221223
# -----------------------------------------------------------------------------
222-
function(mrpt_configure_library TARGETNAME)
224+
function(mrpt_configure_library TARGETNAME HEADERS_ONLY_LIBRARY)
225+
223226
# Public hdrs interface:
224-
target_include_directories(${TARGETNAME} PUBLIC
227+
if (HEADERS_ONLY_LIBRARY)
228+
target_include_directories(${TARGETNAME} INTERFACE
229+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
230+
$<INSTALL_INTERFACE:include>
231+
)
232+
else()
233+
target_include_directories(${TARGETNAME} PUBLIC
225234
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
226235
$<INSTALL_INTERFACE:include>
227236
PRIVATE src
228237
)
238+
endif()
229239

230-
# Dynamic libraries output options:
231-
# -----------------------------------
232-
set_target_properties(${TARGETNAME} PROPERTIES
233-
OUTPUT_NAME "${TARGETNAME}${MRPT_DLL_VERSION_POSTFIX}"
234-
COMPILE_PDB_NAME "${TARGETNAME}${MRPT_DLL_VERSION_POSTFIX}"
235-
COMPILE_PDB_NAME_DEBUG "${TARGETNAME}${MRPT_DLL_VERSION_POSTFIX}${CMAKE_DEBUG_POSTFIX}"
236-
VERSION "${MRPT_VERSION_NUMBER_MAJOR}.${MRPT_VERSION_NUMBER_MINOR}.${MRPT_VERSION_NUMBER_PATCH}"
237-
SOVERSION ${MRPT_VERSION_NUMBER_MAJOR}.${MRPT_VERSION_NUMBER_MINOR}
238-
)
240+
241+
if (NOT HEADERS_ONLY_LIBRARY)
242+
# Dynamic libraries output options:
243+
# -----------------------------------
244+
set_target_properties(${TARGETNAME} PROPERTIES
245+
OUTPUT_NAME "${TARGETNAME}${MRPT_DLL_VERSION_POSTFIX}"
246+
COMPILE_PDB_NAME "${TARGETNAME}${MRPT_DLL_VERSION_POSTFIX}"
247+
COMPILE_PDB_NAME_DEBUG "${TARGETNAME}${MRPT_DLL_VERSION_POSTFIX}${CMAKE_DEBUG_POSTFIX}"
248+
VERSION "${MRPT_VERSION_NUMBER_MAJOR}.${MRPT_VERSION_NUMBER_MINOR}.${MRPT_VERSION_NUMBER_PATCH}"
249+
SOVERSION ${MRPT_VERSION_NUMBER_MAJOR}.${MRPT_VERSION_NUMBER_MINOR}
250+
)
251+
endif()
239252

240253
# Project "folder":
241254
# -------------------
242255
set_target_properties(${TARGETNAME} PROPERTIES FOLDER "MRPT-modules")
243256

244257
# Install lib:
245-
install(TARGETS ${TARGETNAME} EXPORT ${TARGETNAME}-targets
246-
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
247-
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
248-
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
249-
)
258+
if (NOT HEADERS_ONLY_LIBRARY)
259+
install(TARGETS ${TARGETNAME} EXPORT ${TARGETNAME}-targets
260+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
261+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
262+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
263+
)
264+
else()
265+
install(TARGETS ${TARGETNAME} EXPORT ${TARGETNAME}-targets)
266+
endif()
267+
250268
# Install hdrs:
251269
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/include/)
252270
install(
@@ -255,9 +273,7 @@ function(mrpt_configure_library TARGETNAME)
255273
)
256274
endif()
257275

258-
259276
strip_mrpt_name(${TARGETNAME} MRPT_LIB_NAME)
260-
261277

262278
# Create module/config.h file
263279
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/config.h.in)
@@ -270,8 +286,11 @@ function(mrpt_configure_library TARGETNAME)
270286
target_include_directories(${TARGETNAME} PUBLIC
271287
$<BUILD_INTERFACE:${MODULES_BASE_CONFIG_INCLUDES_DIR}>
272288
)
273-
# TODO!
274-
install()
289+
# And install it:
290+
install(
291+
FILES ${MODULE_CONFIG_FILE_INCLUDE_DIR}/config.h
292+
DESTINATION ${CMAKE_INSTALL_PREFIX}/include/mrpt/${MRPT_LIB_NAME}/
293+
)
275294
unset(MODULES_BASE_CONFIG_INCLUDES_DIR)
276295
endif()
277296

@@ -284,7 +303,7 @@ function(mrpt_configure_library TARGETNAME)
284303
)
285304

286305
# Add alias to use the namespaced name within local builds from source:
287-
add_library(mrpt::${TARGETNAME} ALIAS ${TARGETNAME})
306+
#add_library(mrpt::${TARGETNAME} ALIAS ${TARGETNAME})
288307

289308
# And generate the -config.cmake file:
290309
set(ALL_DEPS_LIST ${ARGN}) # used in xxx-config.cmake.in
@@ -300,6 +319,15 @@ function(mrpt_configure_library TARGETNAME)
300319
COMPATIBILITY AnyNewerVersion
301320
)
302321

322+
# Regular arch-dep libraries get to LIBDIR (/usr/lib), while
323+
# arch-indep (headers-only) go to DATADIR (/usr/share):
324+
#if (${headers_only})
325+
# set(LIB_TARGET_INSTALL_DEST ${CMAKE_INSTALL_DATADIR})
326+
#else()
327+
# set(LIB_TARGET_INSTALL_DEST ${CMAKE_INSTALL_LIBDIR})
328+
#endif()
329+
330+
303331
# Install cmake config module
304332
install(
305333
EXPORT
@@ -384,6 +412,7 @@ endfunction()
384412
# mrpt_add_library(
385413
# TARGET name
386414
# SOURCES ${SRC_FILES}
415+
# [HEADERS_ONLY_LIBRARY]
387416
# [PUBLIC_LINK_LIBRARIES lib1 lib2]
388417
# [PRIVATE_LINK_LIBRARIES lib3 lib4]
389418
# [CMAKE_DEPENDENCIES pkg1 pkg2]
@@ -393,7 +422,7 @@ endfunction()
393422
# that needs to be find_package'd in this library's xxx-config.cmake file.
394423
# -----------------------------------------------------------------------------
395424
function(mrpt_add_library)
396-
set(options "")
425+
set(options HEADERS_ONLY_LIBRARY)
397426
set(oneValueArgs TARGET)
398427
set(multiValueArgs SOURCES PUBLIC_LINK_LIBRARIES PRIVATE_LINK_LIBRARIES CMAKE_DEPENDENCIES)
399428
cmake_parse_arguments(MRPT_ADD_LIBRARY "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
@@ -424,20 +453,33 @@ function(mrpt_add_library)
424453
handle_special_simd_flags("${MRPT_ADD_LIBRARY_SOURCES}" ".*\.AVX.cpp" "-mavx")
425454
handle_special_simd_flags("${MRPT_ADD_LIBRARY_SOURCES}" ".*\.AVX2.cpp" "-mavx2")
426455

427-
428456
# Don't include here the unit testing code:
429457
remove_matching_files_from_list(".*_unittest.cpp" MRPT_ADD_LIBRARY_SOURCES)
430458

459+
# Library type:
460+
# - HEADERS_ONLY_LIBRARY: `INTERFACE`
461+
# - Regular lib: (none: default to SHARED / STATIC)
431462

432463
# Create library target:
433-
add_library(${MRPT_ADD_LIBRARY_TARGET}
434-
SHARED
435-
${MRPT_ADD_LIBRARY_SOURCES}
436-
)
464+
if (NOT MRPT_ADD_LIBRARY_HEADERS_ONLY_LIBRARY)
465+
# Regular library:
466+
add_library(${MRPT_ADD_LIBRARY_TARGET}
467+
${MRPT_ADD_LIBRARY_SOURCES}
468+
)
469+
#add_coverage(${name})
470+
471+
mrpt_set_target_build_options(${MRPT_ADD_LIBRARY_TARGET} ${MRPT_ADD_LIBRARY_HEADERS_ONLY_LIBRARY})
472+
473+
else()
474+
# A hdr-only library: needs no real compiling
475+
add_library(${MRPT_ADD_LIBRARY_TARGET} INTERFACE)
476+
477+
# List of hdr files (for editing in IDEs,etc.):
478+
#target_sources(${MRPT_ADD_LIBRARY_TARGET} INTERFACE ${MRPT_ADD_LIBRARY_SOURCES})
479+
endif()
437480

438481
# Define common flags:
439-
mrpt_set_target_build_options(${MRPT_ADD_LIBRARY_TARGET})
440-
mrpt_configure_library(${MRPT_ADD_LIBRARY_TARGET} ${MRPT_ADD_LIBRARY_CMAKE_DEPENDENCIES})
482+
mrpt_configure_library(${MRPT_ADD_LIBRARY_TARGET} ${MRPT_ADD_LIBRARY_CMAKE_DEPENDENCIES} ${MRPT_ADD_LIBRARY_HEADERS_ONLY_LIBRARY})
441483

442484
# lib Dependencies:
443485
target_link_libraries(${MRPT_ADD_LIBRARY_TARGET}
@@ -473,7 +515,7 @@ function(mrpt_add_executable)
473515
)
474516

475517
# Define common flags:
476-
mrpt_set_target_build_options(${MRPT_ADD_EXECUTABLE_TARGET})
518+
mrpt_set_target_build_options(${MRPT_ADD_EXECUTABLE_TARGET} FALSE) # FALSE: no headers-only target
477519
mrpt_configure_app(${MRPT_ADD_EXECUTABLE_TARGET})
478520

479521
# lib Dependencies:
@@ -516,7 +558,7 @@ function(mrpt_add_test)
516558
)
517559

518560
# Define common flags:
519-
mrpt_set_target_build_options(${MRPT_ADD_TEST_TARGET})
561+
mrpt_set_target_build_options(${MRPT_ADD_TEST_TARGET} FALSE) # FALSE: no headers-only target
520562
mrpt_configure_app(${MRPT_ADD_TEST_TARGET})
521563

522564
# lib Dependencies:

modules/mrpt_typemeta/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ set(LIB_PUBLIC_HDRS
3434
mrpt_add_library(
3535
TARGET ${PROJECT_NAME}
3636
SOURCES ${LIB_SRCS} ${LIB_PUBLIC_HDRS}
37+
HEADERS_ONLY_LIBRARY
3738
# PUBLIC_LINK_LIBRARIES
3839
# xxx
3940
# PRIVATE_LINK_LIBRARIES

0 commit comments

Comments
 (0)