[mbed-cmake] PinNames.h not found in custom targets

Hello all, I am evaluating the mbed-cmake toolset (GitHub - USCRPL/mbed-cmake: Use the power of CMake to create your MBed applications) and I have successfuly compiled the hello_world program on different targets, next step is to follow the process to use custom targets. I have decided to use a very simple way to try it out. I copied the current target TARGET_NUCLEO_G474RE into the mbed-cmake-example-project directory and renamed it to TARGET_NUCLEOS_G474RE, I changed the CMakeLists into this directory in that way:

# Copyright (c) 2020 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

add_library(mbed-nucleos-g474re INTERFACE)

target_sources(mbed-nucleos-g474re
    INTERFACE
        PeripheralPins.c
)

target_include_directories(mbed-nucleos-g474re
    INTERFACE
        .
)

target_link_libraries(mbed-nucleos-g474re INTERFACE mbed-stm32g474xe)

I created a custom_targets.json in same directory as follows (I took same json portion from the targets.json for this board):

{
    "NUCLEOS_G474RE": {
        "inherits": [
            "MCU_STM32G474xE"
        ],
        "supported_form_factors": [
            "ARDUINO_UNO"
        ],
        "overrides": {
            "hse_value": 24000000
        },
        "detect_code": [
            "0841"
        ],
        "device_name": "STM32G474RETx"
    }
}

Next I executed the following commands:

python3 mbed-cmake/configure_for_target.py -a mbed_app.json -i .mbedignore -x C:\Mbed-Cmake\mbed_ws\mbed-cmake-example-project\TARGET_NUCLEOS_G474RE NUCLEOS_G474RE

create a build directory , enter into it

cmake --fresh "-G Ninja" C:\Mbed-Cmake\mbed_ws\mbed-cmake-example-project -DUPLOAD_METHOD=STM32CUBE 

ninja -j2 hello_world

And this error appears …

In file included from C:/Mbed-Cmake/mbed_ws/mbed-cmake-example-project/mbed-cmake/mbed-src/targets/TARGET_STM/device.h:38,
                 from C:/Mbed-Cmake/mbed_ws/mbed-cmake-example-project/mbed-cmake/mbed-src/platform/include/platform/platform.h:28,
                 from c:\mbed-cmake\mbed_ws\mbed-cmake-example-project\mbed-cmake\mbed-src\mbed.h:40,
                 from C:/Mbed-Cmake/mbed_ws/mbed-cmake-example-project/HelloWorld.cpp:1:
C:/Mbed-Cmake/mbed_ws/mbed-cmake-example-project/mbed-cmake/mbed-src/targets/TARGET_STM/TARGET_STM32G4/objects.h:22:10: fatal error: PinNames.h: No such file or directory
   22 | #include "PinNames.h"
      |          ^~~~~~~~~~~~
compilation terminated.
[7/262] Building C object mbed-cmake/mbed-src/CMakeFiles/mbed-os-static.dir/cmsis/CMSIS_5/CMSIS/RTOS2/RTX/Library/cmsis_os1.c.obj
ninja: build stopped: subcommand failed.

The file structure is as follows:

mbed-cmake-example-project
	|
	---build
	---cmake
	---mbed-cmake
	---mbed-cmake-config
	---TARGET_NUCLEOS_G474RE
		---CMakeLists
		---custom_targets
		---PeripheralPins
		---PinNames
	---.gitignore
	---.gitmodules
	---.mbedignore
	---CMakeLists
	---HelloWorld
	---mbed_app
	---README

I have tried to follow the documentation but probably I missed something, any idea?

Thank you very much

The current development of this project is continued in

In the wiki of this repo you will find instructions for the setup and also for using custom targets.
Maybe you can try this.

Hello, I know the existence of Mbed-CE, it is also another track I’ll try in the future but I find mbed-cmake very interesting in the case I need to use the original mbed-os.

Thank you.

Ah, the issue is that you need to pass the -x option the directory containing the custom target folder(s).

So instead of

-x C:\Mbed-Cmake\mbed_ws\mbed-cmake-example-project\TARGET_NUCLEOS_G474RE

do

-x C:\Mbed-Cmake\mbed_ws\mbed-cmake-example-project\

Also, with mbed-cmake (not mbed-ce), you don’t need any CMakeLists.txt in the custom target directory. It will just include any and all source files in that directory.

Applying your proposal a new error occured, this is the command as you noted:

python3 mbed-cmake/configure_for_target.py -a mbed_app.json -i .mbedignore -x C:\Mbed-Cmake\mbed_ws\mbed-cmake-example-project\ NUCLEOS_G474RE

Now this is the error:

C:\Mbed-Cmake\mbed_ws\mbed-cmake-example-project>python3 mbed-cmake/configure_for_target.py -a mbed_app.json -i .mbedignore -x C:\Mbed-Cmake\mbed_ws\mbed-cmake-example-project\ NUCLEOS_G474RE
>> Configuring build system for target: NUCLEOS_G474RE
>> Collecting data for config C:\Mbed-Cmake\mbed_ws\mbed-cmake-example-project\mbed-cmake\mbed-src\tools/profiles/develop.json
Traceback (most recent call last):
  File "C:\Mbed-Cmake\mbed_ws\mbed-cmake-example-project\mbed-cmake\mbed-src\tools\targets\__init__.py", line 153, in target
    resolution_order = get_resolution_order(json_data, name, [])
  File "C:\Mbed-Cmake\mbed_ws\mbed-cmake-example-project\mbed-cmake\mbed-src\tools\targets\__init__.py", line 139, in get_resolution_order
    parents = json_data[target_name].get("inherits", [])
KeyError: 'NUCLEOS_G474RE'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Mbed-Cmake\mbed_ws\mbed-cmake-example-project\mbed-cmake\configure_for_target.py", line 216, in <module>
    profile_toolchain = build_api.prepare_toolchain(src_paths=[mbed_os_dir], build_dir=config_header_dir, target=target_name, toolchain_name=toolchain_name, build_profile=[profile_data], app_config=app_config_path)
  File "C:\Mbed-Cmake\mbed_ws\mbed-cmake-example-project\mbed-cmake\mbed-src\tools\build_api.py", line 484, in prepare_toolchain
    config = config or Config(target, src_paths, app_config=app_config)
  File "C:\Mbed-Cmake\mbed_ws\mbed-cmake-example-project\mbed-cmake\mbed-src\tools\config\__init__.py", line 535, in __init__
    self.target = generate_py_target(
  File "C:\Mbed-Cmake\mbed_ws\mbed-cmake-example-project\mbed-cmake\mbed-src\tools\targets\__init__.py", line 185, in generate_py_target
    return target(name, total_data)
  File "C:\Mbed-Cmake\mbed_ws\mbed-cmake-example-project\mbed-cmake\mbed-src\tools\targets\__init__.py", line 155, in target
    raise_from(NotSupportedException(
  File "C:\Users\permigue\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\future\utils\__init__.py", line 403, in raise_from
    exec(execstr, myglobals, mylocals)
  File "<string>", line 1, in <module>
tools.utils.NotSupportedException: target NUCLEOS_G474RE has an incomplete target definition

Now the tree is as follows, without CMakeLists:

mbed-cmake-example-project
	|
	---build
	---cmake
	---mbed-cmake
	---mbed-cmake-config
	---TARGET_NUCLEOS_G474RE
		---custom_targets
		---PeripheralPins
		---PinNames
	---.gitignore
	---.gitmodules
	---.mbedignore
	---CMakeLists
	---HelloWorld
	---mbed_app
	---README

Thank you very much for your help.

Ah, you need to move custom_targets.json up one level to the root dir.

Ok, one step further now fails when I execute:

ninja -j2 hello_world

The error:

In file included from C:/Mbed-Cmake/mbed_ws/mbed-cmake-example-project/mbed-cmake/mbed-src/targets/TARGET_STM/device.h:38,
                 from C:/Mbed-Cmake/mbed_ws/mbed-cmake-example-project/mbed-cmake/mbed-src/platform/include/platform/platform.h:28,
                 from c:\mbed-cmake\mbed_ws\mbed-cmake-example-project\mbed-cmake\mbed-src\mbed.h:40,
                 from C:/Mbed-Cmake/mbed_ws/mbed-cmake-example-project/HelloWorld.cpp:1:
C:/Mbed-Cmake/mbed_ws/mbed-cmake-example-project/mbed-cmake/mbed-src/targets/TARGET_STM/TARGET_STM32G4/objects.h:22:10: fatal error: PinNames.h: No such file or directory
   22 | #include "PinNames.h"
      |          ^~~~~~~~~~~~
compilation terminated.
[7/262] Building C object mbed-cmake/mbed-src/CMakeFiles/mbed-os-static.dir/cmsis/CMSIS_5/CMSIS/RTOS2/RTX/Library/cmsis_os1.c.obj
ninja: build stopped: subcommand failed.

Now the tree is:

mbed-cmake-example-project
	|
	---build
	---cmake
	---mbed-cmake
	---mbed-cmake-config
	---TARGET_NUCLEOS_G474RE
		---PeripheralPins
		---PinNames
	---.gitignore
	---.gitmodules
	---.mbedignore
	---CMakeLists
	---custom_targets
	---HelloWorld
	---mbed_app
	---README

Thank you very much

It looks like there is some issue with the CMakeList files, does any one managed to have custom targets working with mbed-cmake?

Thank you very much

I really am sorry but it seems I have a problem with my toolset installation in my PC, I followed the hole installation process (taking care about Jamie comments) in a different PC and everything is working fine, thank you very much to all of you.