2015-07-05 18:05:44 +00:00
|
|
|
# CMakeLists.txt -- Build system for the pybind11 examples
|
|
|
|
#
|
|
|
|
# Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
|
|
|
|
#
|
|
|
|
# All rights reserved. Use of this source code is governed by a
|
|
|
|
# BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
cmake_minimum_required(VERSION 2.8)
|
|
|
|
|
2015-10-15 16:13:33 +00:00
|
|
|
project(pybind11)
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2015-11-24 20:33:18 +00:00
|
|
|
option(PYBIND11_INSTALL "Install pybind11 header files?" ON)
|
|
|
|
|
2015-10-12 21:57:20 +00:00
|
|
|
# Add a CMake parameter for choosing a desired Python version
|
2015-10-18 14:48:30 +00:00
|
|
|
set(PYBIND11_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling the example application")
|
2015-10-12 21:57:20 +00:00
|
|
|
|
2016-01-17 21:36:34 +00:00
|
|
|
include(CheckCXXCompilerFlag)
|
|
|
|
|
2015-10-12 21:57:20 +00:00
|
|
|
# Set a default build configuration if none is specified. 'MinSizeRel' produces the smallest binaries
|
2015-07-05 18:05:44 +00:00
|
|
|
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
|
|
|
|
message(STATUS "Setting build type to 'MinSizeRel' as none was specified.")
|
|
|
|
set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE)
|
|
|
|
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
|
|
|
|
"MinSizeRel" "RelWithDebInfo")
|
|
|
|
endif()
|
2015-10-12 21:57:20 +00:00
|
|
|
string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
|
2015-10-11 14:29:35 +00:00
|
|
|
|
2016-01-17 21:36:34 +00:00
|
|
|
set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6 3.7)
|
|
|
|
if (NOT ${PYBIND11_PYTHON_VERSION} STREQUAL "")
|
|
|
|
find_package(PythonLibs ${PYBIND11_PYTHON_VERSION} EXACT)
|
|
|
|
if (NOT PythonLibs_FOUND)
|
|
|
|
find_package(PythonLibs ${PYBIND11_PYTHON_VERSION} REQUIRED)
|
|
|
|
endif()
|
|
|
|
else()
|
|
|
|
find_package(PythonLibs REQUIRED)
|
|
|
|
endif()
|
2016-02-07 14:45:56 +00:00
|
|
|
# The above sometimes returns version numbers like "3.4.3+"; the "+" must be removed for the next line to work
|
|
|
|
string(REPLACE "+" "" PYTHONLIBS_VERSION_STRING "+${PYTHONLIBS_VERSION_STRING}")
|
2016-01-17 21:36:34 +00:00
|
|
|
find_package(PythonInterp ${PYTHONLIBS_VERSION_STRING} EXACT REQUIRED)
|
2015-11-30 09:17:01 +00:00
|
|
|
|
2016-02-18 18:06:43 +00:00
|
|
|
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Intel")
|
2016-01-17 21:36:36 +00:00
|
|
|
CHECK_CXX_COMPILER_FLAG("-std=c++14" HAS_CPP14_FLAG)
|
|
|
|
CHECK_CXX_COMPILER_FLAG("-std=c++11" HAS_CPP11_FLAG)
|
|
|
|
|
|
|
|
if (HAS_CPP14_FLAG)
|
|
|
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
|
|
|
|
elseif (HAS_CPP11_FLAG)
|
|
|
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
|
|
|
else()
|
|
|
|
message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!")
|
|
|
|
endif()
|
2015-10-12 21:57:20 +00:00
|
|
|
|
|
|
|
# Enable link time optimization and set the default symbol
|
|
|
|
# visibility to hidden (very important to obtain small binaries)
|
2015-07-05 18:05:44 +00:00
|
|
|
if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
|
2015-11-30 09:17:01 +00:00
|
|
|
# Default symbol visibility
|
|
|
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
|
|
|
|
|
|
|
|
# Check for Link Time Optimization support
|
2016-02-18 18:06:43 +00:00
|
|
|
# (GCC/Clang)
|
2015-11-30 09:17:01 +00:00
|
|
|
CHECK_CXX_COMPILER_FLAG("-flto" HAS_LTO_FLAG)
|
|
|
|
if (HAS_LTO_FLAG)
|
|
|
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto")
|
|
|
|
endif()
|
2016-02-18 18:06:43 +00:00
|
|
|
|
|
|
|
# Intel equivalent to LTO is called IPO
|
2016-02-19 12:27:20 +00:00
|
|
|
if (CMAKE_CXX_COMPILER_ID MATCHES "Intel")
|
|
|
|
CHECK_CXX_COMPILER_FLAG("-ipo" HAS_IPO_FLAG)
|
|
|
|
if (HAS_IPO_FLAG)
|
|
|
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ipo")
|
|
|
|
endif()
|
2016-02-18 18:06:43 +00:00
|
|
|
endif()
|
2015-07-05 18:05:44 +00:00
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# Compile with compiler warnings turned on
|
|
|
|
if(MSVC)
|
|
|
|
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
|
|
|
|
string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
|
|
|
|
else()
|
|
|
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
|
|
|
|
endif()
|
2015-11-28 13:24:44 +00:00
|
|
|
elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
|
2015-07-05 18:05:44 +00:00
|
|
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
|
|
|
|
endif()
|
|
|
|
|
2015-10-12 21:57:20 +00:00
|
|
|
# Include path for pybind11 header files
|
|
|
|
include_directories(include)
|
|
|
|
|
2016-04-30 20:59:58 +00:00
|
|
|
# Include path for Python header files
|
|
|
|
include_directories(${PYTHON_INCLUDE_DIR})
|
|
|
|
|
2015-11-24 20:20:56 +00:00
|
|
|
set(PYBIND11_HEADERS
|
2016-01-17 21:36:44 +00:00
|
|
|
include/pybind11/attr.h
|
2015-10-15 16:13:33 +00:00
|
|
|
include/pybind11/cast.h
|
|
|
|
include/pybind11/common.h
|
2016-01-17 21:36:36 +00:00
|
|
|
include/pybind11/complex.h
|
|
|
|
include/pybind11/descr.h
|
|
|
|
include/pybind11/functional.h
|
|
|
|
include/pybind11/numpy.h
|
2015-10-15 16:13:33 +00:00
|
|
|
include/pybind11/operators.h
|
|
|
|
include/pybind11/pybind11.h
|
|
|
|
include/pybind11/pytypes.h
|
2015-12-15 10:32:29 +00:00
|
|
|
include/pybind11/stl.h
|
2016-01-17 21:36:36 +00:00
|
|
|
include/pybind11/typeid.h
|
2015-11-24 20:20:56 +00:00
|
|
|
)
|
|
|
|
|
2016-01-20 08:24:25 +00:00
|
|
|
set(PYBIND11_EXAMPLES
|
2015-07-05 18:05:44 +00:00
|
|
|
example/example1.cpp
|
|
|
|
example/example2.cpp
|
|
|
|
example/example3.cpp
|
|
|
|
example/example4.cpp
|
|
|
|
example/example5.cpp
|
|
|
|
example/example6.cpp
|
|
|
|
example/example7.cpp
|
|
|
|
example/example8.cpp
|
|
|
|
example/example9.cpp
|
2015-07-26 14:33:49 +00:00
|
|
|
example/example10.cpp
|
2015-07-29 15:51:54 +00:00
|
|
|
example/example11.cpp
|
2015-10-01 14:46:03 +00:00
|
|
|
example/example12.cpp
|
2016-01-17 21:36:39 +00:00
|
|
|
example/example13.cpp
|
2016-03-15 14:05:40 +00:00
|
|
|
example/example14.cpp
|
2016-04-13 00:37:36 +00:00
|
|
|
example/example15.cpp
|
2016-04-13 11:45:09 +00:00
|
|
|
example/example16.cpp
|
2016-05-07 04:26:19 +00:00
|
|
|
example/example17.cpp
|
2016-03-10 12:24:10 +00:00
|
|
|
example/issues.cpp
|
2015-07-05 18:05:44 +00:00
|
|
|
)
|
2016-01-20 08:24:25 +00:00
|
|
|
|
|
|
|
# Create the binding library
|
|
|
|
add_library(example SHARED
|
|
|
|
${PYBIND11_HEADERS}
|
|
|
|
example/example.cpp
|
|
|
|
${PYBIND11_EXAMPLES}
|
|
|
|
)
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2015-10-12 21:57:20 +00:00
|
|
|
# Don't add a 'lib' prefix to the shared library
|
2015-07-05 18:05:44 +00:00
|
|
|
set_target_properties(example PROPERTIES PREFIX "")
|
2015-10-12 21:57:20 +00:00
|
|
|
|
2015-10-13 21:58:10 +00:00
|
|
|
# Always write the output file directly into the 'example' directory (even on MSVC)
|
|
|
|
set(CompilerFlags
|
|
|
|
LIBRARY_OUTPUT_DIRECTORY LIBRARY_OUTPUT_DIRECTORY_RELEASE LIBRARY_OUTPUT_DIRECTORY_DEBUG
|
|
|
|
LIBRARY_OUTPUT_DIRECTORY_MINSIZEREL LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO
|
|
|
|
RUNTIME_OUTPUT_DIRECTORY RUNTIME_OUTPUT_DIRECTORY_RELEASE RUNTIME_OUTPUT_DIRECTORY_DEBUG
|
|
|
|
RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO)
|
|
|
|
|
|
|
|
foreach(CompilerFlag ${CompilerFlags})
|
|
|
|
set_target_properties(example PROPERTIES ${CompilerFlag} ${PROJECT_SOURCE_DIR}/example)
|
|
|
|
endforeach()
|
2015-07-05 18:05:44 +00:00
|
|
|
|
|
|
|
if (WIN32)
|
|
|
|
if (MSVC)
|
2016-05-01 10:56:09 +00:00
|
|
|
# /MP enables multithreaded builds (relevant when there are many files), /bigobj is
|
|
|
|
# needed for bigger binding projects due to the limit to 64k addressable sections
|
|
|
|
set_property(TARGET example APPEND PROPERTY COMPILE_OPTIONS /MP /bigobj)
|
|
|
|
# Enforce size-based optimization and link time code generation on MSVC
|
|
|
|
# (~30% smaller binaries in experiments); do nothing in debug mode.
|
|
|
|
set_property(TARGET example APPEND PROPERTY COMPILE_OPTIONS
|
|
|
|
"$<$<CONFIG:Release>:/Os>" "$<$<CONFIG:Release>:/GL>"
|
|
|
|
"$<$<CONFIG:MinSizeRel>:/Os>" "$<$<CONFIG:MinSizeRel>:/GL>"
|
|
|
|
"$<$<CONFIG:RelWithDebInfo>:/Os>" "$<$<CONFIG:RelWithDebInfo>:/GL>"
|
|
|
|
)
|
|
|
|
set_property(TARGET example APPEND_STRING PROPERTY LINK_FLAGS_RELEASE "/LTCG ")
|
|
|
|
set_property(TARGET example APPEND_STRING PROPERTY LINK_FLAGS_MINSIZEREL "/LTCG ")
|
|
|
|
set_property(TARGET example APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO "/LTCG ")
|
2015-07-05 18:05:44 +00:00
|
|
|
endif()
|
|
|
|
|
|
|
|
# .PYD file extension on Windows
|
|
|
|
set_target_properties(example PROPERTIES SUFFIX ".pyd")
|
|
|
|
|
|
|
|
# Link against the Python shared library
|
|
|
|
target_link_libraries(example ${PYTHON_LIBRARY})
|
|
|
|
elseif (UNIX)
|
|
|
|
# It's quite common to have multiple copies of the same Python version
|
|
|
|
# installed on one's system. E.g.: one copy from the OS and another copy
|
|
|
|
# that's statically linked into an application like Blender or Maya.
|
|
|
|
# If we link our plugin library against the OS Python here and import it
|
|
|
|
# into Blender or Maya later on, this will cause segfaults when multiple
|
2016-01-17 21:36:36 +00:00
|
|
|
# conflicting Python instances are active at the same time (even when they
|
|
|
|
# are of the same version).
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2016-01-21 18:17:58 +00:00
|
|
|
# Windows is not affected by this issue since it handles DLL imports
|
2015-10-12 21:57:20 +00:00
|
|
|
# differently. The solution for Linux and Mac OS is simple: we just don't
|
|
|
|
# link against the Python library. The resulting shared library will have
|
|
|
|
# missing symbols, but that's perfectly fine -- they will be resolved at
|
|
|
|
# import time.
|
2015-07-05 18:05:44 +00:00
|
|
|
|
|
|
|
# .SO file extension on Linux/Mac OS
|
|
|
|
set_target_properties(example PROPERTIES SUFFIX ".so")
|
|
|
|
|
2015-10-15 20:41:25 +00:00
|
|
|
# Optimize for a small binary size
|
|
|
|
if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
|
|
|
|
set_target_properties(example PROPERTIES COMPILE_FLAGS "-Os")
|
|
|
|
endif()
|
|
|
|
|
2015-07-05 18:05:44 +00:00
|
|
|
# Strip unnecessary sections of the binary on Linux/Mac OS
|
|
|
|
if(APPLE)
|
|
|
|
set_target_properties(example PROPERTIES MACOSX_RPATH ".")
|
2016-01-17 21:36:44 +00:00
|
|
|
set_target_properties(example PROPERTIES LINK_FLAGS "-undefined dynamic_lookup ")
|
2015-07-05 18:05:44 +00:00
|
|
|
if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
|
|
|
|
add_custom_command(TARGET example POST_BUILD COMMAND strip -u -r ${PROJECT_SOURCE_DIR}/example/example.so)
|
|
|
|
endif()
|
|
|
|
else()
|
|
|
|
if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
|
|
|
|
add_custom_command(TARGET example POST_BUILD COMMAND strip ${PROJECT_SOURCE_DIR}/example/example.so)
|
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
|
|
|
|
enable_testing()
|
2016-02-20 19:53:10 +00:00
|
|
|
|
2015-07-05 18:05:44 +00:00
|
|
|
set(RUN_TEST ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/example/run_test.py)
|
2016-02-20 19:53:10 +00:00
|
|
|
if (MSVC OR CMAKE_CXX_COMPILER_ID MATCHES "Intel")
|
|
|
|
set(RUN_TEST ${RUN_TEST} --relaxed)
|
|
|
|
endif()
|
|
|
|
|
2016-03-10 12:24:10 +00:00
|
|
|
foreach(VALUE ${PYBIND11_EXAMPLES})
|
|
|
|
string(REGEX REPLACE "^example/(.+).cpp$" "\\1" EXAMPLE_NAME "${VALUE}")
|
|
|
|
add_test(NAME ${EXAMPLE_NAME} COMMAND ${RUN_TEST} ${EXAMPLE_NAME})
|
2015-07-05 18:05:44 +00:00
|
|
|
endforeach()
|
2015-11-24 20:20:56 +00:00
|
|
|
|
2015-11-24 20:33:18 +00:00
|
|
|
if (PYBIND11_INSTALL)
|
2015-11-24 20:20:56 +00:00
|
|
|
install(FILES ${PYBIND11_HEADERS} DESTINATION include/pybind11)
|
|
|
|
endif()
|