mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-22 13:15:12 +00:00
Add LTO and strip to pybind11_add_module
This commit is contained in:
parent
24ddf4b3f1
commit
9fb50c56d0
@ -59,14 +59,14 @@ function(pybind11_add_module target_name)
|
||||
# missing symbols, but that's perfectly fine -- they will be resolved at
|
||||
# import time.
|
||||
if(MSVC)
|
||||
target_link_libraries(${target_name} ${PYTHON_LIBRARIES})
|
||||
target_link_libraries(${target_name} PRIVATE ${PYTHON_LIBRARIES})
|
||||
elseif(APPLE)
|
||||
# Make sure OS X does not have any issues with missing symbols
|
||||
target_link_libraries(${target_name} PRIVATE "-undefined dynamic_lookup")
|
||||
endif()
|
||||
|
||||
# Make sure C++11/14 are enabled
|
||||
if(NOT MSVC)
|
||||
# Make sure C++11/14 are enabled
|
||||
check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG)
|
||||
check_cxx_compiler_flag("-std=c++11" HAS_CPP11_FLAG)
|
||||
|
||||
@ -78,12 +78,59 @@ function(pybind11_add_module target_name)
|
||||
message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!")
|
||||
endif()
|
||||
|
||||
# Enable link time optimization and set the default symbol
|
||||
# visibility to hidden (very important to obtain small binaries)
|
||||
string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
|
||||
if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
|
||||
# Check for Link Time Optimization support (GCC/Clang)
|
||||
check_cxx_compiler_flag("-flto" HAS_LTO_FLAG)
|
||||
if(HAS_LTO_FLAG)
|
||||
target_compile_options(${target_name} PRIVATE -flto)
|
||||
endif()
|
||||
|
||||
# Intel equivalent to LTO is called IPO
|
||||
if(CMAKE_CXX_COMPILER_ID MATCHES "Intel")
|
||||
check_cxx_compiler_flag("-ipo" HAS_IPO_FLAG)
|
||||
if(HAS_IPO_FLAG)
|
||||
target_compile_options(${target_name} PRIVATE -ipo)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Default symbol visibility
|
||||
target_compile_options(${target_name} PRIVATE "-fvisibility=hidden")
|
||||
|
||||
# Strip unnecessary sections of the binary on Linux/Mac OS
|
||||
if(CMAKE_STRIP)
|
||||
if(APPLE)
|
||||
add_custom_command(TARGET ${target_name} POST_BUILD
|
||||
COMMAND ${CMAKE_STRIP} -u -r $<TARGET_FILE:${target_name}>)
|
||||
else()
|
||||
add_custom_command(TARGET ${target_name} POST_BUILD
|
||||
COMMAND ${CMAKE_STRIP} $<TARGET_FILE:${target_name}>)
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
elseif(MSVC)
|
||||
# /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
|
||||
target_compile_options(${target_name} PRIVATE /MP /bigobj)
|
||||
|
||||
# Enforce link time code generation on MSVC, except in debug mode
|
||||
target_compile_options(${target_name} PRIVATE $<$<NOT:$<CONFIG:Debug>>:/GL>)
|
||||
# Fancy generator expressions don't work with linker flags, for reasons unknown
|
||||
set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE /LTCG)
|
||||
set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_MINSIZEREL /LTCG)
|
||||
set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO /LTCG)
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
# Compile with compiler warnings turned on
|
||||
function(pybind11_turn_on_warnings target_name)
|
||||
if(MSVC)
|
||||
target_compile_options(${target_name} PRIVATE /W4)
|
||||
else()
|
||||
target_compile_options(${target_name} PRIVATE -Wall -Wextra)
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
if (PYBIND11_TEST)
|
||||
|
@ -1,5 +1,3 @@
|
||||
include(CheckCXXCompilerFlag)
|
||||
|
||||
# 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.")
|
||||
@ -7,39 +5,6 @@ if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
|
||||
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
|
||||
"MinSizeRel" "RelWithDebInfo")
|
||||
endif()
|
||||
string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
|
||||
|
||||
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Intel")
|
||||
# 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)
|
||||
# Check for Link Time Optimization support
|
||||
# (GCC/Clang)
|
||||
CHECK_CXX_COMPILER_FLAG("-flto" HAS_LTO_FLAG)
|
||||
if(HAS_LTO_FLAG)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto")
|
||||
endif()
|
||||
|
||||
# Intel equivalent to LTO is called IPO
|
||||
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()
|
||||
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()
|
||||
|
||||
set(PYBIND11_EXAMPLES
|
||||
example1.cpp
|
||||
@ -74,6 +39,8 @@ endif()
|
||||
|
||||
# Create the binding library
|
||||
pybind11_add_module(example example.cpp ${PYBIND11_EXAMPLES})
|
||||
pybind11_turn_on_warnings(example)
|
||||
|
||||
if(EIGEN3_FOUND)
|
||||
target_include_directories(example PRIVATE ${EIGEN3_INCLUDE_DIR})
|
||||
target_compile_definitions(example PRIVATE -DPYBIND11_TEST_EIGEN)
|
||||
@ -90,40 +57,6 @@ foreach(CompilerFlag ${CompilerFlags})
|
||||
set_target_properties(example PROPERTIES ${CompilerFlag} ${PROJECT_SOURCE_DIR}/example)
|
||||
endforeach()
|
||||
|
||||
if(WIN32)
|
||||
if(MSVC)
|
||||
# /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 ")
|
||||
endif()
|
||||
elseif(UNIX)
|
||||
# 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)
|
||||
if(NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
|
||||
add_custom_command(TARGET example POST_BUILD COMMAND strip -u -r $<TARGET_FILE:example>)
|
||||
endif()
|
||||
else()
|
||||
if(NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
|
||||
add_custom_command(TARGET example POST_BUILD COMMAND strip $<TARGET_FILE:example>)
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
set(RUN_TEST ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/run_test.py)
|
||||
if(MSVC OR CMAKE_CXX_COMPILER_ID MATCHES "Intel")
|
||||
set(RUN_TEST ${RUN_TEST} --relaxed)
|
||||
|
Loading…
Reference in New Issue
Block a user