From b5840e4417476267f1694c10bb2705df00d28820 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Mon, 21 Oct 2019 10:08:20 +0200 Subject: [PATCH 1/2] [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: /drivers/entropy/CMakeLists.txt zephyr_library_amend() zephyr_library_sources() # Sources are amended to the original library Signed-off-by: Torsten Rasmussen --- CMakeLists.txt | 3 +++ cmake/extensions.cmake | 38 +++++++++++++++++++++++++++++++++----- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e6cf280889e..248ddb0d513 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -479,8 +479,11 @@ if(EXISTS ${CMAKE_BINARY_DIR}/zephyr_modules.txt) # this binary_dir is created but stays empty. Object files land in # the main binary dir instead. # https://cmake.org/pipermail/cmake/2019-June/069547.html + set(ZEPHYR_CURRENT_MODULE_DIR ${module_path}) add_subdirectory(${module_path} ${CMAKE_CURRENT_BINARY_DIR}/modules/${module_name}) endforeach() + # Done processing modules, clear ZEPHYR_CURRENT_MODULE_DIR. + set(ZEPHYR_CURRENT_MODULE_DIR) endif() set(syscall_macros_h ${ZEPHYR_BINARY_DIR}/include/generated/syscall_macros.h) diff --git a/cmake/extensions.cmake b/cmake/extensions.cmake index 713448c5fee..e0797867f11 100644 --- a/cmake/extensions.cmake +++ b/cmake/extensions.cmake @@ -384,15 +384,15 @@ endmacro() # Constructor with a directory-inferred name macro(zephyr_library) - zephyr_library_get_current_dir_lib_name(lib_name) + zephyr_library_get_current_dir_lib_name(${ZEPHYR_BASE} lib_name) zephyr_library_named(${lib_name}) endmacro() -# Determines what the current directory's lib name would be and writes -# it to the argument "lib_name" -macro(zephyr_library_get_current_dir_lib_name lib_name) +# Determines what the current directory's lib name would be according to the +# provided base and writes it to the argument "lib_name" +macro(zephyr_library_get_current_dir_lib_name base lib_name) # Remove the prefix (/home/sebo/zephyr/driver/serial/CMakeLists.txt => driver/serial/CMakeLists.txt) - file(RELATIVE_PATH name ${ZEPHYR_BASE} ${CMAKE_CURRENT_LIST_FILE}) + file(RELATIVE_PATH name ${base} ${CMAKE_CURRENT_LIST_FILE}) # Remove the filename (driver/serial/CMakeLists.txt => driver/serial) get_filename_component(name ${name} DIRECTORY) @@ -415,6 +415,34 @@ macro(zephyr_library_named name) target_link_libraries(${ZEPHYR_CURRENT_LIBRARY} PUBLIC ${IMAGE}zephyr_interface) endmacro() +# Provides amend functionality to a Zephyr library for out-of-tree usage. +# +# When called from a Zephyr module, the corresponding zephyr library defined +# within Zephyr will be looked up. +# +# Note, in order to ensure correct library when amending, the folder structure in the +# Zephyr module must resemble the structure used in Zephyr, as example: +# +# Example: to amend the zephyr library created in +# ZEPHYR_BASE/drivers/entropy/CMakeLists.txt +# add the following file: +# ZEPHYR_MODULE/drivers/entropy/CMakeLists.txt +# with content: +# zephyr_library_amend() +# zephyr_libray_add_sources(...) +# +macro(zephyr_library_amend) + # This is a macro because we need to ensure the ZEPHYR_CURRENT_LIBRARY and + # following zephyr_library_* calls are executed within the scope of the + # caller. + if(NOT ZEPHYR_CURRENT_MODULE_DIR) + message(FATAL_ERROR "Function only available for Zephyr modules.") + endif() + + zephyr_library_get_current_dir_lib_name(${ZEPHYR_CURRENT_MODULE_DIR} lib_name) + + set(ZEPHYR_CURRENT_LIBRARY ${lib_name}) +endmacro() function(zephyr_link_interface interface) target_link_libraries(${interface} INTERFACE ${IMAGE}zephyr_interface) From 0f791148618987abe798915153a7cf7821525cc5 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Tue, 22 Oct 2019 11:02:25 +0200 Subject: [PATCH 2/2] [nrf noup] cmake: multiimage: zephyr_library_amend feature This commits adds ${IMAGE} in order to support multiimage in the amend feature. Signed-off-by: Torsten Rasmussen --- cmake/extensions.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/extensions.cmake b/cmake/extensions.cmake index e0797867f11..1950f745fb8 100644 --- a/cmake/extensions.cmake +++ b/cmake/extensions.cmake @@ -441,7 +441,7 @@ macro(zephyr_library_amend) zephyr_library_get_current_dir_lib_name(${ZEPHYR_CURRENT_MODULE_DIR} lib_name) - set(ZEPHYR_CURRENT_LIBRARY ${lib_name}) + set(ZEPHYR_CURRENT_LIBRARY ${IMAGE}${lib_name}) endmacro() function(zephyr_link_interface interface)