Skip to content

Commit

Permalink
Support for BUILD_OBJECT_LIBS (#28)
Browse files Browse the repository at this point in the history
- when PTL is a cmake subproject, it can be useful to build an object library so that compiled translation units can be treated as part of the library using PTL
  - E.g. when parent cmake project compiles with -fvisibility=hidden or -fvisibility=internal
  - In subproject mode, this eliminates the need to compile all of PTL with default visibility or decorating PTL functions/classes with __attribute__((visibility("default")))
- Bump version to 2.3.2
  • Loading branch information
jrmadsen authored Mar 3, 2022
1 parent 61f873c commit 4afd2bd
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 23 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.1
2.3.2
22 changes: 18 additions & 4 deletions cmake/Modules/PTLBuildSettings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,24 @@ endif()
# -------------------------------------------------------------------------------------- #
ptl_add_option(BUILD_STATIC_LIBS "Build static library" ON)
ptl_add_option(BUILD_SHARED_LIBS "Build shared library" ON)
if((NOT BUILD_SHARED_LIBS) AND (NOT BUILD_STATIC_LIBS))
message(
FATAL_ERROR
"Neither BUILD_STATIC_LIBS nor BUILD_SHARED_LIBS are set. One must be ON")

if(NOT "${CMAKE_PROJECT_NAME}" STREQUAL "${PROJECT_NAME}")
ptl_add_option(BUILD_OBJECT_LIBS "Build object library (only valid as subproject)"
OFF)
set(PTL_EXCLUDE_FROM_ALL EXCLUDE_FROM_ALL)
set(PTL_BUILD_LIBS_ERROR_DESC
"BUILD_STATIC_LIBS, BUILD_SHARED_LIBS, and BUILD_OBJECT_LIBS are all OFF")
else()
set(BUILD_OBJECT_LIBS OFF)
set(PTL_EXCLUDE_FROM_ALL)
set(PTL_BUILD_LIBS_ERROR_DESC
"Neither BUILD_STATIC_LIBS nor BUILD_SHARED_LIBS are set")
endif()

if((NOT BUILD_SHARED_LIBS)
AND (NOT BUILD_STATIC_LIBS)
AND (NOT BUILD_OBJECT_LIBS))
message(FATAL_ERROR "${PTL_BUILD_LIBS_ERROR_DESC}. One must be ON")
endif()

# -------------------------------------------------------------------------------------- #
Expand Down
33 changes: 19 additions & 14 deletions cmake/Modules/PTLCMakeUtilities.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,11 @@ function(ptl_build_library)
set(LIB_OUTPUT_NAME ${LIB_TARGET_NAME})
endif()

add_library(${LIB_TARGET_NAME} ${LIB_TYPE} ${LIB_SOURCES})
add_library(${LIB_TARGET_NAME} ${LIB_TYPE} ${PTL_EXCLUDE_FROM_ALL})

add_library(${PROJECT_NAME}::${LIB_TARGET_NAME} ALIAS ${LIB_TARGET_NAME})

set_target_properties(
${LIB_TARGET_NAME}
PROPERTIES OUTPUT_NAME ${LIB_OUTPUT_NAME}
VERSION ${${PROJECT_NAME}_VERSION}
SOVERSION ${${PROJECT_NAME}_VERSION_MAJOR}
WINDOWS_EXPORT_ALL_SYMBOLS ON)
target_sources(${LIB_TARGET_NAME} PRIVATE ${LIB_SOURCES})

target_compile_definitions(${LIB_TARGET_NAME} PRIVATE $<$<CONFIG:Debug>:DEBUG>)

Expand All @@ -106,12 +102,21 @@ function(ptl_build_library)
$<$<COMPILE_LANGUAGE:CXX>:${${PROJECT_NAME}_CXX_FLAGS}>)
endif()

set_target_properties(
${LIB_TARGET_NAME}
PROPERTIES OUTPUT_NAME ${LIB_OUTPUT_NAME}
VERSION ${${PROJECT_NAME}_VERSION}
SOVERSION ${${PROJECT_NAME}_VERSION_MAJOR}
WINDOWS_EXPORT_ALL_SYMBOLS ON)

# Install the targets and export libraries
install(
TARGETS ${LIB_TARGET_NAME}
EXPORT ${PROJECT_NAME}Targets
COMPONENT Development
ARCHIVE DESTINATION ${PTL_INSTALL_LIBDIR}
LIBRARY DESTINATION ${PTL_INSTALL_LIBDIR}
RUNTIME DESTINATION ${PTL_INSTALL_BINDIR})
if(NOT "${LIB_TYPE}" STREQUAL "OBJECT")
install(
TARGETS ${LIB_TARGET_NAME}
EXPORT ${PROJECT_NAME}Targets
COMPONENT Development
ARCHIVE DESTINATION ${PTL_INSTALL_LIBDIR}
LIBRARY DESTINATION ${PTL_INSTALL_LIBDIR}
RUNTIME DESTINATION ${PTL_INSTALL_BINDIR} OPTIONAL)
endif()
endfunction()
26 changes: 22 additions & 4 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -------------------------------------------------------------------------------------- #
# # General # #
# General
# -------------------------------------------------------------------------------------- #
# Locate sources and headers for this project - headers are included so they will show up
# in IDEs
Expand All @@ -8,7 +8,7 @@ file(GLOB_RECURSE ptl_headers ${CMAKE_CURRENT_LIST_DIR}/PTL/*.hh
file(GLOB_RECURSE ptl_sources ${CMAKE_CURRENT_LIST_DIR}/*.cc)

# -------------------------------------------------------------------------------------- #
# # Config, Version # #
# Config, Version
# -------------------------------------------------------------------------------------- #

configure_file(${PROJECT_SOURCE_DIR}/cmake/Templates/Config.hh.in
Expand All @@ -22,9 +22,27 @@ configure_file(${PROJECT_SOURCE_DIR}/cmake/Templates/Version.hh.in
list(APPEND ptl_headers ${CMAKE_CURRENT_BINARY_DIR}/PTL/Version.hh)

# -------------------------------------------------------------------------------------- #
# # PTL Library # #
# PTL Library
# -------------------------------------------------------------------------------------- #

if(BUILD_OBJECT_LIBS)

ptl_build_library(
TYPE OBJECT
TARGET_NAME ptl-object
OUTPUT_NAME ptl
SOURCES ${ptl_headers} ${ptl_sources})

target_link_libraries(ptl-object PUBLIC Threads::Threads)
if(PTL_USE_TBB)
target_link_libraries(ptl-object PUBLIC TBB::tbb)
endif()

target_include_directories(
ptl-object PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/source>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)
endif()

if(BUILD_SHARED_LIBS)

ptl_build_library(
Expand Down Expand Up @@ -82,7 +100,7 @@ if(BUILD_STATIC_LIBS)
endif()

# -------------------------------------------------------------------------------------- #
# # Installation # #
# Installation
# -------------------------------------------------------------------------------------- #

if(PTL_INSTALL_CONFIG)
Expand Down

0 comments on commit 4afd2bd

Please sign in to comment.