Skip to content

Commit

Permalink
Switch to using target_link_libraries. (#183)
Browse files Browse the repository at this point in the history
This lets us have finer control over how we link against libraries.

Signed-off-by: Chris Lalancette <[email protected]>
  • Loading branch information
clalancette authored Nov 27, 2023
1 parent 4759809 commit c7df827
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 23 deletions.
14 changes: 6 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ endif()

add_library(${PROJECT_NAME}
src/asserts.cpp
src/env.cpp
src/filesystem_helper.cpp
src/find_library.cpp
src/env.cpp
src/process.cpp
src/shared_library.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
Expand All @@ -36,7 +37,7 @@ if(WIN32)
target_compile_definitions(${PROJECT_NAME}
PRIVATE "RCPPUTILS_BUILDING_LIBRARY")
endif()
ament_target_dependencies(${PROJECT_NAME} rcutils)
target_link_libraries(${PROJECT_NAME} PUBLIC rcutils::rcutils)

# Export old-style CMake variables
ament_export_include_directories("include/${PROJECT_NAME}")
Expand Down Expand Up @@ -74,15 +75,13 @@ if(BUILD_TESTING)
target_link_libraries(test_join ${PROJECT_NAME})

ament_add_gtest(test_time test/test_time.cpp)
target_link_libraries(test_time ${PROJECT_NAME})
ament_target_dependencies(test_time rcutils)
target_link_libraries(test_time ${PROJECT_NAME} rcutils::rcutils)

ament_add_gtest(test_env test/test_env.cpp
ENV
EMPTY_TEST=
NORMAL_TEST=foo
)
ament_target_dependencies(test_env rcutils)
target_link_libraries(test_env ${PROJECT_NAME})

ament_add_gtest(test_scope_exit test/test_scope_exit.cpp)
Expand All @@ -95,7 +94,6 @@ if(BUILD_TESTING)
ENV
EXPECTED_WORKING_DIRECTORY=$<SHELL_PATH:${CMAKE_CURRENT_BINARY_DIR}>
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
ament_target_dependencies(test_filesystem_helper rcutils)
target_link_libraries(test_filesystem_helper ${PROJECT_NAME})

ament_add_gtest(test_find_and_replace test/test_find_and_replace.cpp)
Expand All @@ -106,7 +104,6 @@ if(BUILD_TESTING)

ament_add_gtest(test_process test/test_process.cpp)
target_link_libraries(test_process ${PROJECT_NAME})
ament_target_dependencies(test_process rcutils)

set(append_library_dirs "$<TARGET_FILE_DIR:${PROJECT_NAME}>")

Expand All @@ -129,8 +126,9 @@ if(BUILD_TESTING)

add_library(test_library SHARED test/test_library.cpp)
target_link_libraries(test_library PUBLIC ${PROJECT_NAME})

ament_add_gtest(test_find_library test/test_find_library.cpp)
target_link_libraries(test_find_library test_library)
target_link_libraries(test_find_library test_library rcutils::rcutils)
set_tests_properties(test_find_library PROPERTIES
ENVIRONMENT
"_TEST_LIBRARY_DIR=$<TARGET_FILE_DIR:test_library>;_TEST_LIBRARY=$<TARGET_FILE:test_library>")
Expand Down
17 changes: 4 additions & 13 deletions include/rcpputils/process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
#ifndef RCPPUTILS__PROCESS_HPP_
#define RCPPUTILS__PROCESS_HPP_

#include <rcutils/process.h>

#include <string>

#include "rcpputils/visibility_control.hpp"

namespace rcpputils
{

Expand All @@ -32,17 +32,8 @@ namespace rcpputils
* \return The program name.
* \throws std::runtime_error on error
*/
std::string get_executable_name()
{
rcutils_allocator_t allocator = rcutils_get_default_allocator();
char * executable_name = rcutils_get_executable_name(allocator);
if (nullptr == executable_name) {
throw std::runtime_error("Failed to get executable name");
}
std::string ret(executable_name);
allocator.deallocate(executable_name, allocator.state);
return ret;
}
RCPPUTILS_PUBLIC
std::string get_executable_name();

} // namespace rcpputils

Expand Down
2 changes: 0 additions & 2 deletions include/rcpputils/time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

#include <chrono>

#include "rcutils/time.h"

namespace rcpputils
{

Expand Down
38 changes: 38 additions & 0 deletions src/process.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2023 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.

#include "rcpputils/process.hpp"

#include <stdexcept>
#include <string>

#include "rcutils/allocator.h"
#include "rcutils/process.h"

namespace rcpputils
{

std::string get_executable_name()
{
rcutils_allocator_t allocator = rcutils_get_default_allocator();
char * executable_name = rcutils_get_executable_name(allocator);
if (nullptr == executable_name) {
throw std::runtime_error("Failed to get executable name");
}
std::string ret(executable_name);
allocator.deallocate(executable_name, allocator.state);
return ret;
}

} // namespace rcpputils
2 changes: 2 additions & 0 deletions test/test_time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include <rcpputils/time.hpp>

#include "rcutils/time.h"

TEST(test_time, test_convert_to_nanoseconds) {
rcutils_duration_value_t expect_value = RCUTILS_S_TO_NS(5 * 60); // 5 minutes
rcutils_duration_value_t cast_val = 0;
Expand Down

0 comments on commit c7df827

Please sign in to comment.