Skip to content

Commit b5840e4

Browse files
committed
[nrf fromlist] cmake: zephyr_library_amend feature
This commit introduces the cmake extension zephyr_library_amend. This function allows for adding files in an out-of-tree Zephyr module to a zephyr library created in zephyr repo CMake files. As example: drivers/entropy/CMakeLists.txt creates an zephyr library as: zephyr_library() only available to zephyr itself. The amend function allows to amend to such a lib, by creating a CMakeLists.txt file following identical folder structure in a Zephyr Module: <zephyr_module_oot>/drivers/entropy/CMakeLists.txt zephyr_library_amend() zephyr_library_sources() # Sources are amended to the original library Signed-off-by: Torsten Rasmussen <[email protected]>
1 parent ede6735 commit b5840e4

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,11 @@ if(EXISTS ${CMAKE_BINARY_DIR}/zephyr_modules.txt)
479479
# this binary_dir is created but stays empty. Object files land in
480480
# the main binary dir instead.
481481
# https://cmake.org/pipermail/cmake/2019-June/069547.html
482+
set(ZEPHYR_CURRENT_MODULE_DIR ${module_path})
482483
add_subdirectory(${module_path} ${CMAKE_CURRENT_BINARY_DIR}/modules/${module_name})
483484
endforeach()
485+
# Done processing modules, clear ZEPHYR_CURRENT_MODULE_DIR.
486+
set(ZEPHYR_CURRENT_MODULE_DIR)
484487
endif()
485488

486489
set(syscall_macros_h ${ZEPHYR_BINARY_DIR}/include/generated/syscall_macros.h)

cmake/extensions.cmake

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -384,15 +384,15 @@ endmacro()
384384

385385
# Constructor with a directory-inferred name
386386
macro(zephyr_library)
387-
zephyr_library_get_current_dir_lib_name(lib_name)
387+
zephyr_library_get_current_dir_lib_name(${ZEPHYR_BASE} lib_name)
388388
zephyr_library_named(${lib_name})
389389
endmacro()
390390

391-
# Determines what the current directory's lib name would be and writes
392-
# it to the argument "lib_name"
393-
macro(zephyr_library_get_current_dir_lib_name lib_name)
391+
# Determines what the current directory's lib name would be according to the
392+
# provided base and writes it to the argument "lib_name"
393+
macro(zephyr_library_get_current_dir_lib_name base lib_name)
394394
# Remove the prefix (/home/sebo/zephyr/driver/serial/CMakeLists.txt => driver/serial/CMakeLists.txt)
395-
file(RELATIVE_PATH name ${ZEPHYR_BASE} ${CMAKE_CURRENT_LIST_FILE})
395+
file(RELATIVE_PATH name ${base} ${CMAKE_CURRENT_LIST_FILE})
396396

397397
# Remove the filename (driver/serial/CMakeLists.txt => driver/serial)
398398
get_filename_component(name ${name} DIRECTORY)
@@ -415,6 +415,34 @@ macro(zephyr_library_named name)
415415
target_link_libraries(${ZEPHYR_CURRENT_LIBRARY} PUBLIC ${IMAGE}zephyr_interface)
416416
endmacro()
417417

418+
# Provides amend functionality to a Zephyr library for out-of-tree usage.
419+
#
420+
# When called from a Zephyr module, the corresponding zephyr library defined
421+
# within Zephyr will be looked up.
422+
#
423+
# Note, in order to ensure correct library when amending, the folder structure in the
424+
# Zephyr module must resemble the structure used in Zephyr, as example:
425+
#
426+
# Example: to amend the zephyr library created in
427+
# ZEPHYR_BASE/drivers/entropy/CMakeLists.txt
428+
# add the following file:
429+
# ZEPHYR_MODULE/drivers/entropy/CMakeLists.txt
430+
# with content:
431+
# zephyr_library_amend()
432+
# zephyr_libray_add_sources(...)
433+
#
434+
macro(zephyr_library_amend)
435+
# This is a macro because we need to ensure the ZEPHYR_CURRENT_LIBRARY and
436+
# following zephyr_library_* calls are executed within the scope of the
437+
# caller.
438+
if(NOT ZEPHYR_CURRENT_MODULE_DIR)
439+
message(FATAL_ERROR "Function only available for Zephyr modules.")
440+
endif()
441+
442+
zephyr_library_get_current_dir_lib_name(${ZEPHYR_CURRENT_MODULE_DIR} lib_name)
443+
444+
set(ZEPHYR_CURRENT_LIBRARY ${lib_name})
445+
endmacro()
418446

419447
function(zephyr_link_interface interface)
420448
target_link_libraries(${interface} INTERFACE ${IMAGE}zephyr_interface)

0 commit comments

Comments
 (0)