Skip to content

Commit 9b8bc85

Browse files
committed
separate main CMakeLists.txt into Tools file also available upon installation
1 parent ae6ed96 commit 9b8bc85

File tree

2 files changed

+141
-107
lines changed

2 files changed

+141
-107
lines changed

CMakeLists.txt

Lines changed: 1 addition & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,9 @@ option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJE
1919
option(PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT})
2020
option(PYBIND11_WERROR "Report all warnings as errors" OFF)
2121

22-
# Add a CMake parameter for choosing a desired Python version
23-
set(PYBIND11_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling modules")
24-
2522
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/tools")
26-
set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6 3.7)
27-
find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} REQUIRED)
28-
29-
include(CheckCXXCompilerFlag)
3023

31-
if(NOT MSVC AND NOT PYBIND11_CPP_STANDARD)
32-
check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG)
33-
check_cxx_compiler_flag("-std=c++11" HAS_CPP11_FLAG)
34-
35-
if (HAS_CPP14_FLAG)
36-
set(PYBIND11_CPP_STANDARD -std=c++14)
37-
elseif (HAS_CPP11_FLAG)
38-
set(PYBIND11_CPP_STANDARD -std=c++11)
39-
else()
40-
message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!")
41-
endif()
42-
43-
set(PYBIND11_CPP_STANDARD ${PYBIND11_CPP_STANDARD} CACHE STRING
44-
"C++ standard flag, e.g. -std=c++11 or -std=c++14. Defaults to latest available." FORCE)
45-
endif()
24+
include(pybind11Tools)
4625

4726
# Cache variables so pybind11_add_module can be used in parent projects
4827
set(PYBIND11_INCLUDE_DIR "${CMAKE_CURRENT_LIST_DIR}/include" CACHE INTERNAL "")
@@ -51,91 +30,6 @@ set(PYTHON_LIBRARIES ${PYTHON_LIBRARIES} CACHE INTERNAL "")
5130
set(PYTHON_MODULE_PREFIX ${PYTHON_MODULE_PREFIX} CACHE INTERNAL "")
5231
set(PYTHON_MODULE_EXTENSION ${PYTHON_MODULE_EXTENSION} CACHE INTERNAL "")
5332

54-
# Build a Python extension module:
55-
# pybind11_add_module(<name> source1 [source2 ...])
56-
#
57-
function(pybind11_add_module target_name)
58-
add_library(${target_name} MODULE ${ARGN})
59-
target_include_directories(${target_name}
60-
PRIVATE ${PYBIND11_INCLUDE_DIR}
61-
PRIVATE ${PYTHON_INCLUDE_DIRS})
62-
63-
# The prefix and extension are provided by FindPythonLibsNew.cmake
64-
set_target_properties(${target_name} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}")
65-
set_target_properties(${target_name} PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}")
66-
67-
if(WIN32 OR CYGWIN)
68-
# Link against the Python shared library on Windows
69-
target_link_libraries(${target_name} PRIVATE ${PYTHON_LIBRARIES})
70-
elseif(APPLE)
71-
# It's quite common to have multiple copies of the same Python version
72-
# installed on one's system. E.g.: one copy from the OS and another copy
73-
# that's statically linked into an application like Blender or Maya.
74-
# If we link our plugin library against the OS Python here and import it
75-
# into Blender or Maya later on, this will cause segfaults when multiple
76-
# conflicting Python instances are active at the same time (even when they
77-
# are of the same version).
78-
79-
# Windows is not affected by this issue since it handles DLL imports
80-
# differently. The solution for Linux and Mac OS is simple: we just don't
81-
# link against the Python library. The resulting shared library will have
82-
# missing symbols, but that's perfectly fine -- they will be resolved at
83-
# import time.
84-
85-
target_link_libraries(${target_name} PRIVATE "-undefined dynamic_lookup")
86-
endif()
87-
88-
if(NOT MSVC)
89-
# Make sure C++11/14 are enabled
90-
target_compile_options(${target_name} PUBLIC ${PYBIND11_CPP_STANDARD})
91-
92-
# Enable link time optimization and set the default symbol
93-
# visibility to hidden (very important to obtain small binaries)
94-
string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
95-
if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
96-
# Check for Link Time Optimization support (GCC/Clang)
97-
check_cxx_compiler_flag("-flto" HAS_LTO_FLAG)
98-
if(HAS_LTO_FLAG AND NOT CYGWIN)
99-
target_compile_options(${target_name} PRIVATE -flto)
100-
endif()
101-
102-
# Intel equivalent to LTO is called IPO
103-
if(CMAKE_CXX_COMPILER_ID MATCHES "Intel")
104-
check_cxx_compiler_flag("-ipo" HAS_IPO_FLAG)
105-
if(HAS_IPO_FLAG)
106-
target_compile_options(${target_name} PRIVATE -ipo)
107-
endif()
108-
endif()
109-
110-
# Default symbol visibility
111-
target_compile_options(${target_name} PRIVATE "-fvisibility=hidden")
112-
113-
# Strip unnecessary sections of the binary on Linux/Mac OS
114-
if(CMAKE_STRIP)
115-
if(APPLE)
116-
add_custom_command(TARGET ${target_name} POST_BUILD
117-
COMMAND ${CMAKE_STRIP} -u -r $<TARGET_FILE:${target_name}>)
118-
else()
119-
add_custom_command(TARGET ${target_name} POST_BUILD
120-
COMMAND ${CMAKE_STRIP} $<TARGET_FILE:${target_name}>)
121-
endif()
122-
endif()
123-
endif()
124-
elseif(MSVC)
125-
# /MP enables multithreaded builds (relevant when there are many files), /bigobj is
126-
# needed for bigger binding projects due to the limit to 64k addressable sections
127-
target_compile_options(${target_name} PRIVATE /MP /bigobj)
128-
129-
# Enforce link time code generation on MSVC, except in debug mode
130-
target_compile_options(${target_name} PRIVATE $<$<NOT:$<CONFIG:Debug>>:/GL>)
131-
132-
# Fancy generator expressions don't work with linker flags, for reasons unknown
133-
set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE /LTCG)
134-
set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_MINSIZEREL /LTCG)
135-
set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO /LTCG)
136-
endif()
137-
endfunction()
138-
13933
# Compile with compiler warnings turned on
14034
function(pybind11_enable_warnings target_name)
14135
if(MSVC)

tools/pybind11Tools.cmake

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# tools/pybind11Tools.cmake -- Build system for the pybind11 modules
2+
#
3+
# Copyright (c) 2015 Wenzel Jakob <[email protected]>
4+
#
5+
# All rights reserved. Use of this source code is governed by a
6+
# BSD-style license that can be found in the LICENSE file.
7+
8+
cmake_minimum_required(VERSION 2.8.12)
9+
10+
# Add a CMake parameter for choosing a desired Python version
11+
set(PYBIND11_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling modules")
12+
13+
set(Python_ADDITIONAL_VERSIONS 3.7 3.6 3.5 3.4)
14+
find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} REQUIRED)
15+
16+
include(CheckCXXCompilerFlag)
17+
18+
function(select_cxx_standard)
19+
if(NOT MSVC AND NOT PYBIND11_CPP_STANDARD)
20+
check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG)
21+
check_cxx_compiler_flag("-std=c++11" HAS_CPP11_FLAG)
22+
23+
if (HAS_CPP14_FLAG)
24+
set(PYBIND11_CPP_STANDARD -std=c++14)
25+
elseif (HAS_CPP11_FLAG)
26+
set(PYBIND11_CPP_STANDARD -std=c++11)
27+
else()
28+
message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!")
29+
endif()
30+
31+
set(PYBIND11_CPP_STANDARD ${PYBIND11_CPP_STANDARD} CACHE STRING
32+
"C++ standard flag, e.g. -std=c++11 or -std=c++14. Defaults to latest available." FORCE)
33+
endif()
34+
endfunction()
35+
36+
# Build a Python extension module:
37+
# pybind11_add_module(<name> [MODULE | SHARED] [EXCLUDE_FROM_ALL] source1 [source2 ...])
38+
#
39+
function(pybind11_add_module target_name)
40+
set(lib_type "MODULE")
41+
set(do_lto True)
42+
set(exclude_from_all "")
43+
set(sources "")
44+
45+
set(_args_to_try "${ARGN}")
46+
foreach(_ex_arg IN LISTS _args_to_try)
47+
if(${_ex_arg} STREQUAL "MODULE")
48+
set(lib_type "MODULE")
49+
elseif(${_ex_arg} STREQUAL "SHARED")
50+
set(lib_type "SHARED")
51+
elseif(${_ex_arg} STREQUAL "EXCLUDE_FROM_ALL")
52+
set(exclude_from_all "EXCLUDE_FROM_ALL")
53+
else()
54+
list(APPEND sources "${_ex_arg}")
55+
endif()
56+
endforeach()
57+
58+
add_library(${target_name} ${lib_type} ${exclude_from_all} ${sources})
59+
60+
target_include_directories(${target_name}
61+
PRIVATE ${PYBIND11_INCLUDE_DIR} # from project CMakeLists.txt
62+
PRIVATE ${pybind11_INCLUDE_DIR} # from pybind11Config
63+
PRIVATE ${PYTHON_INCLUDE_DIRS})
64+
65+
# The prefix and extension are provided by FindPythonLibsNew.cmake
66+
set_target_properties(${target_name} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}")
67+
set_target_properties(${target_name} PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}")
68+
69+
if(WIN32 OR CYGWIN)
70+
# Link against the Python shared library on Windows
71+
target_link_libraries(${target_name} PRIVATE ${PYTHON_LIBRARIES})
72+
elseif(APPLE)
73+
# It's quite common to have multiple copies of the same Python version
74+
# installed on one's system. E.g.: one copy from the OS and another copy
75+
# that's statically linked into an application like Blender or Maya.
76+
# If we link our plugin library against the OS Python here and import it
77+
# into Blender or Maya later on, this will cause segfaults when multiple
78+
# conflicting Python instances are active at the same time (even when they
79+
# are of the same version).
80+
81+
# Windows is not affected by this issue since it handles DLL imports
82+
# differently. The solution for Linux and Mac OS is simple: we just don't
83+
# link against the Python library. The resulting shared library will have
84+
# missing symbols, but that's perfectly fine -- they will be resolved at
85+
# import time.
86+
87+
target_link_libraries(${target_name} PRIVATE "-undefined dynamic_lookup")
88+
endif()
89+
90+
select_cxx_standard()
91+
if(NOT MSVC)
92+
# Make sure C++11/14 are enabled
93+
target_compile_options(${target_name} PUBLIC ${PYBIND11_CPP_STANDARD})
94+
95+
# Enable link time optimization and set the default symbol
96+
# visibility to hidden (very important to obtain small binaries)
97+
string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
98+
if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
99+
# Check for Link Time Optimization support (GCC/Clang)
100+
check_cxx_compiler_flag("-flto" HAS_LTO_FLAG)
101+
if(HAS_LTO_FLAG AND NOT CYGWIN)
102+
target_compile_options(${target_name} PRIVATE -flto)
103+
endif()
104+
105+
# Intel equivalent to LTO is called IPO
106+
if(CMAKE_CXX_COMPILER_ID MATCHES "Intel")
107+
check_cxx_compiler_flag("-ipo" HAS_IPO_FLAG)
108+
if(HAS_IPO_FLAG)
109+
target_compile_options(${target_name} PRIVATE -ipo)
110+
endif()
111+
endif()
112+
113+
# Default symbol visibility
114+
target_compile_options(${target_name} PRIVATE "-fvisibility=hidden")
115+
116+
# Strip unnecessary sections of the binary on Linux/Mac OS
117+
if(CMAKE_STRIP)
118+
if(APPLE)
119+
add_custom_command(TARGET ${target_name} POST_BUILD
120+
COMMAND ${CMAKE_STRIP} -u -r $<TARGET_FILE:${target_name}>)
121+
else()
122+
add_custom_command(TARGET ${target_name} POST_BUILD
123+
COMMAND ${CMAKE_STRIP} $<TARGET_FILE:${target_name}>)
124+
endif()
125+
endif()
126+
endif()
127+
elseif(MSVC)
128+
# /MP enables multithreaded builds (relevant when there are many files), /bigobj is
129+
# needed for bigger binding projects due to the limit to 64k addressable sections
130+
target_compile_options(${target_name} PRIVATE /MP /bigobj)
131+
132+
# Enforce link time code generation on MSVC, except in debug mode
133+
target_compile_options(${target_name} PRIVATE $<$<NOT:$<CONFIG:Debug>>:/GL>)
134+
135+
# Fancy generator expressions don't work with linker flags, for reasons unknown
136+
set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE /LTCG)
137+
set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_MINSIZEREL /LTCG)
138+
set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO /LTCG)
139+
endif()
140+
endfunction()

0 commit comments

Comments
 (0)