Skip to content

Commit

Permalink
Resolve #7: add auto-conversation from *.ino to valid *.cpp for Ardui…
Browse files Browse the repository at this point in the history
…no/Energia frameworks
  • Loading branch information
ivankravets committed Aug 3, 2014
1 parent 1b75d3e commit 00e5155
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
52 changes: 47 additions & 5 deletions platformio/builder/tools/platformio.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Copyright (C) Ivan Kravets <[email protected]>
# 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

Expand All @@ -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")),
Expand Down Expand Up @@ -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])

Expand Down Expand Up @@ -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

Expand All @@ -164,4 +205,5 @@ def generate(env):
env.AddMethod(ParseIncludesRecurive)
env.AddMethod(VariantDirRecursive)
env.AddMethod(ParseBoardOptions)
env.AddMethod(ConvertInotoCpp)
return env
3 changes: 2 additions & 1 deletion platformio/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 00e5155

Please sign in to comment.