Skip to content

Commit 2904e10

Browse files
committed
Merge pull request #203 from ARMmbed/get_config
Added support for viewing the configuration
2 parents 028c9b9 + 6f1298b commit 2904e10

File tree

3 files changed

+124
-7
lines changed

3 files changed

+124
-7
lines changed

tools/config.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,22 @@ def set_value(self, value, unit_name, unit_kind, label = None):
103103

104104
# Return the string representation of this configuration parameter
105105
def __str__(self):
106-
return '"%s" = %s (set in "%s", defined in "%s")' % (self.name, self.value, self.set_by, self.defined_by)
106+
if self.value is not None:
107+
return '%s = %s (macro name: "%s")' % (self.name, self.value, self.macro_name)
108+
else:
109+
return '%s has no value' % self.name
110+
111+
# Return a verbose description of this configuration paramater as a string
112+
def get_verbose_description(self):
113+
desc = "Name: %s%s\n" % (self.name, " (required parameter)" if self.required else "")
114+
if self.help_text:
115+
desc += " Description: %s\n" % self.help_text
116+
desc += " Defined by: %s\n" % self.defined_by
117+
if not self.value:
118+
return desc + " No value set"
119+
desc += " Macro name: %s\n" % self.macro_name
120+
desc += " Value: %s (set by %s)" % (self.value, self.set_by)
121+
return desc
107122

108123
# A representation of a configuration macro. It handles both macros without a value (MACRO)
109124
# and with a value (MACRO=VALUE)
@@ -297,8 +312,14 @@ def _check_required_parameters(self, params):
297312
if p.required and (p.value is None):
298313
raise ConfigException("Required parameter '%s' defined by '%s' doesn't have a value" % (p.name, p.defined_by))
299314

315+
# Return the macro definitions generated for a dictionary of configuration parameters
316+
# params: a dictionary of (name, ConfigParameters instance) mappings
317+
@staticmethod
318+
def parameters_to_macros(params):
319+
return ['%s=%s' % (m.macro_name, m.value) for m in params.values() if m.value is not None]
320+
300321
# Return the configuration data converted to a list of C macros
301322
def get_config_data_macros(self):
302323
params, macros = self.get_config_data()
303324
self._check_required_parameters(params)
304-
return macros + ['%s=%s' % (m.macro_name, m.value) for m in params.values() if m.value is not None]
325+
return macros + self.parameters_to_macros(params)

tools/get_config.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#! /usr/bin/env python2
2+
"""
3+
mbed SDK
4+
Copyright (c) 2011-2013 ARM Limited
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
"""
19+
import sys
20+
from os.path import isdir, abspath, dirname, join
21+
from os import _exit
22+
23+
# Be sure that the tools directory is in the search path
24+
ROOT = abspath(join(dirname(__file__), ".."))
25+
sys.path.insert(0, ROOT)
26+
27+
from tools.utils import args_error
28+
from tools.options import get_default_options_parser
29+
from tools.build_api import get_config
30+
from config import Config
31+
try:
32+
import tools.private_settings as ps
33+
except:
34+
ps = object()
35+
36+
if __name__ == '__main__':
37+
# Parse Options
38+
parser = get_default_options_parser(add_clean=False, add_options=False)
39+
parser.add_option("--source", dest="source_dir",
40+
default=None, help="The source (input) directory", action="append")
41+
parser.add_option("--prefix", dest="prefix", action="append",
42+
default=None, help="Restrict listing to parameters that have this prefix")
43+
parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
44+
default=False, help="Verbose diagnostic output")
45+
46+
(options, args) = parser.parse_args()
47+
48+
for path in options.source_dir :
49+
if not isdir(path) :
50+
args_error(parser, "[ERROR] you passed \"{}\" to --source, which does not exist".
51+
format(path))
52+
# Target
53+
if options.mcu is None :
54+
args_error(parser, "[ERROR] You should specify an MCU")
55+
target = options.mcu
56+
57+
# Toolchain
58+
if options.tool is None:
59+
args_error(parser, "[ERROR] You should specify a TOOLCHAIN")
60+
toolchain = options.tool
61+
62+
options.prefix = options.prefix or [""]
63+
64+
try:
65+
params, macros = get_config(options.source_dir, target, toolchain)
66+
if not params and not macros:
67+
print "No configuration data available."
68+
_exit(0)
69+
if params:
70+
print "Configuration parameters"
71+
print "------------------------"
72+
for p in params:
73+
for s in options.prefix:
74+
if p.startswith(s):
75+
print(str(params[p]) if not options.verbose else params[p].get_verbose_description())
76+
break
77+
print ""
78+
79+
print "Macros"
80+
print "------"
81+
if macros:
82+
print 'Defined with "macros":', macros
83+
print "Generated from configuration parameters:", Config.parameters_to_macros(params)
84+
85+
except KeyboardInterrupt, e:
86+
print "\n[CTRL+c] exit"
87+
except Exception,e:
88+
if options.verbose:
89+
import traceback
90+
traceback.print_exc(file=sys.stdout)
91+
else:
92+
print "[ERROR] %s" % str(e)
93+
94+
sys.exit(1)

tools/options.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from tools.targets import TARGET_NAMES
2020

2121

22-
def get_default_options_parser():
22+
def get_default_options_parser(add_clean=True, add_options=True):
2323
parser = OptionParser()
2424

2525
targetnames = TARGET_NAMES
@@ -35,10 +35,12 @@ def get_default_options_parser():
3535
help="build using the given TOOLCHAIN (%s)" % ', '.join(toolchainlist),
3636
metavar="TOOLCHAIN")
3737

38-
parser.add_option("-c", "--clean", action="store_true", default=False,
39-
help="clean the build directory")
38+
if add_clean:
39+
parser.add_option("-c", "--clean", action="store_true", default=False,
40+
help="clean the build directory")
4041

41-
parser.add_option("-o", "--options", action="append",
42-
help='Add a build option ("save-asm": save the asm generated by the compiler, "debug-info": generate debugging information, "analyze": run Goanna static code analyzer")')
42+
if add_options:
43+
parser.add_option("-o", "--options", action="append",
44+
help='Add a build option ("save-asm": save the asm generated by the compiler, "debug-info": generate debugging information, "analyze": run Goanna static code analyzer")')
4345

4446
return parser

0 commit comments

Comments
 (0)