Skip to content

Commit 186eaa7

Browse files
committed
pybind11 cmake target and Config
1 parent 8f6e31c commit 186eaa7

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-1
lines changed

CMakeLists.txt

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,41 @@ if (PYBIND11_TEST)
179179
add_subdirectory(tests)
180180
endif()
181181

182+
include(GNUInstallDirs)
183+
include(CMakePackageConfigHelpers)
184+
185+
# Build an interface library target:
186+
# Though any project using pybind11 will need to include python headers and
187+
# define c++11/14-ness, these are not included as interface includes or compile
188+
# defs because (1) that's really the domain of the upstream project to decide,
189+
# (2) we can get away with it since installation (headers) don't care about
190+
# python version or C++ standard, and (3) this will make the easily target relocatable.
191+
add_library(pb11 INTERFACE)
192+
set_target_properties(pb11 PROPERTIES EXPORT_NAME "pybind11")
193+
target_include_directories(pb11 INTERFACE $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
194+
if(APPLE)
195+
target_link_libraries(pb11 INTERFACE "-undefined dynamic_lookup")
196+
endif()
197+
182198
if (PYBIND11_INSTALL)
183-
include(GNUInstallDirs)
184199
install(FILES ${PYBIND11_HEADERS}
185200
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/pybind11)
201+
install(TARGETS pb11
202+
EXPORT "${PROJECT_NAME}Targets")
203+
204+
# explicit "share" not "DATADIR" for CMake search path
205+
set(CMAKECONFIG_INSTALL_DIR "share/cmake/${PROJECT_NAME}")
206+
configure_package_config_file(tools/${PROJECT_NAME}Config.cmake.in
207+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
208+
INSTALL_DESTINATION ${CMAKECONFIG_INSTALL_DIR})
209+
write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
210+
VERSION 1.8
211+
#VERSION ${${PROJECT_NAME}_VERSION}
212+
COMPATIBILITY AnyNewerVersion)
213+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
214+
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
215+
DESTINATION ${CMAKECONFIG_INSTALL_DIR})
216+
install(EXPORT "${PROJECT_NAME}Targets"
217+
NAMESPACE "${PROJECT_NAME}::"
218+
DESTINATION ${CMAKECONFIG_INSTALL_DIR})
186219
endif()

tools/pybind11Config.cmake.in

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# pybind11Config.cmake
2+
# --------------------
3+
#
4+
# PYBIND11 cmake module.
5+
# This module sets the following variables in your project:
6+
#
7+
# ::
8+
#
9+
# pybind11_FOUND - true if pybind11 and all required components found on the system
10+
# pybind11_VERSION - pybind11 version in format Major.Minor.Release
11+
# pybind11_INCLUDE_DIRS - Directory where pybind11 headers are located.
12+
# pybind11_INCLUDE_DIR - same as DIRS
13+
#
14+
#
15+
# Available components: None
16+
#
17+
#
18+
# Exported targets:
19+
#
20+
# ::
21+
#
22+
# If pybind11 is found, this module defines the following :prop_tgt:`IMPORTED`
23+
# target. Note that python headers and C++ standard (11 or 14) of your choice and
24+
# detection will need to be applied to your code ::
25+
#
26+
# pybind11::pybind11 - the main pybind11 interface library (i.e., headers)
27+
#
28+
# find_package(PythonLibsNew REQUIRED)
29+
# find_package(pybind11 CONFIG REQUIRED)
30+
# message(STATUS "Found pybind11: ${pybind11_INCLUDE_DIR} (found version ${pybind11_VERSION})")
31+
# set_property(TARGET pybind11::pybind11 APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${PYTHON_INCLUDE_DIRS}
32+
# INTERFACE_COMPILE_OPTIONS "-std=c++11")
33+
#
34+
#
35+
# Suggested usage:
36+
#
37+
# ::
38+
#
39+
# find_package(pybind11)
40+
# find_package(pybind11 2.0 EXACT CONFIG REQUIRED)
41+
#
42+
#
43+
# The following variables can be set to guide the search for this package:
44+
#
45+
# ::
46+
#
47+
# pybind11_DIR - CMake variable, set to directory containing this Config file
48+
# CMAKE_PREFIX_PATH - CMake variable, set to root directory of this package
49+
# PATH - environment variable, set to bin directory of this package
50+
# CMAKE_DISABLE_FIND_PACKAGE_pybind11 - CMake variable, disables
51+
# find_package(pybind11) when not REQUIRED, perhaps to force internal build
52+
53+
@PACKAGE_INIT@
54+
55+
set(PN pybind11)
56+
57+
# find includes
58+
unset(_temp_h CACHE)
59+
find_path(_temp_h
60+
NAMES pybind11/pybind11.h
61+
PATHS ${PACKAGE_PREFIX_DIR}/@CMAKE_INSTALL_INCLUDEDIR@
62+
NO_DEFAULT_PATH)
63+
if(_temp_h)
64+
set(${PN}_INCLUDE_DIR "${_temp_h}")
65+
set(${PN}_INCLUDE_DIRS ${${PN}_INCLUDE_DIR})
66+
else()
67+
set(${PN}_FOUND 0)
68+
if(NOT CMAKE_REQUIRED_QUIET)
69+
message(STATUS "${PN}Config missing component: header (${PN}: ${_temp_h})")
70+
endif()
71+
endif()
72+
73+
check_required_components(${PN})
74+
75+
#-----------------------------------------------------------------------------
76+
# Don't include targets if this file is being picked up by another
77+
# project which has already built this as a subproject
78+
#-----------------------------------------------------------------------------
79+
if(NOT TARGET ${PN}::pybind11)
80+
include("${CMAKE_CURRENT_LIST_DIR}/${PN}Targets.cmake")
81+
endif()
82+

0 commit comments

Comments
 (0)