Skip to content

Commit

Permalink
Merge pull request #68 from KIT-MRT/modernize_cmake
Browse files Browse the repository at this point in the history
Modernize cmake
  • Loading branch information
orzechow authored Nov 5, 2024
2 parents b2a1f67 + bbd8ddc commit 061414c
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 98 deletions.
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)
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

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

# 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
```

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

```bash
find -executable -type f -name '*-gtest-*' -exec {} \;
cmake --build . --target test
```

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,6 +18,7 @@ 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
COPY --chown=blinky:blinky .motd /home/blinky/.motd
RUN echo "\n\nsource /home/blinky/.motd" >> /home/blinky/.bashrc
Expand All @@ -26,6 +27,7 @@ WORKDIR /home/blinky/demo

CMD [ "/bin/bash" ]


FROM tutorial AS demo

COPY --chown=blinky:blinky cmake /home/blinky/demo/cmake
Expand Down
39 changes: 39 additions & 0 deletions demo/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
###################
## 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()
else()
message(WARNING "GTest not found. Cannot compile tests!")
endif()
1 change: 1 addition & 0 deletions demo/version
Loading

0 comments on commit 061414c

Please sign in to comment.