Skip to content

Commit a79e221

Browse files
committed
Root step added to wrap application build steps
This makes it possible to build specific targets using 'zig build <name> ...' command. Also includes: * cmake: add_zig_build now passes a target name to `zig build`. Default target name is NAME arg, but can be overridden using ZIG_TARGET_NAME. * Refactoring changes to zapps/CMakeLists.txt.
1 parent 59b9295 commit a79e221

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

cmake/BuildZig.cmake

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ==================================================================================================
2-
# add_zig_build (NAME name
2+
# add_zig_build (
3+
# NAME name
4+
# [ZIG_TARGET_NAME <target name>]
35
# SOURCES <source> [<source> ...]
46
# OUTPUT <zig build output file> COPY_TO <copy output to this directory>
57
# [OUTPUT <zig build output file> COPY_TO <copy output to this directory>]
@@ -11,6 +13,14 @@
1113
# Invokes `zig build` command to build output artifacts. These are then copied to some destination
1214
# directory.
1315
#
16+
# NAME
17+
# Name of the CMake target that will be created.
18+
#
19+
# ZIG_TARGET_NAME
20+
# Overrides the default name of the Zig build target that gets passed to `zig build` command. This
21+
# can also be set to a blank string to invoke `zig build` without any target name. Default is same
22+
# as the NAME argument.
23+
#
1424
# OUTPUT
1525
# A file that the zig build generates. A matching `COPY_TO <dir>` will copy this file to `<dir>`
1626
# directory.
@@ -36,7 +46,7 @@
3646
# Changes to the provided directory before `zig build` is run.
3747
# ==================================================================================================
3848
function(add_zig_build)
39-
set(oneValueArgs NAME WORKING_DIRECTORY)
49+
set(oneValueArgs NAME ZIG_TARGET_NAME WORKING_DIRECTORY)
4050
set(multiValueArgs OUTPUT COPY_TO DEPENDS SOURCES FLAGS DEFINITIONS)
4151
set(options)
4252

@@ -73,12 +83,16 @@ function(add_zig_build)
7383
if (NOT ZBUILD_WORKING_DIRECTORY)
7484
set(ZBUILD_WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
7585
endif()
86+
87+
if (NOT ZBUILD_ZIG_TARGET_NAME)
88+
set(ZBUILD_ZIG_TARGET_NAME ${ZBUILD_NAME})
89+
endif()
7690
# -------------------------------------------------------------------------------------------
7791
# Command to build the executable and copy to destination folders
7892
# -------------------------------------------------------------------------------------------
7993
add_custom_command(
8094
OUTPUT ${ZBUILD_OUTPUT}
81-
COMMAND ${ZIG_EXECUTABLE} build ${ZBUILD_FLAGS} ${ZBUILD_DEFINITIONS}
95+
COMMAND ${ZIG_EXECUTABLE} build ${ZBUILD_ZIG_TARGET_NAME} ${ZBUILD_FLAGS} ${ZBUILD_DEFINITIONS}
8296
DEPENDS ${ZBUILD_SOURCES}
8397
WORKING_DIRECTORY ${ZBUILD_WORKING_DIRECTORY}
8498
)

src/apps/zapps/CMakeLists.txt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
find_package(Zig 0.15.1 EXACT REQUIRED)
22

3-
set(ZIG_COMMON_SOURCE_FILES
3+
set(zig_common_source_files
44
${CMAKE_CURRENT_LIST_DIR}/mos_build.zig
55
)
66

7+
set(source_dir ${CMAKE_CURRENT_LIST_DIR}/hello)
8+
set(name hello)
79
add_zig_build(
8-
NAME hello
10+
NAME ${name}
911
SOURCES
10-
${ZIG_COMMON_SOURCE_FILES}
11-
${CMAKE_CURRENT_LIST_DIR}/hello/main.zig
12-
${CMAKE_CURRENT_LIST_DIR}/hello/build.zig
12+
${zig_common_source_files}
13+
${source_dir}/main.zig
14+
${source_dir}/build.zig
1315
DEPENDS
1416
cm
1517
crta
1618
FLAGS ${MOS_ZIG_BUILD_OPTIONS}
1719
DEFINITIONS ${MOS_ZIG_BUILD_REQUIRED_DEFINES}
18-
OUTPUT ${CMAKE_CURRENT_LIST_DIR}/hello/zig-out/bin/hello COPY_TO ${MOS_OBJ_DIR}
19-
OUTPUT ${CMAKE_CURRENT_LIST_DIR}/hello/zig-out/bin/hello.flt COPY_TO ${MOS_BIN_DIR}
20-
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/hello
20+
OUTPUT ${source_dir}/zig-out/bin/${name} COPY_TO ${MOS_OBJ_DIR}
21+
OUTPUT ${source_dir}/zig-out/bin/${name}.flt COPY_TO ${MOS_BIN_DIR}
22+
WORKING_DIRECTORY ${source_dir}
2123
)
2224

2325
add_dependencies(build-all hello)

src/apps/zapps/mos_build.zig

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,26 @@ pub fn addDefaultOptions(b: *Build) !BuildOptions {
6868
}
6969

7070
pub fn addExecutable(b: *Build, comptime name: []const u8, options: ExecutableOptions) *Step {
71+
// 1. Compilation and installation of the generated binary
7172
const exe = elf_compilation(b, name, &options);
7273
const exe_install = b.addInstallArtifact(exe, .{});
7374

74-
const flatten_output_file = name ++ ".flt";
75+
// 2. Run objcopy on the compiler output (generated from above step) and install the output into
76+
// the 'bin' folder.
7577
const objcopy_run = b.addSystemCommand(&.{ "objcopy", "-O", "binary" });
78+
objcopy_run.step.dependOn(&exe_install.step);
79+
80+
const flatten_output_file = name ++ ".flt";
7681
objcopy_run.addArg(b.getInstallPath(.bin, exe.name));
7782
const flatten_output_path = objcopy_run.addOutputFileArg(flatten_output_file);
7883

7984
const flatten_install = b.addInstallFileWithDir(flatten_output_path, .bin, flatten_output_file);
8085

81-
objcopy_run.step.dependOn(&exe_install.step);
82-
return &flatten_install.step;
86+
// 3. A Root target for the above steps. This was added so that 'zig build <name>' can be used
87+
// to build one particular target.
88+
const root = b.step(name, "Builds '" ++ name ++ "' target.");
89+
root.dependOn(&flatten_install.step);
90+
return root;
8391
}
8492

8593
fn elf_compilation(b: *Build, comptime name: []const u8, options: *const ExecutableOptions) *Step.Compile {

0 commit comments

Comments
 (0)