1# Copyright (c) 2023 Intel Corporation.
2# SPDX-License-Identifier: Apache-2.0
3
4cmake_minimum_required(VERSION 3.20.0)
5find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6project(llext_test)
7
8target_sources(app PRIVATE
9  src/test_llext.c
10)
11
12target_include_directories(app PRIVATE
13  ${ZEPHYR_BASE}/include
14  ${ZEPHYR_BASE}/kernel/include
15  ${ZEPHYR_BASE}/arch/${ARCH}/include
16)
17
18set(ext_names
19	hello_world
20	logging
21	relative_jump
22	object
23	syscalls
24	threads_kernel_objects
25	export_dependent
26	export_dependency
27)
28
29if(CONFIG_ARM)
30  if(NOT CONFIG_CPU_CORTEX_M0 AND NOT CONFIG_CPU_CORTEX_M0PLUS AND NOT CONFIG_CPU_CORTEX_M1)
31    list(APPEND ext_names movwmovt)
32  endif()
33endif()
34
35if (CONFIG_LLEXT_TYPE_ELF_RELOCATABLE AND NOT CONFIG_ARM)
36    list(APPEND ext_names pre_located)
37endif()
38
39if (CONFIG_LLEXT_STORAGE_WRITABLE)
40    list(APPEND ext_names find_section detached_fn)
41endif()
42
43if (NOT CONFIG_LLEXT_TYPE_ELF_SHAREDLIB)
44    # ELF shared libraries do not support init sections
45    list(APPEND ext_names init_fini)
46endif()
47
48# generate extension targets foreach extension given by 'ext_names'
49foreach(ext_name ${ext_names})
50  set(ext_src ${PROJECT_SOURCE_DIR}/src/${ext_name}_ext.c)
51  set(ext_bin ${ZEPHYR_BINARY_DIR}/${ext_name}.llext)
52  set(ext_inc ${ZEPHYR_BINARY_DIR}/include/generated/${ext_name}.inc)
53  add_llext_target(${ext_name}_ext
54    OUTPUT  ${ext_bin}
55    SOURCES ${ext_src}
56  )
57  generate_inc_file_for_target(app ${ext_bin} ${ext_inc})
58endforeach()
59
60if(NOT CONFIG_LLEXT_TYPE_ELF_OBJECT)
61  add_llext_target(multi_file_ext
62    OUTPUT  ${ZEPHYR_BINARY_DIR}/multi_file.llext
63    SOURCES ${PROJECT_SOURCE_DIR}/src/multi_file_ext1.c ${PROJECT_SOURCE_DIR}/src/multi_file_ext2.c
64  )
65  generate_inc_file_for_target(app ${ZEPHYR_BINARY_DIR}/multi_file.llext
66    ${ZEPHYR_BINARY_DIR}/include/generated/multi_file.inc
67  )
68endif()
69
70if (CONFIG_LLEXT_TYPE_ELF_RELOCATABLE AND NOT CONFIG_ARM AND NOT CONFIG_RISCV)
71  # Manually fix the pre_located extension's text address at a multiple of 4
72  get_target_property(pre_located_target pre_located_ext lib_target)
73  get_target_property(pre_located_file pre_located_ext pkg_input)
74  add_llext_command(
75    TARGET pre_located_ext
76    POST_BUILD
77    COMMAND ${CMAKE_C_COMPILER}
78    -Wl,-r -Wl,-Ttext=0xbada110c -nostdlib -nodefaultlibs -nostartfiles
79    $<TARGET_OBJECTS:${pre_located_target}> -o ${pre_located_file}
80  )
81endif()
82