Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broken launch.json with multiple targets #15

Open
Rimpampa opened this issue Jul 16, 2022 · 1 comment
Open

Broken launch.json with multiple targets #15

Rimpampa opened this issue Jul 16, 2022 · 1 comment

Comments

@Rimpampa
Copy link

I have a project that is structured in this way:

project/
├── target-1/
│   ├── ...
│   └── target-1.ioc
├── target-2/
│   ├── ...
│   └── target-2.ioc
├── ...
└── CMakeLists.txt

Where the CMakeLists.txt file contains something like this:

include(cubemx.cmake/cubemx.cmake)

project(project)

add_executable(target-1)
cubemx_target(
    TARGET target-1
    CUBEMX_SOURCE_DIR target-1
    FLASH_TARGET_NAME target-1-flash
    IOC "${CMAKE_CURRENT_LIST_DIR}/target-1/target-1.ioc"
)

...

add_executable(target-2)
cubemx_target(
    TARGET target-2
    CUBEMX_SOURCE_DIR target-2
    FLASH_TARGET_NAME target-2-flash
    IOC "${CMAKE_CURRENT_LIST_DIR}/target-2/target-2.ioc"
)

...

The problem with this configuration is that the .vscode/launch.json is (kinda) broken because it contains the configuration for only one of the targets.


The problem is that vscode-debug.cmake for every configured target, it replaces the entire contents of that file, overwriting any previously saved configuration.

What it should do instead is to overwrite only the entries of the "configurations" JSON array object that have the same name of the selected targets.


This is obviously much more difficult to implement, given that (IIRC) there is no JSON support to CMake and that it's not a trivial operation. One of the edge cases I can think of is removing a target, that would leave the configuration for that target untouched, which means that the user would have to delete it manually.

There is also the fact that this type of project structure can probably be changed for a better one or that I've missed something that could make this work without any changes.

@patrislav1
Copy link
Owner

I solved it in a different project like this:

  • Create launch.json.part per target
  • Read the launch.json.part contents into a CMake variable
  • Append the contents to a environment variable LAUNCH_JSON
  • Write the LAUNCH_JSON to the file

It's a bit ugly b/c the launch.json gets generated and overwritten once for each target, but it works

# Create launch.json for a target and append it to the global launch.json
function(launch_json PROJ_NAME)
    set(LAUNCH_JSON $ENV{LAUNCH_JSON})
    configure_file(${TEMPLATE_DIR}/vscode-pyocd.in ${CMAKE_BINARY_DIR}/launch.json.part @ONLY)
    file(READ ${CMAKE_BINARY_DIR}/launch.json.part LAUNCH_JSON_PART)
    string(APPEND LAUNCH_JSON "${LAUNCH_JSON_PART}")
    set(ENV{LAUNCH_JSON} "${LAUNCH_JSON}")
    configure_file("${TEMPLATE_DIR}/launch.json.in" "${VSCODE_DIR}/launch.json" @ONLY)
endfunction()

vscode-pyocd.in

        {
            "name": "@PROJ_NAME@",
            "cwd": "${workspaceRoot}/pyocd",
            "executable": "${workspaceRoot}/build/@PROJ_NAME@/@PROJ_NAME@.elf",
            "request": "attach",
            "type": "cortex-debug",
            "servertype": "pyocd",
            "serverArgs": [@PYOCD_ARGLIST_OPT@],
            "svdFile": "${workspaceRoot}/pyocd/..."
        },

launch.json.in

{
    "configurations": [
@LAUNCH_JSON@
    ]
}

I won't be able to implement and test it myself right now in cubemx.cmake, b/c I'm not working with this stuff at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants