Skip to content

Commit 51540d0

Browse files
authored
Merge pull request #2177 from mbedmicro/build_on_config_change
Rebuild sources if configuration is changed
2 parents 518afae + a558c49 commit 51540d0

File tree

1 file changed

+56
-13
lines changed

1 file changed

+56
-13
lines changed

tools/toolchains/__init__.py

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import re
1919
import sys
20-
from os import stat, walk, getcwd, sep
20+
from os import stat, walk, getcwd, sep, remove
2121
from copy import copy
2222
from time import time, sleep
2323
from types import ListType
@@ -235,6 +235,12 @@ def __init__(self, target, options=None, notify=None, macros=None, silent=False,
235235
# This will hold the configuration data (as returned by Config.get_config_data())
236236
self.config_data = None
237237

238+
# This will hold the location of the configuration file or None if there's no configuration available
239+
self.config_file = None
240+
241+
# Call guard for "get_config_data" (see the comments of get_config_data for details)
242+
self.config_processed = False
243+
238244
# Non-incremental compile
239245
self.build_all = False
240246

@@ -700,6 +706,9 @@ def compile_sources(self, resources, build_path, inc_dirs=None):
700706
work_dir = getcwd()
701707
self.prev_dir = None
702708

709+
# Generate configuration header (this will update self.build_all if needed)
710+
self.get_config_header()
711+
703712
# Sort compile queue for consistency
704713
files_to_compile.sort()
705714
for source in files_to_compile:
@@ -978,19 +987,53 @@ def mem_stats(self, map):
978987
def set_config_data(self, config_data):
979988
self.config_data = config_data
980989

981-
# Return the location of the config header. This function will create the config
982-
# header first if needed. The header will be written in a file called "mbed_conf.h"
983-
# located in the project's build directory.
984-
# If config headers are not used (self.config_header_content is None), the function
985-
# returns None
990+
# Creates the configuration header if needed:
991+
# - if there is no configuration data, "mbed_config.h" is not create (or deleted if it exists).
992+
# - if there is configuration data and "mbed_config.h" does not exist, it is created.
993+
# - if there is configuration data similar to the previous configuration data,
994+
# "mbed_config.h" is left untouched.
995+
# - if there is new configuration data, "mbed_config.h" is overriden.
996+
# The function needs to be called exactly once for the lifetime of this toolchain instance.
997+
# The "config_processed" variable (below) ensures this behaviour.
998+
# The function returns the location of the configuration file, or None if there is no
999+
# configuration data available (and thus no configuration file)
9861000
def get_config_header(self):
987-
if self.config_data is None:
988-
return None
989-
config_file = join(self.build_dir, self.MBED_CONFIG_FILE_NAME)
990-
if not exists(config_file):
991-
with open(config_file, "wt") as f:
992-
f.write(Config.config_to_header(self.config_data))
993-
return config_file
1001+
if self.config_processed: # this function was already called, return its result
1002+
return self.config_file
1003+
# The config file is located in the build directory
1004+
self.config_file = join(self.build_dir, self.MBED_CONFIG_FILE_NAME)
1005+
# If the file exists, read its current content in prev_data
1006+
if exists(self.config_file):
1007+
with open(self.config_file, "rt") as f:
1008+
prev_data = f.read()
1009+
else:
1010+
prev_data = None
1011+
# Get the current configuration data
1012+
crt_data = Config.config_to_header(self.config_data) if self.config_data else None
1013+
# "changed" indicates if a configuration change was detected
1014+
changed = False
1015+
if prev_data is not None: # a previous mbed_config.h exists
1016+
if crt_data is None: # no configuration data, so "mbed_config.h" needs to be removed
1017+
remove(self.config_file)
1018+
self.config_file = None # this means "config file not present"
1019+
changed = True
1020+
elif crt_data != prev_data: # different content of config file
1021+
with open(self.config_file, "wt") as f:
1022+
f.write(crt_data)
1023+
changed = True
1024+
else: # a previous mbed_config.h does not exist
1025+
if crt_data is not None: # there's configuration data available
1026+
with open(self.config_file, "wt") as f:
1027+
f.write(crt_data)
1028+
changed = True
1029+
else:
1030+
self.config_file = None # this means "config file not present"
1031+
# If there was a change in configuration, rebuild everything
1032+
self.build_all = changed
1033+
# Make sure that this function will only return the location of the configuration
1034+
# file for subsequent calls, without trying to manipulate its content in any way.
1035+
self.config_processed = True
1036+
return self.config_file
9941037

9951038
# Return the list of macros geenrated by the build system
9961039
def get_config_macros(self):

0 commit comments

Comments
 (0)