From c234667c7975c989d4d80bd6ca0bd35418bfe59a Mon Sep 17 00:00:00 2001 From: Ben Deane Date: Thu, 2 Oct 2025 08:07:59 -0600 Subject: [PATCH] :memo: :art: :construction_worker: Refactor examples Problem: - Examples are few, and do not build with CI. Solution: - Restructure examples in anticipation of adding more. - Add a workflow to build examples. --- .github/workflows/examples.yml | 58 +++++++++++++++++++ .gitignore | 2 +- examples/CMakeLists.txt | 20 ++++++- examples/cib_serial_port/CMakeLists.txt | 26 --------- examples/flow/CMakeLists.txt | 1 + examples/flow/daily_routine/CMakeLists.txt | 39 +++++++++++++ .../daily_routine}/main.cpp | 0 examples/flow_daily_routine/CMakeLists.txt | 26 --------- examples/hello_world/CMakeLists.txt | 26 --------- examples/log/CMakeLists.txt | 1 + examples/nexus/CMakeLists.txt | 2 + examples/nexus/hello_world/CMakeLists.txt | 39 +++++++++++++ examples/{ => nexus}/hello_world/README.md | 0 examples/{ => nexus}/hello_world/core.hpp | 0 .../{ => nexus}/hello_world/dont_panic.cpp | 0 .../{ => nexus}/hello_world/dont_panic.hpp | 0 .../{ => nexus}/hello_world/hello_world.hpp | 0 examples/{ => nexus}/hello_world/lazy_dog.hpp | 0 examples/{ => nexus}/hello_world/main.cpp | 0 .../hello_world/say_hello_world.hpp | 0 examples/nexus/serial_port/CMakeLists.txt | 39 +++++++++++++ .../serial_port}/main.cpp | 0 22 files changed, 197 insertions(+), 82 deletions(-) create mode 100644 .github/workflows/examples.yml delete mode 100644 examples/cib_serial_port/CMakeLists.txt create mode 100644 examples/flow/CMakeLists.txt create mode 100644 examples/flow/daily_routine/CMakeLists.txt rename examples/{flow_daily_routine => flow/daily_routine}/main.cpp (100%) delete mode 100644 examples/flow_daily_routine/CMakeLists.txt delete mode 100644 examples/hello_world/CMakeLists.txt create mode 100644 examples/log/CMakeLists.txt create mode 100644 examples/nexus/CMakeLists.txt create mode 100644 examples/nexus/hello_world/CMakeLists.txt rename examples/{ => nexus}/hello_world/README.md (100%) rename examples/{ => nexus}/hello_world/core.hpp (100%) rename examples/{ => nexus}/hello_world/dont_panic.cpp (100%) rename examples/{ => nexus}/hello_world/dont_panic.hpp (100%) rename examples/{ => nexus}/hello_world/hello_world.hpp (100%) rename examples/{ => nexus}/hello_world/lazy_dog.hpp (100%) rename examples/{ => nexus}/hello_world/main.cpp (100%) rename examples/{ => nexus}/hello_world/say_hello_world.hpp (100%) create mode 100644 examples/nexus/serial_port/CMakeLists.txt rename examples/{cib_serial_port => nexus/serial_port}/main.cpp (100%) diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml new file mode 100644 index 00000000..2532eb8e --- /dev/null +++ b/.github/workflows/examples.yml @@ -0,0 +1,58 @@ +name: Examples +permissions: read-all + +on: + workflow_dispatch: + merge_group: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +env: + DEBIAN_FRONTEND: noninteractive + CMAKE_GENERATOR: Ninja + DEFAULT_CXX_STANDARD: 20 + DEFAULT_LLVM_VERSION: 21 + +jobs: + examples: + runs-on: ${{ github.repository_owner == 'intel' && 'intel-' || '' }}ubuntu-24.04 + steps: + - name: Checkout PR branch + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 + + - name: Install build tools + run: | + wget https://apt.llvm.org/llvm.sh && chmod +x llvm.sh && sudo ./llvm.sh ${{env.DEFAULT_LLVM_VERSION}} + sudo apt install -y ninja-build + + - name: Restore CPM cache + env: + cache-name: cpm-cache-0 + id: cpm-cache-restore + uses: actions/cache/restore@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 + with: + path: ~/cpm-cache + key: ${{runner.os}}-${{env.cache-name}}-${{ hashFiles('**/CMakeLists.txt', 'cmake/**') }} + restore-keys: | + ${{runner.os}}-${{env.cache-name}}- + + - name: Configure CMake + env: + CC: "/usr/lib/llvm-${{env.DEFAULT_LLVM_VERSION}}/bin/clang" + CXX: "/usr/lib/llvm-${{env.DEFAULT_LLVM_VERSION}}/bin/clang++" + PR_TARGET_BRANCH: ${{ steps.target_branch.outputs.branch }} + run: cmake -B ${{github.workspace}}/build -DCMAKE_CXX_STANDARD=${{env.DEFAULT_CXX_STANDARD}} -DCPM_SOURCE_CACHE=~/cpm-cache + + - name: Save CPM cache + env: + cache-name: cpm-cache-0 + if: steps.cpm-cache-restore.outputs.cache-hit != 'true' + uses: actions/cache/save@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 + with: + path: ~/cpm-cache + key: ${{runner.os}}-${{env.cache-name}}-${{ hashFiles('**/CMakeLists.txt', 'cmake/**') }} + + - name: Build examples + run: cmake --build ${{github.workspace}}/build -t examples diff --git a/.gitignore b/.gitignore index 4274cd02..e8099e28 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -/build +build/ /cmake-build-* /venv /.vscode diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 2c95b91f..f25aca11 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,3 +1,17 @@ -add_subdirectory(hello_world) -add_subdirectory(cib_serial_port) -add_subdirectory(flow_daily_routine) +add_custom_target(examples) + +function(add_example) + set(singleValueArgs NAME) + set(multiValueArgs FILES) + cmake_parse_arguments(EX "" "${singleValueArgs}" "${multiValueArgs}" + ${ARGN}) + string(PREPEND EX_NAME "EXAMPLE.") + + add_executable(${EX_NAME} ${EX_FILES}) + target_link_libraries(${EX_NAME} cib) + add_dependencies(examples ${EX_NAME}) +endfunction() + +add_subdirectory(flow) +add_subdirectory(log) +add_subdirectory(nexus) diff --git a/examples/cib_serial_port/CMakeLists.txt b/examples/cib_serial_port/CMakeLists.txt deleted file mode 100644 index 488484cd..00000000 --- a/examples/cib_serial_port/CMakeLists.txt +++ /dev/null @@ -1,26 +0,0 @@ -cmake_minimum_required(VERSION 3.16) - -project(cib_serial_port LANGUAGES CXX) - -add_executable(cib_serial_port main.cpp) - -if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) - # to fetch cib, either use CPM (https://github.com/cpm-cmake/CPM.cmake) or - # plain old CMake functionality - - # update this to a more recent commit ID (or tag) for your project - set(CIB_VERSION "476c583") - - if(CPM_DOWNLOAD_VERSION) - cpmaddpackage("gh:intel/compile-time-init-build#${CIB_VERSION}") - else() - include(FetchContent) - FetchContent_Declare( - cib - GIT_REPOSITORY https://github.com/intel/compile-time-init-build.git - GIT_TAG ${CIB_VERSION}) - FetchContent_MakeAvailable(cib) - endif() -endif() - -target_link_libraries(cib_serial_port cib) diff --git a/examples/flow/CMakeLists.txt b/examples/flow/CMakeLists.txt new file mode 100644 index 00000000..2f2b8d9f --- /dev/null +++ b/examples/flow/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(daily_routine) diff --git a/examples/flow/daily_routine/CMakeLists.txt b/examples/flow/daily_routine/CMakeLists.txt new file mode 100644 index 00000000..01878dbe --- /dev/null +++ b/examples/flow/daily_routine/CMakeLists.txt @@ -0,0 +1,39 @@ +cmake_minimum_required(VERSION 3.25) + +project(daily_routine LANGUAGES CXX) + +if(NOT TARGET cib) + # to fetch cib, either use CPM (https://github.com/cpm-cmake/CPM.cmake) or + # use plain old CMake functionality + set(USE_CPM 1) + + set(CIB_VERSION "c388a4d") # update this to a more recent commit ID (or tag) + # for your project + + if(USE_CPM) + file( + DOWNLOAD + https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.42.0/CPM.cmake + ${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake + EXPECTED_HASH + SHA256=2020b4fc42dba44817983e06342e682ecfc3d2f484a581f11cc5731fbe4dce8a + ) + include(${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake) + + cpmaddpackage("gh:intel/compile-time-init-build#${CIB_VERSION}") + else() + include(FetchContent) + FetchContent_Declare( + cib + GIT_REPOSITORY https://github.com/intel/compile-time-init-build.git + GIT_TAG ${CIB_VERSION}) + FetchContent_MakeAvailable(cib) + endif() +endif() + +if(COMMAND add_example) + add_example(NAME daily_routine FILES main.cpp) +else() + add_executable(daily_routine main.cpp) + target_link_libraries(daily_routine cib) +endif() diff --git a/examples/flow_daily_routine/main.cpp b/examples/flow/daily_routine/main.cpp similarity index 100% rename from examples/flow_daily_routine/main.cpp rename to examples/flow/daily_routine/main.cpp diff --git a/examples/flow_daily_routine/CMakeLists.txt b/examples/flow_daily_routine/CMakeLists.txt deleted file mode 100644 index f18a19e6..00000000 --- a/examples/flow_daily_routine/CMakeLists.txt +++ /dev/null @@ -1,26 +0,0 @@ -cmake_minimum_required(VERSION 3.16) - -project(flow_daily_routine LANGUAGES CXX) - -add_executable(flow_daily_routine main.cpp) - -if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) - # to fetch cib, either use CPM (https://github.com/cpm-cmake/CPM.cmake) or - # plain old CMake functionality - - # update this to a more recent commit ID (or tag) for your project - set(CIB_VERSION "476c583") - - if(CPM_DOWNLOAD_VERSION) - cpmaddpackage("gh:intel/compile-time-init-build#${CIB_VERSION}") - else() - include(FetchContent) - FetchContent_Declare( - cib - GIT_REPOSITORY https://github.com/intel/compile-time-init-build.git - GIT_TAG ${CIB_VERSION}) - FetchContent_MakeAvailable(cib) - endif() -endif() - -target_link_libraries(flow_daily_routine cib) diff --git a/examples/hello_world/CMakeLists.txt b/examples/hello_world/CMakeLists.txt deleted file mode 100644 index 883afced..00000000 --- a/examples/hello_world/CMakeLists.txt +++ /dev/null @@ -1,26 +0,0 @@ -cmake_minimum_required(VERSION 3.16) - -project(hello_world LANGUAGES CXX) - -add_executable(hello_world main.cpp dont_panic.cpp) - -if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) - # to fetch cib, either use CPM (https://github.com/cpm-cmake/CPM.cmake) or - # plain old CMake functionality - - # update this to a more recent commit ID (or tag) for your project - set(CIB_VERSION "476c583") - - if(CPM_DOWNLOAD_VERSION) - cpmaddpackage("gh:intel/compile-time-init-build#${CIB_VERSION}") - else() - include(FetchContent) - FetchContent_Declare( - cib - GIT_REPOSITORY https://github.com/intel/compile-time-init-build.git - GIT_TAG ${CIB_VERSION}) - FetchContent_MakeAvailable(cib) - endif() -endif() - -target_link_libraries(hello_world cib) diff --git a/examples/log/CMakeLists.txt b/examples/log/CMakeLists.txt new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/examples/log/CMakeLists.txt @@ -0,0 +1 @@ + diff --git a/examples/nexus/CMakeLists.txt b/examples/nexus/CMakeLists.txt new file mode 100644 index 00000000..4be950dc --- /dev/null +++ b/examples/nexus/CMakeLists.txt @@ -0,0 +1,2 @@ +add_subdirectory(hello_world) +add_subdirectory(serial_port) diff --git a/examples/nexus/hello_world/CMakeLists.txt b/examples/nexus/hello_world/CMakeLists.txt new file mode 100644 index 00000000..f5e0b4e0 --- /dev/null +++ b/examples/nexus/hello_world/CMakeLists.txt @@ -0,0 +1,39 @@ +cmake_minimum_required(VERSION 3.25) + +project(hello_world LANGUAGES CXX) + +if(NOT TARGET cib) + # to fetch cib, either use CPM (https://github.com/cpm-cmake/CPM.cmake) or + # use plain old CMake functionality + set(USE_CPM 1) + + set(CIB_VERSION "c388a4d") # update this to a more recent commit ID (or tag) + # for your project + + if(USE_CPM) + file( + DOWNLOAD + https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.42.0/CPM.cmake + ${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake + EXPECTED_HASH + SHA256=2020b4fc42dba44817983e06342e682ecfc3d2f484a581f11cc5731fbe4dce8a + ) + include(${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake) + + cpmaddpackage("gh:intel/compile-time-init-build#${CIB_VERSION}") + else() + include(FetchContent) + FetchContent_Declare( + cib + GIT_REPOSITORY https://github.com/intel/compile-time-init-build.git + GIT_TAG ${CIB_VERSION}) + FetchContent_MakeAvailable(cib) + endif() +endif() + +if(COMMAND add_example) + add_example(NAME hello_world FILES main.cpp dont_panic.cpp) +else() + add_executable(hello_world main.cpp dont_panic.cpp) + target_link_libraries(hello_world cib) +endif() diff --git a/examples/hello_world/README.md b/examples/nexus/hello_world/README.md similarity index 100% rename from examples/hello_world/README.md rename to examples/nexus/hello_world/README.md diff --git a/examples/hello_world/core.hpp b/examples/nexus/hello_world/core.hpp similarity index 100% rename from examples/hello_world/core.hpp rename to examples/nexus/hello_world/core.hpp diff --git a/examples/hello_world/dont_panic.cpp b/examples/nexus/hello_world/dont_panic.cpp similarity index 100% rename from examples/hello_world/dont_panic.cpp rename to examples/nexus/hello_world/dont_panic.cpp diff --git a/examples/hello_world/dont_panic.hpp b/examples/nexus/hello_world/dont_panic.hpp similarity index 100% rename from examples/hello_world/dont_panic.hpp rename to examples/nexus/hello_world/dont_panic.hpp diff --git a/examples/hello_world/hello_world.hpp b/examples/nexus/hello_world/hello_world.hpp similarity index 100% rename from examples/hello_world/hello_world.hpp rename to examples/nexus/hello_world/hello_world.hpp diff --git a/examples/hello_world/lazy_dog.hpp b/examples/nexus/hello_world/lazy_dog.hpp similarity index 100% rename from examples/hello_world/lazy_dog.hpp rename to examples/nexus/hello_world/lazy_dog.hpp diff --git a/examples/hello_world/main.cpp b/examples/nexus/hello_world/main.cpp similarity index 100% rename from examples/hello_world/main.cpp rename to examples/nexus/hello_world/main.cpp diff --git a/examples/hello_world/say_hello_world.hpp b/examples/nexus/hello_world/say_hello_world.hpp similarity index 100% rename from examples/hello_world/say_hello_world.hpp rename to examples/nexus/hello_world/say_hello_world.hpp diff --git a/examples/nexus/serial_port/CMakeLists.txt b/examples/nexus/serial_port/CMakeLists.txt new file mode 100644 index 00000000..e7a428e3 --- /dev/null +++ b/examples/nexus/serial_port/CMakeLists.txt @@ -0,0 +1,39 @@ +cmake_minimum_required(VERSION 3.25) + +project(serial_port LANGUAGES CXX) + +if(NOT TARGET cib) + # to fetch cib, either use CPM (https://github.com/cpm-cmake/CPM.cmake) or + # use plain old CMake functionality + set(USE_CPM 1) + + set(CIB_VERSION "c388a4d") # update this to a more recent commit ID (or tag) + # for your project + + if(USE_CPM) + file( + DOWNLOAD + https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.42.0/CPM.cmake + ${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake + EXPECTED_HASH + SHA256=2020b4fc42dba44817983e06342e682ecfc3d2f484a581f11cc5731fbe4dce8a + ) + include(${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake) + + cpmaddpackage("gh:intel/compile-time-init-build#${CIB_VERSION}") + else() + include(FetchContent) + FetchContent_Declare( + cib + GIT_REPOSITORY https://github.com/intel/compile-time-init-build.git + GIT_TAG ${CIB_VERSION}) + FetchContent_MakeAvailable(cib) + endif() +endif() + +if(COMMAND add_example) + add_example(NAME serial_port FILES main.cpp) +else() + add_executable(serial_port main.cpp) + target_link_libraries(serial_port cib) +endif() diff --git a/examples/cib_serial_port/main.cpp b/examples/nexus/serial_port/main.cpp similarity index 100% rename from examples/cib_serial_port/main.cpp rename to examples/nexus/serial_port/main.cpp