Skip to content

Commit 085d84d

Browse files
committed
feat: add ic_bundle()
1 parent 5ba2f9f commit 085d84d

17 files changed

Lines changed: 196 additions & 13 deletions

File tree

.githooks/pre-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
set -eo pipefail
99

1010
# Configure and run the tests
11-
cmake -B tests/.build-githook -S tests --log-level=NOTICE
11+
cmake -P tests/script.cmake

.github/workflows/test.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ jobs:
2323
git config --global user.email ic@n0t4d0m41n.1337
2424
git fetch --prune --unshallow --tags
2525
26-
- name: Configure & run tests
27-
run: cmake -B tests/build -S tests --log-level=NOTICE
26+
- name: Run the test script
27+
run: cmake -P tests/script.cmake

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
build
1+
build*

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Tests
44

55
```
6-
cmake -B tests/build -S tests --log-level=NOTICE
6+
cmake -P tests/script.cmake
77
```
88

99
# Contributions
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#
44
# Authors: J.P. Hutchins <jp@intercreate.io>
55

6+
include_guard(GLOBAL)
7+
68
include(FetchContent)
79

810
FetchContent_Declare(test_cmake

ic.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55

66
include_guard(GLOBAL)
77

8+
include(${CMAKE_CURRENT_LIST_DIR}/dependencies/fetch_test_cmake.cmake)
9+
10+
include(${CMAKE_CURRENT_LIST_DIR}/src/ic_bundle.cmake)
811
include(${CMAKE_CURRENT_LIST_DIR}/src/ic_project.cmake)
912
include(${CMAKE_CURRENT_LIST_DIR}/src/ic_utils.cmake)

src/ic_bundle.cmake

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Copyright (c) 2023 Intercreate, Inc.
2+
# SPDX-License-Identifier: Apache-2.0
3+
#
4+
# Authors: J.P. Hutchins <jp@intercreate.io>
5+
6+
include_guard(GLOBAL)
7+
8+
include(${CMAKE_CURRENT_LIST_DIR}/ic_utils.cmake)
9+
10+
function(_add_artifact out_artifact_list
11+
bundle_target bundle_dir prefix postfix name dependency
12+
)
13+
set(srcfile "${prefix}${postfix}")
14+
set(dstfile "${bundle_dir}/${name}${postfix}")
15+
add_custom_command(
16+
OUTPUT "${dstfile}"
17+
DEPENDS "${dependency}" "${srcfile}"
18+
COMMAND ${CMAKE_COMMAND} -E copy "${srcfile}" "${dstfile}"
19+
)
20+
list(APPEND ${out_artifact_list} "${dstfile}")
21+
set(${out_artifact_list} ${${out_artifact_list}} PARENT_SCOPE)
22+
endfunction()
23+
24+
#[[
25+
Add build targets that will create a distributable bundle of build artifacts.
26+
27+
Default artifacts:
28+
*.elf
29+
*.bin
30+
*.hex
31+
*.map
32+
33+
The bundle will be generated at ${BUNDLE_DIR}/${FULL_NAME} dir and zip.
34+
35+
FULL_NAME <name>
36+
The project configuration description string, e.g. ${IC_FULL_NAME}
37+
38+
[PREFIX <prefix>] default: ${PROJECT_NAME}
39+
The path prefix to the artifacts within the build folder. For Zephyr
40+
projects, this is probably `zephyr/zephyr`.
41+
42+
[EXTRA_POSTFIXES <postfix list>] default: [empty cmake list]
43+
A list of extra artifact prefixes to add, for example:
44+
EXTRA_POSTFIXES -merged.hex -app-signed.hex -app-update-s-e.gbl
45+
Would rename and copy targets like "zephyr-merged.hex",
46+
"zephyr-app-update-s-e.gbl", to "${FULL_NAME}-merged.hex"
47+
"${FULL_NAME}-app-update-s-e.gbl" etc.
48+
49+
[EXTRA_FILES <file paths list>] default: [empty cmake list]
50+
A list of paths to extra build artifacts that should be bundled, example:
51+
EXTRA_FILES "${CMAKE_BINARY_DIR}/bootloader/zephyr/zephyr.hex"
52+
53+
[DEPENDS <target>] default: ${PROJECT_NAME}
54+
The target to depend on for the bundle target. For Zephyr projects, this
55+
is probably `zephyr_final`.
56+
57+
[BUNDLE_DIR <path>] default: "${CMAKE_BINARY_DIR}/${_FULL_NAME}"
58+
The name for the created bundle folder and zip, e.g.
59+
"${CMAKE_SOURCE_DIR}/my_build/my_bundle_name"
60+
61+
]]#
62+
function(ic_bundle)
63+
set(required_keyword_args
64+
FULL_NAME
65+
)
66+
set(optional_keyword_args
67+
PREFIX
68+
EXTRA_FILES
69+
EXTRA_POSTFIXES
70+
DEPENDS
71+
BUNDLE_DIR
72+
)
73+
list(APPEND keyword_args
74+
${required_keyword_args}
75+
${optional_keyword_args}
76+
)
77+
cmake_parse_arguments(PARSE_ARGV 0 "" "" "${keyword_args}" "")
78+
79+
if(_UNPARSED_ARGUMENTS)
80+
message(FATAL_ERROR "Unparsed arguments: ${_UNPARSED_ARGUMENTS}")
81+
endif()
82+
83+
set_default(_PREFIX "${PROJECT_NAME}")
84+
set_default(_EXTRA_FILES "")
85+
set_default(_EXTRA_POSTFIXES "")
86+
set_default(_DEPENDS "${PROJECT_NAME}")
87+
set_default(_BUNDLE_DIR "${CMAKE_BINARY_DIR}/${_FULL_NAME}")
88+
89+
macro(add_artifact postfix)
90+
_add_artifact(artifact_list
91+
bundle
92+
"${_BUNDLE_DIR}"
93+
"${_PREFIX}"
94+
"${postfix}"
95+
"${_FULL_NAME}"
96+
"${_DEPENDS}"
97+
)
98+
endmacro()
99+
100+
add_artifact(.elf)
101+
add_artifact(.bin)
102+
add_artifact(.hex)
103+
add_artifact(.map)
104+
105+
foreach(extra_postfix ${_EXTRA_POSTFIXES})
106+
add_artifact("${extra_postfix}")
107+
endforeach()
108+
109+
add_custom_command(
110+
OUTPUT "${_FULL_NAME}.zip"
111+
COMMAND ${CMAKE_COMMAND} -E tar cf "${_FULL_NAME}.zip" "${_FULL_NAME}"
112+
DEPENDS ${artifact_list}
113+
)
114+
115+
add_custom_target(
116+
bundle
117+
ALL
118+
DEPENDS ${artifact_list} "${_FULL_NAME}.zip"
119+
)
120+
121+
endfunction()

src/ic_project.cmake

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function(ic_project
5252
out_c_compiler_version
5353
out_host_system_name
5454
out_host_system_version
55-
out_config_string
55+
out_full_name
5656
)
5757
set(required_keyword_args
5858
CLIENT_NAME
@@ -198,7 +198,7 @@ function(ic_project
198198
${_CLIENT_NAME}_${CMAKE_PROJECT_NAME}_${_BOARD_NAME}${board_rev_string}_${git_tag}_${build_date}${build_type_string}
199199
)
200200
string(REPLACE " " "_" config_string ${config_string})
201-
set_definition("${out_config_string}" ${config_string} PARENT_SCOPE)
201+
set_definition("${out_full_name}" ${config_string} PARENT_SCOPE)
202202

203203
endfunction()
204204

@@ -236,7 +236,7 @@ function(ic_project_out out_variable_name)
236236
IC_C_COMPILER_VERSION
237237
IC_HOST_SYSTEM_NAME
238238
IC_HOST_SYSTEM_VERSION
239-
IC_CONFIG_STRING
239+
IC_FULL_NAME
240240
)
241241
set(${out_variable_name} ${_IC_OUT_VARIABLES} PARENT_SCOPE)
242242
endfunction()

tests/CMakeLists.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
cmake_minimum_required(VERSION 3.20.0)
77
project(test_ic_project)
88

9-
include(${CMAKE_SOURCE_DIR}/../ic.cmake)
10-
11-
include(fetch_test_cmake.cmake)
9+
include(${CMAKE_CURRENT_LIST_DIR}/../dependencies/fetch_test_cmake.cmake)
1210

1311
test_cmake_begin()
1412

1513
include(test_ic_project.cmake)
14+
include(test_ic_bundle/minimal_args.cmake)
1615

1716
test_cmake_end()

tests/fixtures/ic_bundle/project.bin

Whitespace-only changes.

0 commit comments

Comments
 (0)