From 0b0b63aa7d4ea8fd6d5c2eafe1127752707dae80 Mon Sep 17 00:00:00 2001 From: Valerii Koval Date: Fri, 7 Feb 2020 11:26:45 +0200 Subject: [PATCH] Update templates for Atom, VSCode, CLion (#3371) * Wrap flags with whitespace chars when exporting data for IDEs * Update IDEs templates Take into account compiler flags that can contain whitespace characters (e.g. -iprefix) * Update template for VSCode * Add history record --- HISTORY.rst | 2 + platformio/builder/tools/pioide.py | 11 ++++- platformio/ide/tpls/atom/.gcc-flags.json.tpl | 4 +- .../ide/tpls/clion/CMakeListsPrivate.txt.tpl | 10 +++-- .../vscode/.vscode/c_cpp_properties.json.tpl | 41 +++++++++++++++++-- 5 files changed, 57 insertions(+), 11 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index e0eb61f6..c8915ebe 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -42,6 +42,8 @@ PlatformIO Core 4.0 * Fixed an issue when invalid CLI command does not return non-zero exit code * Fixed an issue when Project Inspector crashes when flash use > 100% (`issue #3368 `_) * Fixed a "UnicodeDecodeError" when listing built-in libraries on macOS with Python 2.7 (`issue #3370 `_) +* Fixed an issue with improperly handled compiler flags with space symbols in VSCode template (`issue #3364 `_) + 4.1.0 (2019-11-07) ~~~~~~~~~~~~~~~~~~ diff --git a/platformio/builder/tools/pioide.py b/platformio/builder/tools/pioide.py index 253ce61d..d36d2012 100644 --- a/platformio/builder/tools/pioide.py +++ b/platformio/builder/tools/pioide.py @@ -138,9 +138,16 @@ def _get_svd_path(env): return None +def _escape_build_flag(flags): + return [flag if " " not in flag else '"%s"' % flag for flag in flags] + + def DumpIDEData(env): - LINTCCOM = "$CFLAGS $CCFLAGS $CPPFLAGS" - LINTCXXCOM = "$CXXFLAGS $CCFLAGS $CPPFLAGS" + + env["__escape_build_flag"] = _escape_build_flag + + LINTCCOM = "${__escape_build_flag(CFLAGS)} ${__escape_build_flag(CCFLAGS)} $CPPFLAGS" + LINTCXXCOM = "${__escape_build_flag(CXXFLAGS)} ${__escape_build_flag(CCFLAGS)} $CPPFLAGS" data = { "env_name": env["PIOENV"], diff --git a/platformio/ide/tpls/atom/.gcc-flags.json.tpl b/platformio/ide/tpls/atom/.gcc-flags.json.tpl index d2ddcf78..361c2f04 100644 --- a/platformio/ide/tpls/atom/.gcc-flags.json.tpl +++ b/platformio/ide/tpls/atom/.gcc-flags.json.tpl @@ -1,8 +1,8 @@ % _defines = " ".join(["-D%s" % d.replace(" ", "\\\\ ") for d in defines]) { "execPath": "{{ cxx_path }}", - "gccDefaultCFlags": "-fsyntax-only {{! cc_flags.replace(' -MMD ', ' ').replace('"', '\\"') }} {{ !_defines.replace('"', '\\"') }}", - "gccDefaultCppFlags": "-fsyntax-only {{! cxx_flags.replace(' -MMD ', ' ').replace('"', '\\"') }} {{ !_defines.replace('"', '\\"') }}", + "gccDefaultCFlags": "-fsyntax-only {{! to_unix_path(cc_flags).replace(' -MMD ', ' ').replace('"', '\\"') }} {{ !_defines.replace('"', '\\"') }}", + "gccDefaultCppFlags": "-fsyntax-only {{! to_unix_path(cxx_flags).replace(' -MMD ', ' ').replace('"', '\\"') }} {{ !_defines.replace('"', '\\"') }}", "gccErrorLimit": 15, "gccIncludePaths": "{{ ','.join(includes) }}", "gccSuppressWarnings": false diff --git a/platformio/ide/tpls/clion/CMakeListsPrivate.txt.tpl b/platformio/ide/tpls/clion/CMakeListsPrivate.txt.tpl index daf5f2bc..0c6eda0c 100644 --- a/platformio/ide/tpls/clion/CMakeListsPrivate.txt.tpl +++ b/platformio/ide/tpls/clion/CMakeListsPrivate.txt.tpl @@ -5,7 +5,7 @@ # please create `CMakeListsUser.txt` in the root of project. # The `CMakeListsUser.txt` will not be overwritten by PlatformIO. -%from platformio.project.helpers import (load_project_ide_data) +% from platformio.project.helpers import (load_project_ide_data) % % import re % @@ -22,6 +22,10 @@ % return path % end % +% def _escape(text): +% return to_unix_path(text).replace('"', '\\"') +% end +% % envs = config.envs() % if len(envs) > 1: @@ -37,8 +41,8 @@ set(SVD_PATH "{{ _normalize_path(svd_path) }}") SET(CMAKE_C_COMPILER "{{ _normalize_path(cc_path) }}") SET(CMAKE_CXX_COMPILER "{{ _normalize_path(cxx_path) }}") -SET(CMAKE_CXX_FLAGS "{{cxx_flags}}") -SET(CMAKE_C_FLAGS "{{cc_flags}}") +SET(CMAKE_CXX_FLAGS "{{ _normalize_path(to_unix_path(cxx_flags)) }}") +SET(CMAKE_C_FLAGS "{{ _normalize_path(to_unix_path(cc_flags)) }}") % STD_RE = re.compile(r"\-std=[a-z\+]+(\d+)") % cc_stds = STD_RE.findall(cc_flags) diff --git a/platformio/ide/tpls/vscode/.vscode/c_cpp_properties.json.tpl b/platformio/ide/tpls/vscode/.vscode/c_cpp_properties.json.tpl index 5df41c54..bb94aba2 100644 --- a/platformio/ide/tpls/vscode/.vscode/c_cpp_properties.json.tpl +++ b/platformio/ide/tpls/vscode/.vscode/c_cpp_properties.json.tpl @@ -13,6 +13,35 @@ % return to_unix_path(text).replace('"', '\\"') % end % +% def _escape_required(flag): +% return " " in flag and systype == "windows" +% end +% +% def _split_flags(flags): +% result = [] +% i = 0 +% flags = flags.strip() +% while i < len(flags): +% current_arg = [] +% while i < len(flags) and flags[i] != " ": +% if flags[i] == '"': +% quotes_idx = flags.find('"', i + 1) +% current_arg.extend(flags[i + 1:quotes_idx]) +% i = quotes_idx + 1 +% else: +% current_arg.append(flags[i]) +% i = i + 1 +% end +% end +% arg = "".join(current_arg) +% if arg.strip(): +% result.append(arg.strip()) +% end +% i = i + 1 +% end +% return result +% end +% % cleaned_includes = [] % for include in includes: % if "toolchain-" not in dirname(commonprefix([include, cc_path])) and isdir(include): @@ -55,16 +84,20 @@ % cc_stds = STD_RE.findall(cc_flags) % cxx_stds = STD_RE.findall(cxx_flags) % -% # pass only architecture specific flags -% cc_m_flags = " ".join([f.strip() for f in cc_flags.split(" ") if f.strip().startswith(("-m", "-i", "@"))]) -% % if cc_stds: "cStandard": "c{{ cc_stds[-1] }}", % end % if cxx_stds: "cppStandard": "c++{{ cxx_stds[-1] }}", % end - "compilerPath": "\"{{cc_path}}\" {{! _escape(cc_m_flags) }}" + "compilerPath": "{{ cc_path }}", + "compilerArgs": [ +% for flag in [ '"%s"' % _escape(f) if _escape_required(f) else f for f in _split_flags( +% cc_flags) if f.startswith(("-m", "-i", "@"))]: + "{{ flag }}", +% end + "" + ] } ], "version": 4