From f5fd336343e90ce03fc44b2ad91679f2bbfc6159 Mon Sep 17 00:00:00 2001 From: Jamie Smith Date: Fri, 12 Feb 2021 02:03:53 -0800 Subject: [PATCH 1/4] Add mbed-create-distro --- tools/cmake/app.cmake | 3 + tools/cmake/create_distro.cmake | 73 ++++++++++++++++++++++++ tools/cmake/mbed_set_linker_script.cmake | 1 + 3 files changed, 77 insertions(+) create mode 100644 tools/cmake/create_distro.cmake diff --git a/tools/cmake/app.cmake b/tools/cmake/app.cmake index 9f62df8204b..e82a25a5b88 100644 --- a/tools/cmake/app.cmake +++ b/tools/cmake/app.cmake @@ -61,3 +61,6 @@ else() message(STATUS "Missing Python dependencies (python3, intelhex, prettytable) so the memory map cannot be printed") endif() +# load mbed_create_distro +include(${CMAKE_CURRENT_LIST_DIR}/create_distro.cmake) + diff --git a/tools/cmake/create_distro.cmake b/tools/cmake/create_distro.cmake new file mode 100644 index 00000000000..a6ecc962362 --- /dev/null +++ b/tools/cmake/create_distro.cmake @@ -0,0 +1,73 @@ +# This script provides mbed_create_distro(), a function that lets you compile multiple +# apps that use Mbed OS without waiting for Mbed OS to build multiple times. + +# You can use it like this: +# mbed_create_distro(mbed_for_my_app mbed-os mbed-storage-kvstore mbed-storage-filesystem) +# +# add_executable(myapp1 MyApp1.cpp) +# target_link_libraries(myapp1 PRIVATE mbed_for_my_app) +# mbed_set_post_build(myapp1) +# +# add_executable(myapp2 MyApp2.cpp) +# target_link_libraries(myapp2 PRIVATE mbed_for_my_app) +# mbed_set_post_build(myapp2) +# +# Both myapp1 and myapp2 will act like they were linked to mbed-os mbed-storage-kvstore, +# and mbed-storage-filesystem. Note that if you actually did target_link_libraries(myapp1 PRIVATE mbed-os +# mbed-storage-kvstore mbed-storage-filesystem), it would compile a new version of the Mbed OS source +# files for each target. However, using mbed_create_distro(), Mbed OS will only be compiled once. + +# Append the value of PROPERTY from SOURCE to the value of PROPERTY on DESTINATION +function(copy_append_property PROPERTY SOURCE DESTINATION) + get_property(PROP_IS_DEFINED TARGET ${SOURCE} PROPERTY ${PROPERTY} SET) + if(PROP_IS_DEFINED) + get_property(PROP_VALUE TARGET ${SOURCE} PROPERTY ${PROPERTY}) + #message("Copying ${PROPERTY} from ${SOURCE} -> ${DESTINATION}: ${PROP_VALUE} ") + set_property(TARGET ${DESTINATION} APPEND PROPERTY ${PROPERTY} "${PROP_VALUE}") + endif() +endfunction(copy_append_property) + +# Create a "distribution" of Mbed OS containing the base Mbed and certain modules. +# This distribution only needs to be compiled once and can be referenced in an arbitrary amount of targets. +function(mbed_create_distro NAME) # ARGN: modules... + add_library(${NAME} OBJECT) + mbed_configure_app_target(${NAME}) + + # First link as private dependencies + target_link_libraries(${NAME} PRIVATE ${ARGN}) + + # Now copy include dirs, compile defs, and compile options (but NOT interface source files) over + # to the distribution target so they will be passed into things that link to it. + # To do this, we need to recursively traverse the tree of dependencies. + set(REMAINING_MODULES ${ARGN}) + set(COMPLETED_MODULES ${ARGN}) + while(NOT "${REMAINING_MODULES}" STREQUAL "") + + list(GET REMAINING_MODULES 0 CURR_MODULE) + + #message("Processing ${CURR_MODULE}") + + copy_append_property(INTERFACE_COMPILE_DEFINITIONS ${CURR_MODULE} ${NAME}) + copy_append_property(INTERFACE_COMPILE_OPTIONS ${CURR_MODULE} ${NAME}) + copy_append_property(INTERFACE_INCLUDE_DIRECTORIES ${CURR_MODULE} ${NAME}) + copy_append_property(INTERFACE_LINK_OPTIONS ${CURR_MODULE} ${NAME}) + + list(REMOVE_AT REMAINING_MODULES 0) + list(APPEND COMPLETED_MODULES ${CURR_MODULE}) + + # find sub-modules of this module + get_property(SUBMODULES TARGET ${CURR_MODULE} PROPERTY INTERFACE_LINK_LIBRARIES) + #message("Deps of ${CURR_MODULE}: ${SUBMODULES}") + foreach(SUBMODULE ${SUBMODULES}) + if(NOT "${SUBMODULE}" MATCHES "::@") # remove CMake internal CMAKE_DIRECTORY_ID_SEP markers + if(NOT ${SUBMODULE} IN_LIST COMPLETED_MODULES) + list(APPEND REMAINING_MODULES ${SUBMODULE}) + endif() + endif() + endforeach() + + #message("REMAINING_MODULES: ${REMAINING_MODULES}") + + endwhile() + +endfunction(mbed_create_distro) \ No newline at end of file diff --git a/tools/cmake/mbed_set_linker_script.cmake b/tools/cmake/mbed_set_linker_script.cmake index 390de139854..3a29b0873aa 100644 --- a/tools/cmake/mbed_set_linker_script.cmake +++ b/tools/cmake/mbed_set_linker_script.cmake @@ -3,6 +3,7 @@ # # Preprocesses and sets the linker script for an Mbed target. +# Called once for each MCU target in the build system. # function(mbed_set_linker_script input_target raw_linker_script_path) set(LINKER_SCRIPT_PATH ${CMAKE_CURRENT_BINARY_DIR}/${input_target}.link_script.ld) From 22a7ebc0fce72882a85c041909980e38e913e3db Mon Sep 17 00:00:00 2001 From: Jamie Smith Date: Fri, 12 Feb 2021 02:14:32 -0800 Subject: [PATCH 2/4] Whoops, add license header --- tools/cmake/create_distro.cmake | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/cmake/create_distro.cmake b/tools/cmake/create_distro.cmake index a6ecc962362..aebeb1c4043 100644 --- a/tools/cmake/create_distro.cmake +++ b/tools/cmake/create_distro.cmake @@ -1,3 +1,6 @@ +# Copyright (c) 2021 ARM Limited. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + # This script provides mbed_create_distro(), a function that lets you compile multiple # apps that use Mbed OS without waiting for Mbed OS to build multiple times. @@ -12,7 +15,7 @@ # target_link_libraries(myapp2 PRIVATE mbed_for_my_app) # mbed_set_post_build(myapp2) # -# Both myapp1 and myapp2 will act like they were linked to mbed-os mbed-storage-kvstore, +# Both myapp1 and myapp2 will act like they were linked to mbed-os, mbed-storage-kvstore, # and mbed-storage-filesystem. Note that if you actually did target_link_libraries(myapp1 PRIVATE mbed-os # mbed-storage-kvstore mbed-storage-filesystem), it would compile a new version of the Mbed OS source # files for each target. However, using mbed_create_distro(), Mbed OS will only be compiled once. From 7663be15c1c49afad2a094b9cd1a301889a5b0dd Mon Sep 17 00:00:00 2001 From: Jamie Smith Date: Mon, 4 Oct 2021 21:57:39 -0700 Subject: [PATCH 3/4] Remove debug messages --- tools/cmake/create_distro.cmake | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tools/cmake/create_distro.cmake b/tools/cmake/create_distro.cmake index aebeb1c4043..fb5df813bdf 100644 --- a/tools/cmake/create_distro.cmake +++ b/tools/cmake/create_distro.cmake @@ -48,8 +48,6 @@ function(mbed_create_distro NAME) # ARGN: modules... list(GET REMAINING_MODULES 0 CURR_MODULE) - #message("Processing ${CURR_MODULE}") - copy_append_property(INTERFACE_COMPILE_DEFINITIONS ${CURR_MODULE} ${NAME}) copy_append_property(INTERFACE_COMPILE_OPTIONS ${CURR_MODULE} ${NAME}) copy_append_property(INTERFACE_INCLUDE_DIRECTORIES ${CURR_MODULE} ${NAME}) @@ -60,7 +58,6 @@ function(mbed_create_distro NAME) # ARGN: modules... # find sub-modules of this module get_property(SUBMODULES TARGET ${CURR_MODULE} PROPERTY INTERFACE_LINK_LIBRARIES) - #message("Deps of ${CURR_MODULE}: ${SUBMODULES}") foreach(SUBMODULE ${SUBMODULES}) if(NOT "${SUBMODULE}" MATCHES "::@") # remove CMake internal CMAKE_DIRECTORY_ID_SEP markers if(NOT ${SUBMODULE} IN_LIST COMPLETED_MODULES) @@ -68,9 +65,7 @@ function(mbed_create_distro NAME) # ARGN: modules... endif() endif() endforeach() - - #message("REMAINING_MODULES: ${REMAINING_MODULES}") - + endwhile() endfunction(mbed_create_distro) \ No newline at end of file From 503b9b8ac7fd753c2bf77277a3c46d888a1900cd Mon Sep 17 00:00:00 2001 From: Jamie Smith Date: Tue, 5 Oct 2021 08:16:56 -0700 Subject: [PATCH 4/4] One more debug message --- tools/cmake/create_distro.cmake | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tools/cmake/create_distro.cmake b/tools/cmake/create_distro.cmake index fb5df813bdf..2e518014f7d 100644 --- a/tools/cmake/create_distro.cmake +++ b/tools/cmake/create_distro.cmake @@ -25,7 +25,6 @@ function(copy_append_property PROPERTY SOURCE DESTINATION) get_property(PROP_IS_DEFINED TARGET ${SOURCE} PROPERTY ${PROPERTY} SET) if(PROP_IS_DEFINED) get_property(PROP_VALUE TARGET ${SOURCE} PROPERTY ${PROPERTY}) - #message("Copying ${PROPERTY} from ${SOURCE} -> ${DESTINATION}: ${PROP_VALUE} ") set_property(TARGET ${DESTINATION} APPEND PROPERTY ${PROPERTY} "${PROP_VALUE}") endif() endfunction(copy_append_property) @@ -65,7 +64,7 @@ function(mbed_create_distro NAME) # ARGN: modules... endif() endif() endforeach() - + endwhile() endfunction(mbed_create_distro) \ No newline at end of file