Commit 7a21c16f authored by Andreas Schuh's avatar Andreas Schuh

cmake: Rename targets to gflags[_nothreads]_(static|shared), support

find_package COMPONENTS
parent a4382e5f
......@@ -15,7 +15,7 @@
## When this project is a subproject (GFLAGS_IS_SUBPROJECT is TRUE), the default
## settings are such that only the static single-threaded library is built without
## installation of the gflags files. The "gflags" target is in this case an ALIAS
## library target for the "gflags_nothreads-static" library target. Targets which
## library target for the "gflags_nothreads_static" library target. Targets which
## depend on the gflags library should link to the "gflags" library target.
##
## Example CMakeLists.txt of user project which requires separate gflags installation:
......@@ -28,6 +28,16 @@
## add_executable(foo src/foo.cc)
## target_link_libraries(foo gflags)
##
## Example CMakeLists.txt of user project which requires separate single-threaded static gflags installation:
## cmake_minimum_required(VERSION 2.8.12 FATAL_ERROR)
##
## project(Foo)
##
## find_package(gflags COMPONENTS nothreads_static)
##
## add_executable(foo src/foo.cc)
## target_link_libraries(foo gflags)
##
## Example CMakeLists.txt of super-project which contains gflags source tree:
## cmake_minimum_required(VERSION 2.8.12 FATAL_ERROR)
##
......@@ -411,12 +421,12 @@ foreach (TYPE IN ITEMS STATIC SHARED)
# multi-threaded vs. single-threaded
foreach (opts IN ITEMS "" _nothreads)
if (BUILD_gflags${opts}_LIB)
add_library (gflags${opts}-${type} ${TYPE} ${GFLAGS_SRCS} ${PRIVATE_HDRS} ${PUBLIC_HDRS})
add_library (gflags${opts}_${type} ${TYPE} ${GFLAGS_SRCS} ${PRIVATE_HDRS} ${PUBLIC_HDRS})
set (include_dirs "$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>")
if (INSTALL_HEADERS)
list (APPEND include_dirs "$<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>")
endif ()
target_include_directories (gflags${opts}-${type}
target_include_directories (gflags${opts}_${type}
PUBLIC "${include_dirs}"
PRIVATE "${PROJECT_SOURCE_DIR}/src;${PROJECT_BINARY_DIR}/include/${GFLAGS_INCLUDE_DIR}"
)
......@@ -425,25 +435,25 @@ foreach (TYPE IN ITEMS STATIC SHARED)
else ()
set (defines "GFLAGS_IS_A_DLL=${GFLAGS_IS_A_DLL}")
if (CMAKE_USE_PTHREADS_INIT)
target_link_libraries (gflags${opts}-${type} ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (gflags${opts}_${type} ${CMAKE_THREAD_LIBS_INIT})
endif ()
endif ()
set_target_properties (
gflags${opts}-${type} PROPERTIES COMPILE_DEFINITIONS "${defines}"
gflags${opts}_${type} PROPERTIES COMPILE_DEFINITIONS "${defines}"
OUTPUT_NAME "gflags${opts}${type_suffix}"
VERSION "${PACKAGE_VERSION}"
SOVERSION "${PACKAGE_SOVERSION}"
)
if (HAVE_SHLWAPI_H)
target_link_libraries (gflags${opts}-${type} shlwapi.lib)
target_link_libraries (gflags${opts}_${type} shlwapi.lib)
endif ()
list (APPEND TARGETS gflags${opts}-${type})
list (APPEND TARGETS gflags${opts}_${type})
# add convenience make target for build of both shared and static libraries
if (NOT GFLAGS_IS_SUBPROJECT)
if (NOT TARGET gflags${opts})
add_custom_target (gflags${opts})
endif ()
add_dependencies (gflags${opts} gflags${opts}-${type})
add_dependencies (gflags${opts} gflags${opts}_${type})
endif ()
endif ()
endforeach ()
......@@ -454,8 +464,8 @@ endforeach ()
if (GFLAGS_IS_SUBPROJECT)
foreach (type IN ITEMS static shared)
foreach (opts IN ITEMS "_nothreads" "")
if (TARGET gflags${opts}-${type})
add_library (gflags ALIAS gflags${opts}-${type})
if (TARGET gflags${opts}_${type})
add_library (gflags ALIAS gflags${opts}_${type})
break ()
endif ()
endforeach ()
......@@ -474,14 +484,14 @@ configure_file (cmake/version.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-co
if (BUILD_SHARED_LIBS AND INSTALL_SHARED_LIBS)
foreach (opts IN ITEMS "" _nothreads)
if (BUILD_gflags${opts}_LIB)
install (TARGETS gflags${opts}-shared DESTINATION ${LIBRARY_INSTALL_DIR} EXPORT gflags-lib)
install (TARGETS gflags${opts}_shared DESTINATION ${LIBRARY_INSTALL_DIR} EXPORT gflags-lib)
endif ()
endforeach ()
endif ()
if (BUILD_STATIC_LIBS AND INSTALL_STATIC_LIBS)
foreach (opts IN ITEMS "" _nothreads)
if (BUILD_gflags${opts}_LIB)
install (TARGETS gflags${opts}-static DESTINATION ${LIBRARY_INSTALL_DIR} EXPORT gflags-lib)
install (TARGETS gflags${opts}_static DESTINATION ${LIBRARY_INSTALL_DIR} EXPORT gflags-lib)
endif ()
endforeach ()
endif ()
......@@ -497,7 +507,7 @@ if (INSTALL_HEADERS)
FILES "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake"
DESTINATION ${CONFIG_INSTALL_DIR}
)
install (EXPORT gflags-lib DESTINATION ${CONFIG_INSTALL_DIR} FILE ${PACKAGE_NAME}-export.cmake)
install (EXPORT gflags-lib DESTINATION ${CONFIG_INSTALL_DIR} FILE ${PACKAGE_NAME}-targets.cmake)
if (UNIX)
install (PROGRAMS src/gflags_completions.sh DESTINATION ${RUNTIME_INSTALL_DIR})
endif ()
......@@ -506,7 +516,7 @@ endif ()
# ----------------------------------------------------------------------------
# support direct use of build tree
set (INSTALL_PREFIX_REL2CONFIG_DIR .)
export (TARGETS ${TARGETS} FILE "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-export.cmake")
export (TARGETS ${TARGETS} FILE "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-targets.cmake")
export (PACKAGE gflags)
configure_file (cmake/config.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config.cmake" @ONLY)
......
......@@ -7,7 +7,7 @@ set (@PACKAGE_PREFIX@_VERSION_MINOR @PACKAGE_VERSION_MINOR@)
set (@PACKAGE_PREFIX@_VERSION_PATCH @PACKAGE_VERSION_PATCH@)
# import targets
include ("${CMAKE_CURRENT_LIST_DIR}/@PACKAGE_NAME@-export.cmake")
include ("${CMAKE_CURRENT_LIST_DIR}/@PACKAGE_NAME@-targets.cmake")
# installation prefix
get_filename_component (CMAKE_CURRENT_LIST_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
......@@ -20,20 +20,51 @@ get_filename_component (_INSTALL_PREFIX "${CMAKE_CURRENT_LIST_DIR}/@INSTALL_PREF
# manually to the include search path for targets which link to gflags.
set (@PACKAGE_PREFIX@_INCLUDE_DIR "${_INSTALL_PREFIX}/@INCLUDE_INSTALL_DIR@")
# default settings
if (@PACKAGE_NAME@_FIND_COMPONENTS)
foreach (@PACKAGE_NAME@_FIND_COMPONENT IN LISTS @PACKAGE_NAME@_FIND_COMPONENTS)
if (@PACKAGE_NAME@_FIND_REQUIRED_${@PACKAGE_NAME@_FIND_COMPONENT} AND NOT TARGET @PACKAGE_NAME@_${@PACKAGE_NAME@_FIND_COMPONENT})
message (FATAL_ERROR "Package @PACKAGE_NAME@ was installed without required component ${@PACKAGE_NAME@_FIND_COMPONENT}!")
endif ()
endforeach ()
list (GET @PACKAGE_NAME@_FIND_COMPONENTS 0 @PACKAGE_NAME@_FIND_COMPONENT)
else ()
set (@PACKAGE_NAME@_FIND_COMPONENT)
endif ()
# default settings of @PACKAGE_PREFIX@_SHARED and @PACKAGE_PREFIX@_NOTHREADS
#
# It is recommended to use either one of the following find_package commands
# instead of setting the @PACKAGE_PREFIX@_(SHARED|NOTHREADS) variables:
# - find_package(@PACKAGE_NAME@ REQUIRED)
# - find_package(@PACKAGE_NAME@ COMPONENTS nothreads_static)
# - find_package(@PACKAGE_NAME@ COMPONENTS nothreads_shared)
# - find_package(@PACKAGE_NAME@ COMPONENTS static)
# - find_package(@PACKAGE_NAME@ COMPONENTS shared)
if (NOT DEFINED @PACKAGE_PREFIX@_SHARED)
if (DEFINED @PACKAGE_NAME@_SHARED)
set (@PACKAGE_PREFIX@_SHARED ${@PACKAGE_NAME@_SHARED})
elseif (TARGET @PACKAGE_NAME@-static OR TARGET @PACKAGE_NAME@_nothreads-static)
set (@PACKAGE_PREFIX@_SHARED FALSE)
else ()
elseif (@PACKAGE_NAME@_FIND_COMPONENT)
if (@PACKAGE_NAME@_FIND_COMPONENT MATCHES "shared")
set (@PACKAGE_PREFIX@_SHARED TRUE)
else ()
set (@PACKAGE_PREFIX@_SHARED FALSE)
endif ()
elseif (TARGET @PACKAGE_NAME@_shared OR TARGET @PACKAGE_NAME@_nothreads_shared)
set (@PACKAGE_PREFIX@_SHARED TRUE)
else ()
set (@PACKAGE_PREFIX@_SHARED FALSE)
endif ()
endif ()
if (NOT DEFINED @PACKAGE_PREFIX@_NOTHREADS)
if (DEFINED @PACKAGE_NAME@_NOTHREADS)
set (@PACKAGE_PREFIX@_NOTHREADS ${@PACKAGE_NAME@_NOTHREADS})
elseif (TARGET @PACKAGE_NAME@-static OR TARGET @PACKAGE_NAME@-shared)
elseif (@PACKAGE_NAME@_FIND_COMPONENT)
if (@PACKAGE_NAME@_FIND_COMPONENT MATCHES "nothreads")
set (@PACKAGE_PREFIX@_NOTHREADS TRUE)
else ()
set (@PACKAGE_PREFIX@_NOTHREADS FALSE)
endif ()
elseif (TARGET @PACKAGE_NAME@_static OR TARGET @PACKAGE_NAME@_shared)
set (@PACKAGE_PREFIX@_NOTHREADS FALSE)
else ()
set (@PACKAGE_PREFIX@_NOTHREADS TRUE)
......@@ -46,15 +77,15 @@ if (NOT @PACKAGE_PREFIX@_TARGET)
set (@PACKAGE_PREFIX@_TARGET ${@PACKAGE_NAME@_TARGET})
elseif (@PACKAGE_PREFIX@_SHARED)
if (@PACKAGE_PREFIX@_NOTHREADS)
set (@PACKAGE_PREFIX@_TARGET @PACKAGE_NAME@_nothreads-shared)
set (@PACKAGE_PREFIX@_TARGET @PACKAGE_NAME@_nothreads_shared)
else ()
set (@PACKAGE_PREFIX@_TARGET @PACKAGE_NAME@-shared)
set (@PACKAGE_PREFIX@_TARGET @PACKAGE_NAME@_shared)
endif ()
else ()
if (@PACKAGE_PREFIX@_NOTHREADS)
set (@PACKAGE_PREFIX@_TARGET @PACKAGE_NAME@_nothreads-static)
set (@PACKAGE_PREFIX@_TARGET @PACKAGE_NAME@_nothreads_static)
else ()
set (@PACKAGE_PREFIX@_TARGET @PACKAGE_NAME@-static)
set (@PACKAGE_PREFIX@_TARGET @PACKAGE_NAME@_static)
endif ()
endif ()
endif ()
......@@ -133,4 +164,5 @@ if (NOT "^@PACKAGE_NAME@$" STREQUAL "^@PACKAGE_PREFIX@$")
endif ()
# unset private variables
unset (@PACKAGE_NAME@_FIND_COMPONENT)
unset (_INSTALL_PREFIX)
......@@ -22,9 +22,9 @@ else ()
set (type static)
endif ()
if (BUILD_gflags_LIB)
link_libraries (gflags-${type})
link_libraries (gflags_${type})
else ()
link_libraries (gflags_nothreads-${type})
link_libraries (gflags_nothreads_${type})
endif ()
# ----------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment