diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e516ba5..06cbfade 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,10 +1,52 @@ -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 ## @@ -12,22 +54,15 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 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 @@ -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) @@ -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 @@ -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) \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index f3bf2b32..72bee28d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM ubuntu:22.04 +FROM ubuntu:22.04 AS base ARG DEBIAN_FRONTEND=noninteractive @@ -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 \ No newline at end of file diff --git a/README.md b/README.md index bc5f871a..dd0e96d5 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/demo/.env b/demo/.env index 4a82335f..ca291e96 120000 --- a/demo/.env +++ b/demo/.env @@ -1 +1 @@ -../.env \ No newline at end of file +version \ No newline at end of file diff --git a/demo/CMakeLists.txt b/demo/CMakeLists.txt index ef22ec3f..016eabaa 100644 --- a/demo/CMakeLists.txt +++ b/demo/CMakeLists.txt @@ -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() ################### @@ -19,6 +54,7 @@ find_package(SDL2 REQUIRED) find_package(util_caching REQUIRED) find_package(Yaml-cpp REQUIRED) + ########### ## Build ## ########### @@ -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() diff --git a/demo/Dockerfile b/demo/Dockerfile index 3f56f24c..d587b6b2 100644 --- a/demo/Dockerfile +++ b/demo/Dockerfile @@ -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 @@ -26,6 +27,7 @@ WORKDIR /home/blinky/demo CMD [ "/bin/bash" ] + FROM tutorial AS demo COPY --chown=blinky:blinky cmake /home/blinky/demo/cmake diff --git a/demo/test/CMakeLists.txt b/demo/test/CMakeLists.txt new file mode 100644 index 00000000..ed4e4abb --- /dev/null +++ b/demo/test/CMakeLists.txt @@ -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() diff --git a/demo/version b/demo/version new file mode 120000 index 00000000..1748d5b2 --- /dev/null +++ b/demo/version @@ -0,0 +1 @@ +../version \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 32e507e7..26bfbcf3 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -4,6 +4,9 @@ services: build: . env_file: .env arbitration_graphs_devel: + build: + context: . + target: devel extends: service: arbitration_graphs image: arbitration_graphs_devel diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 00000000..ed4e4abb --- /dev/null +++ b/test/CMakeLists.txt @@ -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() diff --git a/version b/version index 9939df2c..b8651586 100644 --- a/version +++ b/version @@ -1 +1 @@ -VERSION=v0.0.1 \ No newline at end of file +VERSION=v0.0.2 \ No newline at end of file