2016-01-01 20:51:48 +02:00
|
|
|
# Copyright 2014-2016 Ivan Kravets <me@ikravets.com>
|
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
|
|
|
|
from glob import glob
|
2016-01-27 01:37:20 +02:00
|
|
|
from os import environ, listdir, remove
|
2016-04-26 18:14:30 +03:00
|
|
|
from os.path import isdir, isfile, join
|
2015-06-22 15:06:39 +03:00
|
|
|
|
2015-09-04 19:31:59 +03:00
|
|
|
from platformio.util import exec_command, where_is_program
|
2015-08-01 17:39:15 +03:00
|
|
|
|
2015-06-22 15:06:39 +03:00
|
|
|
|
|
|
|
class InoToCPPConverter(object):
|
|
|
|
|
|
|
|
PROTOTYPE_RE = re.compile(
|
|
|
|
r"""^(
|
2016-02-11 00:43:52 +02:00
|
|
|
(\s*[a-z_\d]+\*?){1,2} # return type
|
2015-06-22 15:06:39 +03:00
|
|
|
(\s+[a-z_\d]+\s*) # name of prototype
|
|
|
|
\([a-z_,\.\*\&\[\]\s\d]*\) # arguments
|
|
|
|
)\s*\{ # must end with {
|
|
|
|
""",
|
|
|
|
re.X | re.M | re.I
|
|
|
|
)
|
|
|
|
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
|
|
|
|
|
|
|
def __init__(self, nodes):
|
|
|
|
self.nodes = nodes
|
|
|
|
|
|
|
|
def is_main_node(self, contents):
|
|
|
|
return self.DETECTMAIN_RE.search(contents)
|
|
|
|
|
2016-04-26 18:05:11 +03:00
|
|
|
def _parse_prototypes(self, file_path, 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-04-30 13:28:57 +03:00
|
|
|
prototypes.append({"path": file_path, "match": match})
|
2015-06-22 15:06:39 +03:00
|
|
|
return prototypes
|
|
|
|
|
2016-04-30 17:19:18 +03:00
|
|
|
def append_prototypes(self, file_path, contents, prototypes):
|
2015-06-22 15:06:39 +03:00
|
|
|
result = []
|
2016-04-26 18:05:11 +03:00
|
|
|
if not prototypes:
|
|
|
|
return result
|
2015-06-22 15:06:39 +03:00
|
|
|
|
2016-04-30 13:28:57 +03:00
|
|
|
prototype_names = set(
|
2016-04-30 17:35:41 +03:00
|
|
|
[p['match'].group(3).strip() for p in prototypes])
|
2016-04-30 17:19:18 +03:00
|
|
|
split_pos = prototypes[0]['match'].start()
|
|
|
|
for item in prototypes:
|
|
|
|
if item['path'] == file_path:
|
|
|
|
split_pos = item['match'].start()
|
|
|
|
break
|
2016-04-30 13:28: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())
|
|
|
|
|
|
|
|
result.append(contents[:split_pos].strip())
|
|
|
|
result.append("%s;" %
|
|
|
|
";\n".join([p['match'].group(1) for p in prototypes]))
|
2016-04-26 18:05:11 +03:00
|
|
|
result.append('#line %d "%s"' % (
|
2016-04-30 17:23:51 +03:00
|
|
|
contents.count("\n", 0, split_pos) + 2,
|
|
|
|
file_path.replace("\\", "/")))
|
2016-04-30 13:28:57 +03:00
|
|
|
result.append(contents[split_pos:].strip())
|
2015-06-22 15:06:39 +03:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
def convert(self):
|
|
|
|
prototypes = []
|
|
|
|
data = []
|
|
|
|
for node in self.nodes:
|
|
|
|
ino_contents = node.get_text_contents()
|
2016-04-26 18:05:11 +03:00
|
|
|
prototypes += self._parse_prototypes(node.get_path(), ino_contents)
|
2015-06-22 15:06:39 +03:00
|
|
|
|
2016-04-26 18:05:11 +03:00
|
|
|
item = (node.get_path(), ino_contents)
|
2015-06-22 15:06:39 +03:00
|
|
|
if self.is_main_node(ino_contents):
|
|
|
|
data = [item] + data
|
|
|
|
else:
|
|
|
|
data.append(item)
|
|
|
|
|
|
|
|
if not data:
|
|
|
|
return None
|
|
|
|
|
|
|
|
result = ["#include <Arduino.h>"]
|
|
|
|
is_first = True
|
2016-04-26 18:05:11 +03:00
|
|
|
for file_path, contents in data:
|
2016-04-28 14:52:53 +03:00
|
|
|
result.append('#line 1 "%s"' % file_path.replace("\\", "/"))
|
2015-06-22 15:06:39 +03:00
|
|
|
|
|
|
|
if is_first and prototypes:
|
2016-04-30 17:19:18 +03:00
|
|
|
result += self.append_prototypes(
|
|
|
|
file_path, contents, prototypes)
|
2015-06-22 15:06:39 +03:00
|
|
|
else:
|
|
|
|
result.append(contents)
|
|
|
|
is_first = False
|
|
|
|
|
|
|
|
return "\n".join(result)
|
|
|
|
|
|
|
|
|
|
|
|
def ConvertInoToCpp(env):
|
|
|
|
|
|
|
|
def delete_tmpcpp_file(file_):
|
2016-01-24 16:45:04 +02:00
|
|
|
try:
|
|
|
|
remove(file_)
|
2016-02-01 00:18:04 +02:00
|
|
|
except: # pylint: disable=bare-except
|
|
|
|
if isfile(file_):
|
2016-06-18 23:37:58 +03:00
|
|
|
print("Warning: Could not remove temporary file '%s'. "
|
|
|
|
"Please remove it manually." % file_)
|
2015-06-22 15:06:39 +03:00
|
|
|
|
|
|
|
ino_nodes = (env.Glob(join("$PROJECTSRC_DIR", "*.ino")) +
|
|
|
|
env.Glob(join("$PROJECTSRC_DIR", "*.pde")))
|
|
|
|
|
|
|
|
c = InoToCPPConverter(ino_nodes)
|
|
|
|
data = c.convert()
|
|
|
|
|
|
|
|
if not data:
|
|
|
|
return
|
|
|
|
|
|
|
|
tmpcpp_file = join(env.subst("$PROJECTSRC_DIR"), "tmp_ino_to.cpp")
|
|
|
|
with open(tmpcpp_file, "w") as f:
|
|
|
|
f.write(data)
|
|
|
|
|
|
|
|
atexit.register(delete_tmpcpp_file, tmpcpp_file)
|
|
|
|
|
|
|
|
|
2016-01-27 20:04:35 +02:00
|
|
|
def DumpIDEData(env):
|
2015-06-22 15:06:39 +03:00
|
|
|
|
2016-01-28 00:20:01 +02:00
|
|
|
BOARD_CORE = env.get("BOARD_OPTIONS", {}).get("build", {}).get("core")
|
|
|
|
|
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-07-23 23:11:50 +03:00
|
|
|
invardir = False
|
|
|
|
for vardiritem in env_.get("VARIANT_DIRS", []):
|
|
|
|
if item == vardiritem[0]:
|
|
|
|
includes.append(vardiritem[1])
|
|
|
|
invardir = True
|
|
|
|
break
|
|
|
|
if not invardir:
|
|
|
|
includes.append(env_.subst(item))
|
2016-01-27 20:04:35 +02:00
|
|
|
|
|
|
|
# installed libs
|
2016-02-03 01:37:43 +02:00
|
|
|
for d in env_.get("LIBSOURCE_DIRS", []):
|
|
|
|
lsd_dir = env_.subst(d)
|
|
|
|
_append_lib_includes(env_, lsd_dir, includes)
|
2016-01-27 20:04:35 +02:00
|
|
|
|
|
|
|
# includes from toolchain
|
2016-02-03 01:37:43 +02:00
|
|
|
toolchain_dir = env_.subst(
|
2016-01-27 20:04:35 +02:00
|
|
|
join("$PIOPACKAGES_DIR", "$PIOPACKAGE_TOOLCHAIN"))
|
|
|
|
toolchain_incglobs = [
|
|
|
|
join(toolchain_dir, "*", "include*"),
|
|
|
|
join(toolchain_dir, "lib", "gcc", "*", "*", "include*")
|
|
|
|
]
|
|
|
|
for g in toolchain_incglobs:
|
|
|
|
includes.extend(glob(g))
|
|
|
|
|
|
|
|
return includes
|
|
|
|
|
2016-02-03 01:37:43 +02:00
|
|
|
def _append_lib_includes(env_, libs_dir, includes):
|
2016-01-28 20:35:02 +02:00
|
|
|
if not isdir(libs_dir):
|
|
|
|
return
|
2016-02-03 01:37:43 +02:00
|
|
|
for name in env_.get("LIB_USE", []) + sorted(listdir(libs_dir)):
|
2016-01-28 00:20:01 +02:00
|
|
|
if not isdir(join(libs_dir, name)):
|
|
|
|
continue
|
|
|
|
# ignore user's specified libs
|
2016-02-03 01:37:43 +02:00
|
|
|
if name in env_.get("LIB_IGNORE", []):
|
2016-01-28 00:20:01 +02:00
|
|
|
continue
|
2016-01-28 18:42:56 +02:00
|
|
|
if name == "__cores__":
|
|
|
|
if isdir(join(libs_dir, name, BOARD_CORE)):
|
|
|
|
_append_lib_includes(
|
2016-02-03 01:37:43 +02:00
|
|
|
env_, join(libs_dir, name, BOARD_CORE), includes)
|
2016-01-28 18:42:56 +02:00
|
|
|
return
|
2016-01-28 00:20:01 +02:00
|
|
|
|
|
|
|
include = (
|
|
|
|
join(libs_dir, name, "src")
|
|
|
|
if isdir(join(libs_dir, name, "src"))
|
|
|
|
else join(libs_dir, name)
|
|
|
|
)
|
|
|
|
if include not in includes:
|
|
|
|
includes.append(include)
|
|
|
|
|
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-02-03 01:37:43 +02:00
|
|
|
board = env_.get("BOARD_OPTIONS", {})
|
2016-01-27 20:04:35 +02:00
|
|
|
if board and board['platform'] == "atmelavr":
|
|
|
|
defines.append(
|
|
|
|
"__AVR_%s__" % board['build']['mcu'].upper()
|
|
|
|
.replace("ATMEGA", "ATmega")
|
|
|
|
.replace("ATTINY", "ATtiny")
|
|
|
|
)
|
|
|
|
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 18:44:45 +03:00
|
|
|
"cc_path": where_is_program(
|
|
|
|
env_.subst("$CC"), env_.subst("${ENV['PATH']}")),
|
2016-01-27 20:04:35 +02:00
|
|
|
"cxx_path": 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'])
|
|
|
|
result = 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:
|
|
|
|
env.Exit("Error: Could not find '%s' LD script in LDPATH '%s'" % (
|
|
|
|
script, env.subst("$LIBPATH")))
|
|
|
|
|
2015-12-28 01:15:06 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
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)
|
2015-06-22 15:06:39 +03:00
|
|
|
return env
|