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
28 changes: 28 additions & 0 deletions rosbag2_py/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,31 @@ if(WIN32 AND CMAKE_BUILD_TYPE STREQUAL "Debug")
set(PYTHON_LIBRARIES "${PYTHON_DEBUG_LIBRARIES}")
endif()

function(clean_windows_flags target)
# Hack to avoid pybind11 issue.
#
# TODO(ivanpauno):
# This can be deleted when we update `pybind11_vendor` to a version including
# https://github.com/pybind/pybind11/pull/2590.
#
# They are enabling /LTCG on Windows to reduce binary size,
# but that doesn't play well with MSVC incremental linking (default for Debug/RelWithDebInfo).
#
# 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 +74,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 +83,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 +92,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