# Copyright (c) 2022 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
# Script to automatically generate debug configurations for various IDEs.
# Detect IDEs
# -------------------------------------------------------------
if($ENV{CLION_IDE})
message(STATUS "Detected CLion IDE, will generate CLion debug configurations")
set(MBED_GENERATE_CLION_DEBUG_CFGS TRUE)
elseif(CMAKE_EXPORT_COMPILE_COMMANDS) # TODO: Is this actually a reliable way of detecting VS Code? Not sure if it will create false positives.
message(STATUS "Detected VS Code IDE, will generate VS Code debug configurations")
set(MBED_GENERATE_VS_CODE_DEBUG_CFGS TRUE)
endif()
# CLion generator
# -------------------------------------------------------------
if(MBED_GENERATE_CLION_DEBUG_CFGS)
# Find CLion run config dir
set(CLION_RUN_CONF_DIR ${CMAKE_SOURCE_DIR}/.idea/runConfigurations)
file(MAKE_DIRECTORY ${CLION_RUN_CONF_DIR})
function(mbed_generate_ide_debug_configuration CMAKE_TARGET)
# Create name (combine target name, Mbed target, and build type to generate a unique string)
set(CONFIG_NAME "GDB ${CMAKE_TARGET} ${MBED_TARGET} ${CMAKE_BUILD_TYPE}")
set(RUN_CONF_PATH "${CLION_RUN_CONF_DIR}/${CONFIG_NAME}.xml")
file(GENERATE OUTPUT ${RUN_CONF_PATH} CONTENT
"
\" sysroot=\"\">
")
endfunction(mbed_generate_ide_debug_configuration)
function(mbed_finalize_ide_debug_configurations)
# Don't need to do anything
endfunction(mbed_finalize_ide_debug_configurations)
# VS Code generator
# -------------------------------------------------------------
elseif(MBED_GENERATE_VS_CODE_DEBUG_CFGS)
set(VSCODE_LAUNCH_JSON_PATH ${CMAKE_SOURCE_DIR}/.vscode/launch.json)
# Start building up json file. Needs to be a global property so we can append to it from anywhere.
# Note: Cannot use a cache variable for this because cache variables aren't allowed to contain newlines.
set_property(GLOBAL PROPERTY VSCODE_LAUNCH_JSON_CONTENT
"{
\"configurations\": [")
# Find objdump as the extension uses it. In a sane world it should be in the compiler bin dir.
find_program(MBED_OBJDUMP
NAMES arm-none-eabi-objdump objdump
HINTS ${CMAKE_COMPILER_BIN_DIR}
DOC "Path to the GNU objdump program. Needed for VS Code cortex-debug"
REQURED)
function(mbed_generate_ide_debug_configuration CMAKE_TARGET)
# Create name (combine target name, Mbed target, and build config to generate a unique string)
set(CONFIG_NAME "Connect to GDB ${CMAKE_TARGET} ${MBED_TARGET} ${CMAKE_BUILD_TYPE}")
# property list here: https://github.com/Marus/cortex-debug/blob/master/debug_attributes.md
set_property(GLOBAL APPEND_STRING PROPERTY VSCODE_LAUNCH_JSON_CONTENT "
{
\"type\": \"cortex-debug\",
\"name\": \"${CONFIG_NAME}\",
\"executable\": \"$\",
\"cwd\": \"\${workspaceRoot}\",
\"gdbPath\": \"${MBED_GDB}\",
\"objdumpPath\": \"${MBED_OBJDUMP}\",
\"servertype\": \"external\",
\"gdbTarget\": \"localhost:${GDB_PORT}\",
\"request\": \"attach\"
},")
endfunction(mbed_generate_ide_debug_configuration)
# Take all generated debug configurations and write them to launch.json.
function(mbed_finalize_ide_debug_configurations)
# Add footer
set_property(GLOBAL APPEND_STRING PROPERTY VSCODE_LAUNCH_JSON_CONTENT "
]
}")
get_property(VSCODE_LAUNCH_JSON_CONTENT GLOBAL PROPERTY VSCODE_LAUNCH_JSON_CONTENT)
file(GENERATE OUTPUT ${VSCODE_LAUNCH_JSON_PATH} CONTENT ${VSCODE_LAUNCH_JSON_CONTENT})
endfunction(mbed_finalize_ide_debug_configurations)
# No-op generator
# -------------------------------------------------------------
else()
function(mbed_generate_ide_debug_configuration CMAKE_TARGET)
# Empty
endfunction(mbed_generate_ide_debug_configuration)
function(mbed_finalize_ide_debug_configurations)
# Empty
endfunction(mbed_finalize_ide_debug_configurations)
endif()