forked from platformio/platformio-core
Resolve #7: add auto-conversation from *.ino to valid *.cpp for Arduino/Energia frameworks
This commit is contained in:
@ -1,9 +1,10 @@
|
||||
# Copyright (C) Ivan Kravets <me@ikravets.com>
|
||||
# See LICENSE for details.
|
||||
|
||||
import atexit
|
||||
import re
|
||||
from os import getenv, listdir, walk
|
||||
from os.path import isdir, isfile, join
|
||||
from os import getenv, listdir, remove, walk
|
||||
from os.path import basename, isdir, isfile, join
|
||||
|
||||
from SCons.Script import SConscript, SConscriptChdir
|
||||
|
||||
@ -19,6 +20,8 @@ def ProcessGeneral(env):
|
||||
)
|
||||
|
||||
if "FRAMEWORK" in env:
|
||||
if env['FRAMEWORK'] in ("arduino", "energia"):
|
||||
env.ConvertInotoCpp()
|
||||
SConscriptChdir(0)
|
||||
corelibs = SConscript(env.subst(join("$PIOBUILDER_DIR", "scripts",
|
||||
"frameworks", "${FRAMEWORK}.py")),
|
||||
@ -79,11 +82,9 @@ def BuildDependentLibraries(env, src_dir):
|
||||
|
||||
def GetDependentLibraries(env, src_dir):
|
||||
includes = {}
|
||||
regexp = re.compile(r"^\s?#include\s+(?:\<|\"|\')([^\>\"\']+)(?:\>|\"|\')",
|
||||
re.M)
|
||||
regexp = re.compile(r"^\s*#include\s+(?:\<|\")([^\>\"\']+)(?:\>|\")", re.M)
|
||||
for node in env.GlobCXXFiles(src_dir):
|
||||
env.ParseIncludesRecurive(regexp, node, includes)
|
||||
|
||||
includes = sorted(includes.items(), key=lambda s: s[0])
|
||||
return set([(i[1][1], i[1][2]) for i in includes])
|
||||
|
||||
@ -150,6 +151,46 @@ def ParseBoardOptions(env, path, name):
|
||||
return data
|
||||
|
||||
|
||||
def ConvertInotoCpp(env):
|
||||
|
||||
def delete_tmpcpp(files):
|
||||
for f in files:
|
||||
remove(f)
|
||||
|
||||
tmpcpp = []
|
||||
|
||||
for item in env.Glob(join("$PROJECT_DIR", "src", "*.ino")):
|
||||
cppfile = item.get_path()[:-3] + "cpp"
|
||||
if isfile(cppfile):
|
||||
continue
|
||||
ino_contents = item.get_text_contents()
|
||||
|
||||
# fetch prototypes
|
||||
regexp = re.compile(
|
||||
r"""^(
|
||||
(?:\s*[a-z_\d]+){1,2} # return type
|
||||
\s+[a-z_\d]+\s* # name of prototype
|
||||
\([a-z_,\.\*\&\s\d]+\) # args
|
||||
)\s*\{ # must end with {
|
||||
""",
|
||||
re.X | re.M | re.I
|
||||
)
|
||||
prototypes = regexp.findall(ino_contents)
|
||||
# print prototypes
|
||||
|
||||
# create new temporary C++ valid file
|
||||
with open(cppfile, "w") as f:
|
||||
f.write("#include <Arduino.h>\n")
|
||||
if prototypes:
|
||||
f.write("%s;\n" % ";\n".join(prototypes))
|
||||
f.write("#line 1 \"%s\"\n" % basename(item.path))
|
||||
f.write(ino_contents)
|
||||
tmpcpp.append(cppfile)
|
||||
|
||||
if tmpcpp:
|
||||
atexit.register(delete_tmpcpp, tmpcpp)
|
||||
|
||||
|
||||
def exists(_):
|
||||
return True
|
||||
|
||||
@ -164,4 +205,5 @@ def generate(env):
|
||||
env.AddMethod(ParseIncludesRecurive)
|
||||
env.AddMethod(VariantDirRecursive)
|
||||
env.AddMethod(ParseBoardOptions)
|
||||
env.AddMethod(ConvertInotoCpp)
|
||||
return env
|
||||
|
@ -56,4 +56,5 @@ def cli(environment, target, upload_port):
|
||||
p = PlatformFactory().newPlatform(config.get(section, "platform"))
|
||||
result = p.run(variables, envtargets)
|
||||
secho(result['out'], fg="green")
|
||||
secho(result['err'], fg="red")
|
||||
secho(result['err'],
|
||||
fg="red" if "Error" in result['err'] else "yellow")
|
||||
|
Reference in New Issue
Block a user