diff --git a/HISTORY.rst b/HISTORY.rst index cd912885..5c88bb68 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -1,6 +1,13 @@ Release History =============== +1.3.0 (2015-??-??) +------------------ + +* Improved handling of multi-file ``*.ino/pde`` sketches + (`issue #130 `_) + + 1.2.0 (2015-03-20) ------------------ diff --git a/platformio/__init__.py b/platformio/__init__.py index f40ab4c5..aa4e62f4 100644 --- a/platformio/__init__.py +++ b/platformio/__init__.py @@ -1,7 +1,7 @@ # Copyright (C) Ivan Kravets # See LICENSE for details. -VERSION = (1, 2, 0) +VERSION = (1, 3, "0.dev0") __version__ = ".".join([str(s) for s in VERSION]) __title__ = "platformio" diff --git a/platformio/builder/tools/platformio.py b/platformio/builder/tools/platformio.py index a27d8654..6f06bdef 100644 --- a/platformio/builder/tools/platformio.py +++ b/platformio/builder/tools/platformio.py @@ -275,43 +275,54 @@ def BuildDependentLibraries(env, src_dir): # pylint: disable=R0914 def ConvertInoToCpp(env): - def delete_tmpcpp(files): - for f in files: - remove(f) + PROTOTYPE_RE = re.compile( + r"""^( + (?:\s*[a-z_\d]+){1,2} # return type + \s+[a-z_\d]+\s* # name of prototype + \([a-z_,\.\*\&\[\]\s\d]*\) # arguments + )\s*\{ # must end with { + """, + re.X | re.M | re.I + ) - tmpcpp = [] - items = (env.Glob(join("$PROJECTSRC_DIR", "*.ino")) + - env.Glob(join("$PROJECTSRC_DIR", "*.pde"))) - for item in items: - cppfile = item.get_path()[:-3] + "cpp" - if isfile(cppfile): - continue - ino_contents = item.get_text_contents() + DETECTMAIN_RE = re.compile(r"void\s+(setup|loop)\s*\(", re.M | re.I) - prototypes = re.findall( - 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 { - """, - ino_contents, - re.X | re.M | re.I - ) - prototypes = [p.strip() for p in prototypes] - # print prototypes + def delete_tmpcpp_file(file_): + remove(file_) - # 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) + def is_main_ino(contents): + return DETECTMAIN_RE.search(contents) - if tmpcpp: - atexit.register(delete_tmpcpp, tmpcpp) + ino_nodes = (env.Glob(join("$PROJECTSRC_DIR", "*.ino")) + + env.Glob(join("$PROJECTSRC_DIR", "*.pde"))) + prototypes = [] + data = [] + for node in ino_nodes: + ino_contents = node.get_text_contents() + prototypes += PROTOTYPE_RE.findall(ino_contents) + + item = (basename(node.get_path()), ino_contents) + if is_main_ino(ino_contents): + data = [item] + data + else: + data.append(item) + + if not data: + return + + # create new temporary C++ valid file + tmpcpp_file = join(env.subst("$PROJECTSRC_DIR"), "piomain.cpp") + with open(tmpcpp_file, "w") as f: + f.write("#include \n") + + if prototypes: + f.write("%s;" % ";\n".join(prototypes)) + + for name, contents in data: + f.write('\n#line 1 "%s"\n' % name) + f.write(contents) + + atexit.register(delete_tmpcpp_file, tmpcpp_file) def exists(_):