Create a config-file and export library targets for downstream users

This commit is contained in:
Michael X. Grey 2018-11-14 18:28:21 +08:00
parent 85504b45f6
commit b65e29badc
4 changed files with 283 additions and 26 deletions

View File

@ -880,6 +880,65 @@ IF(NOT DEFINED LIB_POSTFIX)
SET(LIB_POSTFIX "")
ENDIF()
# Set installation variables
SET(INSTALL_INCDIR include)
SET(INSTALL_BINDIR bin)
IF(WIN32)
SET(INSTALL_LIBDIR bin)
SET(INSTALL_ARCHIVEDIR lib)
ELSE()
SET(INSTALL_LIBDIR lib${LIB_POSTFIX})
SET(INSTALL_ARCHIVEDIR lib${LIB_POSTFIX})
ENDIF()
# If the version is equal to or greater than 2.8.8, then we can use the
# CMakePackageConfigHelpers module, which we use for configuring the package
# config file. If we are not producing the package config file, then there is no
# point in exporting the targets.
IF(CMAKE_VERSION VERSION_EQUAL "2.8.8" OR
CMAKE_VERSION VERSION_GREATER "2.8.8")
# Tell the project that we will be exporting targets
SET(OSG_EXPORT_TARGETS TRUE CACHE BOOL "Install a config-file and export targets")
# Set the package namespace for exporting
SET(PKG_NAMESPACE osg${OPENSCENEGRAPH_MAJOR_VERSION})
IF(WIN32)
SET(INSTALL_CONFIGDIR cmake)
ELSE()
SET(INSTALL_CONFIGDIR lib${LIB_POSTFIX}/cmake)
ENDIF()
INCLUDE(CMakePackageConfigHelpers)
CONFIGURE_PACKAGE_CONFIG_FILE(
${PROJECT_SOURCE_DIR}/CMakeModules/OpenSceneGraph-config.cmake.in
configure/OpenSceneGraph-config.cmake
INSTALL_DESTINATION ${INSTALL_CONFIGDIR}
PATH_VARS INSTALL_INCDIR
)
WRITE_BASIC_PACKAGE_VERSION_FILE(
configure/OpenSceneGraph-config-version.cmake
VERSION ${OPENSCENEGRAPH_VERSION}
COMPATIBILITY SameMajorVersion
)
INSTALL(
FILES
${PROJECT_BINARY_DIR}/configure/OpenSceneGraph-config.cmake
${PROJECT_BINARY_DIR}/configure/OpenSceneGraph-config-version.cmake
DESTINATION ${INSTALL_CONFIGDIR}
COMPONENT libopenscenegraph-dev
)
ELSE()
# We don't support exporting creating config files or exporting targets without >=2.8.8
SET(OSG_EXPORT_TARGETS FALSE CACHE BOOL "Cannot install config files without CMake >= 2.8.8" FORCE)
ENDIF()
# Here we apparently do some funky stuff with making the bin/ and lib/
# folders which is probably needed to work around a very old CMake bug?

View File

@ -4,16 +4,6 @@
# ${LIB_NAME}
# ${TARGET_H}
SET(INSTALL_INCDIR include)
SET(INSTALL_BINDIR bin)
IF(WIN32)
SET(INSTALL_LIBDIR bin)
SET(INSTALL_ARCHIVEDIR lib)
ELSE()
SET(INSTALL_LIBDIR lib${LIB_POSTFIX})
SET(INSTALL_ARCHIVEDIR lib${LIB_POSTFIX})
ENDIF()
SET(HEADERS_GROUP "Header Files")
SOURCE_GROUP(
@ -25,12 +15,36 @@ IF(MSVC AND OSG_MSVC_VERSIONED_DLL)
HANDLE_MSVC_DLL()
ENDIF()
INSTALL(
TARGETS ${LIB_NAME}
RUNTIME DESTINATION ${INSTALL_BINDIR} COMPONENT libopenscenegraph
LIBRARY DESTINATION ${INSTALL_LIBDIR} COMPONENT libopenscenegraph
ARCHIVE DESTINATION ${INSTALL_ARCHIVEDIR} COMPONENT libopenscenegraph-dev
)
IF(OSG_EXPORT_TARGETS)
INSTALL(
TARGETS ${LIB_NAME}
EXPORT ${LIB_NAME}
RUNTIME DESTINATION ${INSTALL_BINDIR} COMPONENT libopenscenegraph
LIBRARY DESTINATION ${INSTALL_LIBDIR} COMPONENT libopenscenegraph
ARCHIVE DESTINATION ${INSTALL_ARCHIVEDIR} COMPONENT libopenscenegraph-dev
)
INSTALL(
EXPORT ${LIB_NAME}
NAMESPACE ${PKG_NAMESPACE}::
DESTINATION ${INSTALL_CONFIGDIR}
FILE ${LIB_NAME}-targets.cmake
COMPONENT libopenscenegraph-dev
)
ELSE()
INSTALL(
TARGETS ${LIB_NAME}
RUNTIME DESTINATION ${INSTALL_BINDIR} COMPONENT libopenscenegraph
LIBRARY DESTINATION ${INSTALL_LIBDIR} COMPONENT libopenscenegraph
ARCHIVE DESTINATION ${INSTALL_ARCHIVEDIR} COMPONENT libopenscenegraph-dev
)
ENDIF()
IF(MSVC AND DYNAMIC_OPENSCENEGRAPH)
GET_TARGET_PROPERTY(PREFIX ${LIB_NAME} PREFIX)

View File

@ -0,0 +1,162 @@
# - Config file for the OpenSceneGraph package.
#
# For finding and loading OpenSceneGraph from your project, type:
#
# find_package(OpenSceneGraph [version] [COMPONENTS comp])
#
# This creates the following variables:
#
# OpenSceneGraph_LIBRARIES : All the libraries of all the requested components
# OPENSCENEGRAPH_LIBRARIES
#
# OpenSceneGraph_INCLUDE_DIRS : All the include directories of all the requested components
# OPENSCENEGRAPH_INCLUDE_DIRS
#
# OpenSceneGraph_FOUND : Set to true if all required components can be found
# OPENSCENEGRAPH_FOUND
#
# OSG_LIBRARY : The library for the core OSG component
# OSG_LIBRARIES
#
# OSG_INCLUDE_DIR : The include directory for the core OSG component
#
# <COMP>_LIBRARY : This is created for each requested component <comp>
# <COMP>_LIBRARIES and refers to the component's library
#
# <COMP>_INCLUDE_DIR : This is created for each requested component <comp>
# and refers to the component's include directory
################################################################################
#
# This config-file will also import the following targets:
#
# @PKG_NAMESPACE@::osg (for the core OSG library)
# @PKG_NAMESPACE@::<comp> (for each requested component library)
#
# Use target_link_libraries(tgt [PUBLIC|PRIVATE] @PKG_NAMESPACE@::<comp>)
# for each component that you want to link to.
#
################################################################################
# =========================== DO NOT MODIFY THIS FILE DIRECTLY ===========================
# This file is auto-generated from OpenSceneGraph/CMakeModules/OpenSceneGraph-config.cmake
# ========================================================================================
# We depend on 2.8.8 for CMakePackageConfigHelpers. This config file is not
# guaranteed to work with lower versions of cmake.
cmake_minimum_required(VERSION 2.8.8)
# Shortcut if find_package(OpenSceneGraph ...) has been called before
if(OpenSceneGraph_CONFIG_INCLUDED)
# Check that each of the components requested have already been found.
set(OpenSceneGraph_all_components_found TRUE)
foreach(component ${OpenSceneGraph_FIND_COMPONENTS})
if(NOT TARGET @PKG_NAMESPACE@::${component})
set(OpenSceneGraph_all_components_found FALSE)
break()
endif()
endforeach()
# If this config file has been called before, and all the requested
# components are already found, then exit early.
if(OpenSceneGraph_all_components_found)
return()
endif()
endif()
set(OpenSceneGraph_CONFIG_INCLUDED TRUE)
@PACKAGE_INIT@
set(OpenSceneGraph_INCLUDE_DIRS @PACKAGE_INSTALL_INCDIR@)
set(OPENSCENEGRAPH_INCLUDE_DIRS ${OpenSceneGraph_INCLUDE_DIRS})
set(OpenSceneGraph_LIBRARIES)
foreach(component OpenThreads osg ${OpenSceneGraph_FIND_COMPONENTS})
if(TARGET @PKG_NAMESPACE@::${component})
# This component has already been found, so we'll skip it
continue()
endif()
if(NOT EXISTS ${CMAKE_CURRENT_LIST_DIR}/${component}-targets.cmake)
if(OpenSceneGraph_FIND_REQUIRED_${component} OR NOT OpenSceneGraph_FIND_QUIETLY)
message("Could not find [${component}] component of OpenSceneGraph")
endif()
continue()
endif()
include(${CMAKE_CURRENT_LIST_DIR}/${component}-targets.cmake)
string(TOUPPER ${component} UC_COMPONENT)
# Components all get installed to the same include directory
set(${UC_COMPONENT}_INCLUDE_DIR ${OpenSceneGraph_INCLUDE_DIRS})
if(WIN32)
# For Windows, we need to provide the consumer with the IMPLIB (.lib) file
set(osg_imported_lib_property IMPORTED_IMPLIB)
else()
# For UNIX, we need to provide the shared object (.so) file
set(osg_imported_lib_property IMPORTED_LOCATION)
endif()
# For each possible configuration, find the relevant file on disk
foreach(osg_configuration "" _DEBUG _RELEASE)
get_property(${UC_COMPONENT}_LIBRARY${osg_configuration}
TARGET @PKG_NAMESPACE@::${component}
PROPERTY ${osg_imported_lib_property}${osg_configuration}
)
set(${UC_COMPONENT}_LIBRARIES${osg_configuration} ${${UC_COMPONENT}_LIBRARY})
endforeach()
# If a default library configuration was not found, create it
if(NOT ${UC_COMPONENT}_LIBRARY)
if(${UC_COMPONENT}_LIBRARY_DEBUG AND ${UC_COMPONENT}_LIBRARY_RELEASE)
# We have a release and a debug library, so let's use both
set(${UC_COMPONENT}_LIBRARY
optimized ${${UC_COMPONENT}_LIBRARY_RELEASE}
debug ${${UC_COMPONENT}_LIBRARY_DEBUG})
elseif(${UC_COMPONENT}_LIBRARY_RELEASE)
# There is only a release library, so let's use that
set(${UC_COMPONENT}_LIBRARY optimized ${${UC_COMPONENT}_LIBRARY_RELEASE})
elseif(${UC_COMPONENT}_LIBRARY_DEBUG)
# There is only a debug library, so let's use that
set(${UC_COMPONENT}_LIBRARY debug ${${UC_COMPONENT}_LIBRARY_DEBUG})
else()
# This should never happen, or else something is severely broken
# with the configuration, so we'll quit early.
message(FATAL_ERROR
"Could not find either an optimized or a debug version of the "
"requested OSG component: [${component}]")
endif()
set(${UC_COMPONENT}_LIBRARIES ${${UC_COMPONENT}_LIBRARY})
endif()
list(APPEND OpenSceneGraph_LIBRARIES ${${UC_COMPONENT}_LIBRARIES})
endforeach()
set(OPENSCENEGRAPH_LIBRARIES ${OpenSceneGraph_LIBRARIES})
check_required_components(OpenSceneGraph)

View File

@ -148,18 +148,40 @@ ELSE()
${CMAKE_CURRENT_BINARY_DIR}
)
INSTALL(
TARGETS OpenThreads
ARCHIVE DESTINATION lib${LIB_POSTFIX} COMPONENT libopenthreads-dev
LIBRARY DESTINATION lib${LIB_POSTFIX} COMPONENT libopenthreads
RUNTIME DESTINATION bin COMPONENT libopenthreads
)
IF(OSG_EXPORT_TARGETS)
INSTALL(
TARGETS OpenThreads
EXPORT OpenThreads
ARCHIVE DESTINATION lib${LIB_POSTFIX} COMPONENT libopenthreads-dev
LIBRARY DESTINATION lib${LIB_POSTFIX} COMPONENT libopenthreads
RUNTIME DESTINATION bin COMPONENT libopenthreads
)
INSTALL(
EXPORT OpenThreads
NAMESPACE ${PKG_NAMESPACE}::
DESTINATION ${INSTALL_CONFIGDIR}
FILE OpenThreads-targets.cmake
COMPONENT libopenthreads-dev)
ELSE()
INSTALL(
TARGETS OpenThreads
ARCHIVE DESTINATION lib${LIB_POSTFIX} COMPONENT libopenthreads-dev
LIBRARY DESTINATION lib${LIB_POSTFIX} COMPONENT libopenthreads
RUNTIME DESTINATION bin COMPONENT libopenthreads
)
ENDIF()
IF(NOT OSG_COMPILE_FRAMEWORKS)
INSTALL(
FILES ${OpenThreads_PUBLIC_HEADERS}
DESTINATION include/OpenThreads
COMPONENT libopenthreads-dev
INSTALL(
FILES ${OpenThreads_PUBLIC_HEADERS}
DESTINATION ${INSTALL_INCDIR}/OpenThreads
COMPONENT libopenthreads-dev
)
ELSE()