Newer
Older
mbed-os / tools / cmake / mbed_ide_debug_cfg_generator.cmake
# 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
"<!-- Autogenerated by Mbed OS.  Do not edit! -->
<component name=\"ProjectRunConfigurationManager\">
  <configuration default=\"false\" name=\"${CONFIG_NAME}\" type=\"CLion_Remote\" remoteCommand=\"localhost:${GDB_PORT}\" symbolFile=\"$<TARGET_FILE:${CMAKE_TARGET}>\" sysroot=\"\">
    <method v=\"2\" />
  </configuration>
</component>
")
	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\": [")

	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}")

	    # from here: https://stackoverflow.com/questions/38089178/is-it-possible-to-attach-to-a-remote-gdb-target-with-vscode
	    set_property(GLOBAL APPEND_STRING PROPERTY VSCODE_LAUNCH_JSON_CONTENT "
	{
	    \"type\": \"gdb\",
	    \"request\": \"attach\",
	    \"name\": \"${CONFIG_NAME}\",
	    \"executable\": \"$<TARGET_FILE:${CMAKE_TARGET}>\",
	    \"target\": \"localhost:${GDB_PORT}\",
	    \"remote\": true,
	    \"cwd\": \"\${workspaceRoot}\",
	    \"gdbpath\": \"arm-none-eabi-gdb\"
	},")

	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()