Skip to content

Commit 315397e

Browse files
committed
Improve examples
* Simpler and standard build workflow * Utilize CTest with the recommended workflow of optional tests * Skip using make directly so these become more platform portable * Skip having to manually create the build directory * Do all operations from the source directory when possible to reduce the number of commands Signed-off-by: Ryan Friedman <[email protected]>
1 parent 6af7445 commit 315397e

File tree

9 files changed

+146
-113
lines changed

9 files changed

+146
-113
lines changed

examples/standalone/acoustic_comms_demo/README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@ will receive the signal and start moving before Daphne.
1515

1616
From this directory, run the following to compile:
1717

18-
mkdir build
19-
cd build
20-
cmake ..
21-
make
18+
```bash
19+
cmake -B build
20+
cmake --build build
21+
```
2222

2323
## Execute Instructions
2424

25-
From the `build` directory, run Gazebo Sim and the example controller:
25+
Run Gazebo Sim and the example controller:
2626

27-
gz sim -r ../../../worlds/acoustic_comms_demo.sdf
28-
./acoustic_comms_demo
27+
```bash
28+
gz sim -r ../../worlds/acoustic_comms_demo.sdf
29+
./build/acoustic_comms_demo
30+
```
2931

3032
It can be seen visually that one of the vehicles (Triton) starts moving
3133
immediately, then afer a while Tethys will start moving, and then finally Daphne.

examples/standalone/custom_server/README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ using the C++ API, instead of using Gazebo from the command line.
55

66
## Build Instructions
77

8-
From this directory:
8+
From this directory, run the following to compile:
99

10-
cd gz-sim/examples/standalone/custom_server
11-
mkdir build
12-
cd build
13-
cmake ..
14-
make
10+
```bash
11+
cmake -B build
12+
cmake --build build
13+
```
1514

1615
## Execute Instructions
1716

18-
./custom_server
17+
```bash
18+
./build/custom_server
19+
```
1920

2021
The server will run `shapes.sdf` for 100 iterations and exit. No GUI will
2122
show up.

examples/standalone/each_performance/README.md

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
11
Example program to generate performance data
22

3-
### Build
3+
## Build Instructions
44

5-
cd examples/standalong/each_performance
6-
mkdir build
7-
cd build
8-
cmake ..
9-
make
5+
From this directory, run the following to compile:
6+
7+
```bash
8+
cmake -B build
9+
cmake --build build
10+
```
1011

1112
### Run
1213

13-
./each
14+
```bash
15+
cd build
16+
./each
17+
```
18+
19+
This will generate `./build/each.data` which is the performance data.
20+
21+
### Generate and view plots
22+
23+
One the above program is complete, generate the plots.
24+
25+
```bash
26+
cd build
27+
# Generate plots
28+
gnuplot -e "filename='each.data'" ../each.gp
29+
eog *.png
30+
```
1431

15-
### Generate and view plot
32+
You can now scroll through two plots to view performance data.
1633

17-
gnuplot -e "filename='each.data'" ../each.gp
18-
eog *.png

examples/standalone/entity_creation/README.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,21 @@
22

33
## Build
44

5-
```
6-
mkdir build
7-
cd build
8-
cmake ..
9-
make
5+
```bash
6+
cmake -B build
7+
cmake --build build
108
```
119

1210
## Run
1311

1412
This example only works if the world is called `empty`. Start an empty world with:
1513

16-
```
14+
```bash
1715
gz sim empty.sdf
1816
```
1917

2018
Then run the create program to spawn entities into the world:
2119

22-
```
23-
cd build
24-
./entity_creation
20+
```bash
21+
./build/entity_creation
2522
```

examples/standalone/external_ecm/README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,35 @@
33
Example showing how to get a snapshot of all entities and components in a
44
running simulation from an external program using the state message.
55

6-
## Build
6+
## Build Instructions
77

8-
From the root of the `gz-sim` repository, do the following to build the example:
8+
From this directory, run the following to compile:
99

10-
~~~
11-
cd gz-sim/examples/standalone/external_ecm
12-
mkdir build
13-
cd build
14-
cmake ..
15-
make
16-
~~~
10+
```bash
11+
cmake -B build
12+
cmake --build build
13+
```
1714

1815
This will generate the `external_ecm` executable under `build`.
1916

2017
## Run
2118

2219
Start a simulation, for example:
2320

24-
gz sim shapes.sdf
21+
```bash
22+
gz sim shapes.sdf
23+
```
2524

26-
On another terminal, run the `external_ecm` executable, passing the name of the
25+
In another terminal, from this directory, run the `external_ecm` executable, passing the name of the
2726
running world you want to inspect:
2827

29-
cd gz-sim/examples/standalone/external_ecm
30-
./external_ecm shapes
28+
```bash
29+
./build/external_ecm shapes
30+
```
3131

3232
You should see something like this:
3333

34-
```
34+
```text
3535
$ ./external_ecm shapes
3636
3737
Requesting state for world [shapes] on service [/world/shapes/state]...
Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
1-
cmake_minimum_required(VERSION 3.11.0 FATAL_ERROR)
1+
cmake_minimum_required(VERSION 3.16 FATAL_ERROR)
22

33
project(GTestSetup)
44

55
# Find Gazebo
66
find_package(gz-sim8 REQUIRED)
77
set(GZ_SIM_VER ${gz-sim8_VERSION_MAJOR})
88

9-
# Fetch and configure GTest
10-
include(FetchContent)
11-
FetchContent_Declare(
12-
googletest
13-
# Version 1.14. Use commit hash to prevent tag relocation
14-
URL https://github.com/google/googletest/archive/f8d7d77c06936315286eb55f8de22cd23c188571.zip
15-
)
16-
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
17-
FetchContent_MakeAvailable(googletest)
9+
find_package(GTest REQUIRED)
10+
if(NOT TARGET GTest::gtest_main AND TARGET gtest_main)
11+
add_library(GTest::gtest_main ALIAS gtest_main)
12+
endif()
1813

19-
enable_testing()
20-
include(Dart)
14+
include(CTest)
15+
# CTest defines "BUILD_TESTING" so that users can easily disable tests in a common way.
16+
if(BUILD_TESTING)
17+
# Generate tests
18+
foreach(TEST_TARGET
19+
command_TEST
20+
gravity_TEST)
2121

22-
# Generate tests
23-
foreach(TEST_TARGET
24-
command_TEST
25-
gravity_TEST)
26-
27-
add_executable(
28-
${TEST_TARGET}
29-
${TEST_TARGET}.cc
30-
)
31-
target_link_libraries(${TEST_TARGET}
32-
gtest_main
33-
gz-sim${GZ_SIM_VER}::gz-sim${GZ_SIM_VER}
34-
)
35-
include(GoogleTest)
36-
gtest_discover_tests(${TEST_TARGET})
37-
endforeach()
22+
add_executable(
23+
${TEST_TARGET}
24+
${TEST_TARGET}.cc
25+
)
26+
target_link_libraries(${TEST_TARGET}
27+
GTest::gtest_main
28+
gz-sim${GZ_SIM_VER}::gz-sim${GZ_SIM_VER}
29+
)
30+
include(GoogleTest)
31+
gtest_discover_tests(${TEST_TARGET})
32+
endforeach()
33+
endif()

examples/standalone/gtest_setup/README.md

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,37 @@ The example contains 2 tests:
99

1010
See the comments on the source code for more explanations.
1111

12-
## Build
12+
## Build Instructions
1313

14-
From the root of the repository:
14+
From this directory, run the following to compile:
1515

16-
cd examples/standalone/gtest_setup
17-
mkdir build
18-
cd build
19-
cmake ..
20-
make
16+
```bash
17+
cmake -B build
18+
cmake --build build
19+
```
2120

2221
## Run tests
2322

24-
cd examples/standalone/gtest_setup/build
25-
./gravity_TEST
26-
./command_TEST
23+
From this directory, run the following to run tests:
24+
25+
### CMake 3.20 or newer
26+
27+
```bash
28+
ctest --test-dir build
29+
```
30+
31+
By default, ctest hides stdout from tests.
32+
To enable test output, add `-V`.
33+
CTest also hides colors, which can be re-enabled.
34+
```bash
35+
GTEST_COLOR=1 ctest -V --test-dir build
36+
```
37+
38+
39+
### CMake 3.19 or earlier
40+
41+
```bash
42+
cd build
43+
ctest
44+
```
45+

examples/standalone/joy_to_twist/README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@ messages and converts publishes
66
[gz::msgs::Twist](https://gazebosim.org/api/msgs/9/classgz_1_1msgs_1_1Twist.html)
77
messages according to user-defined configuration.
88

9-
## Build
9+
## Build Instructions
1010

11-
From the root of the `gz-sim` repository, do the following to build the example:
11+
From this directory, run the following to compile:
1212

13-
~~~
14-
cd examples/standalone/joy_to_twist
15-
mkdir build
16-
cd build
17-
cmake ..
18-
make
19-
~~~
13+
```bash
14+
cmake -B build
15+
cmake --build build
16+
```
2017

2118
This will generate the `joy_to_twist` executable under `build`.
2219

@@ -27,7 +24,9 @@ An example file, `joy_to_twist.sdf`, is provided.
2724

2825
You can run the example as follows:
2926

30-
./joy_to_twist ../joy_to_twist.sdf
27+
```bash
28+
./build/joy_to_twist joy_to_twist.sdf
29+
```
3130

3231
This program by itself won't do much unless there is another source of joy
3332
messages that it can consume. See the demo below for a full integrated example.
@@ -41,8 +40,10 @@ that can be controlled using a joystick. You can run it as follows:
4140
messages. See that standalone program's instructions to details on how
4241
to build it. Once it's built, you can run it as follows:
4342

44-
cd examples/standalone/joystick
45-
./joystick ../joystick.sdf
43+
```bash
44+
cd examples/standalone/joystick
45+
./build/joystick ../joystick.sdf
46+
```
4647

4748
1. On another terminal, run the `joy_to_twist` executable as described above,
4849
which will convert joy messages to twist messages:

0 commit comments

Comments
 (0)