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