Overhauled handling of compiler flags

Previously all compiler flags (cxxflags etc) were hardcoded into `ino',
which is inflexible since, for instance, it is not even possible to
define constants (via `-D') on the command-line at compile time.  This
commit adds the `--cppflags', `--cflags', `--cxxflags' and `--ldflags'
command-line options in order to improve this situation.

In summary, all are documented under `ino build --help'; defaults are
preserved as before; and the user is not able to void any flags (e.g.
include paths for the required libraries) which are essential for the
building of a sketch
Esse commit está contido em:
Rob Cornish
2013-07-05 23:18:48 +10:00
commit 79149e73ed
4 arquivos alterados com 76 adições e 20 exclusões
+25 -16
Ver Arquivo
@@ -6,6 +6,7 @@ import inspect
import subprocess
import platform
import jinja2
import shlex
from jinja2.runtime import StrictUndefined
@@ -43,6 +44,10 @@ class Build(Command):
super(Build, self).setup_arg_parser(parser)
self.e.add_board_model_arg(parser)
self.e.add_arduino_dist_arg(parser)
self.e.add_cppflags_arg(parser)
self.e.add_cflags_arg(parser)
self.e.add_cxxflags_arg(parser)
self.e.add_ldflags_arg(parser)
parser.add_argument('-v', '--verbose', default=False, action='store_true',
help='Verbose make output')
@@ -72,33 +77,37 @@ class Build(Command):
tool_key, ['hardware', 'tools', 'avr', 'bin'],
items=[tool_binary], human_name=tool_binary)
def setup_flags(self, board_key):
board = self.e.board_model(board_key)
def setup_flags(self, args):
board = self.e.board_model(args.board_model)
mcu = '-mmcu=' + board['build']['mcu']
self.e['cflags'] = SpaceList([
# Hard-code the flags that are essential to building the sketch
self.e['cppflags'] = SpaceList([
mcu,
'-ffunction-sections',
'-fdata-sections',
'-g',
'-Os',
'-w',
'-DF_CPU=' + board['build']['f_cpu'],
'-DARDUINO=' + str(self.e.arduino_lib_version.as_int()),
'-I' + self.e['arduino_core_dir'],
])
])
# Add additional flags as specified
self.e['cppflags'] += SpaceList(shlex.split(args.cppflags))
if 'vid' in board['build']:
self.e['cflags'].append('-DUSB_VID=%s' % board['build']['vid'])
self.e['cppflags'].append('-DUSB_VID=%s' % board['build']['vid'])
if 'pid' in board['build']:
self.e['cflags'].append('-DUSB_PID=%s' % board['build']['pid'])
self.e['cppflags'].append('-DUSB_PID=%s' % board['build']['pid'])
if self.e.arduino_lib_version.major:
variant_dir = os.path.join(self.e.arduino_variants_dir,
board['build']['variant'])
self.e.cflags.append('-I' + variant_dir)
self.e.cppflags.append('-I' + variant_dir)
self.e['cxxflags'] = SpaceList(['-fno-exceptions'])
self.e['elfflags'] = SpaceList(['-Os', '-Wl,--gc-sections', mcu])
self.e['cflags'] = SpaceList(shlex.split(args.cflags))
self.e['cxxflags'] = SpaceList(shlex.split(args.cxxflags))
# Again, hard-code the flags that are essential to building the sketch
self.e['ldflags'] = SpaceList([mcu])
self.e['ldflags'] += SpaceList([
'-Wl,' + flag for flag in shlex.split(args.ldflags)
])
self.e['names'] = {
'obj': '%s.o',
@@ -199,11 +208,11 @@ class Build(Command):
scanned_libs.add(lib)
self.e['used_libs'] = used_libs
self.e['cflags'].extend(self.recursive_inc_lib_flags(used_libs))
self.e['cppflags'].extend(self.recursive_inc_lib_flags(used_libs))
def run(self, args):
self.discover()
self.setup_flags(args.board_model)
self.setup_flags(args)
self.create_jinja(verbose=args.verbose)
self.make('Makefile.sketch')
self.scan_dependencies()
+47
Ver Arquivo
@@ -67,6 +67,11 @@ class Environment(dict):
if platform.system() == 'Darwin':
arduino_dist_dir_guesses.insert(0, '/Applications/Arduino.app/Contents/Resources/Java')
default_cppflags = '-ffunction-sections -fdata-sections -g -Os -w'
default_cflags = ''
default_cxxflags = '-fno-exceptions'
default_ldflags = '-Os --gc-sections'
default_board_model = 'uno'
ino = sys.argv[0]
@@ -211,6 +216,48 @@ class Environment(dict):
parser.add_argument('-d', '--arduino-dist', metavar='PATH',
help='Path to Arduino distribution, e.g. ~/Downloads/arduino-0022.\nTry to guess if not specified')
def add_cppflags_arg(self, parser):
help = '\n'.join([
'Flags that will be passed to the compiler.',
'Note that multiple (space-separated) flags',
'must be surrounded by quotes, e.g.',
'`--cflags="-DC1 -DC2"\' specifies flags to',
'define the constants C1 and C2. Default:',
'%(default)s',
])
parser.add_argument('-p', '--cppflags', metavar='FLAGS',
default=self.default_cppflags, help=help)
def add_cflags_arg(self, parser):
help = '\n'.join([
'Like --cppflags, but the flags specified',
'are only passed to compilations of C source',
'files. Default: %(default)s',
])
parser.add_argument('-c', '--cflags', metavar='FLAGS',
default=self.default_cflags, help=help)
def add_cxxflags_arg(self, parser):
help = '\n'.join([
'Like --cppflags, but the flags specified',
'are only passed to compilations of C++ source',
'files. Default: %(default)s',
])
parser.add_argument('-x', '--cxxflags', metavar='FLAGS',
default=self.default_cxxflags, help=help)
def add_ldflags_arg(self, parser):
help = '\n'.join([
'Like --cppflags, but the flags specified',
'are only passed during the linking stage.',
'Note these flags should be specified as if',
'`ld\' were being invoked directly (i.e. the',
'`-Wl,\' prefix should be omitted). Default:',
'%(default)s',
])
parser.add_argument('-l', '--ldflags', metavar='FLAGS',
default=self.default_ldflags, help=help)
def serial_port_patterns(self):
system = platform.system()
if system == 'Linux':
+1 -1
Ver Arquivo
@@ -16,7 +16,7 @@
{% for source, target in cpp.items() %}
{{ target.path }} : {{ source.path }}
@mkdir -p {{ target.path|dirname }}
{{v}}{{ e.cc }} {{ e.cflags }} {{ inc_flags }} {{ iquote(source) }} -MM $^ > $@
{{v}}{{ e.cc }} {{ e.cppflags }} {{ inc_flags }} {{ iquote(source) }} -MM $^ > $@
{# prepend build path to a target in the generated file and
add .d file itself as a target so that changes in a header file would rebuild dependency files
See: http://make.paulandlesley.org/autodep.html #}
+3 -3
Ver Arquivo
@@ -15,11 +15,11 @@ include {{ target.path|depsname }}
{% endmacro %}
{% macro compile_c(filemap) %}
{{ compile(filemap, e.cc ~ ' ' ~ e.cflags) }}
{{ compile(filemap, e.cc ~ ' ' ~ e.cppflags ~ ' ' ~ e.cflags) }}
{% endmacro %}
{% macro compile_cpp(filemap) %}
{{ compile(filemap, e.cxx ~ ' ' ~ e.cflags ~ ' ' ~ e.cxxflags) }}
{{ compile(filemap, e.cxx ~ ' ' ~ e.cppflags ~ ' ' ~ e.cxxflags) }}
{% endmacro %}
{#
@@ -56,7 +56,7 @@ include {{ target.path|depsname }}
{% set elf = e.build_dir|pjoin('firmware.elf') %}
{{ elf }} : {{ objs }}
@echo {{ 'Linking firmware.elf'|colorize('green') }}
{{v}}{{ e.cc }} {{ e.elfflags }} -o $@ $^ -lm
{{v}}{{ e.cc }} {{ e.ldflags }} -o $@ $^ -lm
{#
# elf -> hex