Skip to content

Commit 6b759f7

Browse files
author
Bogdan Marinescu
committed
Change how the config system is used
This commit address the issue presented in #2073. Previously, mbed_config.h was automatically included by the toolchain. With this change, all the sources that need "mbed_config.h" need to include it manually. However, "mbed.h" now includes "mbed_config.h", so configuration data is automatically available to sources that include "mbed.h". With this change, even if there's no configuration data in the project being built, an empty "mbed_config.h" is generated by the build system. This change looks big, but most of it is an almost complete reversal of #1975. This is likely a breaking change, and needs to be properly communicated.
1 parent d0d023a commit 6b759f7

File tree

15 files changed

+40
-76
lines changed

15 files changed

+40
-76
lines changed

hal/api/mbed.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
#define MBED_LIBRARY_VERSION 121
2020

21+
// Include the configuration data as early as possible
22+
#include "mbed_config.h"
23+
2124
#include "toolchain.h"
2225
#include "platform.h"
2326

tools/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ def load_resources(self, resources):
435435
# (as returned by get_config_data())
436436
@staticmethod
437437
def config_to_header(config, fname = None):
438-
params, macros = config[0], config[1]
438+
params, macros = (config[0], config[1]) if config else ({}, {})
439439
Config._check_required_parameters(params)
440440
header_data = "// Automatically generated configuration file.\n"
441441
header_data += "// DO NOT EDIT, content will be overwritten.\n\n"

tools/export/atmelstudio.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ class AtmelStudio(Exporter):
3333

3434
DOT_IN_RELATIVE_PATH = True
3535

36-
MBED_CONFIG_HEADER_SUPPORTED = True
37-
3836
def generate(self):
3937

4038
source_files = []

tools/export/codered.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ class CodeRed(Exporter):
2222
NAME = 'CodeRed'
2323
TOOLCHAIN = 'GCC_CR'
2424

25-
MBED_CONFIG_HEADER_SUPPORTED = True
26-
2725
TARGETS = [
2826
'LPC1768',
2927
'LPC4088',

tools/export/emblocks.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ class IntermediateFile(Exporter):
3131
# we support all GCC targets (is handled on IDE side)
3232
TARGETS = gccTargets
3333

34-
MBED_CONFIG_HEADER_SUPPORTED = True
35-
3634
FILE_TYPES = {
3735
'headers': 'h',
3836
'c_sources': 'c',

tools/export/exporters.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ def __init__(self, target, inputDir, program_name, build_url_resolver, extra_sym
3636
self.extra_symbols = extra_symbols
3737
self.config_macros = []
3838
self.sources_relative = sources_relative
39-
self.config_header = None
4039

4140
def get_toolchain(self):
4241
return self.TOOLCHAIN
@@ -49,9 +48,6 @@ def flags(self):
4948
def progen_flags(self):
5049
if not hasattr(self, "_progen_flag_cache") :
5150
self._progen_flag_cache = dict([(key + "_flags", value) for key,value in self.flags.iteritems()])
52-
if self.config_header:
53-
self._progen_flag_cache['c_flags'] += self.toolchain.get_config_option(self.config_header)
54-
self._progen_flag_cache['cxx_flags'] += self.toolchain.get_config_option(self.config_header)
5551
return self._progen_flag_cache
5652

5753
def __scan_and_copy(self, src_path, trg_path):
@@ -166,20 +162,14 @@ def scan_and_copy_resources(self, prj_paths, trg_path, relative=False):
166162
# use the prj_dir (source, not destination)
167163
resources = self.toolchain.scan_resources(prj_paths[0])
168164
for path in prj_paths[1:]:
169-
resources.add(toolchain.scan_resources(path))
165+
resources.add(self.toolchain.scan_resources(path))
170166

171167
# Loads the resources into the config system which might expand/modify resources based on config data
172168
self.resources = config.load_resources(resources)
173169

170+
# Generate configuration header
171+
config.get_config_data_header(join(trg_path, self.toolchain.MBED_CONFIG_FILE_NAME))
174172

175-
if hasattr(self, "MBED_CONFIG_HEADER_SUPPORTED") and self.MBED_CONFIG_HEADER_SUPPORTED :
176-
# Add the configuration file to the target directory
177-
self.config_header = self.toolchain.MBED_CONFIG_FILE_NAME
178-
config.get_config_data_header(join(trg_path, self.config_header))
179-
self.config_macros = []
180-
else :
181-
# And add the configuration macros to the toolchain
182-
self.config_macros = config.get_config_data_macros()
183173
# Check the existence of a binary build of the mbed library for the desired target
184174
# This prevents exporting the mbed libraries from source
185175
# if not self.toolchain.mbed_libs:

tools/export/gccarm.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,6 @@ class GccArm(Exporter):
128128

129129
DOT_IN_RELATIVE_PATH = True
130130

131-
MBED_CONFIG_HEADER_SUPPORTED = True
132-
133131
def generate(self):
134132
# "make" wants Unix paths
135133
if self.sources_relative:

tools/export/iar.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ class IAREmbeddedWorkbench(Exporter):
3333
# PROGEN_ACTIVE contains information for exporter scripts that this is using progen
3434
PROGEN_ACTIVE = True
3535

36-
MBED_CONFIG_HEADER_SUPPORTED = True
37-
3836
# backward compatibility with our scripts
3937
TARGETS = []
4038
for target in TARGET_NAMES:

tools/export/simplicityv3.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ class SimplicityV3(Exporter):
101101

102102
DOT_IN_RELATIVE_PATH = False
103103

104-
MBED_CONFIG_HEADER_SUPPORTED = True
105-
106104
orderedPaths = Folder("Root")
107105

108106
def check_and_add_path(self, path):

tools/export/uvision4.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ class Uvision4(Exporter):
3434
# PROGEN_ACTIVE contains information for exporter scripts that this is using progen
3535
PROGEN_ACTIVE = True
3636

37-
MBED_CONFIG_HEADER_SUPPORTED = True
38-
3937
# backward compatibility with our scripts
4038
TARGETS = []
4139
for target in TARGET_NAMES:

tools/export/uvision5.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ class Uvision5(Exporter):
3434
# PROGEN_ACTIVE contains information for exporter scripts that this is using progen
3535
PROGEN_ACTIVE = True
3636

37-
MBED_CONFIG_HEADER_SUPPORTED = True
38-
3937
# backward compatibility with our scripts
4038
TARGETS = []
4139
for target in TARGET_NAMES:

tools/toolchains/__init__.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,11 @@ def compile_sources(self, resources, build_path, inc_dirs=None):
647647
self.to_be_compiled = len(files_to_compile)
648648
self.compiled = 0
649649

650-
inc_paths = resources.inc_dirs
650+
# Create the configuration header and append its path to the include list
651+
# "create_config_header" needs to know the build directory
652+
self.build_dir = build_path
653+
config_file_location = self.create_config_header()
654+
inc_paths = resources.inc_dirs + [dirname(config_file_location)]
651655
if inc_dirs is not None:
652656
inc_paths.extend(inc_dirs)
653657
# De-duplicate include paths
@@ -656,8 +660,6 @@ def compile_sources(self, resources, build_path, inc_dirs=None):
656660
inc_paths = sorted(set(inc_paths))
657661
# Unique id of all include paths
658662
self.inc_md5 = md5(' '.join(inc_paths)).hexdigest()
659-
# Where to store response files
660-
self.build_dir = build_path
661663

662664
objects = []
663665
queue = []
@@ -944,18 +946,30 @@ def mem_stats(self, map):
944946
def set_config_data(self, config_data):
945947
self.config_data = config_data
946948

947-
# Return the location of the config header. This function will create the config
948-
# header first if needed. The header will be written in a file called "mbed_conf.h"
949-
# located in the project's build directory.
950-
# If config headers are not used (self.config_header_content is None), the function
951-
# returns None
952-
def get_config_header(self):
953-
if self.config_data is None:
954-
return None
955-
config_file = join(self.build_dir, self.MBED_CONFIG_FILE_NAME)
956-
if not exists(config_file):
949+
# Creates the configuration header if needed:
950+
# - if there is no configuration data, an empty (no config data) "mbed_config.h" is created
951+
# - if there is configuration data and "mbed_config.h" does not exist, it is created.
952+
# - if there is configuration data similar to the previous configuration data,
953+
# "mbed_config.h" is left untouched
954+
# - if there is new configuration data, "mbed_config.h" is overriden
955+
# basedir: the base directory for "mbed_config.h" (defaults to self.build.dir if not specified)
956+
# The function returns the location of "mbed_config.h"
957+
def create_config_header(self, basedir = None):
958+
config_file = join(basedir or self.build_dir, self.MBED_CONFIG_FILE_NAME)
959+
# If the file exists, read its current content in prev_data
960+
if exists(config_file):
961+
with open(config_file, "rt") as f:
962+
prev_data = f.read()
963+
else:
964+
prev_data = None
965+
# Get the current configuration data
966+
crt_data = Config.config_to_header(self.config_data)
967+
# Create the config file if it contains different data or if it doesn't exist
968+
# (both conditions are covered by 'prev_data != crt_data', since crt_data can
969+
# never be None).
970+
if prev_data != crt_data:
957971
with open(config_file, "wt") as f:
958-
f.write(Config.config_to_header(self.config_data))
972+
f.write(crt_data)
959973
return config_file
960974

961975
# Return the list of macros geenrated by the build system

tools/toolchains/arm.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,8 @@ def get_dep_option(self, object):
116116
dep_path = base + '.d'
117117
return ["--depend", dep_path]
118118

119-
def get_config_option(self, config_header) :
120-
return ['--preinclude=' + config_header]
121-
122119
def get_compile_options(self, defines, includes):
123-
opts = ['-D%s' % d for d in defines] + ['--via', self.get_inc_file(includes)]
124-
config_header = self.get_config_header()
125-
if config_header is not None:
126-
opts = opts + self.get_config_option(config_header)
127-
return opts
120+
return ['-D%s' % d for d in defines] + ['--via', self.get_inc_file(includes)]
128121

129122
@hook_tool
130123
def assemble(self, source, object, includes):

tools/toolchains/gcc.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,8 @@ def get_dep_option(self, object):
166166
dep_path = base + '.d'
167167
return ["-MD", "-MF", dep_path]
168168

169-
def get_config_option(self, config_header):
170-
return ['-include', config_header]
171-
172169
def get_compile_options(self, defines, includes):
173-
opts = ['-D%s' % d for d in defines] + ['@%s' % self.get_inc_file(includes)]
174-
config_header = self.get_config_header()
175-
if config_header is not None:
176-
opts = opts + self.get_config_option(config_header)
177-
return opts
170+
return ['-D%s' % d for d in defines] + ['@%s' % self.get_inc_file(includes)]
178171

179172
@hook_tool
180173
def assemble(self, source, object, includes):

tools/toolchains/iar.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -137,26 +137,13 @@ def cc_extra(self, object):
137137
base, _ = splitext(object)
138138
return ["-l", base + '.s.txt']
139139

140-
def get_config_option(self, config_header):
141-
return ['--preinclude=' + config_header]
142-
143-
def get_compile_options(self, defines, includes, for_asm=False):
144-
opts = ['-D%s' % d for d in defines] + ['-f', self.get_inc_file(includes)]
145-
config_header = self.get_config_header()
146-
if for_asm:
147-
# The assembler doesn't support '--preinclude', so we need to add
148-
# the macros directly
149-
opts = opts + ['-D%s' % d for d in self.get_config_macros()]
150-
else:
151-
config_header = self.get_config_header()
152-
if config_header is not None:
153-
opts = opts + self.get_config_option(config_header)
154-
return opts
140+
def get_compile_options(self, defines, includes):
141+
return ['-D%s' % d for d in defines] + ['-f', self.get_inc_file(includes)]
155142

156143
@hook_tool
157144
def assemble(self, source, object, includes):
158145
# Build assemble command
159-
cmd = self.asm + self.get_compile_options(self.get_symbols(), includes, for_asm=True) + ["-o", object, source]
146+
cmd = self.asm + self.get_compile_options(self.get_symbols(), includes) + ["-o", object, source]
160147

161148
# Call cmdline hook
162149
cmd = self.hook.get_cmdline_assembler(cmd)

0 commit comments

Comments
 (0)