1# SPDX-License-Identifier: Apache-2.0
2
3# kernel is a normal CMake library and not a zephyr_library because it
4# should usually 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
13# Helper macro to append files to the kernel_files list
14macro(kernel_sources_ifdef feature_toggle)
15  if(${${feature_toggle}})
16    list(APPEND kernel_files ${ARGN})
17  endif()
18endmacro()
19
20macro(kernel_sources)
21    list(APPEND kernel_files ${ARGN})
22endmacro()
23
24if(NOT CONFIG_ERRNO_IN_TLS AND NOT CONFIG_LIBC_ERRNO)
25  zephyr_syscall_header(${ZEPHYR_BASE}/include/zephyr/sys/errno_private.h)
26endif()
27
28zephyr_syscall_header_ifdef(
29  CONFIG_ATOMIC_OPERATIONS_C
30  ${ZEPHYR_BASE}/include/zephyr/sys/atomic_c.h
31)
32
33zephyr_syscall_header_ifdef(
34  CONFIG_MMU
35  ${ZEPHYR_BASE}/include/zephyr/kernel/mm.h
36  ${ZEPHYR_BASE}/include/zephyr/sys/mem_manage.h
37)
38
39zephyr_syscall_header_ifdef(
40  CONFIG_DEMAND_PAGING
41  ${ZEPHYR_BASE}/include/zephyr/kernel/mm/demand_paging.h
42)
43
44# If a pre-built static library containing kernel code exists in
45# this directory, libkernel.a, link it with the application code
46# instead of building from source.
47zephyr_library_get_current_dir_lib_name(${ZEPHYR_BASE} libkernel_stem)
48set(libkernel ${CMAKE_CURRENT_SOURCE_DIR}/lib${libkernel_stem}${CMAKE_STATIC_LIBRARY_SUFFIX})
49unset(libkernel_stem)
50
51if(EXISTS ${libkernel})
52
53add_library(kernel INTERFACE)
54target_link_libraries(kernel INTERFACE ${libkernel})
55
56else()
57
58# FIXME: SHADOW_VARS: Remove this once we have enabled -Wshadow globally.
59add_compile_options($<TARGET_PROPERTY:compiler,warning_shadow_variables>)
60
61kernel_sources(
62  main_weak.c
63  banner.c
64  busy_wait.c
65  device.c
66  errno.c
67  fatal.c
68  init.c
69  kheap.c
70  mem_slab.c
71  float.c
72  version.c
73  )
74
75kernel_sources_ifdef(CONFIG_SCHED_CPU_MASK
76  cpu_mask.c
77  )
78
79kernel_sources_ifdef(CONFIG_MULTITHREADING
80  idle.c
81  mailbox.c
82  msg_q.c
83  mutex.c
84  queue.c
85  sem.c
86  stack.c
87  system_work_q.c
88  work.c
89  condvar.c
90  thread.c
91  sched.c
92  pipe.c
93  )
94
95if(CONFIG_MULTITHREADING)
96if(CONFIG_SCHED_SCALABLE OR CONFIG_WAITQ_SCALABLE)
97kernel_sources(priority_queues.c)
98endif()
99
100kernel_sources_ifdef(CONFIG_SMP
101     smp.c
102     ipi.c)
103else() # CONFIG_MULTITHREADING
104kernel_sources(nothread.c)
105endif() # CONFIG_MULTITHREADING
106
107kernel_sources_ifdef(CONFIG_TIMESLICING timeslicing.c)
108kernel_sources_ifdef(CONFIG_SPIN_VALIDATE spinlock_validate.c)
109kernel_sources_ifdef(CONFIG_IRQ_OFFLOAD irq_offload.c)
110kernel_sources_ifdef(CONFIG_BOOTARGS boot_args.c)
111kernel_sources_ifdef(CONFIG_THREAD_MONITOR thread_monitor.c)
112kernel_sources_ifdef(CONFIG_DEMAND_PAGING_STATS paging/statistics.c)
113
114add_library(kernel ${kernel_files})
115
116# Kernel files has the macro __ZEPHYR_SUPERVISOR__ set so that it
117# optimizes the code when userspace is enabled.
118
119set_target_properties(
120  kernel
121  PROPERTIES
122  COMPILE_DEFINITIONS
123  __ZEPHYR_SUPERVISOR__
124  )
125
126target_sources_ifdef(CONFIG_REQUIRES_STACK_CANARIES   kernel PRIVATE compiler_stack_protect.c)
127target_sources_ifdef(CONFIG_SYS_CLOCK_EXISTS      kernel PRIVATE timeout.c timer.c)
128target_sources_ifdef(CONFIG_ATOMIC_OPERATIONS_C   kernel PRIVATE atomic_c.c)
129target_sources_ifdef(CONFIG_MMU                   kernel PRIVATE mmu.c)
130target_sources_ifdef(CONFIG_POLL                  kernel PRIVATE poll.c)
131target_sources_ifdef(CONFIG_EVENTS                kernel PRIVATE events.c)
132target_sources_ifdef(CONFIG_SCHED_THREAD_USAGE    kernel PRIVATE usage.c)
133target_sources_ifdef(CONFIG_OBJ_CORE              kernel PRIVATE obj_core.c)
134
135if(${CONFIG_KERNEL_MEM_POOL})
136  target_sources(kernel PRIVATE mempool.c)
137
138  if(CONFIG_HEAP_MEM_POOL_IGNORE_MIN)
139    set(final_heap_size ${CONFIG_HEAP_MEM_POOL_SIZE})
140  else()
141    # Import all custom HEAP_MEM_POOL size requirements
142    import_kconfig(CONFIG_HEAP_MEM_POOL_ADD_SIZE_ ${DOTCONFIG} add_size_keys)
143
144    # Calculate the sum of all "ADD_SIZE" requirements
145    set(add_size_sum 0)
146    foreach(add_size ${add_size_keys})
147      math(EXPR add_size_sum "${add_size_sum} + ${${add_size}}")
148    endforeach()
149
150    if(CONFIG_HEAP_MEM_POOL_SIZE LESS "${add_size_sum}")
151      # Only warn if default value 0 has been modified
152      if(NOT CONFIG_HEAP_MEM_POOL_SIZE EQUAL 0)
153        message(WARNING "
154        CONFIG_HEAP_MEM_POOL_SIZE is less than requested minimum:
155          ${CONFIG_HEAP_MEM_POOL_SIZE} < ${add_size_sum}
156        Setting the system heap size to ${add_size_sum}")
157      endif()
158
159      set(final_heap_size ${add_size_sum})
160    else()
161      # CONFIG_HEAP_MEM_POOL_SIZE was greater than the sum of the requirements
162      set(final_heap_size ${CONFIG_HEAP_MEM_POOL_SIZE})
163    endif()
164
165  endif()
166
167  zephyr_compile_definitions(K_HEAP_MEM_POOL_SIZE=${final_heap_size})
168endif()
169
170# The last 2 files inside the target_sources_ifdef should be
171# userspace_handler.c and userspace.c. If not the linker would complain.
172# This order has to be maintained. Any new file should be placed
173# above these 2 files.
174target_sources_ifdef(
175  CONFIG_USERSPACE
176  kernel PRIVATE
177  futex.c
178  mem_domain.c
179  userspace_handler.c
180  userspace.c
181  )
182
183if(${CONFIG_DYNAMIC_THREAD})
184  target_sources(kernel PRIVATE dynamic.c)
185else()
186  target_sources(kernel PRIVATE dynamic_disabled.c)
187endif()
188
189target_include_directories(kernel PRIVATE
190  ${ZEPHYR_BASE}/kernel/include
191  ${ARCH_DIR}/${ARCH}/include
192  )
193
194target_link_libraries(kernel zephyr_interface)
195
196endif()
197
198add_dependencies(kernel zephyr_generated_headers)
199
200unset(libkernel)
201