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