1+ # This script provides mbed_create_distro(), a function that lets you compile multiple
2+ # apps that use Mbed OS without waiting for Mbed OS to build multiple times.
3+
4+ # You can use it like this:
5+ # mbed_create_distro(mbed_for_my_app mbed-os mbed-storage-kvstore mbed-storage-filesystem)
6+ #
7+ # add_executable(myapp1 MyApp1.cpp)
8+ # target_link_libraries(myapp1 PRIVATE mbed_for_my_app)
9+ # mbed_set_post_build(myapp1)
10+ #
11+ # add_executable(myapp2 MyApp2.cpp)
12+ # target_link_libraries(myapp2 PRIVATE mbed_for_my_app)
13+ # mbed_set_post_build(myapp2)
14+ #
15+ # Both myapp1 and myapp2 will act like they were linked to mbed-os mbed-storage-kvstore,
16+ # and mbed-storage-filesystem. Note that if you actually did target_link_libraries(myapp1 PRIVATE mbed-os
17+ # mbed-storage-kvstore mbed-storage-filesystem), it would compile a new version of the Mbed OS source
18+ # files for each target. However, using mbed_create_distro(), Mbed OS will only be compiled once.
19+
20+ # Append the value of PROPERTY from SOURCE to the value of PROPERTY on DESTINATION
21+ function (copy_append_property PROPERTY SOURCE DESTINATION )
22+ get_property (PROP_IS_DEFINED TARGET ${SOURCE} PROPERTY ${PROPERTY} SET)
23+ if (PROP_IS_DEFINED)
24+ get_property (PROP_VALUE TARGET ${SOURCE} PROPERTY ${PROPERTY} )
25+ #message("Copying ${PROPERTY} from ${SOURCE} -> ${DESTINATION}: ${PROP_VALUE} ")
26+ set_property (TARGET ${DESTINATION} APPEND PROPERTY ${PROPERTY} "${PROP_VALUE} " )
27+ endif ()
28+ endfunction (copy_append_property)
29+
30+ # Create a "distribution" of Mbed OS containing the base Mbed and certain modules.
31+ # This distribution only needs to be compiled once and can be referenced in an arbitrary amount of targets.
32+ function (mbed_create_distro NAME ) # ARGN: modules...
33+ add_library (${NAME} OBJECT)
34+ mbed_configure_app_target(${NAME} )
35+
36+ # First link as private dependencies
37+ target_link_libraries (${NAME} PRIVATE ${ARGN} )
38+
39+ # Now copy include dirs, compile defs, and compile options (but NOT interface source files) over
40+ # to the distribution target so they will be passed into things that link to it.
41+ # To do this, we need to recursively traverse the tree of dependencies.
42+ set (REMAINING_MODULES ${ARGN} )
43+ set (COMPLETED_MODULES ${ARGN} )
44+ while (NOT "${REMAINING_MODULES} " STREQUAL "" )
45+
46+ list (GET REMAINING_MODULES 0 CURR_MODULE)
47+
48+ #message("Processing ${CURR_MODULE}")
49+
50+ copy_append_property(INTERFACE_COMPILE_DEFINITIONS ${CURR_MODULE} ${NAME} )
51+ copy_append_property(INTERFACE_COMPILE_OPTIONS ${CURR_MODULE} ${NAME} )
52+ copy_append_property(INTERFACE_INCLUDE_DIRECTORIES ${CURR_MODULE} ${NAME} )
53+ copy_append_property(INTERFACE_LINK_OPTIONS ${CURR_MODULE} ${NAME} )
54+
55+ list (REMOVE_AT REMAINING_MODULES 0)
56+ list (APPEND COMPLETED_MODULES ${CURR_MODULE} )
57+
58+ # find sub-modules of this module
59+ get_property (SUBMODULES TARGET ${CURR_MODULE} PROPERTY INTERFACE_LINK_LIBRARIES)
60+ #message("Deps of ${CURR_MODULE}: ${SUBMODULES}")
61+ foreach (SUBMODULE ${SUBMODULES} )
62+ if (NOT "${SUBMODULE} " MATCHES "::@" ) # remove CMake internal CMAKE_DIRECTORY_ID_SEP markers
63+ if (NOT ${SUBMODULE} IN_LIST COMPLETED_MODULES)
64+ list (APPEND REMAINING_MODULES ${SUBMODULE} )
65+ endif ()
66+ endif ()
67+ endforeach ()
68+
69+ #message("REMAINING_MODULES: ${REMAINING_MODULES}")
70+
71+ endwhile ()
72+
73+ endfunction (mbed_create_distro)
0 commit comments