waf: boards: use classes for boards definition

In order to provide board-specific behavior. For example, the incoming PX4
build will require custom build behavior.
This commit is contained in:
Gustavo Jose de Sousa 2016-02-15 21:26:14 +00:00 committed by Lucas De Marchi
parent 49a4bde5d9
commit 393bea8945
2 changed files with 194 additions and 207 deletions

View File

@ -5,34 +5,38 @@ import sys
import waflib import waflib
BOARDS = {} _board_classes = {}
PROJECT_ENV = waflib.ConfigSet.ConfigSet() class BoardMeta(type):
def __init__(cls, name, bases, dct):
super(BoardMeta, cls).__init__(name, bases, dct)
if name == 'Board':
return
_board_classes[name] = cls
def define_board(func, name, parent_name=None): class Board:
if parent_name is None: def configure(self, cfg):
parent = PROJECT_ENV env = waflib.ConfigSet.ConfigSet()
elif parent_name not in BOARDS: self.configure_env(env)
print("Undefined parent board '%s' for '%s'" % (parent_name, name))
sys.exit(1) d = env.get_merged_dict()
# Always prepend so that arguments passed in the command line get
# the priority.
for k, val in d.items():
# Dictionaries (like 'DEFINES') are converted to lists to
# conform to waf conventions.
if isinstance(val, dict):
for item in val.items():
cfg.env.prepend_value(k, '%s=%s' % item)
else: else:
parent = BOARDS[parent_name] cfg.env.prepend_value(k, val)
env = parent.derive().detach()
if name in BOARDS:
print("Multiple definitions of " + name)
sys.exit(1)
BOARDS[name] = env
func(env)
def get_boards_names():
return sorted(list(BOARDS.keys()))
def configure_env(self, env):
# Use a dictionary instead of the convetional list for definitions to # Use a dictionary instead of the convetional list for definitions to
# make easy to override them. Convert back to list before consumption. # make easy to override them. Convert back to list before consumption.
PROJECT_ENV.DEFINES = {} env.DEFINES = {}
PROJECT_ENV.CFLAGS += [ env.CFLAGS += [
'-ffunction-sections', '-ffunction-sections',
'-fdata-sections', '-fdata-sections',
'-fsigned-char', '-fsigned-char',
@ -48,7 +52,7 @@ PROJECT_ENV.CFLAGS += [
'-Wno-redundant-decls', '-Wno-redundant-decls',
] ]
PROJECT_ENV.CXXFLAGS += [ env.CXXFLAGS += [
'-std=gnu++11', '-std=gnu++11',
'-fdata-sections', '-fdata-sections',
@ -74,24 +78,33 @@ PROJECT_ENV.CXXFLAGS += [
'-Wfatal-errors', '-Wfatal-errors',
] ]
PROJECT_ENV.LINKFLAGS += [ env.LINKFLAGS += [
'-Wl,--gc-sections', '-Wl,--gc-sections',
] ]
Board = BoardMeta('Board', Board.__bases__, dict(Board.__dict__))
def get_boards_names():
return sorted(list(_board_classes.keys()))
def get_board(name):
return _board_classes[name]()
# NOTE: Keeping all the board definitions together so we can easily # NOTE: Keeping all the board definitions together so we can easily
# identify opportunities to simplify common flags. In the future might # identify opportunities to simplify common flags. In the future might
# be worthy to keep board definitions in files of their own. # be worthy to keep board definitions in files of their own.
def sitl(env): class sitl(Board):
def configure_env(self, env):
super(sitl, self).configure_env(env)
env.DEFINES.update( env.DEFINES.update(
CONFIG_HAL_BOARD = 'HAL_BOARD_SITL', CONFIG_HAL_BOARD = 'HAL_BOARD_SITL',
CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_NONE', CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_NONE',
) )
env.CXXFLAGS += [ env.CXXFLAGS += [
'-O3', '-O3',
] ]
env.LIB += [ env.LIB += [
'm', 'm',
] ]
@ -101,19 +114,17 @@ def sitl(env):
'SITL', 'SITL',
] ]
define_board(sitl, 'sitl') class linux(Board):
def configure_env(self, env):
super(linux, self).configure_env(env)
def linux(env):
env.DEFINES.update( env.DEFINES.update(
CONFIG_HAL_BOARD = 'HAL_BOARD_LINUX', CONFIG_HAL_BOARD = 'HAL_BOARD_LINUX',
CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_NONE', CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_NONE',
) )
env.CXXFLAGS += [ env.CXXFLAGS += [
'-O3', '-O3',
] ]
env.LIB += [ env.LIB += [
'm', 'm',
'rt', 'rt',
@ -123,114 +134,102 @@ def linux(env):
'AP_HAL_Linux', 'AP_HAL_Linux',
] ]
define_board(linux, 'linux') class minlure(linux):
def configure_env(self, env):
super(minlure, self).configure_env(env)
def minlure(env):
env.DEFINES.update( env.DEFINES.update(
CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_MINLURE', CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_MINLURE',
) )
define_board(minlure, 'minlure', 'linux')
class erleboard(linux):
def configure_env(self, env):
super(erleboard, self).configure_env(env)
def erleboard(env):
env.TOOLCHAIN = 'arm-linux-gnueabihf' env.TOOLCHAIN = 'arm-linux-gnueabihf'
env.DEFINES.update( env.DEFINES.update(
CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_ERLEBOARD', CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_ERLEBOARD',
) )
define_board(erleboard, 'erleboard', 'linux') class navio(linux):
def configure_env(self, env):
super(navio, self).configure_env(env)
def navio(env):
env.TOOLCHAIN = 'arm-linux-gnueabihf' env.TOOLCHAIN = 'arm-linux-gnueabihf'
env.DEFINES.update( env.DEFINES.update(
CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_NAVIO', CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_NAVIO',
) )
define_board(navio, 'navio', 'linux') class zynq(linux):
def configure_env(self, env):
super(zynq, self).configure_env(env)
def zynq(env):
env.TOOLCHAIN = 'arm-xilinx-linux-gnueabi' env.TOOLCHAIN = 'arm-xilinx-linux-gnueabi'
env.DEFINES.update( env.DEFINES.update(
CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_ZYNQ', CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_ZYNQ',
) )
define_board(zynq, 'zynq', 'linux') class bbbmini(linux):
def configure_env(self, env):
super(bbbmini, self).configure_env(env)
def bbbmini(env):
env.TOOLCHAIN = 'arm-linux-gnueabihf' env.TOOLCHAIN = 'arm-linux-gnueabihf'
env.DEFINES.update( env.DEFINES.update(
CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_BBBMINI', CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_BBBMINI',
) )
define_board(bbbmini, 'bbbmini', 'linux') class pxf(linux):
def configure_env(self, env):
super(pxf, self).configure_env(env)
def pxf(env):
env.TOOLCHAIN = 'arm-linux-gnueabihf' env.TOOLCHAIN = 'arm-linux-gnueabihf'
env.DEFINES.update( env.DEFINES.update(
CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_PXF', CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_PXF',
) )
define_board(pxf, 'pxf', 'linux') class bebop(linux):
def configure_env(self, env):
super(bebop, self).configure_env(env)
def bebop(env):
env.TOOLCHAIN = 'arm-linux-gnueabihf' env.TOOLCHAIN = 'arm-linux-gnueabihf'
env.DEFINES.update( env.DEFINES.update(
CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_BEBOP', CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_BEBOP',
) )
env.STATIC_LINKING = [True] env.STATIC_LINKING = [True]
define_board(bebop, 'bebop', 'linux') class raspilot(linux):
def configure_env(self, env):
super(raspilot, self).configure_env(env)
def raspilot(env):
env.TOOLCHAIN = 'arm-linux-gnueabihf' env.TOOLCHAIN = 'arm-linux-gnueabihf'
env.DEFINES.update( env.DEFINES.update(
CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_RASPILOT', CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_RASPILOT',
) )
define_board(raspilot, 'raspilot', 'linux') class erlebrain2(linux):
def configure_env(self, env):
super(erlebrain2, self).configure_env(env)
def erlebrain2(env):
env.TOOLCHAIN = 'arm-linux-gnueabihf' env.TOOLCHAIN = 'arm-linux-gnueabihf'
env.DEFINES.update( env.DEFINES.update(
CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_ERLEBRAIN2', CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_ERLEBRAIN2',
) )
define_board(erlebrain2, 'erlebrain2', 'linux') class bhat(linux):
def configure_env(self, env):
super(bhat, self).configure_env(env)
def bhat(env):
env.TOOLCHAIN = 'arm-linux-gnueabihf' env.TOOLCHAIN = 'arm-linux-gnueabihf'
env.DEFINES.update( env.DEFINES.update(
CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_BH', CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_BH',
) )
define_board(bhat, 'bhat', 'linux') class pxfmini(linux):
def configure_env(self, env):
super(pxfmini, self).configure_env(env)
def pxfmini(env):
env.TOOLCHAIN = 'arm-linux-gnueabihf' env.TOOLCHAIN = 'arm-linux-gnueabihf'
env.DEFINES.update( env.DEFINES.update(
CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_PXFMINI', CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_PXFMINI',
) )
define_board(pxfmini, 'pxfmini', 'linux')

14
wscript
View File

@ -61,19 +61,7 @@ def configure(cfg):
cfg.msg('Setting board to', cfg.options.board) cfg.msg('Setting board to', cfg.options.board)
cfg.env.BOARD = cfg.options.board cfg.env.BOARD = cfg.options.board
board_dict = boards.BOARDS[cfg.env.BOARD].get_merged_dict() boards.get_board(cfg.env.BOARD).configure(cfg)
# Always prepend so that arguments passed in the command line get
# the priority.
for k in board_dict:
val = board_dict[k]
# Dictionaries (like 'DEFINES') are converted to lists to
# conform to waf conventions.
if isinstance(val, dict):
for item in val.items():
cfg.env.prepend_value(k, '%s=%s' % item)
else:
cfg.env.prepend_value(k, val)
cfg.load('toolchain') cfg.load('toolchain')
cfg.load('compiler_cxx compiler_c') cfg.load('compiler_cxx compiler_c')