Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround pybind11 bug on Windows when CMAKE_BUILD_TYPE=RelWithDebInfo #538

Merged
merged 11 commits into from
Oct 14, 2020
27 changes: 27 additions & 0 deletions rosbag2_py/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ if(WIN32 AND CMAKE_BUILD_TYPE STREQUAL "Debug")
set(PYTHON_LIBRARIES "${PYTHON_DEBUG_LIBRARIES}")
endif()

function(clean_windows_flags target)
# More hacks to avoid pybind11 issues, similar to (but not the same)
ivanpauno marked this conversation as resolved.
Show resolved Hide resolved
# https://github.com/pybind/pybind11/issues/77.
# They are enabling /LTCG on Windows to reduce binary size,
# but that doesn't play well with MSVC incremental linking (default for Debug/RelWithDebInfo).
#
# Line of the bug (those options shouldn't be set when CONFIG is RELWITHDEBINFO).
# https://github.com/pybind/pybind11/blob/3b1dbebabc801c9cf6f0953a4c20b904d444f879/tools/pybind11Tools.cmake#L103-L108
#
# See:
# - https://docs.microsoft.com/en-us/cpp/build/reference/incremental-link-incrementally?view=vs-2019
# - https://docs.microsoft.com/en-us/cpp/build/reference/ltcg-link-time-code-generation?view=vs-2019

if(MSVC AND "${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")
get_target_property(target_link_libraries ${target} LINK_LIBRARIES)
list(REMOVE_ITEM target_link_libraries "$<$<NOT:$<CONFIG:Debug>>:-LTCG>")
set_target_properties(${target} PROPERTIES LINK_LIBRARIES "${target_link_libraries}")

get_target_property(target_compile_options ${target} COMPILE_OPTIONS)
list(REMOVE_ITEM target_compile_options "$<$<NOT:$<CONFIG:Debug>>:/GL>")
set_target_properties(${target} PROPERTIES COMPILE_OPTIONS "${target_compile_options}")
endif()
endfunction()

ament_python_install_package(${PROJECT_NAME})

pybind11_add_module(_reader SHARED
Expand All @@ -49,6 +73,7 @@ ament_target_dependencies(_reader PUBLIC
"rosbag2_compression"
"rosbag2_cpp"
)
clean_windows_flags(_reader)

pybind11_add_module(_storage SHARED
src/rosbag2_py/_storage.cpp
Expand All @@ -57,6 +82,7 @@ ament_target_dependencies(_storage PUBLIC
"rosbag2_cpp"
"rosbag2_storage"
)
clean_windows_flags(_storage)

pybind11_add_module(_writer SHARED
src/rosbag2_py/_writer.cpp
Expand All @@ -65,6 +91,7 @@ ament_target_dependencies(_writer PUBLIC
"rosbag2_compression"
"rosbag2_cpp"
)
clean_windows_flags(_writer)

# Install cython modules as sub-modules of the project
install(
Expand Down