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

Fix rosbag2_py bug when using libc++ #529

Merged
merged 18 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions rosbag2_py/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,21 @@ if(BUILD_TESTING)
if(WIN32 AND "${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
set(_PYTHON_EXECUTABLE "${PYTHON_EXECUTABLE_DEBUG}")
endif()
set(pythonpath "${CMAKE_CURRENT_BINARY_DIR}/rosbag2_py;${CMAKE_CURRENT_SOURCE_DIR}")
if(NOT WIN32)
string(REPLACE ";" ":" pythonpath "${pythonpath}")
ivanpauno marked this conversation as resolved.
Show resolved Hide resolved
set(other_environment_vars)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# Work around a rtti bug when using class_loader from a
# python extension built with libc++.
set(other_environment_vars "ROSBAG2_PY_TEST_WITH_RTLD_GLOBAL=True")
endif()
ament_add_pytest_test(test_sequential_reader_py "test/test_sequential_reader.py"
PYTHON_EXECUTABLE "${_PYTHON_EXECUTABLE}"
APPEND_ENV "PYTHONPATH=${pythonpath}"
APPEND_LIBRARY_DIRS "${_append_library_dirs}"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/resources"
APPEND_ENV
PYTHONPATH=${CMAKE_CURRENT_SOURCE_DIR}
${other_environment_vars}
)
ament_add_pytest_test(test_sequential_writer_py "test/test_sequential_writer.py"
PYTHON_EXECUTABLE "${_PYTHON_EXECUTABLE}"
APPEND_ENV "PYTHONPATH=${pythonpath}"
APPEND_LIBRARY_DIRS "${_append_library_dirs}"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/resources"
APPEND_ENV "PYTHONPATH=${CMAKE_CURRENT_SOURCE_DIR}" ${other_environment_vars}
)
endif()

Expand Down
5 changes: 2 additions & 3 deletions rosbag2_py/src/rosbag2_py/_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include <memory>
#include <string>
#include <vector>
Expand All @@ -27,6 +24,8 @@
#include "rosbag2_storage/storage_filter.hpp"
#include "rosbag2_storage/topic_metadata.hpp"

#include "./pybind11.hpp"

namespace rosbag2_py
{

Expand Down
5 changes: 2 additions & 3 deletions rosbag2_py/src/rosbag2_py/_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include <string>
#include <vector>

Expand All @@ -23,6 +20,8 @@
#include "rosbag2_storage/storage_filter.hpp"
#include "rosbag2_storage/topic_metadata.hpp"

#include "./pybind11.hpp"

PYBIND11_MODULE(_storage, m) {
m.doc() = "Python wrapper of the rosbag2 utilities API";

Expand Down
5 changes: 2 additions & 3 deletions rosbag2_py/src/rosbag2_py/_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include <memory>
#include <string>

Expand All @@ -27,6 +24,8 @@
#include "rosbag2_storage/storage_filter.hpp"
#include "rosbag2_storage/topic_metadata.hpp"

#include "./pybind11.hpp"

namespace rosbag2_py
{

Expand Down
30 changes: 30 additions & 0 deletions rosbag2_py/src/rosbag2_py/pybind11.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2020 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef ROSBAG2_PY__PYBIND11_HPP_
#define ROSBAG2_PY__PYBIND11_HPP_

// Ignore -Wunused-value for clang.
// Based on https://github.com/pybind/pybind11/issues/2225
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-value"
#endif
#include <pybind11/pybind11.h>
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
#include <pybind11/stl.h>

#endif // ROSBAG2_PY__PYBIND11_HPP_
14 changes: 12 additions & 2 deletions rosbag2_py/test/test_sequential_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
from pathlib import Path
import sys

from common import get_rosbag_options
from rcl_interfaces.msg import Log
from rclpy.serialization import deserialize_message
import rosbag2_py
from rosidl_runtime_py.utilities import get_message
from std_msgs.msg import String

if os.environ.get('ROSBAG2_PY_TEST_WITH_RTLD_GLOBAL', None) is not None:
# This is needed on Linux when compiling with clang/libc++.
# TL;DR This makes class_loader work when using a python extension compiled with libc++.
#
# For the fun RTTI ABI details, see https://whatofhow.wordpress.com/2015/03/17/odr-rtti-dso/.
sys.setdlopenflags(os.RTLD_GLOBAL | os.RTLD_LAZY)

from common import get_rosbag_options # noqa
import rosbag2_py # noqa


def test_sequential_reader():
bag_path = str(Path(__file__).parent.parent / 'resources' / 'talker')
Expand Down
15 changes: 13 additions & 2 deletions rosbag2_py/test/test_sequential_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from common import get_rosbag_options
import os
import sys

from rclpy.serialization import deserialize_message, serialize_message
import rosbag2_py
from rosidl_runtime_py.utilities import get_message
from std_msgs.msg import String

if os.environ.get('ROSBAG2_PY_TEST_WITH_RTLD_GLOBAL', None) is not None:
# This is needed on Linux when compiling with clang/libc++.
# TL;DR This makes class_loader work when using a python extension compiled with libc++.
#
# For the fun RTTI ABI details, see https://whatofhow.wordpress.com/2015/03/17/odr-rtti-dso/.
sys.setdlopenflags(os.RTLD_GLOBAL | os.RTLD_LAZY)

from common import get_rosbag_options # noqa
import rosbag2_py # noqa


def create_topic(writer, topic_name, topic_type, serialization_format='cdr'):
"""
Expand Down