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

Add ROSEnv example #164

Merged
merged 22 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
4 changes: 4 additions & 0 deletions examples/tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@

- Build a [Bazel 6.x compatible project](bazel/bazeltoolchain/6_x/string_formatter/) using Conan and [fmt](https://fmt.dev/). [Docs](https://docs.conan.io/2/examples/tools/google/bazeltoolchain/build_simple_bazel_project.rst)
- Build a [Bazel >= 7.1 compatible project](bazel/bazeltoolchain/7_x/string_formatter/) using Conan and [fmt](https://fmt.dev/). [Docs](https://docs.conan.io/2/examples/tools/google/bazeltoolchain/build_simple_bazel_7x_project.rst)

### [tools.ros](ros)

- Build [ROS packages inside their workspace](ros/rosenv/workspace/) using dependencies from Conan Center and consuming them also as transitive depdendencies.
danimtb marked this conversation as resolved.
Show resolved Hide resolved
34 changes: 34 additions & 0 deletions examples/tools/ros/rosenv/ci_test_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os
import platform
import subprocess

from test.examples_tools import chdir, run

test_dir_path = os.path.dirname(os.path.realpath(__file__))
if platform.system() == "Windows":
test_dir_path = test_dir_path.replace("/", "\\")

docker_command = f"docker run --rm -v {test_dir_path}:/mnt/local osrf/ros:humble-desktop bash -c \"cd mnt/local && ./docker_commands.sh\""

run(docker_command)

# try:
# # Setup the ROS environment
# run("source /opt/ros/humble/setup.bash")

# # Install the Conan dependencies of str_printer's package
# with chdir("workspace"):
# run("conan install str_printer/conanfile.txt --build=missing --output-folder install/conan")

# # Setup the environment to find Conan installed dependencies
# run("source install/conan/conanrosenv.sh")
# # Perform the build
# run("colcon build")

# # Setup the run environment
# run("source install/setup.bash")
# # Run the consumer
# run("ros2 run consumer main")
# finally:
# # Remove all the bazel symlinks and clean its cache
# shutil.rmtree("workspace/install", ignore_errors=True)
25 changes: 25 additions & 0 deletions examples/tools/ros/rosenv/docker_commands.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

# Install pip and Conan
apt-get update && apt-get install -y python3 python3-pip
python3 -m pip install conan

# Setup the ROS environment
. /opt/ros/humble/setup.bash

# Setup Conan profile
conan profile detect

cd workspace
# Install the Conan dependencies of Consumer's package
conan install consumer/conanfile.txt --build=missing --output-folder install/conan

# Setup the environment to find Conan installed dependencies
. install/conan/conanrosenv.sh

# Perform the build
colcon build

# Setup the run environment
. install/setup.bash
# Run the app
ros2 run app main
24 changes: 24 additions & 0 deletions examples/tools/ros/rosenv/workspace/consumer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.8)
project(consumer)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(str_printer REQUIRED)

add_executable(main src/main.cpp)

target_include_directories(main PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/app>
danimtb marked this conversation as resolved.
Show resolved Hide resolved
$<INSTALL_INTERFACE:include>)

target_compile_features(main PUBLIC c_std_99 cxx_std_17) # Require C99 and C++17
ament_target_dependencies(main str_printer)

install(TARGETS main
DESTINATION lib/${PROJECT_NAME})

ament_package()
20 changes: 20 additions & 0 deletions examples/tools/ros/rosenv/workspace/consumer/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>consumer</name>
<version>0.0.0</version>
<description>Consumer application that prints a fancy string</description>
<maintainer email="danielm@jfrog.com">danimtb</maintainer>
<license>MIT</license>

<buildtool_depend>ament_cmake</buildtool_depend>

<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>

<depend>str_printer</depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
10 changes: 10 additions & 0 deletions examples/tools/ros/rosenv/workspace/consumer/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "str_printer/str_printer.h"

int main(int argc, char ** argv)
{
(void) argc;
(void) argv;

str_printer("Hi there! I am using fmt library fetched with Conan C/C++ Package Manager");
return 0;
}
38 changes: 38 additions & 0 deletions examples/tools/ros/rosenv/workspace/str_printer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
cmake_minimum_required(VERSION 3.8)
project(str_printer)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(fmt REQUIRED) # Retrieved with Conan C/C++ Package Manager

add_library(str_printer src/str_printer.cpp)

target_include_directories(str_printer PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/str_printer>
$<INSTALL_INTERFACE:include>)

target_compile_features(str_printer PUBLIC c_std_99 cxx_std_17) # Require C99 and C++17
ament_target_dependencies(str_printer fmt)

ament_export_targets(str_printerTargets HAS_LIBRARY_TARGET)
ament_export_dependencies(fmt)

install(
DIRECTORY include/
DESTINATION include
)

install(
TARGETS str_printer
EXPORT str_printerTargets
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin
INCLUDES DESTINATION include
)

ament_package()
7 changes: 7 additions & 0 deletions examples/tools/ros/rosenv/workspace/str_printer/conanfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[requires]
fmt/11.0.2

[generators]
CMakeDeps
CMakeToolchain
ROSEnv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <string>

#ifdef WIN32
#define FN_EXPORT __declspec(dllexport)
#else
#define FN_EXPORT
#endif

FN_EXPORT void str_printer(std::string);
18 changes: 18 additions & 0 deletions examples/tools/ros/rosenv/workspace/str_printer/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>str_printer</name>
<version>0.0.0</version>
<description>Library that provides a function to print a fancy string using the fmt library fetched by Conan C/C++ Package Manager</description>
<maintainer email="danielm@jfrog.com">danimtb</maintainer>
<license>MIT</license>

<buildtool_depend>ament_cmake</buildtool_depend>

<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <iostream>
#include <string>

#include "fmt/core.h"
#include "fmt/color.h"

#include "str_printer.h"

void str_printer(std::string str) {
fmt::print(fmt::fg(fmt::color::gold) | fmt::emphasis::bold, "{}\n", str);
}
2 changes: 1 addition & 1 deletion test/examples_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def run(cmd, error=False):
print("Running: {}".format(cmd))
start_time = time.time()

process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, text=True)
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, text=True, encoding='utf-8')

output = ''

Expand Down