Skip to content

Commit b2b5d3c

Browse files
committed
📝 🎨 👷 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.
1 parent bff154e commit b2b5d3c

File tree

22 files changed

+200
-82
lines changed

22 files changed

+200
-82
lines changed

.github/workflows/examples.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Examples
2+
permissions: read-all
3+
4+
on:
5+
workflow_dispatch:
6+
merge_group:
7+
push:
8+
branches: [ main ]
9+
pull_request:
10+
branches: [ main ]
11+
12+
env:
13+
DEBIAN_FRONTEND: noninteractive
14+
CMAKE_GENERATOR: Ninja
15+
DEFAULT_CXX_STANDARD: 20
16+
DEFAULT_LLVM_VERSION: 21
17+
18+
concurrency:
19+
group: ${{ github.head_ref || github.run_id }}
20+
cancel-in-progress: true
21+
22+
jobs:
23+
examples:
24+
runs-on: ${{ github.repository_owner == 'intel' && 'intel-' || '' }}ubuntu-24.04
25+
steps:
26+
- name: Checkout PR branch
27+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
28+
29+
- name: Install build tools
30+
run: |
31+
wget https://apt.llvm.org/llvm.sh && chmod +x llvm.sh && sudo ./llvm.sh ${{env.DEFAULT_LLVM_VERSION}}
32+
33+
- name: Restore CPM cache
34+
env:
35+
cache-name: cpm-cache-0
36+
id: cpm-cache-restore
37+
uses: actions/cache/restore@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
38+
with:
39+
path: ~/cpm-cache
40+
key: ${{runner.os}}-${{env.cache-name}}-${{ hashFiles('**/CMakeLists.txt', 'cmake/**') }}
41+
restore-keys: |
42+
${{runner.os}}-${{env.cache-name}}-
43+
44+
- name: Configure CMake
45+
env:
46+
CC: "/usr/lib/llvm-${{env.DEFAULT_LLVM_VERSION}}/bin/clang"
47+
CXX: "/usr/lib/llvm-${{env.DEFAULT_LLVM_VERSION}}/bin/clang++"
48+
PR_TARGET_BRANCH: ${{ steps.target_branch.outputs.branch }}
49+
run: cmake -B ${{github.workspace}}/build -DCMAKE_CXX_STANDARD=${{env.DEFAULT_CXX_STANDARD}} -DCPM_SOURCE_CACHE=~/cpm-cache
50+
51+
- name: Save CPM cache
52+
env:
53+
cache-name: cpm-cache-0
54+
if: steps.cpm-cache-restore.outputs.cache-hit != 'true'
55+
uses: actions/cache/save@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
56+
with:
57+
path: ~/cpm-cache
58+
key: ${{runner.os}}-${{env.cache-name}}-${{ hashFiles('**/CMakeLists.txt', 'cmake/**') }}
59+
60+
- name: Build examples
61+
run: cmake --build ${{github.workspace}}/build -t examples

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/build
1+
build/
22
/cmake-build-*
33
/venv
44
/.vscode

examples/CMakeLists.txt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1-
add_subdirectory(hello_world)
2-
add_subdirectory(cib_serial_port)
3-
add_subdirectory(flow_daily_routine)
1+
add_custom_target(examples)
2+
3+
function(add_example)
4+
set(singleValueArgs NAME)
5+
set(multiValueArgs FILES)
6+
cmake_parse_arguments(EX "" "${singleValueArgs}" "${multiValueArgs}"
7+
${ARGN})
8+
string(PREPEND EX_NAME "EXAMPLE.")
9+
10+
add_executable(${EX_NAME} ${EX_FILES})
11+
target_link_libraries(${EX_NAME} cib)
12+
add_dependencies(examples ${EX_NAME})
13+
endfunction()
14+
15+
add_subdirectory(flow)
16+
add_subdirectory(log)
17+
add_subdirectory(nexus)

examples/cib_serial_port/CMakeLists.txt

Lines changed: 0 additions & 26 deletions
This file was deleted.

examples/flow/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(daily_routine)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
cmake_minimum_required(VERSION 3.25)
2+
3+
project(daily_routine LANGUAGES CXX)
4+
5+
if(NOT TARGET cib)
6+
# to fetch cib, either use CPM (https://github.com/cpm-cmake/CPM.cmake) or
7+
# use plain old CMake functionality
8+
set(USE_CPM 1)
9+
10+
set(CIB_VERSION "c388a4d") # update this to a more recent commit ID (or tag)
11+
# for your project
12+
13+
if(USE_CPM)
14+
file(
15+
DOWNLOAD
16+
https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.42.0/CPM.cmake
17+
${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake
18+
EXPECTED_HASH
19+
SHA256=2020b4fc42dba44817983e06342e682ecfc3d2f484a581f11cc5731fbe4dce8a
20+
)
21+
include(${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake)
22+
23+
cpmaddpackage("gh:intel/compile-time-init-build#${CIB_VERSION}")
24+
else()
25+
include(FetchContent)
26+
FetchContent_Declare(
27+
cib
28+
GIT_REPOSITORY https://github.com/intel/compile-time-init-build.git
29+
GIT_TAG ${CIB_VERSION})
30+
FetchContent_MakeAvailable(cib)
31+
endif()
32+
endif()
33+
34+
if(COMMAND add_example)
35+
add_example(NAME daily_routine FILES main.cpp)
36+
else()
37+
add_executable(daily_routine main.cpp)
38+
target_link_libraries(daily_routine cib)
39+
endif()
File renamed without changes.

examples/flow_daily_routine/CMakeLists.txt

Lines changed: 0 additions & 26 deletions
This file was deleted.

examples/hello_world/CMakeLists.txt

Lines changed: 0 additions & 26 deletions
This file was deleted.

examples/log/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)