11# ==================================================================================================
22# 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> ...]
73# SOURCES <source> [<source> ...]
4+ # OUTPUT <zig build output file> COPY_TO <copy output to this directory>
5+ # [OUTPUT <zig build output file> COPY_TO <copy output to this directory>]
86# [DEPENDS <target> ...]
97# [FLAGS <flag> ...]
10- # [DEFINITIONS <compiler macros> ...])
8+ # [DEFINITIONS <compiler macros> ...]
9+ # [WORKING_DIRECTORY <dir>])
1110#
12- # Invokes Zig build command build of output artifacts. These can be copied to some destination path.
11+ # Invokes `zig build` command to build output artifacts. These are then copied to some destination
12+ # directory.
1313#
14- # ZIG_BUILD_BIN_DIR
15- # Directory where zig builds the binaries.
14+ # OUTPUT
15+ # A file that the zig build generates. A matching `COPY_TO <dir>` will copy this file to `<dir>`
16+ # directory.
1617#
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.
18+ # COPY_TO
19+ # Destination directory where the corresponding `OUTPUT` <file> will be copied to. Since each OUTPUT
20+ # and COPY_TO arguments are linked, their count must match.
2221#
2322# SOURCES
2423# Zig source files which this build depends on. Zig build is triggered if any of these files is
3231#
3332# DEFINITIONS
3433# Definitions/macros which are passed to the compiler.
34+ #
35+ # WORKING_DIRECTORY
36+ # Changes to the provided directory before `zig build` is run.
3537# ==================================================================================================
3638function (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 (oneValueArgs NAME WORKING_DIRECTORY )
40+ set (multiValueArgs OUTPUT COPY_TO DEPENDS SOURCES FLAGS DEFINITIONS )
3941 set (options )
4042
4143 cmake_parse_arguments (PARSE_ARGV 0 ZBUILD "${options} " "${oneValueArgs} " "${multiValueArgs} " )
@@ -51,64 +53,56 @@ function(add_zig_build)
5153 message (FATAL_ERROR "Name must be given." )
5254 endif ()
5355
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-
6656 list (LENGTH ZBUILD_SOURCES SOURCE_LEN)
6757 if (NOT SOURCE_LEN)
6858 message (FATAL_ERROR "There must be at least one source file." )
6959 endif ()
7060
71- list (LENGTH ZBUILD_OUTPUTS OUTPUTS_LEN )
72- if (NOT OUTPUTS_LEN )
61+ list (LENGTH ZBUILD_OUTPUT OUTPUT_LEN )
62+ if (NOT OUTPUT_LEN )
7363 message (FATAL_ERROR "There must be at least one output file." )
7464 endif ()
7565
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 ()
66+ list (LENGTH ZBUILD_COPY_TO COPY_TO_LEN)
67+ if (NOT COPY_TO_LEN EQUAL OUTPUT_LEN)
68+ message (
69+ FATAL_ERROR
70+ "There must be as many COPY_TO arguments as there are OUTPUT arguments." )
71+ endif ()
8572
73+ if (NOT ZBUILD_WORKING_DIRECTORY)
74+ set (ZBUILD_WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} )
75+ endif ()
8676 # -------------------------------------------------------------------------------------------
87- # Command ot build the executable and copy to destination folders
77+ # Command to build the executable and copy to destination folders
8878 # -------------------------------------------------------------------------------------------
8979 add_custom_command (
90- OUTPUT
91- ${DEST_OBJS}
92- ${DEST_FLTS}
93- BYPRODUCTS
94- ${SRC_OBJS}
95- ${SRC_FLTS}
80+ OUTPUT ${ZBUILD_OUTPUT}
9681 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}
9982 DEPENDS ${ZBUILD_SOURCES}
100- WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR }
83+ WORKING_DIRECTORY ${ZBUILD_WORKING_DIRECTORY }
10184 )
10285
86+ foreach (path IN ZIP_LISTS ZBUILD_OUTPUT ZBUILD_COPY_TO)
87+ set (src_output_file ${path_0} )
88+ set (dest_output_dir ${path_1} )
89+
90+ get_filename_component (src_output_filetitle ${src_output_file} NAME )
91+ set (dest_output_file ${dest_output_dir} /${src_output_filetitle} )
92+
93+ list (APPEND DESTINATION_OUTPUT_FILES ${dest_output_file} )
94+
95+ add_custom_command (
96+ OUTPUT ${dest_output_file}
97+ COMMAND ${CMAKE_COMMAND} -E copy ${src_output_file} ${dest_output_file}
98+ DEPENDS ${src_output_file}
99+ )
100+ endforeach ()
101+
103102 # -------------------------------------------------------------------------------------------
104103 # Custom target for other targets to depend on
105104 # -------------------------------------------------------------------------------------------
106- add_custom_target (
107- ${ZBUILD_NAME}
108- SOURCES
109- ${DEST_OBJS}
110- ${DEST_FLTS}
111- )
105+ add_custom_target (${ZBUILD_NAME} SOURCES ${DESTINATION_OUTPUT_FILES} )
112106
113107 # -------------------------------------------------------------------------------------------
114108 # Add dependencies. So that they get build first
0 commit comments