1# SPDX-License-Identifier: Apache-2.0
2
3# kernel is a normal CMake library and not a zephyr_library because it
4# should not be --whole-archive'd
5
6zephyr_syscall_header(
7  ${ZEPHYR_BASE}/include/zephyr/device.h
8  ${ZEPHYR_BASE}/include/zephyr/kernel.h
9  ${ZEPHYR_BASE}/include/zephyr/sys/kobject.h
10  ${ZEPHYR_BASE}/include/zephyr/sys/time_units.h
11)
12
13if(NOT CONFIG_ERRNO_IN_TLS AND NOT CONFIG_LIBC_ERRNO)
14  zephyr_syscall_header(${ZEPHYR_BASE}/include/zephyr/sys/errno_private.h)
15endif()
16
17zephyr_syscall_header_ifdef(
18  CONFIG_ATOMIC_OPERATIONS_C
19  ${ZEPHYR_BASE}/include/zephyr/sys/atomic_c.h
20)
21
22zephyr_syscall_header_ifdef(
23  CONFIG_MMU
24  ${ZEPHYR_BASE}/include/zephyr/sys/mem_manage.h
25)
26
27# If a pre-built static library containing kernel code exists in
28# this directory, libkernel.a, link it with the application code
29# instead of building from source.
30zephyr_library_get_current_dir_lib_name(${ZEPHYR_BASE} libkernel_stem)
31set(libkernel ${CMAKE_CURRENT_SOURCE_DIR}/lib${libkernel_stem}${CMAKE_STATIC_LIBRARY_SUFFIX})
32unset(libkernel_stem)
33
34if(EXISTS ${libkernel})
35
36add_library(kernel INTERFACE)
37target_link_libraries(kernel INTERFACE ${libkernel})
38
39else()
40
41# FIXME: SHADOW_VARS: Remove this once we have enabled -Wshadow globally.
42add_compile_options($<TARGET_PROPERTY:compiler,warning_shadow_variables>)
43
44list(APPEND kernel_files
45  main_weak.c
46  banner.c
47  busy_wait.c
48  device.c
49  errno.c
50  fatal.c
51  init.c
52  kheap.c
53  mem_slab.c
54  thread.c
55  version.c
56  )
57
58if(CONFIG_MULTITHREADING)
59list(APPEND kernel_files
60  idle.c
61  mailbox.c
62  msg_q.c
63  mutex.c
64  queue.c
65  sem.c
66  stack.c
67  system_work_q.c
68  work.c
69  sched.c
70  condvar.c
71  )
72
73if(CONFIG_SMP)
74list(APPEND kernel_files
75     smp.c)
76endif()
77
78endif()
79
80if(CONFIG_XIP)
81list(APPEND kernel_files
82     xip.c)
83endif()
84
85if(CONFIG_DEMAND_PAGING_STATS)
86list(APPEND kernel_files
87     paging/statistics.c)
88endif()
89
90add_library(kernel ${kernel_files})
91
92# Kernel files has the macro __ZEPHYR_SUPERVISOR__ set so that it
93# optimizes the code when userspace is enabled.
94
95set_target_properties(
96  kernel
97  PROPERTIES
98  COMPILE_DEFINITIONS
99  __ZEPHYR_SUPERVISOR__
100  )
101
102target_sources_ifdef(CONFIG_STACK_CANARIES        kernel PRIVATE compiler_stack_protect.c)
103target_sources_ifdef(CONFIG_SYS_CLOCK_EXISTS      kernel PRIVATE timeout.c timer.c)
104target_sources_ifdef(CONFIG_ATOMIC_OPERATIONS_C   kernel PRIVATE atomic_c.c)
105target_sources_ifdef(CONFIG_MMU                   kernel PRIVATE mmu.c)
106target_sources_ifdef(CONFIG_POLL                  kernel PRIVATE poll.c)
107target_sources_ifdef(CONFIG_EVENTS                kernel PRIVATE events.c)
108target_sources_ifdef(CONFIG_PIPES                 kernel PRIVATE pipes.c)
109target_sources_ifdef(CONFIG_SCHED_THREAD_USAGE    kernel PRIVATE usage.c)
110target_sources_ifdef(CONFIG_OBJ_CORE              kernel PRIVATE obj_core.c)
111
112if(${CONFIG_KERNEL_MEM_POOL})
113  target_sources(kernel PRIVATE mempool.c)
114endif()
115
116
117# The last 2 files inside the target_sources_ifdef should be
118# userspace_handler.c and userspace.c. If not the linker would complain.
119# This order has to be maintained. Any new file should be placed
120# above these 2 files.
121target_sources_ifdef(
122  CONFIG_USERSPACE
123  kernel PRIVATE
124  futex.c
125  mem_domain.c
126  userspace_handler.c
127  userspace.c
128  )
129
130if(${CONFIG_DYNAMIC_THREAD})
131  target_sources(kernel PRIVATE dynamic.c)
132else()
133  target_sources(kernel PRIVATE dynamic_disabled.c)
134endif()
135
136target_include_directories(kernel PRIVATE
137  ${ZEPHYR_BASE}/kernel/include
138  ${ARCH_DIR}/${ARCH}/include
139  )
140
141target_link_libraries(kernel zephyr_interface)
142
143endif()
144
145add_dependencies(kernel zephyr_generated_headers)
146
147unset(libkernel)
148