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
6# If a pre-built static library containing kernel code exists in
7# this directory, libkernel.a, link it with the application code
8# instead of building from source.
9zephyr_library_get_current_dir_lib_name(${ZEPHYR_BASE} libkernel_stem)
10set(libkernel ${CMAKE_CURRENT_SOURCE_DIR}/lib${libkernel_stem}${CMAKE_STATIC_LIBRARY_SUFFIX})
11unset(libkernel_stem)
12
13if(EXISTS ${libkernel})
14
15add_library(kernel INTERFACE)
16target_link_libraries(kernel INTERFACE ${libkernel})
17
18else()
19
20list(APPEND kernel_files
21  main_weak.c
22  banner.c
23  device.c
24  errno.c
25  fatal.c
26  init.c
27  kheap.c
28  mem_slab.c
29  thread.c
30  version.c
31  )
32
33if(CONFIG_MULTITHREADING)
34list(APPEND kernel_files
35  idle.c
36  mailbox.c
37  msg_q.c
38  mutex.c
39  pipes.c
40  queue.c
41  sem.c
42  stack.c
43  system_work_q.c
44  work.c
45  sched.c
46  condvar.c
47  )
48
49if(CONFIG_SMP)
50list(APPEND kernel_files
51     smp.c)
52endif()
53
54endif()
55
56if(CONFIG_XIP)
57list(APPEND kernel_files
58     xip.c)
59endif()
60
61if(CONFIG_DEMAND_PAGING_STATS)
62list(APPEND kernel_files
63     paging/statistics.c)
64endif()
65
66add_library(kernel ${kernel_files})
67
68# Kernel files has the macro __ZEPHYR_SUPERVISOR__ set so that it
69# optimizes the code when userspace is enabled.
70
71set_target_properties(
72  kernel
73  PROPERTIES
74  COMPILE_DEFINITIONS
75  __ZEPHYR_SUPERVISOR__
76  )
77
78target_sources_ifdef(CONFIG_STACK_CANARIES        kernel PRIVATE compiler_stack_protect.c)
79target_sources_ifdef(CONFIG_SYS_CLOCK_EXISTS      kernel PRIVATE timeout.c timer.c)
80target_sources_ifdef(CONFIG_ATOMIC_OPERATIONS_C   kernel PRIVATE atomic_c.c)
81target_sources_ifdef(CONFIG_MMU                   kernel PRIVATE mmu.c)
82target_sources_ifdef(CONFIG_POLL                  kernel PRIVATE poll.c)
83
84if(${CONFIG_KERNEL_MEM_POOL})
85  target_sources(kernel PRIVATE mempool.c)
86endif()
87
88# The last 2 files inside the target_sources_ifdef should be
89# userspace_handler.c and userspace.c. If not the linker would complain.
90# This order has to be maintained. Any new file should be placed
91# above these 2 files.
92target_sources_ifdef(
93  CONFIG_USERSPACE
94  kernel PRIVATE
95  futex.c
96  mem_domain.c
97  userspace_handler.c
98  userspace.c
99  )
100
101if(CONFIG_CACHE_MANAGEMENT AND CONFIG_USERSPACE)
102  target_sources(kernel PRIVATE cache_handlers.c)
103endif()
104
105target_include_directories(kernel PRIVATE
106  ${ZEPHYR_BASE}/kernel/include
107  ${ARCH_DIR}/${ARCH}/include
108  )
109
110target_link_libraries(kernel zephyr_interface)
111
112endif()
113
114add_dependencies(kernel zephyr_generated_headers)
115
116unset(libkernel)
117