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/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  thread.c
61  version.c
62  sched.c
63  )
64
65if(CONFIG_MULTITHREADING)
66list(APPEND kernel_files
67  idle.c
68  mailbox.c
69  msg_q.c
70  mutex.c
71  queue.c
72  sem.c
73  stack.c
74  system_work_q.c
75  work.c
76  condvar.c
77  )
78
79if(CONFIG_SMP)
80list(APPEND kernel_files
81     smp.c)
82endif()
83
84endif()
85
86if(CONFIG_XIP)
87list(APPEND kernel_files
88     xip.c)
89endif()
90
91if(CONFIG_DEMAND_PAGING_STATS)
92list(APPEND kernel_files
93     paging/statistics.c)
94endif()
95
96add_library(kernel ${kernel_files})
97
98# Kernel files has the macro __ZEPHYR_SUPERVISOR__ set so that it
99# optimizes the code when userspace is enabled.
100
101set_target_properties(
102  kernel
103  PROPERTIES
104  COMPILE_DEFINITIONS
105  __ZEPHYR_SUPERVISOR__
106  )
107
108target_sources_ifdef(CONFIG_STACK_CANARIES        kernel PRIVATE compiler_stack_protect.c)
109target_sources_ifdef(CONFIG_SYS_CLOCK_EXISTS      kernel PRIVATE timeout.c timer.c)
110target_sources_ifdef(CONFIG_ATOMIC_OPERATIONS_C   kernel PRIVATE atomic_c.c)
111target_sources_ifdef(CONFIG_MMU                   kernel PRIVATE mmu.c)
112target_sources_ifdef(CONFIG_POLL                  kernel PRIVATE poll.c)
113target_sources_ifdef(CONFIG_EVENTS                kernel PRIVATE events.c)
114target_sources_ifdef(CONFIG_PIPES                 kernel PRIVATE pipes.c)
115target_sources_ifdef(CONFIG_SCHED_THREAD_USAGE    kernel PRIVATE usage.c)
116target_sources_ifdef(CONFIG_OBJ_CORE              kernel PRIVATE obj_core.c)
117
118if(${CONFIG_KERNEL_MEM_POOL})
119  target_sources(kernel PRIVATE mempool.c)
120
121  if(CONFIG_HEAP_MEM_POOL_IGNORE_MIN)
122    set(final_heap_size ${CONFIG_HEAP_MEM_POOL_SIZE})
123  else()
124    # Import all custom HEAP_MEM_POOL size requirements
125    import_kconfig(CONFIG_HEAP_MEM_POOL_ADD_SIZE_ ${DOTCONFIG} add_size_keys)
126
127    # Calculate the sum of all "ADD_SIZE" requirements
128    set(add_size_sum 0)
129    foreach(add_size ${add_size_keys})
130      math(EXPR add_size_sum "${add_size_sum} + ${${add_size}}")
131    endforeach()
132
133    if(CONFIG_HEAP_MEM_POOL_SIZE LESS "${add_size_sum}")
134      # Only warn if default value 0 has been modified
135      if(NOT CONFIG_HEAP_MEM_POOL_SIZE EQUAL 0)
136        message(WARNING "
137        CONFIG_HEAP_MEM_POOL_SIZE is less than requested minimum:
138          ${CONFIG_HEAP_MEM_POOL_SIZE} < ${add_size_sum}
139        Setting the system heap size to ${add_size_sum}")
140      endif()
141
142      set(final_heap_size ${add_size_sum})
143    else()
144      # CONFIG_HEAP_MEM_POOL_SIZE was greater than the sum of the requirements
145      set(final_heap_size ${CONFIG_HEAP_MEM_POOL_SIZE})
146    endif()
147
148  endif()
149
150  zephyr_compile_definitions(K_HEAP_MEM_POOL_SIZE=${final_heap_size})
151endif()
152
153# The last 2 files inside the target_sources_ifdef should be
154# userspace_handler.c and userspace.c. If not the linker would complain.
155# This order has to be maintained. Any new file should be placed
156# above these 2 files.
157target_sources_ifdef(
158  CONFIG_USERSPACE
159  kernel PRIVATE
160  futex.c
161  mem_domain.c
162  userspace_handler.c
163  userspace.c
164  )
165
166if(${CONFIG_DYNAMIC_THREAD})
167  target_sources(kernel PRIVATE dynamic.c)
168else()
169  target_sources(kernel PRIVATE dynamic_disabled.c)
170endif()
171
172target_include_directories(kernel PRIVATE
173  ${ZEPHYR_BASE}/kernel/include
174  ${ARCH_DIR}/${ARCH}/include
175  )
176
177target_link_libraries(kernel zephyr_interface)
178
179endif()
180
181add_dependencies(kernel zephyr_generated_headers)
182
183unset(libkernel)
184