Skip to content

Commit 1822755

Browse files
committed
CMake function to invoke Zig build
1 parent f6721ec commit 1822755

File tree

4 files changed

+159
-34
lines changed

4 files changed

+159
-34
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ endif()
5858
#---------------------------------------------------------------------------
5959

6060
include(cmake/compile_assemble_link.cmake)
61+
include(cmake/BuildZig.cmake)
6162

6263
set(MOS_BIN_DIR ${CMAKE_BINARY_DIR}/bin)
6364
set(MOS_OBJ_DIR ${CMAKE_BINARY_DIR}/obj)

cmake/BuildZig.cmake

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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()

cmake/x86/userflags.cmake

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,27 @@ set(MOS_USER_GCC_INCLUDE_DIRS
1717

1818
set(MOS_USER_LINKER_SCRIPT_FILE ${PROJECT_SOURCE_DIR}/src/kernel/x86/process.ld)
1919
set(MOS_USER_APP_ENTRY_POINT proc_main)
20+
21+
# ----------------------------------------------------
22+
# Zig Compiler flags
23+
# ----------------------------------------------------
24+
set(MOS_ZIG_BUILD_OPTIONS
25+
--prominent-compile-errors
26+
)
27+
28+
if (MOS_BUILD_MODE STREQUAL "DEBUG")
29+
list(APPEND MOS_ZIG_BUILD_OPTIONS -Doptimize=Debug)
30+
else()
31+
list(APPEND MOS_ZIG_BUILD_OPTIONS -Doptimize=ReleaseSafe)
32+
endif()
33+
34+
# ----------------------------------------------------
35+
# Zig Compiler Definitions
36+
# ----------------------------------------------------
37+
set(MOS_ZIG_BUILD_REQUIRED_DEFINES
38+
-DLinkerScriptPath=${MOS_USER_LINKER_SCRIPT_FILE}
39+
-DLibCMPath=$<TARGET_FILE_DIR:cm>
40+
-DCRTPath=$<TARGET_OBJECTS:crta>
41+
-DCInludePath=${MOS_USER_GCC_INCLUDE_DIRS}
42+
-DEntryPoint=${MOS_USER_APP_ENTRY_POINT}
43+
)

src/apps/zapps/CMakeLists.txt

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,20 @@
11
find_package(Zig 0.15.1 EXACT REQUIRED)
22

3-
set(ZIG_BUILD_OPTIONS
4-
--prominent-compile-errors
5-
-Doptimize=ReleaseSafe
6-
)
7-
8-
set(ZIG_BUILD_DEFINES
9-
-DLinkerScriptPath=${MOS_USER_LINKER_SCRIPT_FILE}
10-
-DLibCMPath=$<TARGET_FILE_DIR:cm>
11-
-DCRTPath=$<TARGET_OBJECTS:crta>
12-
-DCInludePath=${MOS_USER_GCC_INCLUDE_DIRS}
13-
-DEntryPoint=${MOS_USER_APP_ENTRY_POINT}
14-
)
15-
16-
add_custom_command(
17-
OUTPUT
18-
${MOS_OBJ_DIR}/hello
19-
${MOS_BIN_DIR}/hello.flt
20-
BYPRODUCTS
21-
${CMAKE_CURRENT_LIST_DIR}/zig-out/bin/hello
22-
${CMAKE_CURRENT_LIST_DIR}/zig-out/hello.flt
23-
COMMAND ${ZIG_EXECUTABLE} build ${ZIG_BUILD_OPTIONS} ${ZIG_BUILD_DEFINES}
24-
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_LIST_DIR}/zig-out/bin/hello ${MOS_OBJ_DIR}
25-
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_LIST_DIR}/zig-out/bin/hello.flt ${MOS_BIN_DIR}
26-
DEPENDS
27-
${CMAKE_CURRENT_LIST_DIR}/hello.zig
28-
${CMAKE_CURRENT_LIST_DIR}/build.zig
29-
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
30-
)
31-
32-
add_custom_target(
33-
hello
3+
add_zig_build(
4+
NAME hello
5+
OUTPUTS hello
346
SOURCES
35-
${MOS_OBJ_DIR}/hello
36-
${MOS_BIN_DIR}/hello.flt
7+
${CMAKE_CURRENT_LIST_DIR}/build.zig
8+
${CMAKE_CURRENT_LIST_DIR}/MosBuild.zig
9+
${CMAKE_CURRENT_LIST_DIR}/hello.zig
10+
DEPENDS
11+
cm
12+
crta
13+
FLAGS ${MOS_ZIG_BUILD_OPTIONS}
14+
DEFINITIONS ${MOS_ZIG_BUILD_REQUIRED_DEFINES}
15+
ZIG_BUILD_BIN_DIR ${CMAKE_CURRENT_LIST_DIR}/zig-out/bin
16+
OUT_BIN_DIR ${MOS_BIN_DIR}
17+
OUT_OBJ_DIR ${MOS_OBJ_DIR}
3718
)
3819

39-
add_dependencies(hello cm crta)
4020
add_dependencies(build-all hello)

0 commit comments

Comments
 (0)