|
| 1 | +# ================================================================================================== |
| 2 | +# add_zig_build (NAME name |
| 3 | +# ZIG_BUILD_BIN_DIR <path> |
| 4 | +# OUT_BIN_DIR <path> |
| 5 | +# OUT_OBJ_DIR <path> |
| 6 | +# OUTPUTS <output> [<outputs> ...] |
| 7 | +# SOURCES <source> [<source> ...] |
| 8 | +# [DEPENDS <target> ...] |
| 9 | +# [FLAGS <flag> ...] |
| 10 | +# [DEFINITIONS <compiler macros> ...]) |
| 11 | +# |
| 12 | +# Invokes Zig build command build of output artifacts. These can be copied to some destination path. |
| 13 | +# |
| 14 | +# ZIG_BUILD_BIN_DIR |
| 15 | +# Directory where zig builds the binaries. |
| 16 | +# |
| 17 | +# OUT_BIN_DIR |
| 18 | +# Destination directory where the FLT output file will be copied to. |
| 19 | +# |
| 20 | +# OUT_OBJ_DIR |
| 21 | +# Destination directory where the ELF output file will be copied to. |
| 22 | +# |
| 23 | +# SOURCES |
| 24 | +# Zig source files which this build depends on. Zig build is triggered if any of these files is |
| 25 | +# latest. |
| 26 | +# |
| 27 | +# DEPENDS |
| 28 | +# Target which must be build before compiling the source files. |
| 29 | +# |
| 30 | +# FLAGS |
| 31 | +# Compiler flags/options. |
| 32 | +# |
| 33 | +# DEFINITIONS |
| 34 | +# Definitions/macros which are passed to the compiler. |
| 35 | +# ================================================================================================== |
| 36 | +function(add_zig_build) |
| 37 | + set(oneValueArgs NAME ZIG_BUILD_BIN_DIR OUT_BIN_DIR OUT_OBJ_DIR) |
| 38 | + set(multiValueArgs OUTPUTS DEPENDS SOURCES FLAGS DEFINITIONS) |
| 39 | + set(options) |
| 40 | + |
| 41 | + cmake_parse_arguments(PARSE_ARGV 0 ZBUILD "${options}" "${oneValueArgs}" "${multiValueArgs}") |
| 42 | + |
| 43 | + # ------------------------------------------------------------------------------------------- |
| 44 | + # Check validity |
| 45 | + # ------------------------------------------------------------------------------------------- |
| 46 | + if (ZBUILD_UNPARSED_ARGUMENTS) |
| 47 | + message(FATAL_ERROR "Invalid option: ${ZBUILD_UNPARSED_ARGUMENTS}") |
| 48 | + endif() |
| 49 | + |
| 50 | + if (NOT ZBUILD_NAME) |
| 51 | + message(FATAL_ERROR "Name must be given.") |
| 52 | + endif() |
| 53 | + |
| 54 | + if (NOT ZBUILD_ZIG_BUILD_BIN_DIR) |
| 55 | + message(FATAL_ERROR "Output path for Zig build must be given.") |
| 56 | + endif() |
| 57 | + |
| 58 | + if (NOT ZBUILD_OUT_BIN_DIR) |
| 59 | + message(FATAL_ERROR "Destination path where .flt files will get copied to must be given.") |
| 60 | + endif() |
| 61 | + |
| 62 | + if (NOT ZBUILD_OUT_OBJ_DIR) |
| 63 | + message(FATAL_ERROR "Destination path where ELF files will get copied to must be given.") |
| 64 | + endif() |
| 65 | + |
| 66 | + list(LENGTH ZBUILD_SOURCES SOURCE_LEN) |
| 67 | + if (NOT SOURCE_LEN) |
| 68 | + message(FATAL_ERROR "There must be at least one source file.") |
| 69 | + endif() |
| 70 | + |
| 71 | + list(LENGTH ZBUILD_OUTPUTS OUTPUTS_LEN) |
| 72 | + if (NOT OUTPUTS_LEN) |
| 73 | + message(FATAL_ERROR "There must be at least one output file.") |
| 74 | + endif() |
| 75 | + |
| 76 | + # ------------------------------------------------------------------------------------------- |
| 77 | + # Build the output list of destination files and source binary files |
| 78 | + # ------------------------------------------------------------------------------------------- |
| 79 | + foreach(output IN LISTS ZBUILD_OUTPUTS) |
| 80 | + list(APPEND DEST_OBJS ${ZBUILD_OUT_OBJ_DIR}/${output}) |
| 81 | + list(APPEND DEST_FLTS ${ZBUILD_OUT_BIN_DIR}/${output}.flt) |
| 82 | + list(APPEND SRC_OBJS ${ZBUILD_ZIG_BUILD_BIN_DIR}/${output}) |
| 83 | + list(APPEND SRC_FLTS ${ZBUILD_ZIG_BUILD_BIN_DIR}/${output}.flt) |
| 84 | + endforeach() |
| 85 | + |
| 86 | + # ------------------------------------------------------------------------------------------- |
| 87 | + # Command ot build the executable and copy to destination folders |
| 88 | + # ------------------------------------------------------------------------------------------- |
| 89 | + add_custom_command( |
| 90 | + OUTPUT |
| 91 | + ${DEST_OBJS} |
| 92 | + ${DEST_FLTS} |
| 93 | + BYPRODUCTS |
| 94 | + ${SRC_OBJS} |
| 95 | + ${SRC_FLTS} |
| 96 | + COMMAND ${ZIG_EXECUTABLE} build ${ZBUILD_FLAGS} ${ZBUILD_DEFINITIONS} |
| 97 | + COMMAND ${CMAKE_COMMAND} -E copy ${SRC_OBJS} ${ZBUILD_OUT_OBJ_DIR} |
| 98 | + COMMAND ${CMAKE_COMMAND} -E copy ${SRC_FLTS} ${ZBUILD_OUT_BIN_DIR} |
| 99 | + DEPENDS ${ZBUILD_SOURCES} |
| 100 | + WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} |
| 101 | + ) |
| 102 | + |
| 103 | + # ------------------------------------------------------------------------------------------- |
| 104 | + # Custom target for other targets to depend on |
| 105 | + # ------------------------------------------------------------------------------------------- |
| 106 | + add_custom_target( |
| 107 | + ${ZBUILD_NAME} |
| 108 | + SOURCES |
| 109 | + ${DEST_OBJS} |
| 110 | + ${DEST_FLTS} |
| 111 | + ) |
| 112 | + |
| 113 | + # ------------------------------------------------------------------------------------------- |
| 114 | + # Add dependencies. So that they get build first |
| 115 | + # ------------------------------------------------------------------------------------------- |
| 116 | + if (ZBUILD_DEPENDS) |
| 117 | + add_dependencies(${ZBUILD_NAME} ${ZBUILD_DEPENDS}) |
| 118 | + endif() |
| 119 | + |
| 120 | +endfunction() |
0 commit comments