From 78878c6183850320e313961deae80ef10bc16c48 Mon Sep 17 00:00:00 2001 From: stephendpmurphy Date: Wed, 13 Nov 2024 09:36:20 -0500 Subject: [PATCH 1/4] Add CMake modules to generate VSCode Launch and Settings files --- .gitignore | 5 +--- CMakeLists.txt | 10 +++++-- cmake/debug_jlink.cmake | 46 +------------------------------- cmake/debug_pyocd.cmake | 23 +--------------- cmake/vscode_debug.cmake | 47 +++++++++++++++++++++++++++++++++ cmake/vscode_intellisense.cmake | 32 ++++++++++++++++++++++ 6 files changed, 90 insertions(+), 73 deletions(-) create mode 100644 cmake/vscode_debug.cmake create mode 100644 cmake/vscode_intellisense.cmake diff --git a/.gitignore b/.gitignore index 84b2d03..5a7364f 100644 --- a/.gitignore +++ b/.gitignore @@ -23,9 +23,6 @@ output/ build_log.txt # VScode settings -.vscode/*cortex-debug* -.vscode/settings.json -.vscode/*_properties.json -.vscode/launch.json +.vscode/ *.jlink \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e7012f..4eea17a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,9 @@ set(DEBUG_TOOLSET "PYOCD") # THIS OPTION IS ONLY SUPPORTED WITH THE JLINK TOOLSET set(ENABLE_SEGGER_RTT false) +set(DEBUG_INTERFACE "swd") +# set(DEBUG_INTERFACE "jtag") + # MCU Target name as seen in debug toolset set(MCU_TARGET LPC5526) @@ -74,7 +77,7 @@ project(${PROJECT_NAME} VERSION 1.0 LANGUAGES C) # Set the C standard and executable suffix set(CMAKE_EXECUTABLE_SUFFIX ".elf") -set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD_REQUIRED ON) # Set our executable output path to the 'output/' folder @@ -162,4 +165,7 @@ add_custom_target(erase ${DEBUG_ERASE_CMD}) add_custom_target(flash ${DEBUG_FLASH_CMD}) # Add a makefile "target" for running unit tests -add_custom_target(test cd ../ && ceedling gcov:all utils:gcov) \ No newline at end of file +add_custom_target(test cd ../ && ceedling gcov:all utils:gcov) + +include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/vscode_debug.cmake) +include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/vscode_intellisense.cmake) \ No newline at end of file diff --git a/cmake/debug_jlink.cmake b/cmake/debug_jlink.cmake index 82d2a86..f8830e8 100644 --- a/cmake/debug_jlink.cmake +++ b/cmake/debug_jlink.cmake @@ -18,48 +18,4 @@ loadfile ${EXECUTABLE_OUTPUT_PATH}/${EXECUTABLE_NAME}.hex r g exit" -) - -write_file(${CMAKE_CURRENT_SOURCE_DIR}/.vscode/launch.json -{\n - \t\"version\": \"0.2.0\",\n - \t\"configurations\": [\n - \t{\n - \t\t\"name\": \"${MCU_TARGET}\",\n - \t\t\"type\": \"cortex-debug\",\n - \t\t\"request\": \"launch\",\n - \t\t\"servertype\":\"jlink\",\n - \t\t\"cwd\": \"${workspaceRoot}\",\n - \t\t\"executable\": \"${EXECUTABLE_OUTPUT_PATH}/${EXECUTABLE_NAME}.elf\",\n - \t\t\"device\": \"${MCU_TARGET}\",\n - \t\t\"runToMain\": true,\n - \t\t\"v1\": false,\n - \t\t\"serverArgs\": [\n - \t\t\t\"--target\",\n - \t\t\t\"${MCU_TARGET}\"\n - \t\t], -) - -if(${ENABLE_SEGGER_RTT}) - -write_file(${CMAKE_CURRENT_SOURCE_DIR}/.vscode/launch.json -\t\t"rttConfig":{\n - \t\t\t"enabled":true,\n - \t\t\t"address":"auto",\n - \t\t\t"decoders":[{\n - \t\t\t\t"port":0,\n - \t\t\t\t"type":"console"\n - \t\t\t}]\n -\t\t}\n -\t}]\n -} -APPEND) - -else() - -write_file(${CMAKE_CURRENT_SOURCE_DIR}/.vscode/launch.json -\t}]\n -} -APPEND) - -endif() \ No newline at end of file +) \ No newline at end of file diff --git a/cmake/debug_pyocd.cmake b/cmake/debug_pyocd.cmake index aefbb14..4bf0bc9 100644 --- a/cmake/debug_pyocd.cmake +++ b/cmake/debug_pyocd.cmake @@ -1,23 +1,2 @@ set(DEBUG_ERASE_CMD "pyocd" "erase" "-t${MCU_TARGET}" "--chip") -set(DEBUG_FLASH_CMD "pyocd" "flash" "-t${MCU_TARGET}" "-f20000khz" "${EXECUTABLE_OUTPUT_PATH}/${EXECUTABLE_NAME}.elf") - -write_file(${CMAKE_CURRENT_SOURCE_DIR}/.vscode/launch.json -{\n - \t\"version\": \"0.2.0\",\n - \t\"configurations\": [\n - \t{\n - \t\t\"name\": \"${MCU_TARGET}\",\n - \t\t\"type\": \"cortex-debug\",\n - \t\t\"request\": \"launch\",\n - \t\t\"servertype\":\"pyocd\",\n - \t\t\"cwd\": \"${workspaceRoot}\",\n - \t\t\"executable\": \"${EXECUTABLE_OUTPUT_PATH}/${EXECUTABLE_NAME}.elf\",\n - \t\t\"device\": \"${MCU_TARGET}\",\n - \t\t\"runToMain\": true,\n - \t\t\"v1\": false,\n - \t\t\"serverArgs\": [\n - \t\t\t\"--target\",\n - \t\t\t\"${MCU_TARGET}\"\n - \t\t]\n - \t}]\n -}) \ No newline at end of file +set(DEBUG_FLASH_CMD "pyocd" "flash" "-t${MCU_TARGET}" "-f20000khz" "${EXECUTABLE_OUTPUT_PATH}/${EXECUTABLE_NAME}.elf") \ No newline at end of file diff --git a/cmake/vscode_debug.cmake b/cmake/vscode_debug.cmake new file mode 100644 index 0000000..ab28a21 --- /dev/null +++ b/cmake/vscode_debug.cmake @@ -0,0 +1,47 @@ +# Make sure to create the .vscode directory if it doesn't exist +file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/.vscode) + +string(TOLOWER ${DEBUG_TOOLSET} DEBUG_SERVER_TYPE) + +# Specify the template for the c_cpp_properties.json file +write_file(${CMAKE_CURRENT_SOURCE_DIR}/.vscode/launch.json " +{ + \"version\":\"0.2.0\", + \"configurations\":[ + { + \"name\":\"${DEBUG_TOOLSET}\", + \"type\":\"cortex-debug\", + \"request\":\"launch\", + \"servertype\":\"${DEBUG_SERVER_TYPE}\", + \"cwd\":\"\", + \"executable\":\"${EXECUTABLE_OUTPUT_PATH}/${EXECUTABLE_NAME}.elf\", + \"device\":\"${MCU_TARGET}\", + \"interface\": \"${DEBUG_INTERFACE}\", + \"v1\":false, + \"serverArgs\":[ + \"--nogui\" + ]," +) + +if(${ENABLE_SEGGER_RTT} AND (${DEBUG_SERVER_TYPE} STREQUAL "jlink")) + +write_file(${CMAKE_CURRENT_SOURCE_DIR}/.vscode/launch.json +" \"rttConfig\":{ + \"enabled\":true, + \"address\":\"auto\", + \"decoders\":[{ + \"port\":0, + \"type\":\"console\" + }] + } + }] +}" APPEND) + +else() + +write_file(${CMAKE_CURRENT_SOURCE_DIR}/.vscode/launch.json +" }] +}" +APPEND) + +endif() \ No newline at end of file diff --git a/cmake/vscode_intellisense.cmake b/cmake/vscode_intellisense.cmake new file mode 100644 index 0000000..4c14cf0 --- /dev/null +++ b/cmake/vscode_intellisense.cmake @@ -0,0 +1,32 @@ +# Make sure to create the .vscode directory if it doesn't exist +file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/.vscode) + +# Note, if a seperate include variable is used for SDK includes you would append it here +set(INTELLISENSE_INCLDUES ${INCLUDE_DIRS}) + +# Generate the list of include directories as a JSON array +set(INCLUDE_DIRS_JSON "") +foreach(INCLUDE_DIR ${INTELLISENSE_INCLDUES}) + list(APPEND INCLUDE_DIRS_JSON "\"${INCLUDE_DIR}\"") +endforeach() +string(JOIN ",\n" INCLUDE_DIRS_JSON ${INCLUDE_DIRS_JSON}) + +# Specify the template for the c_cpp_properties.json file +file(GENERATE OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/.vscode/c_cpp_properties.json CONTENT " +{ + \"configurations\": [ + { + \"name\": \"${CMAKE_BUILD_TYPE}\", + \"includePath\": [ + ${INCLUDE_DIRS_JSON} + ], + \"defines\": [\"\"], + \"compilerPath\": \"${CMAKE_C_COMPILER}\", + \"cStandard\": \"c99\", + \"cppStandard\": \"c++17\", + \"intelliSenseMode\": \"gcc-arm\" + } + ], + \"version\": 1 +} +") \ No newline at end of file From 86d5f433c1ed11ca94fe77c1431cb84798279400 Mon Sep 17 00:00:00 2001 From: stephendpmurphy Date: Wed, 13 Nov 2024 20:26:26 -0500 Subject: [PATCH 2/4] Add compileCommands param to cpp_settings and newlines in each .json file --- cmake/vscode_debug.cmake | 7 +++---- cmake/vscode_intellisense.cmake | 8 ++++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/cmake/vscode_debug.cmake b/cmake/vscode_debug.cmake index ab28a21..0f9a7d3 100644 --- a/cmake/vscode_debug.cmake +++ b/cmake/vscode_debug.cmake @@ -4,8 +4,8 @@ file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/.vscode) string(TOLOWER ${DEBUG_TOOLSET} DEBUG_SERVER_TYPE) # Specify the template for the c_cpp_properties.json file -write_file(${CMAKE_CURRENT_SOURCE_DIR}/.vscode/launch.json " -{ +write_file(${CMAKE_CURRENT_SOURCE_DIR}/.vscode/launch.json +"{ \"version\":\"0.2.0\", \"configurations\":[ { @@ -41,7 +41,6 @@ else() write_file(${CMAKE_CURRENT_SOURCE_DIR}/.vscode/launch.json " }] -}" -APPEND) +}" APPEND) endif() \ No newline at end of file diff --git a/cmake/vscode_intellisense.cmake b/cmake/vscode_intellisense.cmake index 4c14cf0..728f45d 100644 --- a/cmake/vscode_intellisense.cmake +++ b/cmake/vscode_intellisense.cmake @@ -12,14 +12,15 @@ endforeach() string(JOIN ",\n" INCLUDE_DIRS_JSON ${INCLUDE_DIRS_JSON}) # Specify the template for the c_cpp_properties.json file -file(GENERATE OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/.vscode/c_cpp_properties.json CONTENT " -{ +write_file(${CMAKE_CURRENT_SOURCE_DIR}/.vscode/c_cpp_properties.json +"{ \"configurations\": [ { \"name\": \"${CMAKE_BUILD_TYPE}\", \"includePath\": [ ${INCLUDE_DIRS_JSON} ], + \"compileCommands\": \"\$\{workspaceFolder\}/build/compile_command.json\", \"defines\": [\"\"], \"compilerPath\": \"${CMAKE_C_COMPILER}\", \"cStandard\": \"c99\", @@ -28,5 +29,4 @@ file(GENERATE OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/.vscode/c_cpp_properties.json C } ], \"version\": 1 -} -") \ No newline at end of file +}") \ No newline at end of file From f6228b99a61e93c401ee43e18645b3e6cb8873ba Mon Sep 17 00:00:00 2001 From: stephendpmurphy Date: Wed, 13 Nov 2024 20:28:34 -0500 Subject: [PATCH 3/4] Adding newlines to cmake module --- cmake/vscode_debug.cmake | 2 +- cmake/vscode_intellisense.cmake | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmake/vscode_debug.cmake b/cmake/vscode_debug.cmake index 0f9a7d3..989859a 100644 --- a/cmake/vscode_debug.cmake +++ b/cmake/vscode_debug.cmake @@ -43,4 +43,4 @@ write_file(${CMAKE_CURRENT_SOURCE_DIR}/.vscode/launch.json " }] }" APPEND) -endif() \ No newline at end of file +endif() diff --git a/cmake/vscode_intellisense.cmake b/cmake/vscode_intellisense.cmake index 728f45d..5ba8860 100644 --- a/cmake/vscode_intellisense.cmake +++ b/cmake/vscode_intellisense.cmake @@ -29,4 +29,4 @@ write_file(${CMAKE_CURRENT_SOURCE_DIR}/.vscode/c_cpp_properties.json } ], \"version\": 1 -}") \ No newline at end of file +}") From fb08abd901ccb61daba5275fe9b6f79763bc9b22 Mon Sep 17 00:00:00 2001 From: stephendpmurphy Date: Tue, 19 Nov 2024 14:32:01 -0500 Subject: [PATCH 4/4] Rework intellisense module to find all include dirs for the executable target automatically --- cmake/vscode_intellisense.cmake | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/cmake/vscode_intellisense.cmake b/cmake/vscode_intellisense.cmake index 5ba8860..5891dd1 100644 --- a/cmake/vscode_intellisense.cmake +++ b/cmake/vscode_intellisense.cmake @@ -1,12 +1,33 @@ # Make sure to create the .vscode directory if it doesn't exist file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/.vscode) -# Note, if a seperate include variable is used for SDK includes you would append it here -set(INTELLISENSE_INCLDUES ${INCLUDE_DIRS}) +get_target_property(LINKED_LIBRARIES ${EXECUTABLE_NAME} LINK_LIBRARIES) + +if(LINKED_LIBRARIES) + foreach(lib ${LINKED_LIBRARIES}) + get_target_property(LIBRARIES_INCLUDE_DIRS ${lib} INCLUDE_DIRECTORIES) + if(LIBRARIES_INCLUDE_DIRS) + set(INTELLISENSE_INCLUDES ${INTELLISENSE_INCLUDES} ${LIBRARIES_INCLUDE_DIRS}) + endif() + endforeach() + + foreach(lib ${LINKED_LIBRARIES}) + get_target_property(INTF_LIBRARIES_INCLUDE_DIRS ${lib} INTERFACE_INCLUDE_DIRECTORIES) + if(INTF_LIBRARIES_INCLUDE_DIRS) + set(INTELLISENSE_INCLUDES ${INTELLISENSE_INCLUDES} ${INTF_LIBRARIES_INCLUDE_DIRS}) + endif() + endforeach() +endif() + +get_target_property(APP_INCLUDE_DIRS ${EXECUTABLE_NAME} INCLUDE_DIRECTORIES) + +if(APP_INCLUDE_DIRS) + set(INTELLISENSE_INCLUDES ${INTELLISENSE_INCLUDES} ${APP_INCLUDE_DIRS}) +endif() # Generate the list of include directories as a JSON array set(INCLUDE_DIRS_JSON "") -foreach(INCLUDE_DIR ${INTELLISENSE_INCLDUES}) +foreach(INCLUDE_DIR ${INTELLISENSE_INCLUDES}) list(APPEND INCLUDE_DIRS_JSON "\"${INCLUDE_DIR}\"") endforeach() string(JOIN ",\n" INCLUDE_DIRS_JSON ${INCLUDE_DIRS_JSON})