Files
platformio-core/platformio/builder/tools/piomisc.py

371 lines
11 KiB
Python
Raw Normal View History

# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
2015-11-30 01:11:57 +02:00
from __future__ import absolute_import
import atexit
import re
import sys
2016-09-02 18:45:19 +03:00
from os import environ, remove, walk
from os.path import basename, isdir, isfile, join, realpath, relpath, sep
2016-08-31 00:16:23 +03:00
from tempfile import mkstemp
2019-05-07 21:16:42 +03:00
from SCons.Action import Action # pylint: disable=import-error
from SCons.Script import ARGUMENTS # pylint: disable=import-error
2016-08-27 19:30:38 +03:00
from platformio import fs, util
from platformio.compat import glob_escape
2017-04-28 01:38:25 +03:00
from platformio.managers.core import get_core_package_dir
from platformio.proc import exec_command
class InoToCPPConverter(object):
2018-06-08 21:37:57 +03:00
PROTOTYPE_RE = re.compile(
r"""^(
(?:template\<.*\>\s*)? # template
([a-z_\d\&]+\*?\s+){1,2} # return type
([a-z_\d]+\s*) # name of prototype
\([a-z_,\.\*\&\[\]\s\d]*\) # arguments
)\s*(\{|;) # must end with `{` or `;`
""",
re.X | re.M | re.I,
)
DETECTMAIN_RE = re.compile(r"void\s+(setup|loop)\s*\(", re.M | re.I)
PROTOPTRS_TPLRE = r"\([^&\(]*&(%s)[^\)]*\)"
2016-08-31 00:16:23 +03:00
def __init__(self, env):
self.env = env
self._main_ino = None
def is_main_node(self, contents):
return self.DETECTMAIN_RE.search(contents)
2016-08-31 00:16:23 +03:00
def convert(self, nodes):
contents = self.merge(nodes)
if not contents:
return None
2016-08-31 00:16:23 +03:00
return self.process(contents)
def merge(self, nodes):
assert nodes
2016-08-31 00:16:23 +03:00
lines = []
for node in nodes:
contents = fs.get_file_contents(node.get_path())
_lines = ['# 1 "%s"' % node.get_path().replace("\\", "/"), contents]
2016-08-31 00:16:23 +03:00
if self.is_main_node(contents):
lines = _lines + lines
self._main_ino = node.get_path()
else:
lines.extend(_lines)
if not self._main_ino:
self._main_ino = nodes[0].get_path()
2016-08-31 00:16:23 +03:00
return "\n".join(["#include <Arduino.h>"] + lines) if lines else None
def process(self, contents):
out_file = self._main_ino + ".cpp"
assert self._gcc_preprocess(contents, out_file)
contents = fs.get_file_contents(out_file)
contents = self._join_multiline_strings(contents)
fs.write_file_contents(out_file, self.append_prototypes(contents))
2016-08-31 00:16:23 +03:00
return out_file
def _gcc_preprocess(self, contents, out_file):
tmp_path = mkstemp()[1]
fs.write_file_contents(tmp_path, contents)
2016-08-31 00:16:23 +03:00
self.env.Execute(
self.env.VerboseAction(
'$CXX -o "{0}" -x c++ -fpreprocessed -dD -E "{1}"'.format(
out_file, tmp_path
),
"Converting " + basename(out_file[:-4]),
)
)
atexit.register(_delete_file, tmp_path)
2016-08-31 00:16:23 +03:00
return isfile(out_file)
def _join_multiline_strings(self, contents):
if "\\\n" not in contents:
return contents
newlines = []
linenum = 0
stropen = False
for line in contents.split("\n"):
_linenum = self._parse_preproc_line_num(line)
if _linenum is not None:
linenum = _linenum
else:
linenum += 1
if line.endswith("\\"):
if line.startswith('"'):
stropen = True
newlines.append(line[:-1])
continue
if stropen:
newlines[len(newlines) - 1] += line[:-1]
continue
elif stropen and line.endswith(('",', '";')):
newlines[len(newlines) - 1] += line
stropen = False
newlines.append(
'#line %d "%s"' % (linenum, self._main_ino.replace("\\", "/"))
)
continue
newlines.append(line)
return "\n".join(newlines)
@staticmethod
def _parse_preproc_line_num(line):
if not line.startswith("#"):
return None
tokens = line.split(" ", 3)
if len(tokens) > 2 and tokens[1].isdigit():
return int(tokens[1])
return None
2016-08-31 00:16:23 +03:00
def _parse_prototypes(self, contents):
prototypes = []
reserved_keywords = set(["if", "else", "while"])
for match in self.PROTOTYPE_RE.finditer(contents):
if (
set([match.group(2).strip(), match.group(3).strip()])
& reserved_keywords
):
continue
2016-08-31 00:16:23 +03:00
prototypes.append(match)
return prototypes
def _get_total_lines(self, contents):
2016-08-31 00:16:23 +03:00
total = 0
if contents.endswith("\n"):
contents = contents[:-1]
2016-08-31 00:16:23 +03:00
for line in contents.split("\n")[::-1]:
linenum = self._parse_preproc_line_num(line)
if linenum is not None:
return total + linenum
2016-08-31 00:16:23 +03:00
total += 1
return total
def append_prototypes(self, contents):
prototypes = self._parse_prototypes(contents) or []
# skip already declared prototypes
declared = set(m.group(1).strip() for m in prototypes if m.group(4) == ";")
prototypes = [m for m in prototypes if m.group(1).strip() not in declared]
if not prototypes:
return contents
prototype_names = set(m.group(3).strip() for m in prototypes)
2016-08-31 00:16:23 +03:00
split_pos = prototypes[0].start()
2018-06-08 21:37:57 +03:00
match_ptrs = re.search(
self.PROTOPTRS_TPLRE % ("|".join(prototype_names)),
contents[:split_pos],
re.M,
)
if match_ptrs:
split_pos = contents.rfind("\n", 0, match_ptrs.start()) + 1
2016-08-31 00:16:23 +03:00
result = []
result.append(contents[:split_pos].strip())
2016-08-31 00:16:23 +03:00
result.append("%s;" % ";\n".join([m.group(1) for m in prototypes]))
result.append(
'#line %d "%s"'
% (
self._get_total_lines(contents[:split_pos]),
self._main_ino.replace("\\", "/"),
)
)
result.append(contents[split_pos:].strip())
return "\n".join(result)
def ConvertInoToCpp(env):
src_dir = glob_escape(env.subst("$PROJECT_SRC_DIR"))
ino_nodes = env.Glob(join(src_dir, "*.ino")) + env.Glob(join(src_dir, "*.pde"))
if not ino_nodes:
return
2016-08-31 00:16:23 +03:00
c = InoToCPPConverter(env)
out_file = c.convert(ino_nodes)
2016-08-31 00:16:23 +03:00
atexit.register(_delete_file, out_file)
def _delete_file(path):
try:
if isfile(path):
remove(path)
except: # pylint: disable=bare-except
pass
@util.memoized()
def _get_compiler_type(env):
if env.subst("$CC").endswith("-gcc"):
return "gcc"
try:
2015-08-01 18:33:41 +03:00
sysenv = environ.copy()
sysenv["PATH"] = str(env["ENV"]["PATH"])
result = exec_command([env.subst("$CC"), "-v"], env=sysenv)
except OSError:
return None
if result["returncode"] != 0:
return None
output = "".join([result["out"], result["err"]]).lower()
if "clang" in output and "LLVM" in output:
return "clang"
if "gcc" in output:
return "gcc"
return None
def GetCompilerType(env):
return _get_compiler_type(env)
def GetActualLDScript(env):
2016-10-04 01:21:26 +03:00
def _lookup_in_ldpath(script):
for d in env.get("LIBPATH", []):
path = join(env.subst(d), script)
if isfile(path):
return path
return None
2015-12-28 20:09:48 +02:00
script = None
script_in_next = False
for f in env.get("LINKFLAGS", []):
raw_script = None
if f == "-T":
script_in_next = True
continue
if script_in_next:
script_in_next = False
raw_script = f
elif f.startswith("-Wl,-T"):
raw_script = f[6:]
else:
continue
script = env.subst(raw_script.replace('"', "").strip())
if isfile(script):
return script
path = _lookup_in_ldpath(script)
if path:
return path
2015-12-28 20:09:48 +02:00
if script:
sys.stderr.write(
"Error: Could not find '%s' LD script in LDPATH '%s'\n"
% (script, env.subst("$LIBPATH"))
)
env.Exit(1)
2015-12-28 20:09:48 +02:00
2016-10-04 01:21:26 +03:00
if not script and "LDSCRIPT_PATH" in env:
path = _lookup_in_ldpath(env["LDSCRIPT_PATH"])
2016-10-04 01:21:26 +03:00
if path:
return path
sys.stderr.write("Error: Could not find LD script\n")
env.Exit(1)
2016-08-27 19:30:53 +03:00
def VerboseAction(_, act, actstr):
2016-08-27 19:30:38 +03:00
if int(ARGUMENTS.get("PIOVERBOSE", 0)):
return act
2017-04-15 16:36:59 +03:00
return Action(act, actstr)
2016-09-02 18:45:19 +03:00
def PioClean(env, clean_dir):
if not isdir(clean_dir):
2019-09-27 18:53:58 +03:00
print ("Build environment is clean")
2016-09-02 18:45:19 +03:00
env.Exit(0)
clean_rel_path = relpath(clean_dir)
2016-09-02 18:45:19 +03:00
for root, _, files in walk(clean_dir):
for f in files:
dst = join(root, f)
remove(dst)
2019-09-27 18:53:58 +03:00
print (
"Removed %s" % (dst if clean_rel_path.startswith(".") else relpath(dst))
)
2019-09-27 18:53:58 +03:00
print ("Done cleaning")
fs.rmtree(clean_dir)
2016-09-02 18:45:19 +03:00
env.Exit(0)
2019-09-27 17:22:21 +03:00
def ConfigureDebugFlags(env):
def _cleanup_debug_flags(scope):
if scope not in env:
return
unflags = ["-Os", "-g"]
for level in [0, 1, 2]:
for flag in ("O", "g", "ggdb"):
unflags.append("-%s%d" % (flag, level))
env[scope] = [f for f in env.get(scope, []) if f not in unflags]
env.Append(CPPDEFINES=["__PLATFORMIO_BUILD_DEBUG__"])
2019-10-04 13:27:02 +03:00
debug_flags = ["-Og", "-g2", "-ggdb2"]
2019-10-03 18:16:55 +03:00
for scope in ("ASFLAGS", "CCFLAGS", "LINKFLAGS"):
2019-09-27 17:22:21 +03:00
_cleanup_debug_flags(scope)
env.Append(**{scope: debug_flags})
2017-04-28 01:38:25 +03:00
def ConfigureTestTarget(env):
env.Append(
CPPDEFINES=["UNIT_TEST", "UNITY_INCLUDE_CONFIG_H"],
CPPPATH=[join("$BUILD_DIR", "UnityTestLib")],
)
unitylib = env.BuildLibrary(
join("$BUILD_DIR", "UnityTestLib"), get_core_package_dir("tool-unity")
)
2017-04-28 01:38:25 +03:00
env.Prepend(LIBS=[unitylib])
2017-07-03 13:35:39 +03:00
src_filter = ["+<*.cpp>", "+<*.c>"]
if "PIOTEST_RUNNING_NAME" in env:
src_filter.append("+<%s%s>" % (env["PIOTEST_RUNNING_NAME"], sep))
env.Replace(PIOTEST_SRC_FILTER=src_filter)
2017-04-28 01:38:25 +03:00
def GetExtraScripts(env, scope):
items = []
for item in env.GetProjectOption("extra_scripts", []):
if scope == "post" and ":" not in item:
items.append(item)
elif item.startswith("%s:" % scope):
items.append(item[len(scope) + 1 :])
if not items:
return items
with fs.cd(env.subst("$PROJECT_DIR")):
return [realpath(item) for item in items]
def exists(_):
return True
def generate(env):
env.AddMethod(ConvertInoToCpp)
env.AddMethod(GetCompilerType)
env.AddMethod(GetActualLDScript)
2016-08-27 19:30:38 +03:00
env.AddMethod(VerboseAction)
2016-09-02 18:45:19 +03:00
env.AddMethod(PioClean)
2019-09-27 17:22:21 +03:00
env.AddMethod(ConfigureDebugFlags)
env.AddMethod(ConfigureTestTarget)
env.AddMethod(GetExtraScripts)
return env