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

Standalone gz model executable #2724

Open
wants to merge 25 commits into
base: gz-sim9
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
89a6cea
Added gz model executable and updated ruby script
sauk2 Jan 19, 2025
7e1e2ce
Configuration file creation for model command
sauk2 Jan 19, 2025
a45811c
Building ModelCommandAPI tests after creating the executable
sauk2 Jan 20, 2025
d7eaf89
Separated arguments in model exe callback
sauk2 Jan 20, 2025
e76bc12
Removed previous model command api
sauk2 Jan 20, 2025
ad048e0
Reverted model command test
sauk2 Jan 20, 2025
ba180f7
Building sim cmd tests after creating model exe
sauk2 Jan 21, 2025
ce042a9
Removed comments in cmake
sauk2 Jan 21, 2025
e007882
Reverted message statement in cmake
sauk2 Jan 21, 2025
656aecd
Added dummy flags to make tests pass
sauk2 Jan 22, 2025
ef16c35
Merge branch 'gz-sim9' into gz-sim-executable
azeey Feb 3, 2025
14d92b3
Moved tests into cmd and reverted changed file names
sauk2 Feb 5, 2025
39f6db5
Setting env vars for gz_TEST
sauk2 Feb 5, 2025
6c69826
Merge branch 'gz-sim9' into gz-sim-executable
azeey Feb 5, 2025
3cfa88b
Shifted model CLI tests from sim tests
sauk2 Feb 6, 2025
e354baa
Merge branch 'gz-sim-executable' of github.com:sauk2/gz-sim into gz-s…
sauk2 Feb 6, 2025
4f6f9c0
Styling changes
sauk2 Feb 6, 2025
46cde0e
Fixed linting error
sauk2 Feb 6, 2025
0054fae
Removed brew from model tests and updated cmake
sauk2 Feb 7, 2025
a3dbb33
Removed model config yaml creation
sauk2 Feb 7, 2025
c94a6b8
Updated cmake to build test script before exe
sauk2 Feb 9, 2025
5ade7ca
Added necessary includes
sauk2 Feb 10, 2025
848bf9d
Fixed test exe path in config yaml
sauk2 Feb 10, 2025
d58e8ad
Added config path to test properties
sauk2 Feb 10, 2025
a8d0095
Building model test without checking for multi config generator
sauk2 Feb 11, 2025
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
62 changes: 0 additions & 62 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ set(gui_sources

set(cli_sources
gz.cc
cmd/ModelCommandAPI.cc
)

set(material_sources
Expand Down Expand Up @@ -134,23 +133,11 @@ set (gtest_sources
network/NetworkManager_TEST.cc
)

# gz_TEST and ModelCommandAPI_TEST are not supported with multi config
# CMake generators, see also cmd/CMakeLists.txt
get_property(GENERATOR_IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(NOT GENERATOR_IS_MULTI_CONFIG)
list(APPEND gtest_sources gz_TEST.cc)
endif()


# Tests that require a valid display
set(tests_needing_display
Server_Rendering_TEST.cc
)

if(NOT GENERATOR_IS_MULTI_CONFIG)
list(APPEND tests_needing_display ModelCommandAPI_TEST.cc)
endif()

# Add systems that need a valid display here.
# \todo(anyone) Find a way to run these tests with a virtual display such Xvfb
# or Xdummy instead of skipping them
Expand Down Expand Up @@ -265,53 +252,4 @@ if (TARGET UNIT_Server_Rendering_TEST)
)
endif()

# Command line tests need extra settings
foreach(CMD_TEST
UNIT_gz_TEST
UNIT_ModelCommandAPI_TEST)

if(NOT TARGET ${CMD_TEST})
continue()
endif()

# Running `gz sim` on macOS has problems when run with /usr/bin/ruby
# due to System Integrity Protection (SIP). Try to find ruby from
# homebrew as a workaround.
if (APPLE)
find_program(BREW_RUBY ruby HINTS /usr/local/opt/ruby/bin)
endif()

add_dependencies(${CMD_TEST}
${gz_lib_target}
TestModelSystem
TestSensorSystem
TestWorldSystem
)

target_compile_definitions(${CMD_TEST} PRIVATE
"BREW_RUBY=\"${BREW_RUBY} \"")

target_compile_definitions(${CMD_TEST} PRIVATE
"GZ_PATH=\"${GZ_TOOLS_PROGRAM}\"")

set(_env_vars)
list(APPEND _env_vars "GZ_CONFIG_PATH=${CMAKE_BINARY_DIR}/test/conf")
list(APPEND _env_vars "GZ_SIM_SYSTEM_PLUGIN_PATH=$<TARGET_FILE_DIR:TestModelSystem>")

set_tests_properties(${CMD_TEST} PROPERTIES
ENVIRONMENT "${_env_vars}")

# On Windows there is no RPATH, so an alternative way for tests for finding .dll libraries
# in build directory in necessary. For regular tests, the trick is to place all libraries
# and executables in a common CMAKE_RUNTIME_OUTPUT_DIRECTORY, so that the .dll are found
# as they are in the same directory where the executable is loaded. For tests that are
# launched via Ruby, this does not work, so we need to manually add CMAKE_RUNTIME_OUTPUT_DIRECTORY
# to the PATH. This is done via the ENVIRONMENT_MODIFICATION that was added in CMake 3.22.
if (WIN32)
set_tests_properties(${CMD_TEST} PROPERTIES
ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
endif()

endforeach()

add_subdirectory(cmd)
137 changes: 122 additions & 15 deletions src/cmd/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,86 @@
# Collect source files into the "sources" variable and unit test files into the
# "gtest_sources" variable.
gz_get_libsources_and_unittests(sources gtest_sources)

add_library(gz STATIC gz.cc)
target_link_libraries(gz
PUBLIC
${PROJECT_LIBRARY_TARGET_NAME}
)
sauk2 marked this conversation as resolved.
Show resolved Hide resolved

set(model_executable gz-sim-model)
add_executable(${model_executable} model_main.cc)
target_link_libraries(${model_executable}
gz
gz-utils${GZ_UTILS_VER}::cli
${PROJECT_LIBRARY_TARGET_NAME}
)

install(
TARGETS ${model_executable}
DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/gz/${GZ_DESIGNATION}${PROJECT_VERSION_MAJOR}/)

# Build the unit tests.
set(test_sources)

# gz_TEST and ModelCommandAPI_TEST are not supported with multi config
# CMake generators, see also cmd/CMakeLists.txt
get_property(GENERATOR_IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(NOT GENERATOR_IS_MULTI_CONFIG)
Comment on lines +28 to +29
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not needed for ModelCommandAPI since we're using the $<CONFIG> variable below which indicates that multi config generators are actually supported. If you move the ModelCommandAPI_TEST logic out of the if block and things fail, could you just add a TODO so we can come back to it later?

Copy link
Contributor Author

@sauk2 sauk2 Feb 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the check for multi config generator also be removed when building the model command internal test script?

https://github.com/sauk2/gz-sim/blob/a8d00952f8ecd513c7f8a09fd16565f213011af3/src/cmd/CMakeLists.txt#L189

list(APPEND test_sources
../gz_TEST.cc
azeey marked this conversation as resolved.
Show resolved Hide resolved
../ModelCommandAPI_TEST.cc
)
endif()

gz_build_tests(TYPE UNIT
sauk2 marked this conversation as resolved.
Show resolved Hide resolved
SOURCES
${test_sources}
LIB_DEPS
gz-utils${GZ_UTILS_VER}::gz-utils${GZ_UTILS_VER}
${EXTRA_TEST_LIB_DEPS}
gz-sim${PROJECT_VERSION_MAJOR}
ENVIRONMENT
GZ_SIM_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
)

foreach(CMD_TEST
UNIT_gz_TEST UNIT_ModelCommandAPI_TEST)

if(NOT TARGET ${CMD_TEST})
continue()
endif()

# Running `gz sim` on macOS has problems when run with /usr/bin/ruby
# due to System Integrity Protection (SIP). Try to find ruby from
# homebrew as a workaround.
if (APPLE)
find_program(BREW_RUBY ruby HINTS /usr/local/opt/ruby/bin)
endif()
azeey marked this conversation as resolved.
Show resolved Hide resolved


target_compile_definitions(${CMD_TEST}
PRIVATE
"GZ_PATH=\"${GZ_TOOLS_PROGRAM}\"")

target_compile_definitions(${CMD_TEST}
PRIVATE
"BREW_RUBY=\"${BREW_RUBY} \"")


# On Windows there is no RPATH, so an alternative way for tests for finding .dll libraries
# in build directory in necessary. For regular tests, the trick is to place all libraries
# and executables in a common CMAKE_RUNTIME_OUTPUT_DIRECTORY, so that the .dll are found
# as they are in the same directory where the executable is loaded. For tests that are
# launched via Ruby, this does not work, so we need to manually add CMAKE_RUNTIME_OUTPUT_DIRECTORY
# to the PATH. This is done via the ENVIRONMENT_MODIFICATION that was added in CMake 3.22.
if (WIN32)
set_tests_properties(${CMD_TEST} PROPERTIES
ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
endif()

endforeach()

#===============================================================================
# Generate the ruby script.
# Note that the major version of the library is included in the name.
Expand Down Expand Up @@ -47,31 +130,38 @@ install( FILES
# Used for the installed model command version.
# Generate the ruby script that gets installed.
# Note that the major version of the library is included in the name.
set(cmd_model_script_name "cmdmodel${PROJECT_VERSION_MAJOR}.rb")
set(cmd_model_script_generated "${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>_${cmd_model_script_name}")
set(cmd_model_script_configured "${CMAKE_CURRENT_BINARY_DIR}/${cmd_model_script_name}.configured")
# Ex: cmdmodel0.rb
set(cmd_model_script_generated "${CMAKE_CURRENT_BINARY_DIR}/cmdmodel${PROJECT_VERSION_MAJOR}.rb")
set(cmd_model_script_configured "${cmd_model_script_generated}.configured")

# Set the library_location variable to the relative path to the library file
# within the install directory structure.
set(model_exe_location "../../../${CMAKE_INSTALL_LIBEXECDIR}/gz/${GZ_DESIGNATION}${PROJECT_VERSION_MAJOR}/$<TARGET_FILE_NAME:${model_executable}>")

configure_file(
"cmdmodel.rb.in"
"${cmd_model_script_configured}"
@ONLY)
@ONLY
)

file(GENERATE
OUTPUT "${cmd_model_script_generated}"
INPUT "${cmd_model_script_configured}")

install(FILES ${cmd_model_script_generated} DESTINATION lib/ruby/gz RENAME ${cmd_model_script_name})
INPUT "${cmd_model_script_configured}"
)
# Install the ruby command line library in an unversioned location.
install(FILES ${cmd_model_script_generated} DESTINATION lib/ruby/gz)

# Used for the installed version.
set(gz_model_ruby_path "${CMAKE_INSTALL_PREFIX}/lib/ruby/gz/cmdmodel${PROJECT_VERSION_MAJOR}")

set(model_configured "${CMAKE_CURRENT_BINARY_DIR}/model${PROJECT_VERSION_MAJOR}.yaml")
configure_file(
"model.yaml.in"
${model_configured})
${model_configured}
@ONLY)

install(FILES ${model_configured} DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/gz/)


#===============================================================================
# Generate the ruby script for internal testing.
# Note that the major version of the library is included in the name.
Expand Down Expand Up @@ -114,25 +204,42 @@ endif()
# The logic is valid only for single-config CMake generators, so no script is
# generated if a multiple-config CMake geneator is used
if(NOT GENERATOR_IS_MULTI_CONFIG)
set(cmd_model_ruby_test_dir "${CMAKE_BINARY_DIR}/test/lib/ruby/gz")
set(cmd_model_script_generated_test "${cmd_model_ruby_test_dir}/cmdmodel${PROJECT_VERSION_MAJOR}.rb")
set(cmd_model_script_configured_test "${cmd_model_script_generated_test}.configured")
set(cmd_model_script_generated_test
"${CMAKE_BINARY_DIR}/test/lib/$<CONFIG>/ruby/gz/cmdmodel${PROJECT_VERSION_MAJOR}.rb")
set(cmd_model_script_configured_test
"${CMAKE_CURRENT_BINARY_DIR}/test_cmdmodel${PROJECT_VERSION_MAJOR}.rb.configured")

# Set the library_location variable to the full path of the library file within
# the build directory.
set(model_exe_location "$<TARGET_FILE:${model_executable}>")

configure_file(
"cmdmodel.rb.in"
"${cmd_model_script_configured_test}"
@ONLY)
@ONLY
)

file(GENERATE
OUTPUT "${cmd_model_script_generated_test}"
INPUT "${cmd_model_script_configured_test}")
INPUT "${cmd_model_script_configured_test}"
)

# Used for internal testing.
set(gz_model_ruby_path "${cmd_model_script_generated_test}")

# Generate a configuration file for internal testing.
# Note that the major version of the library is included in the name.
# Ex: model0.yaml
configure_file(
"model.yaml.in"
"${CMAKE_BINARY_DIR}/test/conf/model${PROJECT_VERSION_MAJOR}.yaml" @ONLY)
"${CMAKE_CURRENT_BINARY_DIR}/model${PROJECT_VERSION_MAJOR}.yaml.configured"
@ONLY
)

file(GENERATE
OUTPUT "${CMAKE_BINARY_DIR}/test/conf/$<CONFIG>/model${PROJECT_VERSION_MAJOR}.yaml"
INPUT "${CMAKE_CURRENT_BINARY_DIR}/model${PROJECT_VERSION_MAJOR}.yaml.configured"
)
endif()

#===============================================================================
Expand Down
Loading