2016-08-03 22:18:51 +03:00
|
|
|
# Copyright 2014-present PlatformIO <contact@platformio.org>
|
2015-11-18 17:16:17 +02:00
|
|
|
#
|
|
|
|
# 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-06-22 15:06:39 +03:00
|
|
|
|
2015-11-30 01:11:57 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2015-06-22 15:06:39 +03:00
|
|
|
import atexit
|
|
|
|
import re
|
2016-08-26 14:39:23 +03:00
|
|
|
import sys
|
2015-06-22 15:06:39 +03:00
|
|
|
from glob import glob
|
2016-07-06 15:27:46 +03:00
|
|
|
from os import environ, remove
|
2016-08-31 00:16:23 +03:00
|
|
|
from os.path import basename, isfile, join
|
|
|
|
from tempfile import mkstemp
|
2015-06-22 15:06:39 +03:00
|
|
|
|
2016-08-27 19:30:38 +03:00
|
|
|
from SCons.Action import Action
|
|
|
|
from SCons.Script import ARGUMENTS
|
|
|
|
|
2016-05-26 19:43:36 +03:00
|
|
|
from platformio import util
|
2015-08-01 17:39:15 +03:00
|
|
|
|
2015-06-22 15:06:39 +03:00
|
|
|
|
|
|
|
class InoToCPPConverter(object):
|
|
|
|
|
2016-08-03 23:38:20 +03:00
|
|
|
PROTOTYPE_RE = re.compile(r"""^(
|
2016-08-31 12:49:10 +03:00
|
|
|
([a-z_\d]+\*?\s+){1,2} # return type
|
|
|
|
([a-z_\d]+\s*) # name of prototype
|
2015-06-22 15:06:39 +03:00
|
|
|
\([a-z_,\.\*\&\[\]\s\d]*\) # arguments
|
|
|
|
)\s*\{ # must end with {
|
2016-08-03 23:38:20 +03:00
|
|
|
""", re.X | re.M | re.I)
|
2015-06-22 15:06:39 +03:00
|
|
|
DETECTMAIN_RE = re.compile(r"void\s+(setup|loop)\s*\(", re.M | re.I)
|
2016-04-30 13:28:57 +03:00
|
|
|
PROTOPTRS_TPLRE = r"\([^&\(]*&(%s)[^\)]*\)"
|
2015-06-22 15:06:39 +03:00
|
|
|
|
2016-08-31 00:16:23 +03:00
|
|
|
def __init__(self, env):
|
|
|
|
self.env = env
|
|
|
|
self._main_ino = None
|
2015-06-22 15:06:39 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
return self.process(contents)
|
|
|
|
|
|
|
|
def merge(self, nodes):
|
2016-09-02 14:35:07 +03:00
|
|
|
assert nodes
|
2016-08-31 00:16:23 +03:00
|
|
|
lines = []
|
|
|
|
for node in nodes:
|
|
|
|
contents = node.get_text_contents()
|
|
|
|
_lines = [
|
|
|
|
'# 1 "%s"' % node.get_path().replace("\\", "/"), contents
|
|
|
|
]
|
|
|
|
if self.is_main_node(contents):
|
|
|
|
lines = _lines + lines
|
|
|
|
self._main_ino = node.get_path()
|
|
|
|
else:
|
|
|
|
lines.extend(_lines)
|
|
|
|
|
2016-09-02 14:35:07 +03:00
|
|
|
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)
|
|
|
|
with open(out_file) as fp:
|
|
|
|
contents = fp.read()
|
2016-09-01 19:08:32 +03:00
|
|
|
contents = self._join_multiline_strings(contents)
|
2016-08-31 00:16:23 +03:00
|
|
|
with open(out_file, "w") as fp:
|
|
|
|
fp.write(self.append_prototypes(contents))
|
|
|
|
return out_file
|
|
|
|
|
|
|
|
def _gcc_preprocess(self, contents, out_file):
|
|
|
|
tmp_path = mkstemp()[1]
|
|
|
|
with open(tmp_path, "w") as fp:
|
|
|
|
fp.write(contents)
|
|
|
|
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])))
|
2016-08-31 00:34:23 +03:00
|
|
|
atexit.register(_delete_file, tmp_path)
|
2016-08-31 00:16:23 +03:00
|
|
|
return isfile(out_file)
|
|
|
|
|
2016-09-01 19:08:32 +03:00
|
|
|
def _join_multiline_strings(self, contents):
|
2016-09-01 23:19:46 +03:00
|
|
|
if "\\\n" not in contents:
|
2016-09-01 19:08:32 +03:00
|
|
|
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
|
2016-09-02 14:35:07 +03:00
|
|
|
newlines.append(line[:-1])
|
|
|
|
continue
|
2016-09-01 19:08:32 +03:00
|
|
|
elif stropen:
|
|
|
|
newlines[len(newlines) - 1] += line[:-1]
|
2016-09-02 14:35:07 +03:00
|
|
|
continue
|
2016-09-01 19:08:32 +03:00
|
|
|
elif stropen and line.endswith('";'):
|
|
|
|
newlines[len(newlines) - 1] += line
|
|
|
|
stropen = False
|
|
|
|
newlines.append('#line %d "%s"' %
|
|
|
|
(linenum, self._main_ino.replace("\\", "/")))
|
2016-09-02 14:35:07 +03:00
|
|
|
continue
|
|
|
|
|
|
|
|
newlines.append(line)
|
2016-09-01 19:08:32 +03:00
|
|
|
|
|
|
|
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):
|
2015-06-22 15:06:39 +03:00
|
|
|
prototypes = []
|
|
|
|
reserved_keywords = set(["if", "else", "while"])
|
2016-04-26 18:05:11 +03:00
|
|
|
for match in self.PROTOTYPE_RE.finditer(contents):
|
|
|
|
if (set([match.group(2).strip(), match.group(3).strip()]) &
|
|
|
|
reserved_keywords):
|
2015-06-22 15:06:39 +03:00
|
|
|
continue
|
2016-08-31 00:16:23 +03:00
|
|
|
prototypes.append(match)
|
2015-06-22 15:06:39 +03:00
|
|
|
return prototypes
|
|
|
|
|
2016-09-01 19:08:32 +03:00
|
|
|
def _get_total_lines(self, contents):
|
2016-08-31 00:16:23 +03:00
|
|
|
total = 0
|
|
|
|
for line in contents.split("\n")[::-1]:
|
2016-09-01 19:08:32 +03:00
|
|
|
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)
|
2016-04-26 18:05:11 +03:00
|
|
|
if not prototypes:
|
2016-08-31 00:16:23 +03:00
|
|
|
return contents
|
2016-04-30 13:28:57 +03:00
|
|
|
|
2016-08-31 00:16:23 +03:00
|
|
|
prototype_names = set([m.group(3).strip() for m in prototypes])
|
|
|
|
split_pos = prototypes[0].start()
|
2016-08-03 23:38:20 +03:00
|
|
|
match_ptrs = re.search(self.PROTOPTRS_TPLRE %
|
|
|
|
("|".join(prototype_names)),
|
|
|
|
contents[:split_pos], re.M)
|
2016-04-30 13:28:57 +03:00
|
|
|
if match_ptrs:
|
|
|
|
split_pos = contents.rfind("\n", 0, match_ptrs.start())
|
|
|
|
|
2016-08-31 00:16:23 +03:00
|
|
|
result = []
|
2016-04-30 13:28:57 +03:00
|
|
|
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]))
|
2016-08-03 23:38:20 +03:00
|
|
|
result.append('#line %d "%s"' %
|
2016-08-31 00:16:23 +03:00
|
|
|
(self._get_total_lines(contents[:split_pos]),
|
|
|
|
self._main_ino.replace("\\", "/")))
|
2016-04-30 13:28:57 +03:00
|
|
|
result.append(contents[split_pos:].strip())
|
2015-06-22 15:06:39 +03:00
|
|
|
return "\n".join(result)
|
|
|
|
|
|
|
|
|
|
|
|
def ConvertInoToCpp(env):
|
|
|
|
ino_nodes = (env.Glob(join("$PROJECTSRC_DIR", "*.ino")) +
|
|
|
|
env.Glob(join("$PROJECTSRC_DIR", "*.pde")))
|
2016-08-31 00:16:23 +03:00
|
|
|
c = InoToCPPConverter(env)
|
|
|
|
out_file = c.convert(ino_nodes)
|
2015-06-22 15:06:39 +03:00
|
|
|
|
2016-08-31 00:16:23 +03:00
|
|
|
atexit.register(_delete_file, out_file)
|
2015-06-22 15:06:39 +03:00
|
|
|
|
|
|
|
|
2016-08-31 00:34:23 +03:00
|
|
|
def _delete_file(path):
|
|
|
|
try:
|
|
|
|
if isfile(path):
|
|
|
|
remove(path)
|
|
|
|
except: # pylint: disable=bare-except
|
2016-09-01 21:56:43 +03:00
|
|
|
pass
|
2016-08-31 00:34:23 +03:00
|
|
|
|
|
|
|
|
2016-01-27 20:04:35 +02:00
|
|
|
def DumpIDEData(env):
|
2015-06-22 15:06:39 +03:00
|
|
|
|
2016-02-03 01:37:43 +02:00
|
|
|
def get_includes(env_):
|
2016-01-27 20:04:35 +02:00
|
|
|
includes = []
|
|
|
|
|
2016-02-03 01:37:43 +02:00
|
|
|
for item in env_.get("CPPPATH", []):
|
2016-08-31 00:16:23 +03:00
|
|
|
includes.append(env_.subst(item))
|
2016-01-27 20:04:35 +02:00
|
|
|
|
|
|
|
# installed libs
|
2016-06-25 13:23:24 +03:00
|
|
|
for lb in env.GetLibBuilders():
|
2016-07-28 02:08:30 +03:00
|
|
|
includes.extend(lb.get_inc_dirs())
|
2016-01-27 20:04:35 +02:00
|
|
|
|
2016-05-26 19:43:36 +03:00
|
|
|
# includes from toolchains
|
2016-08-03 19:58:35 +03:00
|
|
|
p = env.PioPlatform()
|
2016-07-09 19:01:43 +03:00
|
|
|
for name in p.get_installed_packages():
|
2016-05-26 19:43:36 +03:00
|
|
|
if p.get_package_type(name) != "toolchain":
|
|
|
|
continue
|
|
|
|
toolchain_dir = p.get_package_dir(name)
|
|
|
|
toolchain_incglobs = [
|
|
|
|
join(toolchain_dir, "*", "include*"),
|
|
|
|
join(toolchain_dir, "lib", "gcc", "*", "*", "include*")
|
|
|
|
]
|
|
|
|
for g in toolchain_incglobs:
|
|
|
|
includes.extend(glob(g))
|
2016-01-27 20:04:35 +02:00
|
|
|
|
|
|
|
return includes
|
|
|
|
|
2016-02-03 01:37:43 +02:00
|
|
|
def get_defines(env_):
|
2016-01-27 20:04:35 +02:00
|
|
|
defines = []
|
|
|
|
# global symbols
|
2016-07-09 18:44:45 +03:00
|
|
|
for item in env_.get("CPPDEFINES", []):
|
|
|
|
if isinstance(item, list) or isinstance(item, tuple):
|
|
|
|
item = "=".join(item)
|
2016-02-03 01:37:43 +02:00
|
|
|
defines.append(env_.subst(item).replace('\\"', '"'))
|
2016-01-27 20:04:35 +02:00
|
|
|
|
|
|
|
# special symbol for Atmel AVR MCU
|
2016-07-17 16:05:28 +03:00
|
|
|
if env['PIOPLATFORM'] == "atmelavr":
|
2016-01-27 20:04:35 +02:00
|
|
|
defines.append(
|
2016-05-26 19:43:36 +03:00
|
|
|
"__AVR_%s__" % env.BoardConfig().get("build.mcu").upper()
|
2016-08-03 23:38:20 +03:00
|
|
|
.replace("ATMEGA", "ATmega").replace("ATTINY", "ATtiny"))
|
2016-01-27 20:04:35 +02:00
|
|
|
return defines
|
|
|
|
|
2016-03-06 00:40:28 +02:00
|
|
|
LINTCCOM = "$CFLAGS $CCFLAGS $CPPFLAGS $_CPPDEFFLAGS"
|
|
|
|
LINTCXXCOM = "$CXXFLAGS $CCFLAGS $CPPFLAGS $_CPPDEFFLAGS"
|
2016-02-03 01:37:43 +02:00
|
|
|
env_ = env.Clone()
|
|
|
|
|
2016-02-10 17:16:52 +02:00
|
|
|
data = {
|
2016-02-03 01:37:43 +02:00
|
|
|
"defines": get_defines(env_),
|
|
|
|
"includes": get_includes(env_),
|
2016-03-06 00:40:28 +02:00
|
|
|
"cc_flags": env_.subst(LINTCCOM),
|
|
|
|
"cxx_flags": env_.subst(LINTCXXCOM),
|
2016-07-09 19:01:43 +03:00
|
|
|
"cc_path": util.where_is_program(
|
2016-07-09 18:44:45 +03:00
|
|
|
env_.subst("$CC"), env_.subst("${ENV['PATH']}")),
|
2016-05-26 19:43:36 +03:00
|
|
|
"cxx_path": util.where_is_program(
|
2016-02-03 01:37:43 +02:00
|
|
|
env_.subst("$CXX"), env_.subst("${ENV['PATH']}"))
|
2016-01-27 20:04:35 +02:00
|
|
|
}
|
2015-06-22 15:06:39 +03:00
|
|
|
|
2016-02-10 17:16:52 +02:00
|
|
|
# https://github.com/platformio/platformio-atom-ide/issues/34
|
|
|
|
_new_defines = []
|
2016-07-09 18:44:45 +03:00
|
|
|
for item in env_.get("CPPDEFINES", []):
|
|
|
|
if isinstance(item, list) or isinstance(item, tuple):
|
|
|
|
item = "=".join(item)
|
2016-02-11 00:27:41 +02:00
|
|
|
item = item.replace('\\"', '"')
|
2016-02-10 17:16:52 +02:00
|
|
|
if " " in item:
|
|
|
|
_new_defines.append(item.replace(" ", "\\\\ "))
|
|
|
|
else:
|
|
|
|
_new_defines.append(item)
|
|
|
|
env_.Replace(CPPDEFINES=_new_defines)
|
|
|
|
|
|
|
|
data.update({
|
2016-03-06 00:40:28 +02:00
|
|
|
"cc_flags": env_.subst(LINTCCOM),
|
|
|
|
"cxx_flags": env_.subst(LINTCXXCOM)
|
2016-02-10 17:16:52 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
2015-06-22 15:06:39 +03:00
|
|
|
|
2015-08-02 19:52:37 +03:00
|
|
|
def GetCompilerType(env):
|
2015-08-01 17:39:15 +03:00
|
|
|
try:
|
2015-08-01 18:33:41 +03:00
|
|
|
sysenv = environ.copy()
|
|
|
|
sysenv['PATH'] = str(env['ENV']['PATH'])
|
2016-05-26 19:43:36 +03:00
|
|
|
result = util.exec_command([env.subst("$CC"), "-v"], env=sysenv)
|
2015-08-01 17:39:15 +03:00
|
|
|
except OSError:
|
|
|
|
return None
|
|
|
|
if result['returncode'] != 0:
|
|
|
|
return None
|
|
|
|
output = "".join([result['out'], result['err']]).lower()
|
2016-03-02 00:25:36 +02:00
|
|
|
if "clang" in output and "LLVM" in output:
|
|
|
|
return "clang"
|
|
|
|
elif "gcc" in output:
|
|
|
|
return "gcc"
|
2015-08-01 17:39:15 +03:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2015-12-28 01:15:06 +02:00
|
|
|
def GetActualLDScript(env):
|
2015-12-28 20:09:48 +02:00
|
|
|
script = None
|
2015-12-28 01:15:06 +02:00
|
|
|
for f in env.get("LINKFLAGS", []):
|
|
|
|
if f.startswith("-Wl,-T"):
|
|
|
|
script = env.subst(f[6:].replace('"', "").strip())
|
|
|
|
if isfile(script):
|
|
|
|
return script
|
|
|
|
for d in env.get("LIBPATH", []):
|
|
|
|
path = join(env.subst(d), script)
|
|
|
|
if isfile(path):
|
|
|
|
return path
|
2015-12-28 20:09:48 +02:00
|
|
|
|
|
|
|
if script:
|
2016-08-26 14:39:23 +03:00
|
|
|
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
|
|
|
|
2015-12-28 01:15:06 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
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
|
|
|
|
else:
|
|
|
|
return Action(act, actstr)
|
2016-07-17 00:48:59 +03:00
|
|
|
|
|
|
|
|
2015-06-22 15:06:39 +03:00
|
|
|
def exists(_):
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def generate(env):
|
|
|
|
env.AddMethod(ConvertInoToCpp)
|
|
|
|
env.AddMethod(DumpIDEData)
|
2015-08-02 19:52:37 +03:00
|
|
|
env.AddMethod(GetCompilerType)
|
2015-12-28 01:15:06 +02:00
|
|
|
env.AddMethod(GetActualLDScript)
|
2016-08-27 19:30:38 +03:00
|
|
|
env.AddMethod(VerboseAction)
|
2015-06-22 15:06:39 +03:00
|
|
|
return env
|