From 00e51558db8e8fa3832aca2e7a683f9eacadbf3c Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Sun, 3 Aug 2014 16:23:18 +0300 Subject: [PATCH] Resolve #7: add auto-conversation from *.ino to valid *.cpp for Arduino/Energia frameworks --- platformio/builder/tools/platformio.py | 52 +++++++++++++++++++++++--- platformio/commands/run.py | 3 +- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/platformio/builder/tools/platformio.py b/platformio/builder/tools/platformio.py index 441c6c3d..e1a3ac8f 100644 --- a/platformio/builder/tools/platformio.py +++ b/platformio/builder/tools/platformio.py @@ -1,9 +1,10 @@ # Copyright (C) Ivan Kravets # 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 \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 diff --git a/platformio/commands/run.py b/platformio/commands/run.py index 4422c1d0..5e989c17 100644 --- a/platformio/commands/run.py +++ b/platformio/commands/run.py @@ -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")