Skip to content

Commit

Permalink
Build examples from CMake rather than executable (#482)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carroll <michael@openrobotics.org>
Signed-off-by: Michael Carroll <mjcarroll@intrinsic.ai>
  • Loading branch information
mjcarroll committed Jul 17, 2023
1 parent cf5d7f3 commit f64cb8c
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 441 deletions.
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ project(gz-math7 VERSION 7.2.0)
#============================================================================
# If you get an error at this line, you need to install gz-cmake
find_package(gz-cmake3 REQUIRED)
set(GZ_CMAKE_VER ${gz-cmake3_VERSION_MAJOR})

This comment has been minimized.

Copy link
@Blast545

Blast545 Jul 19, 2023

Why does this line needs to be added? I don't see it being used elsewhere? (out of curiosity)


#============================================================================
# Configure the project
Expand Down Expand Up @@ -144,3 +145,13 @@ configure_file(${CMAKE_SOURCE_DIR}/tutorials.md.in ${CMAKE_BINARY_DIR}/tutorials
gz_create_docs(
API_MAINPAGE_MD "${CMAKE_BINARY_DIR}/api.md"
TUTORIALS_MAINPAGE_MD "${CMAKE_BINARY_DIR}/tutorials.md")

#============================================================================
# Build examples
#============================================================================
if (BUILD_TESTING)
gz_build_examples(
SOURCE_DIR ${PROJECT_SOURCE_DIR}/examples
BINARY_DIR ${PROJECT_BINARY_DIR}/examples
)
endif()
13 changes: 6 additions & 7 deletions examples/color_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@
#include <iostream>
#include <gz/math/Color.hh>

int main(int argc, char **argv)
int main(int /*argc*/, char **/*argv*/)
{

//! [Create a color]
gz::math::Color a = gz::math::Color(0.3, 0.4, 0.5);
gz::math::Color a = gz::math::Color(0.3f, 0.4f, 0.5f);
//! [Create a color]
// The channel order is R, G, B, A and the default alpha value of a should be 1.0
std::cout << "The alpha value of a should be 1: " << a.A() << std::endl;



//! [Set a new color value]
a.gz::math::Color::Set(0.6, 0.7, 0.8, 1.0);
a.gz::math::Color::Set(0.6f, 0.7f, 0.8f, 1.0f);
//! [Set a new color value]
std::cout << "The RGBA value of a: " << a << std::endl;

Expand All @@ -41,8 +40,8 @@ int main(int argc, char **argv)

//! [Math operator]
std::cout << "Check if a is Blue: " << (a == gz::math::Color::Blue) << std::endl;
std::cout << "The RGB value of a should be (0, 0, 1): " << a[0] << ", "
<< a[1] << ", "
std::cout << "The RGB value of a should be (0, 0, 1): " << a[0] << ", "
<< a[1] << ", "
<< a[2] << std::endl;
//! [Math operator]

Expand Down
4 changes: 2 additions & 2 deletions include/gz/math/Helpers.hh
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ namespace gz
T sum = 0;
for (unsigned int i = 0; i < _values.size(); ++i)
sum += _values[i];
return sum / _values.size();
return sum / static_cast<T>(_values.size());
}

/// \brief Get the variance of a vector of values.
Expand All @@ -367,7 +367,7 @@ namespace gz
T sum = 0;
for (unsigned int i = 0; i < _values.size(); ++i)
sum += (_values[i] - avg) * (_values[i] - avg);
return sum / _values.size();
return sum / static_cast<T>(_values.size());
}

/// \brief Get the maximum value of vector of values.
Expand Down
2 changes: 1 addition & 1 deletion include/gz/math/Polynomial3.hh
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ namespace gz
using std::abs; // enable ADL
const T magnitude = abs(this->coeffs[i]);
const bool sign = this->coeffs[i] < T(0);
const int exponent = 3 - i;
const int exponent = 3 - static_cast<int>(i);
if (magnitude >= epsilon)
{
if (streamStarted)
Expand Down
12 changes: 8 additions & 4 deletions src/python_pybind11/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,12 @@ function(configure_build_install_location _library_name)
# Install into test folder in build space for unit tests to import
set_target_properties(${_library_name} PROPERTIES
# Use generator expression to avoid prepending a build type specific directory on Windows
LIBRARY_OUTPUT_DIRECTORY $<1:${CMAKE_CURRENT_BINARY_DIR}/test>
RUNTIME_OUTPUT_DIRECTORY $<1:${CMAKE_CURRENT_BINARY_DIR}/test>)
LIBRARY_OUTPUT_DIRECTORY $<1:${CMAKE_CURRENT_BINARY_DIR}/test/gz>
RUNTIME_OUTPUT_DIRECTORY $<1:${CMAKE_CURRENT_BINARY_DIR}/test/gz>)

# Touch an init file to mark this directory as a usable python module
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/gz/)
file(TOUCH ${CMAKE_CURRENT_BINARY_DIR}/test/gz/__init__.py)

# Install library for actual use
install(TARGETS ${_library_name}
Expand Down Expand Up @@ -187,8 +191,8 @@ if (BUILD_TESTING)
endif()

set(_env_vars)
list(APPEND _env_vars "PYTHONPATH=${FAKE_INSTALL_PREFIX}/lib/python/")
list(APPEND _env_vars "LD_LIBRARY_PATH=${FAKE_INSTALL_PREFIX}/lib:$ENV{LD_LIBRARY_PATH}")
list(APPEND _env_vars "PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR}/test")
list(APPEND _env_vars "LD_LIBRARY_PATH=${CMAKE_CURRENT_BINARY_DIR}/test:$ENV{LD_LIBRARY_PATH}")
set_tests_properties(${test}.py PROPERTIES
ENVIRONMENT "${_env_vars}")
endforeach()
Expand Down
4 changes: 2 additions & 2 deletions src/ruby/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ if (RUBY_FOUND)

# Add the ruby tests
set(_env_vars)
list(APPEND _env_vars "LD_LIBRARY_PATH=${FAKE_INSTALL_PREFIX}/lib:$ENV{LD_LIBRARY_PATH}")
list(APPEND _env_vars "LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/lib:$ENV{LD_LIBRARY_PATH}")
foreach (test ${ruby_tests})
add_test(NAME ${test}.rb COMMAND
ruby -I${FAKE_INSTALL_PREFIX}/lib/ruby/gz ${CMAKE_SOURCE_DIR}/src/ruby/${test}.rb
ruby -I${CMAKE_BINARY_DIR}/lib ${CMAKE_SOURCE_DIR}/src/ruby/${test}.rb
--gtest_output=xml:${CMAKE_BINARY_DIR}/test_results/${test}rb.xml)
set_tests_properties(${test}.rb PROPERTIES
ENVIRONMENT "${_env_vars}")
Expand Down
21 changes: 0 additions & 21 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,6 @@ include_directories (
${PROJECT_BINARY_DIR}/include
)

#============================================================================
# Do a fake install of gz-math in order to test the examples.
#============================================================================
# install to FAKE_INSTALL_PREFIX defined in root CMakeLists.txt

file(MAKE_DIRECTORY ${FAKE_INSTALL_PREFIX})

include(ExternalProject)
ExternalProject_Add(
FAKE_INSTALL

SOURCE_DIR "${CMAKE_SOURCE_DIR}"
EXCLUDE_FROM_ALL 1
LOG_CONFIGURE 1
LOG_BUILD 1
LOG_INSTALL 1
CMAKE_ARGS
"-DBUILD_TESTING=OFF"
"-DCMAKE_INSTALL_PREFIX=${FAKE_INSTALL_PREFIX}"
)

add_subdirectory(gtest_vendor)
add_subdirectory(integration)
add_subdirectory(performance)
Expand Down
9 changes: 0 additions & 9 deletions test/integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,3 @@ if (UNIX AND NOT APPLE)
endif()

gz_build_tests(TYPE INTEGRATION SOURCES ${tests})

if(TARGET INTEGRATION_ExamplesBuild_TEST)
add_dependencies(INTEGRATION_ExamplesBuild_TEST FAKE_INSTALL)

set(_env_vars)
list(APPEND _env_vars "CMAKE_PREFIX_PATH=${FAKE_INSTALL_PREFIX}:$ENV{CMAKE_PREFIX_PATH}")
set_tests_properties(INTEGRATION_ExamplesBuild_TEST PROPERTIES
ENVIRONMENT "${_env_vars}")
endif()
Loading

0 comments on commit f64cb8c

Please sign in to comment.