1#
2# Copyright 2020, Data61, CSIRO (ABN 41 687 119 230)
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6
7cmake_minimum_required(VERSION 3.7.2)
8
9project(libsel4 C)
10
11set(configure_string "")
12
13config_choice(
14    LibSel4FunctionAttributes
15    LIB_SEL4_FUNCTION_ATTRIBUTE
16    "Function attributes \
17    default->Verification friendly default configuration. syscalls will be inlined, \
18        but generated functions will not. \
19    inline->When set to true will mark generated functions as 'inline', allowing \
20        them to be inlined by the callee user code. This may be undesirable \
21        for verification, so setting to 'n' will forcibly prevent the function \
22        from being inlined. \
23    public->When set to true will make all user facing functions available as \
24        public symbols, which can be convenient for some language bindings."
25    "inline;LibSel4FunctionAttributeInline;LIB_SEL4_INLINE_INVOCATIONS"
26    "default;LibSel4FunctionAttributeDefault;LIB_SEL4_DEFAULT_FUNCTION_ATTRIBUTES"
27    "public;LibSel4FunctionAttributePublic;LIB_SEL4_PUBLIC_SYMBOLS"
28)
29
30config_option(
31    LibSel4StubsUseIPCBufferOnly LIB_SEL4_STUBS_USE_IPC_BUFFER_ONLY
32    "use only IPC buffer for syscalls. When generating syscall wrappers, only use the \
33    IPC buffer for marshalling and unmarshalling arguments. Without this option set, \
34    arguments will be passed in registers where possible for better performance."
35    DEFAULT OFF
36)
37
38config_string(
39    LibSel4PrintInvocationErrors LIB_SEL4_PRINT_INVOCATION_ERRORS
40    "Generated stubs on error will print the message contained in the IPC buffer"
41    DEFAULT 1
42    DEPENDS "KernelInvocationReportErrorIPC" DEFAULT_DISABLED 0
43    UNQUOTE
44)
45
46if(LibSel4StubsUseIPCBufferOnly)
47    set(buffer "--buffer")
48endif()
49
50RequireFile(SYSCALL_STUB_GEN_PATH syscall_stub_gen.py PATHS tools)
51
52add_config_library(sel4 "${configure_string}")
53
54# Currently we use autoconf.h, so generate one of those
55generate_autoconf(sel4_autoconf "kernel;sel4")
56
57gen_invocation_header(OUTPUT include/sel4/invocation.h XML include/interfaces/sel4.xml LIBSEL4)
58
59gen_invocation_header(
60    OUTPUT sel4_arch_include/${KernelSel4Arch}/sel4/sel4_arch/invocation.h
61    XML "${CMAKE_CURRENT_SOURCE_DIR}/sel4_arch_include/${KernelSel4Arch}/interfaces/sel4arch.xml"
62    LIBSEL4 SEL4ARCH
63)
64
65gen_invocation_header(
66    OUTPUT arch_include/${KernelArch}/sel4/arch/invocation.h
67    XML "${CMAKE_CURRENT_SOURCE_DIR}/arch_include/${KernelArch}/interfaces/sel4arch.xml"
68    LIBSEL4 ARCH
69)
70
71set(
72    source_header_dirs
73    "${CMAKE_CURRENT_SOURCE_DIR}/include"
74    "${CMAKE_CURRENT_SOURCE_DIR}/arch_include/${KernelArch}"
75    "${CMAKE_CURRENT_SOURCE_DIR}/sel4_arch_include/${KernelSel4Arch}"
76    "${CMAKE_CURRENT_SOURCE_DIR}/sel4_plat_include/${KernelPlatform}"
77    "${CMAKE_CURRENT_SOURCE_DIR}/mode_include/${KernelWordSize}"
78)
79include_directories("${source_header_dirs}")
80
81# Add the include directory of autoconf.h to the cflags for the bitfield generation
82include_directories("$<TARGET_PROPERTY:sel4_autoconf,INTERFACE_INCLUDE_DIRECTORIES>")
83
84function(genbf target_prefix pbf_location bf_location header_output)
85    get_generated_files(gen_list sel4_autoconf_Gen)
86    cppfile(
87        "${pbf_location}"
88        ${target_prefix}_pbf
89        "${bf_location}"
90        EXTRA_FLAGS
91        -P
92        EXTRA_DEPS
93        sel4_autoconf_Gen
94        ${gen_list}
95    )
96    GenHBFTarget(
97        "libsel4"
98        ${target_prefix}_h
99        "${header_output}"
100        "${pbf_location}"
101        ${target_prefix}_pbf
102        ""
103        ""
104    )
105endfunction(genbf)
106
107genbf(
108    "libsel4_shared_types_gen"
109    "${CMAKE_CURRENT_BINARY_DIR}/generated/shared_types_gen/shared_types.pbf"
110    "${CMAKE_CURRENT_SOURCE_DIR}/mode_include/${KernelWordSize}/sel4/shared_types.bf"
111    "${CMAKE_CURRENT_BINARY_DIR}/include/sel4/shared_types_gen.h"
112)
113
114genbf(
115    "libsel4_sel4_arch_types_gen"
116    "${CMAKE_CURRENT_BINARY_DIR}/generated/sel4_arch_shared_types/types.pbf"
117    "${CMAKE_CURRENT_SOURCE_DIR}/sel4_arch_include/${KernelSel4Arch}/sel4/sel4_arch/types.bf"
118    "${CMAKE_CURRENT_BINARY_DIR}/sel4_arch_include/${KernelSel4Arch}/sel4/sel4_arch/types_gen.h"
119)
120
121if(KernelIsMCS)
122    set(mcs --mcs)
123endif()
124add_custom_command(
125    OUTPUT include/sel4/syscall.h
126    COMMAND rm -f include/sel4/syscall.h
127    COMMAND
128        "${XMLLINT_PATH}"
129        --noout
130        --schema
131            "${CMAKE_CURRENT_SOURCE_DIR}/include/api/syscall.xsd"
132            "${CMAKE_CURRENT_SOURCE_DIR}/include/api/syscall.xml"
133    COMMAND
134        ${PYTHON3} "${SYSCALL_ID_GEN_PATH}"
135        --xml "${CMAKE_CURRENT_SOURCE_DIR}/include/api/syscall.xml"
136        --libsel4_header include/sel4/syscall.h ${mcs}
137    DEPENDS
138        "${SYSCALL_ID_GEN_PATH}" "${CMAKE_CURRENT_SOURCE_DIR}/include/api/syscall.xsd"
139        "${CMAKE_CURRENT_SOURCE_DIR}/include/api/syscall.xml"
140    COMMENT "Generate syscall.h"
141    VERBATIM
142)
143
144set(
145    interface_xmls
146    "${CMAKE_CURRENT_SOURCE_DIR}/sel4_arch_include/${KernelSel4Arch}/interfaces/sel4arch.xml"
147    "${CMAKE_CURRENT_SOURCE_DIR}/arch_include/${KernelArch}/interfaces/sel4arch.xml"
148    "${CMAKE_CURRENT_SOURCE_DIR}/include/interfaces/sel4.xml"
149)
150add_custom_command(
151    OUTPUT include/interfaces/sel4_client.h
152    COMMAND rm -f include/interfaces/sel4_client.h
153    COMMAND
154        echo "CONFIG_WORD_SIZE=${KernelWordSize}" > config
155    COMMAND
156        "${PYTHON3}" "${SYSCALL_STUB_GEN_PATH}" ${buffer} ${mcs} -a "${KernelSel4Arch}" -c config -o
157        include/interfaces/sel4_client.h ${interface_xmls}
158    DEPENDS
159        "${SYSCALL_STUB_GEN_PATH}"
160        ${interface_xmls}
161        BYPRODUCTS
162        config
163    COMMENT "Generate sel4_client.h"
164    VERBATIM
165)
166
167add_custom_target(
168    sel4_generated
169    DEPENDS
170        include/interfaces/sel4_client.h
171        include/sel4/syscall.h
172        include/sel4/invocation.h
173        arch_include/${KernelArch}/sel4/arch/invocation.h
174        include/sel4/shared_types_gen.h
175        sel4_arch_include/${KernelSel4Arch}/sel4/sel4_arch/invocation.h
176        sel4_arch_include/${KernelSel4Arch}/sel4/sel4_arch/types_gen.h
177)
178
179add_library(sel4 src/sel4_bootinfo.c)
180target_link_libraries(sel4 PRIVATE kernel_Config sel4_Config sel4_autoconf)
181target_include_directories(
182    sel4
183    PUBLIC
184        ${source_header_dirs}
185        "${CMAKE_CURRENT_BINARY_DIR}/include"
186        "${CMAKE_CURRENT_BINARY_DIR}/arch_include/${KernelArch}"
187        "${CMAKE_CURRENT_BINARY_DIR}/sel4_arch_include/${KernelSel4Arch}"
188)
189add_dependencies(sel4 sel4_generated)
190