mirror of
https://github.com/pybind/pybind11.git
synced 2025-02-22 08:29:23 +00:00
This should insure that both the `PythonLibs` and `PythonInterp` points to the same python version. Without this, the Python library and interpreter might not match version. For example, if both Python 2.7 and 3.4 is installed, `PythonLibs` will find /usr/lib/x86_64-linux-gnu/libpython3.4m.so while `PythonInterp` will find /usr/bin/python2.7 (at least on Ubuntu 14.04). When `PythonLibs` and `PythonInterp` don't point to the same Python version, the examples will all fail: ```bash $ cmake .. -- The C compiler identification is GNU 4.8.4 -- The CXX compiler identification is GNU 4.8.4 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Setting build type to 'MinSizeRel' as none was specified. -- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython3.4m.so (found suitable version "3.4.3", minimum required is "2.7") -- Found PythonInterp: /usr/bin/python2.7 (found suitable version "2.7.6", minimum required is "2.7") -- Performing Test HAS_LTO_FLAG -- Performing Test HAS_LTO_FLAG - Success -- Configuring done -- Generating done -- Build files have been written to: /home/nbigaouette/pybind11/build $ make test Running tests... Test project /home/nbigaouette/pybind11/build Start 1: example1 1/12 Test #1: example1 .........................***Failed 0.02 sec Start 2: example2 2/12 Test #2: example2 .........................***Failed 0.03 sec Start 3: example3 3/12 Test #3: example3 .........................***Failed 0.02 sec Start 4: example4 4/12 Test #4: example4 .........................***Failed 0.02 sec Start 5: example5 5/12 Test #5: example5 .........................***Failed 0.02 sec Start 6: example6 6/12 Test #6: example6 .........................***Failed 0.02 sec Start 7: example7 7/12 Test #7: example7 .........................***Failed 0.02 sec Start 8: example8 8/12 Test #8: example8 .........................***Failed 0.02 sec Start 9: example9 9/12 Test #9: example9 .........................***Failed 0.02 sec Start 10: example10 10/12 Test #10: example10 ........................***Failed 0.02 sec Start 11: example11 11/12 Test #11: example11 ........................***Failed 0.03 sec Start 12: example12 12/12 Test #12: example12 ........................***Failed 0.02 sec 0% tests passed, 12 tests failed out of 12 Total Test time (real) = 0.25 sec The following tests FAILED: 1 - example1 (Failed) 2 - example2 (Failed) 3 - example3 (Failed) 4 - example4 (Failed) 5 - example5 (Failed) 6 - example6 (Failed) 7 - example7 (Failed) 8 - example8 (Failed) 9 - example9 (Failed) 10 - example10 (Failed) 11 - example11 (Failed) 12 - example12 (Failed) Errors while running CTest make: *** [test] Error 8 ``` By adding the `EXACT` version to the `find_package()` calls, the version discrepancy is at least caught at the cmake call: ```bash $ cmake .. -- The C compiler identification is GNU 4.8.4 -- The CXX compiler identification is GNU 4.8.4 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Setting build type to 'MinSizeRel' as none was specified. CMake Error at /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:108 (message): Could NOT find PythonLibs: Found unsuitable version "3.4.3", but required is exact version "2.7" (found /usr/lib/x86_64-linux-gnu/libpython3.4m.so) Call Stack (most recent call first): /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:313 (_FPHSA_FAILURE_MESSAGE) /usr/share/cmake-2.8/Modules/FindPythonLibs.cmake:208 (FIND_PACKAGE_HANDLE_STANDARD_ARGS) CMakeLists.txt:27 (find_package) -- Configuring incomplete, errors occurred! See also "/home/nbigaouette/pybind11/build/CMakeFiles/CMakeOutput.log". ```
172 lines
6.3 KiB
CMake
172 lines
6.3 KiB
CMake
# 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)
|
|
|
|
project(pybind11)
|
|
|
|
option(PYBIND11_INSTALL "Install pybind11 header files?" ON)
|
|
|
|
# Add a CMake parameter for choosing a desired Python version
|
|
set(PYBIND11_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling the example application")
|
|
|
|
# Set a default build configuration if none is specified. 'MinSizeRel' produces the smallest binaries
|
|
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()
|
|
string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
|
|
|
|
set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6)
|
|
find_package(PythonLibs ${PYBIND11_PYTHON_VERSION} EXACT REQUIRED)
|
|
find_package(PythonInterp ${PYBIND11_PYTHON_VERSION} EXACT REQUIRED)
|
|
|
|
include(CheckCXXCompilerFlag)
|
|
|
|
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
|
|
# Enable C++11 mode on C++ / Clang
|
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
|
|
|
# Enable link time optimization and set the default symbol
|
|
# visibility to hidden (very important to obtain small binaries)
|
|
if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
|
|
# Default symbol visibility
|
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
|
|
|
|
# Check for Link Time Optimization support
|
|
CHECK_CXX_COMPILER_FLAG("-flto" HAS_LTO_FLAG)
|
|
if (HAS_LTO_FLAG)
|
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto")
|
|
endif()
|
|
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()
|
|
elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
|
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
|
|
endif()
|
|
|
|
# Include path for Python header files
|
|
include_directories(${PYTHON_INCLUDE_DIR})
|
|
|
|
# Include path for pybind11 header files
|
|
include_directories(include)
|
|
|
|
set(PYBIND11_HEADERS
|
|
include/pybind11/cast.h
|
|
include/pybind11/common.h
|
|
include/pybind11/operators.h
|
|
include/pybind11/pybind11.h
|
|
include/pybind11/pytypes.h
|
|
include/pybind11/typeid.h
|
|
include/pybind11/numpy.h
|
|
include/pybind11/complex.h
|
|
include/pybind11/stl.h
|
|
include/pybind11/functional.h
|
|
)
|
|
|
|
# Create the binding library
|
|
add_library(example SHARED
|
|
${PYBIND11_HEADERS}
|
|
example/example.cpp
|
|
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
|
|
example/example10.cpp
|
|
example/example11.cpp
|
|
example/example12.cpp
|
|
)
|
|
|
|
# Don't add a 'lib' prefix to the shared library
|
|
set_target_properties(example PROPERTIES PREFIX "")
|
|
|
|
# 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()
|
|
|
|
if (WIN32)
|
|
if (MSVC)
|
|
# Enforce size-based optimization and link time code generation
|
|
# on MSVC (~30% smaller binaries in experiments). /bigobj is needed
|
|
# for bigger binding projects due to the limit to 64k addressable sections
|
|
# /MP enables multithreaded builds (relevant when there are many files).
|
|
set_target_properties(example PROPERTIES COMPILE_FLAGS "/Os /GL /MP /bigobj")
|
|
set_target_properties(example PROPERTIES LINK_FLAGS "/LTCG")
|
|
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
|
|
# conflicting Python instances are active at the same time.
|
|
|
|
# Windows is not affected by this issue since it handles DLL imports
|
|
# 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.
|
|
|
|
# .SO file extension on Linux/Mac OS
|
|
set_target_properties(example PROPERTIES SUFFIX ".so")
|
|
|
|
# Optimize for a small binary size
|
|
if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
|
|
set_target_properties(example PROPERTIES COMPILE_FLAGS "-Os")
|
|
endif()
|
|
|
|
# Strip unnecessary sections of the binary on Linux/Mac OS
|
|
if(APPLE)
|
|
set_target_properties(example PROPERTIES MACOSX_RPATH ".")
|
|
set_target_properties(example PROPERTIES LINK_FLAGS "-undefined dynamic_lookup -dead_strip")
|
|
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()
|
|
set(RUN_TEST ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/example/run_test.py)
|
|
foreach(i RANGE 1 12)
|
|
add_test(NAME example${i} COMMAND ${RUN_TEST} example${i})
|
|
endforeach()
|
|
|
|
if (PYBIND11_INSTALL)
|
|
install(FILES ${PYBIND11_HEADERS} DESTINATION include/pybind11)
|
|
endif()
|