Skip to content

Commit 1503d2f

Browse files
committed
Merge pull request #207 from dean0x7d/cmake
Simplify CMake build using add_subdirectory
2 parents 2c76c69 + c3c27c4 commit 1503d2f

File tree

5 files changed

+392
-325
lines changed

5 files changed

+392
-325
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ addons:
99
sources:
1010
- ubuntu-toolchain-r-test
1111
- deadsnakes
12+
- kubuntu-backports # cmake 2.8.12
1213
packages:
1314
- g++-4.8
1415
- g++-4.8-multilib
@@ -17,6 +18,7 @@ addons:
1718
- python3.5-dev
1819
- python3.5-venv
1920
- python3.5-dev:i386
21+
- cmake
2022
matrix:
2123
include:
2224
- os: linux

CMakeLists.txt

Lines changed: 115 additions & 187 deletions
Original file line numberDiff line numberDiff line change
@@ -5,187 +5,62 @@
55
# All rights reserved. Use of this source code is governed by a
66
# BSD-style license that can be found in the LICENSE file.
77

8-
cmake_minimum_required(VERSION 2.8)
8+
cmake_minimum_required(VERSION 2.8.12)
99

1010
project(pybind11)
1111

12-
option(PYBIND11_INSTALL "Install pybind11 header files?" ON)
12+
# Check if pybind11 is being used directly or via add_subdirectory
13+
set(PYBIND11_MASTER_PROJECT OFF)
14+
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
15+
set(PYBIND11_MASTER_PROJECT ON)
16+
endif ()
17+
18+
option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT})
19+
option(PYBIND11_TEST "Build tests?" ${PYBIND11_MASTER_PROJECT})
1320

1421
# Add a CMake parameter for choosing a desired Python version
1522
set(PYBIND11_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling the example application")
1623

17-
include(CheckCXXCompilerFlag)
18-
19-
# Set a default build configuration if none is specified. 'MinSizeRel' produces the smallest binaries
20-
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
21-
message(STATUS "Setting build type to 'MinSizeRel' as none was specified.")
22-
set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE)
23-
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
24-
"MinSizeRel" "RelWithDebInfo")
25-
endif()
26-
string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
27-
24+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/tools")
2825
set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6 3.7)
29-
if (NOT ${PYBIND11_PYTHON_VERSION} STREQUAL "")
30-
find_package(PythonLibs ${PYBIND11_PYTHON_VERSION} EXACT)
31-
if (NOT PYTHONLIBS_FOUND)
32-
find_package(PythonLibs ${PYBIND11_PYTHON_VERSION} REQUIRED)
33-
endif()
34-
else()
35-
find_package(PythonLibs REQUIRED)
36-
endif()
37-
# The above sometimes returns version numbers like "3.4.3+"; the "+" must be removed for the next line to work
38-
string(REPLACE "+" "" PYTHONLIBS_VERSION_STRING "+${PYTHONLIBS_VERSION_STRING}")
39-
find_package(PythonInterp ${PYTHONLIBS_VERSION_STRING} EXACT REQUIRED)
26+
find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} REQUIRED)
27+
28+
include(CheckCXXCompilerFlag)
4029

41-
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Intel")
42-
CHECK_CXX_COMPILER_FLAG("-std=c++14" HAS_CPP14_FLAG)
43-
CHECK_CXX_COMPILER_FLAG("-std=c++11" HAS_CPP11_FLAG)
30+
if(NOT MSVC AND NOT PYBIND11_CPP_STANDARD)
31+
check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG)
32+
check_cxx_compiler_flag("-std=c++11" HAS_CPP11_FLAG)
4433

4534
if (HAS_CPP14_FLAG)
46-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
35+
set(PYBIND11_CPP_STANDARD -std=c++14)
4736
elseif (HAS_CPP11_FLAG)
48-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
37+
set(PYBIND11_CPP_STANDARD -std=c++11)
4938
else()
5039
message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!")
5140
endif()
5241

53-
# Enable link time optimization and set the default symbol
54-
# visibility to hidden (very important to obtain small binaries)
55-
if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
56-
# Default symbol visibility
57-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
58-
59-
# Check for Link Time Optimization support
60-
# (GCC/Clang)
61-
CHECK_CXX_COMPILER_FLAG("-flto" HAS_LTO_FLAG)
62-
if (HAS_LTO_FLAG)
63-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto")
64-
endif()
65-
66-
# Intel equivalent to LTO is called IPO
67-
if (CMAKE_CXX_COMPILER_ID MATCHES "Intel")
68-
CHECK_CXX_COMPILER_FLAG("-ipo" HAS_IPO_FLAG)
69-
if (HAS_IPO_FLAG)
70-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ipo")
71-
endif()
72-
endif()
73-
endif()
74-
endif()
75-
76-
# Compile with compiler warnings turned on
77-
if(MSVC)
78-
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
79-
string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
80-
else()
81-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
82-
endif()
83-
elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
84-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
42+
set(PYBIND11_CPP_STANDARD ${PYBIND11_CPP_STANDARD} CACHE STRING
43+
"C++ standard flag, e.g. -std=c++11 or -std=c++14. Defaults to latest available.")
8544
endif()
8645

46+
# Cache variables so pybind11_add_module can be used in parent projects
47+
set(PYBIND11_INCLUDE_DIR "${CMAKE_CURRENT_LIST_DIR}/include" CACHE INTERNAL "")
48+
set(PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS} CACHE INTERNAL "")
49+
set(PYTHON_LIBRARIES ${PYTHON_LIBRARIES} CACHE INTERNAL "")
50+
set(PYTHON_MODULE_PREFIX ${PYTHON_MODULE_PREFIX} CACHE INTERNAL "")
51+
set(PYTHON_MODULE_EXTENSION ${PYTHON_MODULE_EXTENSION} CACHE INTERNAL "")
8752

88-
# Check if Eigen is available
89-
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tools")
90-
find_package(Eigen3 QUIET)
91-
92-
# Include path for pybind11 header files
93-
include_directories(include)
94-
95-
# Include path for Python header files
96-
include_directories(${PYTHON_INCLUDE_DIR})
97-
98-
set(PYBIND11_HEADERS
99-
include/pybind11/attr.h
100-
include/pybind11/cast.h
101-
include/pybind11/common.h
102-
include/pybind11/complex.h
103-
include/pybind11/descr.h
104-
include/pybind11/eigen.h
105-
include/pybind11/functional.h
106-
include/pybind11/numpy.h
107-
include/pybind11/operators.h
108-
include/pybind11/pybind11.h
109-
include/pybind11/pytypes.h
110-
include/pybind11/stl.h
111-
include/pybind11/stl_bind.h
112-
include/pybind11/typeid.h
113-
)
114-
115-
set(PYBIND11_EXAMPLES
116-
example/example1.cpp
117-
example/example2.cpp
118-
example/example3.cpp
119-
example/example4.cpp
120-
example/example5.cpp
121-
example/example6.cpp
122-
example/example7.cpp
123-
example/example8.cpp
124-
example/example9.cpp
125-
example/example10.cpp
126-
example/example11.cpp
127-
example/example12.cpp
128-
example/example13.cpp
129-
example/example14.cpp
130-
example/example15.cpp
131-
example/example16.cpp
132-
example/example17.cpp
133-
example/issues.cpp
134-
)
135-
136-
if (EIGEN3_FOUND)
137-
include_directories(${EIGEN3_INCLUDE_DIR})
138-
list(APPEND PYBIND11_EXAMPLES example/eigen.cpp)
139-
add_definitions(-DPYBIND11_TEST_EIGEN)
140-
message(STATUS "Building Eigen testcase")
141-
else()
142-
message(STATUS "NOT Building Eigen testcase")
143-
endif()
144-
145-
# Create the binding library
146-
add_library(example SHARED
147-
${PYBIND11_HEADERS}
148-
example/example.cpp
149-
${PYBIND11_EXAMPLES}
150-
)
151-
152-
# Don't add a 'lib' prefix to the shared library
153-
set_target_properties(example PROPERTIES PREFIX "")
154-
155-
# Always write the output file directly into the 'example' directory (even on MSVC)
156-
set(CompilerFlags
157-
LIBRARY_OUTPUT_DIRECTORY LIBRARY_OUTPUT_DIRECTORY_RELEASE LIBRARY_OUTPUT_DIRECTORY_DEBUG
158-
LIBRARY_OUTPUT_DIRECTORY_MINSIZEREL LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO
159-
RUNTIME_OUTPUT_DIRECTORY RUNTIME_OUTPUT_DIRECTORY_RELEASE RUNTIME_OUTPUT_DIRECTORY_DEBUG
160-
RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO)
161-
162-
foreach(CompilerFlag ${CompilerFlags})
163-
set_target_properties(example PROPERTIES ${CompilerFlag} ${PROJECT_SOURCE_DIR}/example)
164-
endforeach()
165-
166-
if (WIN32)
167-
if (MSVC)
168-
# /MP enables multithreaded builds (relevant when there are many files), /bigobj is
169-
# needed for bigger binding projects due to the limit to 64k addressable sections
170-
set_property(TARGET example APPEND PROPERTY COMPILE_OPTIONS /MP /bigobj)
171-
# Enforce size-based optimization and link time code generation on MSVC
172-
# (~30% smaller binaries in experiments); do nothing in debug mode.
173-
set_property(TARGET example APPEND PROPERTY COMPILE_OPTIONS
174-
"$<$<CONFIG:Release>:/Os>" "$<$<CONFIG:Release>:/GL>"
175-
"$<$<CONFIG:MinSizeRel>:/Os>" "$<$<CONFIG:MinSizeRel>:/GL>"
176-
"$<$<CONFIG:RelWithDebInfo>:/Os>" "$<$<CONFIG:RelWithDebInfo>:/GL>"
177-
)
178-
set_property(TARGET example APPEND_STRING PROPERTY LINK_FLAGS_RELEASE "/LTCG ")
179-
set_property(TARGET example APPEND_STRING PROPERTY LINK_FLAGS_MINSIZEREL "/LTCG ")
180-
set_property(TARGET example APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO "/LTCG ")
181-
endif()
53+
# Build a Python extension module:
54+
# pybind11_add_module(<name> source1 [source2 ...])
55+
#
56+
function(pybind11_add_module target_name)
57+
add_library(${target_name} MODULE ${ARGN})
58+
target_include_directories(${target_name} PUBLIC ${PYBIND11_INCLUDE_DIR} ${PYTHON_INCLUDE_DIRS})
18259

183-
# .PYD file extension on Windows
184-
set_target_properties(example PROPERTIES SUFFIX ".pyd")
60+
# The prefix and extension are provided by FindPythonLibsNew.cmake
61+
set_target_properties(${target_name} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}")
62+
set_target_properties(${target_name} PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}")
18563

186-
# Link against the Python shared library
187-
target_link_libraries(example ${PYTHON_LIBRARY})
188-
elseif (UNIX)
18964
# It's quite common to have multiple copies of the same Python version
19065
# installed on one's system. E.g.: one copy from the OS and another copy
19166
# that's statically linked into an application like Blender or Maya.
@@ -199,41 +74,94 @@ elseif (UNIX)
19974
# link against the Python library. The resulting shared library will have
20075
# missing symbols, but that's perfectly fine -- they will be resolved at
20176
# import time.
202-
203-
# .SO file extension on Linux/Mac OS
204-
set_target_properties(example PROPERTIES SUFFIX ".so")
205-
206-
# Optimize for a small binary size
207-
if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
208-
set_target_properties(example PROPERTIES COMPILE_FLAGS "-Os")
77+
if(MSVC)
78+
target_link_libraries(${target_name} PRIVATE ${PYTHON_LIBRARIES})
79+
elseif(APPLE)
80+
# Make sure OS X does not have any issues with missing symbols
81+
target_link_libraries(${target_name} PRIVATE "-undefined dynamic_lookup")
20982
endif()
21083

211-
# Strip unnecessary sections of the binary on Linux/Mac OS
212-
if(APPLE)
213-
set_target_properties(example PROPERTIES MACOSX_RPATH ".")
214-
set_target_properties(example PROPERTIES LINK_FLAGS "-undefined dynamic_lookup ")
215-
if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
216-
add_custom_command(TARGET example POST_BUILD COMMAND strip -u -r ${PROJECT_SOURCE_DIR}/example/example.so)
217-
endif()
218-
else()
84+
if(NOT MSVC)
85+
# Make sure C++11/14 are enabled
86+
target_compile_options(${target_name} PUBLIC ${PYBIND11_CPP_STANDARD})
87+
88+
# Enable link time optimization and set the default symbol
89+
# visibility to hidden (very important to obtain small binaries)
90+
string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
21991
if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
220-
add_custom_command(TARGET example POST_BUILD COMMAND strip ${PROJECT_SOURCE_DIR}/example/example.so)
92+
# Check for Link Time Optimization support (GCC/Clang)
93+
check_cxx_compiler_flag("-flto" HAS_LTO_FLAG)
94+
if(HAS_LTO_FLAG)
95+
target_compile_options(${target_name} PRIVATE -flto)
96+
endif()
97+
98+
# Intel equivalent to LTO is called IPO
99+
if(CMAKE_CXX_COMPILER_ID MATCHES "Intel")
100+
check_cxx_compiler_flag("-ipo" HAS_IPO_FLAG)
101+
if(HAS_IPO_FLAG)
102+
target_compile_options(${target_name} PRIVATE -ipo)
103+
endif()
104+
endif()
105+
106+
# Default symbol visibility
107+
target_compile_options(${target_name} PRIVATE "-fvisibility=hidden")
108+
109+
# Strip unnecessary sections of the binary on Linux/Mac OS
110+
if(CMAKE_STRIP)
111+
if(APPLE)
112+
add_custom_command(TARGET ${target_name} POST_BUILD
113+
COMMAND ${CMAKE_STRIP} -u -r $<TARGET_FILE:${target_name}>)
114+
else()
115+
add_custom_command(TARGET ${target_name} POST_BUILD
116+
COMMAND ${CMAKE_STRIP} $<TARGET_FILE:${target_name}>)
117+
endif()
118+
endif()
221119
endif()
120+
elseif(MSVC)
121+
# /MP enables multithreaded builds (relevant when there are many files), /bigobj is
122+
# needed for bigger binding projects due to the limit to 64k addressable sections
123+
target_compile_options(${target_name} PRIVATE /MP /bigobj)
124+
125+
# Enforce link time code generation on MSVC, except in debug mode
126+
target_compile_options(${target_name} PRIVATE $<$<NOT:$<CONFIG:Debug>>:/GL>)
127+
# Fancy generator expressions don't work with linker flags, for reasons unknown
128+
set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE /LTCG)
129+
set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_MINSIZEREL /LTCG)
130+
set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO /LTCG)
222131
endif()
223-
endif()
132+
endfunction()
224133

225-
enable_testing()
134+
# Compile with compiler warnings turned on
135+
function(pybind11_turn_on_warnings target_name)
136+
if(MSVC)
137+
target_compile_options(${target_name} PRIVATE /W4)
138+
else()
139+
target_compile_options(${target_name} PRIVATE -Wall -Wextra)
140+
endif()
141+
endfunction()
226142

227-
set(RUN_TEST ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/example/run_test.py)
228-
if (MSVC OR CMAKE_CXX_COMPILER_ID MATCHES "Intel")
229-
set(RUN_TEST ${RUN_TEST} --relaxed)
143+
if (PYBIND11_TEST)
144+
enable_testing()
145+
add_subdirectory(example)
230146
endif()
231147

232-
foreach(VALUE ${PYBIND11_EXAMPLES})
233-
string(REGEX REPLACE "^example/(.+).cpp$" "\\1" EXAMPLE_NAME "${VALUE}")
234-
add_test(NAME ${EXAMPLE_NAME} COMMAND ${RUN_TEST} ${EXAMPLE_NAME})
235-
endforeach()
236-
237148
if (PYBIND11_INSTALL)
238-
install(FILES ${PYBIND11_HEADERS} DESTINATION include/pybind11)
149+
set(PYBIND11_HEADERS
150+
include/pybind11/attr.h
151+
include/pybind11/cast.h
152+
include/pybind11/common.h
153+
include/pybind11/complex.h
154+
include/pybind11/descr.h
155+
include/pybind11/eigen.h
156+
include/pybind11/functional.h
157+
include/pybind11/numpy.h
158+
include/pybind11/operators.h
159+
include/pybind11/pybind11.h
160+
include/pybind11/pytypes.h
161+
include/pybind11/stl.h
162+
include/pybind11/stl_bind.h
163+
include/pybind11/typeid.h
164+
)
165+
166+
install(FILES ${PYBIND11_HEADERS} DESTINATION include/pybind11)
239167
endif()

0 commit comments

Comments
 (0)