Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add: preproc command

  • Loading branch information...
commit 182bf759adf8a56b9c48cf51b7165697bd172c69 1 parent b26f3c9
@nailxx nailxx authored
View
1  ino/commands/__init__.py
@@ -1,6 +1,7 @@
# -*- coding: utf-8; -*-
from ino.commands.init import Init
+from ino.commands.preproc import Preprocess
from ino.commands.build import Build
from ino.commands.clean import Clean
from ino.commands.upload import Upload
View
13 ino/commands/build.py
@@ -47,20 +47,9 @@ def setup_arg_parser(self, parser):
help='Verbose make output')
def discover(self):
- self.e.find_arduino_file('version.txt', ['lib'],
- human_name='Arduino lib version file (version.txt)')
-
- if 'arduino_lib_version' not in self.e:
- with open(self.e['version.txt']) as f:
- print 'Detecting Arduino software version ... ',
- v_string = f.read().strip()
- v = Version.parse(v_string)
- self.e['arduino_lib_version'] = v
- print colorize("%s (%s)" % (v, v_string), 'green')
-
self.e.find_arduino_dir('arduino_core_dir',
['hardware', 'arduino', 'cores', 'arduino'],
- ['Arduino.h'] if self.e.arduino_lib_version.major else 'WProgram.h',
+ ['Arduino.h'] if self.e.arduino_lib_version.major else ['WProgram.h'],
'Arduino core library')
self.e.find_arduino_dir('arduino_libraries_dir', ['libraries'],
View
86 ino/commands/preproc.py
@@ -0,0 +1,86 @@
+# -*- coding: utf-8; -*-
+
+import sys
+import re
+
+from ino.commands.base import Command
+from ino.exc import Abort
+
+
+class Preprocess(Command):
+ """
+ Preprocess an .ino or .pde sketch file and produce ready-to-compile .cpp source.
+
+ Ino mimics steps that are performed by official Arduino Software to
+ produce similar result:
+
+ * Either #include <Arduino.h> or <WProgram.h> is prepended
+ * Function prototypes are added at the beginning of file
+ """
+
+ name = 'preproc'
+ help_line = "Transform a sketch file into valid C++ source"
+
+ def setup_arg_parser(self, parser):
+ super(Preprocess, self).setup_arg_parser(parser)
+ self.e.add_arduino_dist_arg(parser)
+ parser.add_argument('sketch', help='Input sketch file name')
+ parser.add_argument('-o', '--output', default='-', help='Output source file name (default: use stdout)')
+
+ def run(self, args):
+ if args.output == '-':
+ out = sys.stdout
+ else:
+ out = open(args.output, 'wt')
+
+ sketch = open(args.sketch, 'rt').read()
+
+ header = 'Arduino.h' if self.e.arduino_lib_version.major else 'WProgram.h'
+ out.write('#include <%s>\n' % header)
+ out.write('\n'.join(self.prototypes(sketch)))
+ out.write('\n#line 1 "%s"\n' % args.sketch)
+ out.write(sketch)
+
+ def prototypes(self, src):
+ src = self.collapse_braces(self.strip(src))
+ regex = re.compile("[\\w\\[\\]\\*]+\\s+[&\\[\\]\\*\\w\\s]+\\([&,\\[\\]\\*\\w\\s]*\\)(?=\\s*\\{)")
+ matches = regex.findall(src)
+ return [m + ';' for m in matches]
+
+ def collapse_braces(self, src):
+ """
+ Remove the contents of all top-level curly brace pairs {}.
+ """
+ result = []
+ nesting = 0;
+
+ for c in src:
+ if not nesting:
+ result.append(c)
+ if c == '{':
+ nesting += 1
+ elif c == '}':
+ nesting -= 1
+ result.append(c)
+
+ return ''.join(result)
+
+ def strip(self, src):
+ """
+ Strips comments, pre-processor directives, single- and double-quoted
+ strings from a string.
+ """
+ # single-quoted character
+ p = "('.')"
+
+ # double-quoted string
+ p += "|(\"(?:[^\"\\\\]|\\\\.)*\")"
+
+ # single and multi-line comment
+ p += "|(//.*?$)|(/\\*[^*]*(?:\\*(?!/)[^*]*)*\\*/)"
+
+ # pre-processor directive
+ p += "|" + "(^\\s*#.*?$)"
+
+ regex = re.compile(p, re.MULTILINE)
+ return regex.sub(' ', src)
View
15 ino/environment.py
@@ -240,6 +240,21 @@ def process_args(self, args):
print all_models.format()
raise Abort('%s is not a valid board model' % board_model)
+ @property
+ def arduino_lib_version(self):
+ self.find_arduino_file('version.txt', ['lib'],
+ human_name='Arduino lib version file (version.txt)')
+
+ if 'arduino_lib_version' not in self:
+ with open(self['version.txt']) as f:
+ print 'Detecting Arduino software version ... ',
+ v_string = f.read().strip()
+ v = Version.parse(v_string)
+ self['arduino_lib_version'] = v
+ print colorize("%s (%s)" % (v, v_string), 'green')
+
+ return self['arduino_lib_version']
+
class BoardModels(OrderedDict):
def format(self):
Please sign in to comment.
Something went wrong with that request. Please try again.