Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declare single library in CMake #134

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 6 additions & 30 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,12 @@ set(docopt_HEADERS
#============================================================================
# Compile targets
#============================================================================
if(MSVC OR XCODE)
# MSVC requires __declspec() attributes, which are achieved via the
# DOCOPT_DLL and DOCOPT_EXPORTS macros below. Since those macros are only
# defined when building a shared library, we must build the shared and
# static libraries completely separately.
# Xcode does not support libraries with only object files as sources.
# See https://cmake.org/cmake/help/v3.0/command/add_library.html?highlight=add_library
add_library(docopt SHARED ${docopt_SOURCES} ${docopt_HEADERS})
add_library(docopt_s STATIC ${docopt_SOURCES} ${docopt_HEADERS})
else()
# If not using MSVC or Xcode, we will create an intermediate object target
# to avoid compiling the source code twice.
add_library(docopt_o OBJECT ${docopt_SOURCES} ${docopt_HEADERS})
set_target_properties(docopt_o PROPERTIES POSITION_INDEPENDENT_CODE TRUE)

add_library(docopt SHARED $<TARGET_OBJECTS:docopt_o>)
set_target_properties(docopt PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
)
add_library(docopt_s STATIC $<TARGET_OBJECTS:docopt_o>)
endif()
add_library(docopt ${docopt_SOURCES} ${docopt_HEADERS})
set_target_properties(docopt PROPERTIES
POSITION_INDEPENDENT_CODE TRUE
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
)

target_include_directories(docopt PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}> $<INSTALL_INTERFACE:include/docopt>)
target_include_directories(docopt_s PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}> $<INSTALL_INTERFACE:include/docopt>)
Expand All @@ -69,11 +53,6 @@ if(MSVC)
PRIVATE DOCOPT_EXPORTS)
endif()

if(NOT MSVC)
set_target_properties(docopt PROPERTIES OUTPUT_NAME docopt)
set_target_properties(docopt_s PROPERTIES OUTPUT_NAME docopt)
endif()

if(USE_BOOST_REGEX)
add_definitions("-DDOCTOPT_USE_BOOST_REGEX")
# This is needed on Linux, where linking a static library into docopt.so
Expand All @@ -82,9 +61,6 @@ if(USE_BOOST_REGEX)
find_package(Boost 1.53 REQUIRED COMPONENTS regex)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(docopt ${Boost_LIBRARIES})
if(WITH_STATIC)
target_link_libraries(docopt_s ${Boost_LIBRARIES})
endif()
endif()

#============================================================================
Expand Down