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

Modernize cmake #68

Merged
merged 13 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
114 changes: 66 additions & 48 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,33 +1,68 @@
cmake_minimum_required(VERSION 3.5.1)
project(arbitration_graphs)
cmake_minimum_required(VERSION 3.22)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
######################
## Project settings ##
######################

# Read the project/package version from ./version file
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/version" ver)
string(REGEX MATCH "VERSION=v([0-9\.]*)" _ ${ver})
set(PACKAGE_VERSION ${CMAKE_MATCH_1})

# Setup project
project(arbitration_graphs
VERSION ${PACKAGE_VERSION}
DESCRIPTION "Hierarchical behavior models for complex decision-making and behavior generation in robotics"
LANGUAGES CXX
)

# Select project features
set(BUILD_TESTS "FALSE" CACHE STRING "Compile the project with all unit tests")


###############
## C++ setup ##
###############

# Only do these if this is the main project, and not if it is included through add_subdirectory
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
# Require C++17
set(CMAKE_CXX_STANDARD 17)
orzechow marked this conversation as resolved.
Show resolved Hide resolved
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Let's ensure -std=c++xx instead of -std=g++xx
set(CMAKE_CXX_EXTENSIONS OFF)

# Let's nicely support folders in IDEs
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# Allow clangd and others to properly understand this C++ project
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Testing only available if this is the main project
# Note this needs to be done in the main CMakeLists
# since it calls enable_testing, which must be in the
# main CMakeLists.
include(CTest)
endif()


###################
## Find packages ##
###################

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")

find_package(Glog)

find_package(Glog REQUIRED)
find_package(util_caching REQUIRED)

find_package(Yaml-cpp REQUIRED)


############################
## Read source code files ##
############################
file(GLOB_RECURSE PROJECT_HEADER_FILES_INC "include/${PROJECT_NAME}/*.h" "include/${PROJECT_NAME}/*.hpp")


###########
## Build ##
###########

# Declare a cpp library
add_library(${PROJECT_NAME} INTERFACE)
target_include_directories(${PROJECT_NAME} INTERFACE
Expand All @@ -39,15 +74,17 @@ target_link_libraries(${PROJECT_NAME} INTERFACE
util_caching
${YAML_CPP_LIBRARIES}
)
set(INSTALL_TARGETS ${PROJECT_NAME})


###################
## Cmake Package ##
###################

include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
VERSION 0.1
VERSION ${PACKAGE_VERSION}
COMPATIBILITY AnyNewerVersion
)
include(CMakePackageConfigHelpers)
Expand All @@ -59,11 +96,22 @@ configure_package_config_file(


#############
## Install ##
## Testing ##
#############

# Testing only available if this is the main project
# Emergency override ARBITRATION_GRAPHS_CMAKE_BUILD_TESTING provided as well
if((CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR ARBITRATION_GRAPHS_CMAKE_BUILD_TESTING)
AND BUILD_TESTS)
add_subdirectory(test)
endif()


#############
## Install ##
#############

install(TARGETS ${PROJECT_NAME}
install(TARGETS ${INSTALL_TARGETS}
EXPORT ${PROJECT_NAME}Targets
LIBRARY DESTINATION lib COMPONENT Runtime
ARCHIVE DESTINATION lib COMPONENT Development
Expand All @@ -75,34 +123,4 @@ install(EXPORT ${PROJECT_NAME}Targets DESTINATION lib/cmake/${PROJECT_NAME})
install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
DESTINATION lib/cmake/${PROJECT_NAME})
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ DESTINATION include)


#############
## Testing ##
#############
find_package(GTest)

if(GTEST_FOUND)
if(BUILD_TESTS)
file(GLOB_RECURSE _tests "test/*.cpp" "test/*.cc")

foreach(_test ${_tests})
get_filename_component(_test_name ${_test} NAME_WE)
# make sure we add only one -test to the target
string(REGEX REPLACE "-test" "" TEST_TARGET_NAME ${_test_name})
set(TEST_TARGET_NAME ${PROJECT_NAME}-gtest-${TEST_TARGET_NAME})

message(STATUS
"Adding gtest unittest \"${TEST_TARGET_NAME}\" with working dir ${PROJECT_SOURCE_DIR}/${TEST_FOLDER} \n _test: ${_test}"
)

add_executable(${TEST_TARGET_NAME} ${_test})

target_link_libraries(${TEST_TARGET_NAME} PUBLIC
${GTEST_BOTH_LIBRARIES} pthread
${PROJECT_NAME}
)
endforeach()
endif()
endif()
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ DESTINATION include)
23 changes: 15 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ubuntu:22.04
FROM ubuntu:22.04 as base
orzechow marked this conversation as resolved.
Show resolved Hide resolved

ARG DEBIAN_FRONTEND=noninteractive

Expand All @@ -23,21 +23,28 @@ RUN git clone https://github.com/KIT-MRT/util_caching.git /tmp/util_caching && \
rm -rf /tmp/util_caching



FROM base AS devel

RUN useradd --create-home --uid 1000 blinky
USER blinky

WORKDIR /home/blinky/



FROM base as install
orzechow marked this conversation as resolved.
Show resolved Hide resolved

# Install arbitration_graphs
COPY CMakeLists.txt /tmp/arbitration_graphs/
COPY cmake /tmp/arbitration_graphs/cmake
COPY include /tmp/arbitration_graphs/include
COPY test /tmp/arbitration_graphs/test
COPY version /tmp/arbitration_graphs/version

RUN mkdir /tmp/arbitration_graphs/build && \
cd /tmp/arbitration_graphs/build && \
cmake .. && \
cmake --build . && \
cmake --install . && \
rm -rf /tmp/arbitration_graphs

RUN useradd --create-home --uid 1000 blinky
USER blinky

WORKDIR /home/blinky/

rm -rf /tmp/arbitration_graphs
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ In order to compile with tests define `BUILD_TESTS=true`
mkdir -p arbitration_graphs/build
cd arbitration_graphs/build
cmake -DBUILD_TESTS=true ..
cmake --build .
cmake --build . -j9
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why 9 btw? :D

Copy link
Member Author

@orzechow orzechow Nov 5, 2024

Choose a reason for hiding this comment

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

Kind of arbitrary, but at least more than one.

A very rough suggestion was once to use #threads + 1, nowadays 2 * #threads seems to also work for many. Anyhow, it's not too crazy to assume a modern dev has >8 threads available (4 cores with hyperthreading).

```

Run all unit tests:
Run all unit tests with CTest:

```bash
find -executable -type f -name '*-gtest-*' -exec {} \;
cmake --build . --target test
orzechow marked this conversation as resolved.
Show resolved Hide resolved
```

2 changes: 1 addition & 1 deletion demo/.env
85 changes: 48 additions & 37 deletions demo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
cmake_minimum_required(VERSION 3.5.1)
project(arbitration_graphs_pacman_demo)
cmake_minimum_required(VERSION 3.22)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
######################
## Project settings ##
######################

# Setup project
project(arbitration_graphs_pacman_demo
DESCRIPTION "Demo and tutorial project for teaching arbitration_graphs"
LANGUAGES CXX
)

# Select project features
set(BUILD_TESTS "FALSE" CACHE STRING "Compile the project with all unit tests")


###############
## C++ setup ##
###############

# Only do these if this is the main project, and not if it is included through add_subdirectory
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
# Require C++17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Let's ensure -std=c++xx instead of -std=g++xx
set(CMAKE_CXX_EXTENSIONS OFF)

# Let's nicely support folders in IDEs
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# Allow clangd and others to properly understand this C++ project
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Testing only available if this is the main project
# Note this needs to be done in the main CMakeLists
# since it calls enable_testing, which must be in the
# main CMakeLists.
include(CTest)
endif()


###################
Expand All @@ -19,6 +54,7 @@ find_package(SDL2 REQUIRED)
find_package(util_caching REQUIRED)
find_package(Yaml-cpp REQUIRED)


###########
## Build ##
###########
Expand Down Expand Up @@ -63,39 +99,14 @@ target_link_libraries(${PROJECT_NAME}_exe PRIVATE
${YAML_CPP_LIBRARIES}
)


#############
## Testing ##
#############
find_package(GTest)

if(GTEST_FOUND)
if(BUILD_TESTS)
file(GLOB_RECURSE _tests "test/*.cpp" "test/*.cc")

foreach(_test ${_tests})
get_filename_component(_test_name ${_test} NAME_WE)
# make sure we add only one -test to the target
string(REGEX REPLACE "-test" "" TEST_TARGET_NAME ${_test_name})
set(TEST_TARGET_NAME ${PROJECT_NAME}-gtest-${TEST_TARGET_NAME})

message(STATUS
"Adding gtest unittest \"${TEST_TARGET_NAME}\" with working dir ${PROJECT_SOURCE_DIR}/${TEST_FOLDER} \n _test: ${_test}"
)

add_executable(${TEST_TARGET_NAME} ${_test})

target_include_directories(${TEST_TARGET_NAME}
PRIVATE
include
${SDL2_INCLUDE_DIR}
)
target_link_libraries(${TEST_TARGET_NAME}
PRIVATE
${GTEST_BOTH_LIBRARIES} pthread
${PROJECT_NAME}
EnTT_Pacman
)
endforeach()
endif()
endif()

# Testing only available if this is the main project
# Emergency override ARBITRATION_GRAPHS_DEMO_CMAKE_BUILD_TESTING provided as well
if((CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR ARBITRATION_GRAPHS_DEMO_CMAKE_BUILD_TESTING)
AND BUILD_TESTS)
add_subdirectory(test)
endif()
2 changes: 2 additions & 0 deletions demo/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ RUN git clone https://github.com/KIT-MRT/EnTT-Pacman.git --branch arbitration_gr
cmake --install . && \
rm -rf /tmp/EnTT-Pacman

RUN useradd --create-home --uid 1000 blinky
USER blinky

WORKDIR /home/blinky/demo

CMD [ "/bin/bash" ]


FROM tutorial AS demo

COPY --chown=blinky:blinky cmake /home/blinky/demo/cmake
Expand Down
37 changes: 37 additions & 0 deletions demo/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
###################
## Find packages ##
###################

find_package(GTest)


###########
## Build ##
###########

if(GTEST_FOUND)
file(GLOB_RECURSE _tests CONFIGURE_DEPENDS "*.cpp" "*.cc")

foreach(_test ${_tests})
get_filename_component(_test_name ${_test} NAME_WE)
# make sure we add only one -test to the target
string(REGEX REPLACE "-test" "" TEST_TARGET_NAME ${_test_name})
set(TEST_TARGET_NAME ${PROJECT_NAME}-gtest-${TEST_TARGET_NAME})

message(STATUS
"Adding gtest unittest \"${TEST_TARGET_NAME}\" with working dir ${PROJECT_SOURCE_DIR}/${TEST_FOLDER} \n _test: ${_test}"
)

add_executable(${TEST_TARGET_NAME} ${_test})

target_link_libraries(${TEST_TARGET_NAME} PUBLIC
${GTEST_BOTH_LIBRARIES} pthread
${PROJECT_NAME}
)

add_test(NAME ${TEST_TARGET_NAME}
COMMAND ${TEST_TARGET_NAME}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
endforeach()
endif()
1 change: 1 addition & 0 deletions demo/version
3 changes: 3 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ services:
build: .
env_file: .env
arbitration_graphs_devel:
build:
context: .
target: devel
extends:
service: arbitration_graphs
image: arbitration_graphs_devel
Expand Down
Loading