Mbed CLI 2 not configuring project

Hello! I’m working on a project that requires several MCUs each with a different firmware and in order to save space I want to use a shared instance of the Mbed OS 6 library. Although I couldn’t find it anywhere in the online documentation apparently Mbed CLI 2 (mbed-tools) also has the option to use a shared copy of Mbed OS when the --mbed-os-path option is added to the compile or configure commands.

To test that feature I first imported Mbed OS (v6.9):

C:\Users\Dani\Documents> mbed-tools import mbed-os

Then I created a new project without mbed-os adding the --create-only option.

C:\Users\Dani\Documents> mbed-tools new --create-only  .\test

Next I tried compiling the project with:

C:\Users\Dani\Documents\test> mbed-tools compile -t GCC_ARM -m NUCLEO_F103RB --mbed-os-path "C:/Users/Dani/Documents/mbed-os"

And got the following error:

CMake Error at CMakeLists.txt:10 (include):
include could not find requested file:
    C:/Users/Dani/Documents/test/mbed-os/tools/cmake/app.cmake

Which of course makes sense because that file path is wrong, nevertheless I was expecting the --mbed-os-path to change that automatically.

Next I edited the CMakeLists.txt file in the root of the test project, replacing this line:

set(MBED_PATH ${CMAKE_CURRENT_SOURCE_DIR}/mbed-os CACHE INTERNAL "")

With:

set(MBED_PATH "C:/Users/Dani/Documents/mbed-os" CACHE INTERNAL "")

Trying again the same command to compile I got a new error:

CMake Error at CMakeLists.txt:12 (add_subdirectory):
add_subdirectory not given a binary directory but the given source
directory "C:/Users/Dani/Documents/mbed-os" is not a subdirectory of
"C:/Users/Dani/Documents/test".  When specifying an out-of-tree source a
binary directory must be explicitly specified.

So I provided a build directory to add_subdirectory( ):

add_subdirectory(${MBED_PATH} "C:/Users/Dani/Documents/mbed-os/build")

Only this way I managed to compile successfully the project.

Next I tried to call:

C:\Users\Dani\Documents\test> mbed-tools compile -t GCC_ARM -m NUCLEO_F103RB

And got:

ERROR: Mbed OS was not found due to the following error: The mbed-os directory does not exist.

Very weird, because the argument of --mbed-os-path seems to be ignored and I was overwriting the file anyway, however if not indicated the compiler complains about the missing library.

I tried to configure the project with an apparent success:

C:\Users\Dani\Documents\test> mbed-tools configure -t GCC_ARM -m NUCLEO_F103RB -- mbed-os-path "C:/Users/Dani/Documents/mbed-os"      
mbed_config.cmake has been generated and written to 'C:\Users\Dani\Documents\test\cmake_build\NUCLEO_F103RB\develop\GCC_ARM\mbed_config.cmake'

However typing:

mbed-tools compile

Outputs:

Error: Both --toolchain and --mbed-target arguments are required when using the compile subcommand. 

So to sum up, I only managed to compile the project referencing an external copy of mbed-os by overwriting the CMakeLists.txt file. The configure command seems to do nothing (not even in a simple sketch with a local copy of mbed-os). I want to know how to properly configure my project so I can reference an external copy of mbed-os, ideally placing all the compilation objects in the build directory of the project I’m compiling, leaving the mbed-os directory untouched so it can be referenced by other projects.

Thanks in advance.

So I found a cleaner way to achieve my initial goal. The CMakeLists.txt has to be modified like so:

...
(-) set(MBED_PATH ${CMAKE_CURRENT_SOURCE_DIR}/mbed-os CACHE INTERNAL "")
(+) set(MBED_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../mbed-os CACHE INTERNAL "")
...
(-) add_subdirectory(${MBED_PATH})
(+) add_subdirectory(${MBED_PATH} ${CMAKE_CURRENT_BINARY_DIR}/mbed-os)

This allows to add an external directory to the main project and place the compiled files in the same place as where they would be placed in a standalone project. The only requirement is that the mbed-os and the mbed program are in the same folder and that the compile command has to include --mbed-os-path "./../mbed-os".

dir
 |-mbed-os
 |-project_folder
    |-cmake_build
        |-NUCLEO_F103RB
            |-develop
                |-GCC_ARM
                   |-mbed-os
                   |-...
                   |-main.bin

Nevertheless, --mbed-os-path seems to be ignored for the build, as I said in my previous post. Mbed CLI 2 requires it when no mbed-os is present in the the project directory, and the argument has to be a folder containing mbed-os otherwise it complains even if that path is not used for the build. For example, editing the CMakeLists.txt as shown above and adding --mbed-os-path "./../mbed-os" or --mbed-os-path "<full_path_to_mbed-os>" to the compile command builds the project just fine.

Probably this is not the best practice, but I think it adds the least modifications to the original files getting the desired result, and can be easily changed back when the functionality is finally added to the Mbed CLI 2 (maybe it is already, but not working for me at least). Again if someone can show me the proper way to configure my project I’ll be very thankful.

Your CMake approach for out-of-tree “subdirectories” is the right one.

--mbed-os-path works for when mbed-os is in a subdirectory of the current project, for example ./extern/mbed-os.

Hi, thanks for replying. What would be the use case for --mbed-os-path then? Having multiple versions of Mbed OS in the same project?

I think Mbed CLI 1 offers a better way to reference an external copy of Mbed OS (Managing multiple Mbed projects), and as far as I understand it can be even configured locally and globally. Can we expect this feature to be included in Mbed CLI 2?

to have mbed-os somewhere that’s not the root of the project, for example we use ./extern/ for external dependencies where mbed-os resides in our case at $root_of_project/extern/mbed-os