|
17 | 17 |
|
18 | 18 | import re
|
19 | 19 | import sys
|
20 |
| -from os import stat, walk, getcwd, sep |
| 20 | +from os import stat, walk, getcwd, sep, remove |
21 | 21 | from copy import copy
|
22 | 22 | from time import time, sleep
|
23 | 23 | from types import ListType
|
@@ -235,6 +235,12 @@ def __init__(self, target, options=None, notify=None, macros=None, silent=False,
|
235 | 235 | # This will hold the configuration data (as returned by Config.get_config_data())
|
236 | 236 | self.config_data = None
|
237 | 237 |
|
| 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 | + |
238 | 244 | # Non-incremental compile
|
239 | 245 | self.build_all = False
|
240 | 246 |
|
@@ -700,6 +706,9 @@ def compile_sources(self, resources, build_path, inc_dirs=None):
|
700 | 706 | work_dir = getcwd()
|
701 | 707 | self.prev_dir = None
|
702 | 708 |
|
| 709 | + # Generate configuration header (this will update self.build_all if needed) |
| 710 | + self.get_config_header() |
| 711 | + |
703 | 712 | # Sort compile queue for consistency
|
704 | 713 | files_to_compile.sort()
|
705 | 714 | for source in files_to_compile:
|
@@ -978,19 +987,53 @@ def mem_stats(self, map):
|
978 | 987 | def set_config_data(self, config_data):
|
979 | 988 | self.config_data = config_data
|
980 | 989 |
|
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) |
986 | 1000 | 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 |
994 | 1037 |
|
995 | 1038 | # Return the list of macros geenrated by the build system
|
996 | 1039 | def get_config_macros(self):
|
|
0 commit comments