Skip to content

Commit 1bb844c

Browse files
committed
Added macro definition from command line
build_api.py now support macros defined at compile time, so build.py and make.py can be used like this: $ make.py/build.py <options> -DMACRO1 -DMACRO2=VALUE2 ...
1 parent 5e6262a commit 1bb844c

File tree

7 files changed

+43
-35
lines changed

7 files changed

+43
-35
lines changed

workspace_tools/build.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
default=False, help="Verbose diagnostic output")
5353
parser.add_option("-b", "--ublox", action="store_true", dest="ublox",
5454
default=False, help="Compile the u-blox library")
55+
parser.add_option("-D", "", action="append", dest="macros",
56+
help="Add a macro definition")
5557
(options, args) = parser.parse_args()
5658

5759
# Get target list
@@ -92,10 +94,12 @@
9294
try:
9395
mcu = TARGET_MAP[target]
9496
build_mbed_libs(mcu, toolchain, options=options.options,
95-
verbose=options.verbose, clean=options.clean)
97+
verbose=options.verbose, clean=options.clean,
98+
macros=options.macros)
9699
for lib_id in libraries:
97100
build_lib(lib_id, mcu, toolchain, options=options.options,
98-
verbose=options.verbose, clean=options.clean)
101+
verbose=options.verbose, clean=options.clean,
102+
macros=options.macros)
99103
successes.append(id)
100104
except Exception, e:
101105
if options.verbose:

workspace_tools/build_api.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626

2727
def build_project(src_path, build_path, target, toolchain_name,
2828
libraries_paths=None, options=None, linker_script=None,
29-
clean=False, notify=None, verbose=False, name=None):
29+
clean=False, notify=None, verbose=False, name=None, macros=None):
3030
# Toolchain instance
31-
toolchain = TOOLCHAIN_CLASSES[toolchain_name](target, options, notify)
31+
toolchain = TOOLCHAIN_CLASSES[toolchain_name](target, options, notify, macros)
3232
toolchain.VERBOSE = verbose
3333
toolchain.build_all = clean
3434

@@ -40,7 +40,6 @@ def build_project(src_path, build_path, target, toolchain_name,
4040
# Scan src_path and libraries_paths for resources
4141
resources = toolchain.scan_resources(src_paths[0])
4242
for path in src_paths[1:]:
43-
print "PATH:", path
4443
resources.add(toolchain.scan_resources(path))
4544
if libraries_paths is not None:
4645
src_paths.extend(libraries_paths)
@@ -78,15 +77,15 @@ def build_project(src_path, build_path, target, toolchain_name,
7877
"""
7978
def build_library(src_paths, build_path, target, toolchain_name,
8079
dependencies_paths=None, options=None, name=None, clean=False,
81-
notify=None, verbose=False):
80+
notify=None, verbose=False, macros=None):
8281
if type(src_paths) != ListType: src_paths = [src_paths]
8382

8483
for src_path in src_paths:
8584
if not exists(src_path):
8685
raise Exception("The library source folder does not exist: %s", src_path)
8786

8887
# Toolchain instance
89-
toolchain = TOOLCHAIN_CLASSES[toolchain_name](target, options, notify)
88+
toolchain = TOOLCHAIN_CLASSES[toolchain_name](target, options, notify, macros)
9089
toolchain.VERBOSE = verbose
9190
toolchain.build_all = clean
9291

@@ -136,14 +135,14 @@ def build_lib(lib_id, target, toolchain, options=None, verbose=False, clean=Fals
136135

137136

138137
# We do have unique legacy conventions about how we build and package the mbed library
139-
def build_mbed_libs(target, toolchain_name, options=None, verbose=False, clean=False):
138+
def build_mbed_libs(target, toolchain_name, options=None, verbose=False, clean=False, macros=None):
140139
# Check toolchain support
141140
if toolchain_name not in target.supported_toolchains:
142141
print '\n%s target is not yet supported by toolchain %s' % (target.name, toolchain_name)
143142
return
144143

145144
# Toolchain
146-
toolchain = TOOLCHAIN_CLASSES[toolchain_name](target, options)
145+
toolchain = TOOLCHAIN_CLASSES[toolchain_name](target, options, macros=macros)
147146
toolchain.VERBOSE = verbose
148147
toolchain.build_all = clean
149148

@@ -192,3 +191,4 @@ def build_mbed_libs(target, toolchain_name, options=None, verbose=False, clean=F
192191
objects.remove(retargeting)
193192
toolchain.build_library(objects, BUILD_TOOLCHAIN, "mbed")
194193
toolchain.copy_files(retargeting, BUILD_TOOLCHAIN)
194+

workspace_tools/make.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@
4848
help="The name of the desired test program")
4949
parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
5050
default=False, help="Verbose diagnostic output")
51-
51+
parser.add_option("-D", "", action="append", dest="macros",
52+
help="Add a macro definition")
53+
5254
# Local run
5355
parser.add_option("-d", "--disk", dest="disk",
5456
default=None, help="The mbed disk")
@@ -114,7 +116,8 @@
114116
bin = build_project(test.source_dir, build_dir, target, toolchain,
115117
test.dependencies, options.options,
116118
linker_script=options.linker_script,
117-
clean=options.clean, verbose=options.verbose)
119+
clean=options.clean, verbose=options.verbose,
120+
macros=options.macros)
118121
print 'Image: %s' % bin
119122

120123
if options.disk:

workspace_tools/toolchains/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class mbedToolchain:
146146
GOANNA_FORMAT = "[Goanna] warning [%FILENAME%:%LINENO%] - [%CHECKNAME%(%SEVERITY%)] %MESSAGE%"
147147
GOANNA_DIAGNOSTIC_PATTERN = re.compile(r'"\[Goanna\] (?P<severity>warning) \[(?P<file>[^:]+):(?P<line>\d+)\] \- (?P<message>.*)"')
148148

149-
def __init__(self, target, options=None, notify=None):
149+
def __init__(self, target, options=None, notify=None, macros=None):
150150
self.target = target
151151
self.name = self.__class__.__name__
152152
self.hook = hooks.Hook(target, self)
@@ -162,6 +162,7 @@ def __init__(self, target, options=None, notify=None):
162162
self.options = []
163163
else:
164164
self.options = options
165+
self.macros = macros or []
165166
self.options.extend(BUILD_OPTIONS)
166167
if self.options:
167168
self.info("Build Options: %s" % (', '.join(self.options)))
@@ -347,7 +348,7 @@ def compile(self, cc, source, object, includes):
347348
self.progress("compile", source, build_update=True)
348349

349350
# Compile
350-
command = cc + ['-D%s' % s for s in self.get_symbols()] + ["-I%s" % i for i in includes] + ["-o", object, source]
351+
command = cc + ['-D%s' % s for s in self.get_symbols() + self.macros] + ["-I%s" % i for i in includes] + ["-o", object, source]
351352
if hasattr(self, "get_dep_opt"):
352353
command.extend(self.get_dep_opt(dep_path))
353354

workspace_tools/toolchains/arm.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class ARM(mbedToolchain):
3030
DIAGNOSTIC_PATTERN = re.compile('"(?P<file>[^"]+)", line (?P<line>\d+): (?P<severity>Warning|Error): (?P<message>.+)')
3131
DEP_PATTERN = re.compile('\S+:\s(?P<file>.+)\n')
3232

33-
def __init__(self, target, options=None, notify=None):
34-
mbedToolchain.__init__(self, target, options, notify)
33+
def __init__(self, target, options=None, notify=None, macros=None):
34+
mbedToolchain.__init__(self, target, options, notify, macros)
3535

3636
if target.core == "Cortex-M0+":
3737
cpu = "Cortex-M0"
@@ -123,16 +123,16 @@ def binary(self, elf, bin):
123123

124124

125125
class ARM_STD(ARM):
126-
def __init__(self, target, options=None, notify=None):
127-
ARM.__init__(self, target, options, notify)
126+
def __init__(self, target, options=None, notify=None, macros=None):
127+
ARM.__init__(self, target, options, notify, macros)
128128
self.ld.append("--libpath=%s" % ARM_LIB)
129129

130130

131131
class ARM_MICRO(ARM):
132132
PATCHED_LIBRARY = False
133133

134-
def __init__(self, target, options=None, notify=None):
135-
ARM.__init__(self, target, notify)
134+
def __init__(self, target, options=None, notify=None, macros=None):
135+
ARM.__init__(self, target, options, notify, macros)
136136

137137
# Compiler
138138
self.asm += ["-D__MICROLIB"]

workspace_tools/toolchains/gcc.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class GCC(mbedToolchain):
2929
CIRCULAR_DEPENDENCIES = True
3030
DIAGNOSTIC_PATTERN = re.compile('((?P<line>\d+):)(\d+:)? (?P<severity>warning|error): (?P<message>.+)')
3131

32-
def __init__(self, target, options=None, notify=None, tool_path=""):
33-
mbedToolchain.__init__(self, target, options, notify)
32+
def __init__(self, target, options=None, notify=None, macros=None, tool_path=""):
33+
mbedToolchain.__init__(self, target, options, notify, macros)
3434

3535
if target.core == "Cortex-M0+":
3636
cpu = "cortex-m0"
@@ -162,8 +162,8 @@ def binary(self, elf, bin):
162162

163163

164164
class GCC_ARM(GCC):
165-
def __init__(self, target, options=None, notify=None):
166-
GCC.__init__(self, target, options, notify, GCC_ARM_PATH)
165+
def __init__(self, target, options=None, notify=None, macros=None):
166+
GCC.__init__(self, target, options, notify, macros, GCC_ARM_PATH)
167167

168168
# Use latest gcc nanolib
169169
self.ld.append("--specs=nano.specs")
@@ -174,8 +174,8 @@ def __init__(self, target, options=None, notify=None):
174174

175175

176176
class GCC_CR(GCC):
177-
def __init__(self, target, options=None, notify=None):
178-
GCC.__init__(self, target, options, notify, GCC_CR_PATH)
177+
def __init__(self, target, options=None, notify=None, macros=None):
178+
GCC.__init__(self, target, options, notify, macros, GCC_CR_PATH)
179179

180180
additional_compiler_flags = [
181181
"-D__NEWLIB__", "-D__CODE_RED", "-D__USE_CMSIS", "-DCPP_USE_HEAP",
@@ -187,22 +187,22 @@ def __init__(self, target, options=None, notify=None):
187187

188188

189189
class GCC_CS(GCC):
190-
def __init__(self, target, options=None, notify=None):
191-
GCC.__init__(self, target, options, notify, GCC_CS_PATH)
190+
def __init__(self, target, options=None, notify=None, macros=None):
191+
GCC.__init__(self, target, options, notify, macros, GCC_CS_PATH)
192192

193193

194194
class GCC_CW(GCC):
195195
ARCH_LIB = {
196196
"Cortex-M0+": "armv6-m",
197197
}
198198

199-
def __init__(self, target, options=None, notify=None):
200-
GCC.__init__(self, target, options, notify, CW_GCC_PATH)
199+
def __init__(self, target, options=None, notify=None, macros=None):
200+
GCC.__init__(self, target, options, notify, macros, CW_GCC_PATH)
201201

202202

203203
class GCC_CW_EWL(GCC_CW):
204-
def __init__(self, target, options=None, notify=None):
205-
GCC_CW.__init__(self, target, options, notify)
204+
def __init__(self, target, options=None, notify=None, macros=None):
205+
GCC_CW.__init__(self, target, options, notify, macros)
206206

207207
# Compiler
208208
common = [
@@ -230,5 +230,5 @@ def __init__(self, target, options=None, notify=None):
230230

231231

232232
class GCC_CW_NEWLIB(GCC_CW):
233-
def __init__(self, target, options=None, notify=None):
234-
GCC_CW.__init__(self, target, options, notify)
233+
def __init__(self, target, options=None, notify=None, macros=None):
234+
GCC_CW.__init__(self, target, options, notify, macros)

workspace_tools/toolchains/iar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class IAR(mbedToolchain):
2929

3030
DIAGNOSTIC_PATTERN = re.compile('"(?P<file>[^"]+)",(?P<line>[\d]+)\s+(?P<severity>Warning|Error)(?P<message>.+)')
3131

32-
def __init__(self, target, options=None, notify=None):
33-
mbedToolchain.__init__(self, target, options, notify)
32+
def __init__(self, target, options=None, notify=None, macros=None):
33+
mbedToolchain.__init__(self, target, options, notify, macros)
3434

3535
c_flags = [
3636
"-Oh",

0 commit comments

Comments
 (0)