|
| 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() |
0 commit comments