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