1#
2# Copyright 2019, Data61
3# Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4# ABN 41 687 119 230.
5#
6# This software may be distributed and modified according to the terms of
7# the BSD 2-Clause license. Note that NO WARRANTY is provided.
8# See "LICENSE_BSD2.txt" for details.
9#
10# @TAG(DATA61_BSD)
11#
12# Compile CRT for given arch and get crt0.o, crti.o, and crtn.o.
13# Add crt0.o and crti.o to the start of all targets link flags.
14# Add crtn.o to the end of all targets link flags.
15
16# Compile sel4runtime library from src.
17# Use src as private includes for crt and sel4runtime.
18# Use include as public includes for crt and sel4runtime.
19
20cmake_minimum_required(VERSION 3.12.0)
21
22project(sel4runtime C)
23
24set(configure_string "")
25config_string(
26    Sel4RuntimeRootStack
27    SEL4RUNTIME_ROOT_STACK
28    "Size of the initial stack for the root task"
29    DEFAULT
30    16384
31    UNQUOTE
32)
33config_string(
34    Sel4RuntimeStaticTLS
35    SEL4RUNTIME_STATIC_TLS
36    "Size of static TLS area for new processes"
37    DEFAULT
38    16384
39    UNQUOTE
40)
41add_config_library(sel4runtime "${configure_string}")
42
43list(APPEND crt_files crt/sel4_arch/${KernelSel4Arch}/crti.S crt/sel4_arch/${KernelSel4Arch}/crtn.S)
44if("${KernelArch}" STREQUAL "riscv")
45    list(APPEND crt_files crt/arch/${KernelArch}/crt0.S crt/arch/${KernelArch}/sel4_crt0.S)
46else()
47    list(
48        APPEND
49            crt_files crt/sel4_arch/${KernelSel4Arch}/crt0.S
50            crt/sel4_arch/${KernelSel4Arch}/sel4_crt0.S
51    )
52endif()
53add_library(sel4runtime_crt_obj OBJECT ${crt_files})
54target_include_directories(
55    sel4runtime_crt_obj
56    PRIVATE
57        src
58        src/mode/${KernelWordSize}
59        src/arch/${KernelArch}
60        src/sel4_arch/${KernelSel4Arch}
61    PUBLIC include
62)
63target_link_libraries(sel4runtime_crt_obj sel4_autoconf sel4runtime_Config)
64add_custom_command(
65    OUTPUT
66        "${CMAKE_BINARY_DIR}/lib/crt0.o" "${CMAKE_BINARY_DIR}/lib/crti.o"
67        "${CMAKE_BINARY_DIR}/lib/crtn.o"
68    DEPENDS sel4runtime_crt_obj
69    COMMAND
70        ${CMAKE_COMMAND} -E env cp "$<TARGET_OBJECTS:sel4runtime_crt_obj>" ${CMAKE_BINARY_DIR}/lib/
71    COMMAND
72        ${CMAKE_COMMAND} -E env mv ${CMAKE_BINARY_DIR}/lib/crt0.S${CMAKE_C_OUTPUT_EXTENSION}
73        ${CMAKE_BINARY_DIR}/lib/crt0.o
74    COMMAND
75        ${CMAKE_COMMAND} -E env mv ${CMAKE_BINARY_DIR}/lib/crti.S${CMAKE_C_OUTPUT_EXTENSION}
76        ${CMAKE_BINARY_DIR}/lib/crti.o
77    COMMAND
78        ${CMAKE_COMMAND} -E env mv ${CMAKE_BINARY_DIR}/lib/crtn.S${CMAKE_C_OUTPUT_EXTENSION}
79        ${CMAKE_BINARY_DIR}/lib/crtn.o COMMAND_EXPAND_LISTS
80)
81add_custom_target(
82    sel4runtime_crt
83    DEPENDS
84        "${CMAKE_BINARY_DIR}/lib/crt0.o" "${CMAKE_BINARY_DIR}/lib/crti.o"
85        "${CMAKE_BINARY_DIR}/lib/crtn.o"
86)
87
88list(
89    APPEND
90        sources
91        src/crt1.c
92        src/start.c
93        src/start_root.c
94        src/env.c
95        src/init.c
96        src/memset.c
97        src/memcpy.c
98        src/vsyscall.c
99)
100if(("${KernelSel4Arch}" STREQUAL "aarch32") OR ("${KernelSel4Arch}" STREQUAL "arm_hyp"))
101    list(
102        APPEND
103            sources src/sel4_arch/${KernelSel4Arch}/__aeabi_read_tp.s
104            src/sel4_arch/${KernelSel4Arch}/__aeabi_read_tp_c.c
105    )
106endif()
107
108# The sel4runtime library.
109add_library(sel4runtime STATIC ${crt_files} ${sources})
110target_include_directories(
111    sel4runtime
112    PRIVATE
113        src
114        src/mode/${KernelWordSize}
115        src/arch/${KernelArch}
116        src/sel4_arch/${KernelSel4Arch}
117    PUBLIC
118        include
119        include/mode/${KernelWordSize}
120        include/arch/${KernelArch}
121        include/sel4_arch/${KernelSel4Arch}
122)
123add_dependencies(sel4runtime sel4runtime_crt)
124
125# A C library is still needed here to provide memcpy, stdint.h, and
126# elf.h
127target_link_libraries(sel4runtime PUBLIC sel4 sel4_autoconf PRIVATE sel4runtime_Config)
128