Skip to content

Commit 7e11174

Browse files
committed
Calculate md5 of all include paths in compile_sources() and remove calculation from <toolchaon>get_compile_options(), thus significantly reduce repetitive md5 calculations
Unify handling of the include response file in mbedToolchain::get_inc_file() Sanitize obsolete no-longer needed methods
1 parent f01e136 commit 7e11174

File tree

4 files changed

+50
-72
lines changed

4 files changed

+50
-72
lines changed

tools/toolchains/__init__.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from tools.utils import run_cmd, mkdir, rel_path, ToolException, NotSupportedException, split_path
3131
from tools.settings import BUILD_OPTIONS, MBED_ORG_USER
3232
import tools.hooks as hooks
33+
from hashlib import md5
3334

3435

3536
#Disables multiprocessing if set to higher number than the host machine CPUs
@@ -478,18 +479,35 @@ def relative_object_path(self, build_path, base_dir, source):
478479
mkdir(obj_dir)
479480
return join(obj_dir, name + '.o')
480481

482+
def get_inc_file(self, includes):
483+
include_file = join(self.temp_dir, "includes_%s.txt" % self.inc_md5)
484+
if not exists(include_file):
485+
with open(include_file, "wb") as f:
486+
cmd_list = []
487+
for c in includes:
488+
if c:
489+
cmd_list.append(('-I%s' % c).replace("\\", "/"))
490+
string = " ".join(cmd_list)
491+
f.write(string)
492+
return include_file
493+
481494
def compile_sources(self, resources, build_path, inc_dirs=None):
482495
# Web IDE progress bar for project build
483496
files_to_compile = resources.s_sources + resources.c_sources + resources.cpp_sources
484497
self.to_be_compiled = len(files_to_compile)
485498
self.compiled = 0
486-
self.temp_dir = build_path
487499

488500
inc_paths = resources.inc_dirs
489501
if inc_dirs is not None:
490502
inc_paths.extend(inc_dirs)
491-
# De-duplicate include paths and sort for consistency
503+
# De-duplicate include paths
504+
inc_paths = set(inc_paths)
505+
# Sort include paths for consistency
492506
inc_paths = sorted(set(inc_paths))
507+
# Unique id of all include paths
508+
self.inc_md5 = md5(' '.join(inc_paths)).hexdigest()
509+
# Where to store response files
510+
self.temp_dir = build_path
493511

494512
objects = []
495513
queue = []

tools/toolchains/arm.py

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"""
1717
import re
1818
from os.path import join, dirname, splitext, basename, exists
19-
from hashlib import md5
2019

2120
from tools.toolchains import mbedToolchain
2221
from tools.settings import ARM_BIN, ARM_INC, ARM_LIB, MY_ARM_CLIB, ARM_CPPLIB, GOANNA_PATH
@@ -79,11 +78,6 @@ def __init__(self, target, options=None, notify=None, macros=None, silent=False,
7978
self.ar = join(ARM_BIN, "armar")
8079
self.elf2bin = join(ARM_BIN, "fromelf")
8180

82-
def remove_option(self, option):
83-
for tool in [self.asm, self.cc, self.cppc]:
84-
if option in tool:
85-
tool.remove(option)
86-
8781
def parse_dependencies(self, dep_path):
8882
dependencies = []
8983
for line in open(dep_path).readlines():
@@ -113,25 +107,13 @@ def parse_output(self, output):
113107
match.group('message')
114108
)
115109

116-
def get_dep_opt(self, dep_path):
110+
def get_dep_option(self, object):
111+
base, _ = splitext(object)
112+
dep_path = base + '.d'
117113
return ["--depend", dep_path]
118114

119-
def get_compile_options(self, defines, includes):
120-
cmd = []
121-
122-
sum = md5(' '.join(includes)).hexdigest()
123-
options_file = join(self.temp_dir, "options_%s.txt" % sum)
124-
if not exists(options_file):
125-
with open(options_file, "wb") as f:
126-
cmd_list = ['-D%s' % d for d in defines]
127-
for c in includes:
128-
if c:
129-
cmd_list.append(('-I%s' % c) if not c.startswith('-') else c)
130-
string = " ".join(cmd_list).replace("\\", "/")
131-
f.write(string)
132-
cmd.extend(['--via', options_file])
133-
134-
return cmd
115+
def get_compile_options(self, defines, includes):
116+
return ['-D%s' % d for d in defines] + ['--via', self.get_inc_file(includes)]
135117

136118
@hook_tool
137119
def assemble(self, source, object, includes):
@@ -158,9 +140,7 @@ def compile(self, cc, source, object, includes):
158140
# Build compile command
159141
cmd = cc + self.get_compile_options(self.get_symbols(), includes)
160142

161-
base, _ = splitext(object)
162-
dep_path = base + '.d'
163-
cmd.extend(self.get_dep_opt(dep_path))
143+
cmd.extend(self.get_dep_option(object))
164144

165145
cmd.extend(["-o", object, source])
166146

tools/toolchains/gcc.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"""
1717
import re
1818
from os.path import join, basename, splitext, dirname, exists
19-
from hashlib import md5
2019

2120
from tools.toolchains import mbedToolchain
2221
from tools.settings import GCC_ARM_PATH, GCC_CR_PATH
@@ -69,7 +68,7 @@ def __init__(self, target, options=None, notify=None, macros=None, silent=False,
6968
"-Wno-unused-parameter", "-Wno-missing-field-initializers",
7069
"-fmessage-length=0", "-fno-exceptions", "-fno-builtin",
7170
"-ffunction-sections", "-fdata-sections",
72-
"-MMD", "-fno-delete-null-pointer-checks", "-fomit-frame-pointer"
71+
"-fno-delete-null-pointer-checks", "-fomit-frame-pointer"
7372
] + self.cpu
7473

7574
if "save-asm" in self.options:
@@ -162,22 +161,13 @@ def parse_output(self, output):
162161
message + match.group('message')
163162
)
164163

164+
def get_dep_option(self, object):
165+
base, _ = splitext(object)
166+
dep_path = base + '.d'
167+
return ["-MD", "-MF", dep_path]
168+
165169
def get_compile_options(self, defines, includes):
166-
cmd = []
167-
168-
sum = md5(' '.join(includes)).hexdigest()
169-
options_file = join(self.temp_dir, "options_%s.txt" % sum)
170-
if not exists(options_file):
171-
with open(options_file, "wb") as f:
172-
cmd_list = ['-D%s' % d for d in defines]
173-
for c in includes:
174-
if c:
175-
cmd_list.append(('-I%s' % c) if not c.startswith('-') else c)
176-
string = " ".join(cmd_list).replace("\\", "/")
177-
f.write(string)
178-
cmd.extend(['@%s' % options_file])
179-
180-
return cmd
170+
return ['-D%s' % d for d in defines] + ['@%s' % self.get_inc_file(includes)]
181171

182172
@hook_tool
183173
def assemble(self, source, object, includes):
@@ -193,8 +183,12 @@ def assemble(self, source, object, includes):
193183
@hook_tool
194184
def compile(self, cc, source, object, includes):
195185
# Build compile command
196-
cmd = cc + self.get_compile_options(self.get_symbols(), includes) + ["-o", object, source]
186+
cmd = cc + self.get_compile_options(self.get_symbols(), includes)
197187

188+
cmd.extend(self.get_dep_option(object))
189+
190+
cmd.extend(["-o", object, source])
191+
198192
# Call cmdline hook
199193
cmd = self.hook.get_cmdline_compiler(cmd)
200194

tools/toolchains/iar.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import re
1818
from os import remove
1919
from os.path import join, exists, dirname, splitext, exists
20-
from hashlib import md5
2120

2221
from tools.toolchains import mbedToolchain
2322
from tools.settings import IAR_PATH
@@ -73,6 +72,10 @@ def __init__(self, target, options=None, notify=None, macros=None, silent=False,
7372
self.ar = join(IAR_BIN, "iarchive")
7473
self.elf2bin = join(IAR_BIN, "ielftool")
7574

75+
def parse_dependencies(self, dep_path):
76+
return [path.strip() for path in open(dep_path).readlines()
77+
if (path and not path.isspace())]
78+
7679
def parse_output(self, output):
7780
for line in output.splitlines():
7881
match = IAR.DIAGNOSTIC_PATTERN.match(line)
@@ -94,32 +97,17 @@ def parse_output(self, output):
9497
match.group('message')
9598
)
9699

97-
def get_dep_opt(self, dep_path):
100+
def get_dep_option(self, object):
101+
base, _ = splitext(object)
102+
dep_path = base + '.d'
98103
return ["--dependencies", dep_path]
99104

100-
def cc_extra(self, base):
105+
def cc_extra(self, object):
106+
base, _ = splitext(object)
101107
return ["-l", base + '.s']
102108

103-
def parse_dependencies(self, dep_path):
104-
return [path.strip() for path in open(dep_path).readlines()
105-
if (path and not path.isspace())]
106-
107109
def get_compile_options(self, defines, includes):
108-
cmd = []
109-
110-
sum = md5(' '.join(includes)).hexdigest()
111-
options_file = join(self.temp_dir, "options_%s.txt" % sum)
112-
if not exists(options_file):
113-
with open(options_file, "wb") as f:
114-
cmd_list = ['-D%s' % d for d in defines]
115-
for c in includes:
116-
if c:
117-
cmd_list.append(('-I%s' % c) if not c.startswith('-') else c)
118-
string = " ".join(cmd_list).replace("\\", "/")
119-
f.write(string)
120-
cmd.extend(['-f', options_file])
121-
122-
return cmd
110+
return ['-D%s' % d for d in defines] + ['-f', self.get_inc_file(includes)]
123111

124112
@hook_tool
125113
def assemble(self, source, object, includes):
@@ -137,11 +125,9 @@ def compile(self, cc, source, object, includes):
137125
# Build compile command
138126
cmd = cc + self.get_compile_options(self.get_symbols(), includes)
139127

140-
base, _ = splitext(object)
141-
dep_path = base + '.d'
142-
cmd.extend(self.get_dep_opt(dep_path))
128+
cmd.extend(self.get_dep_option(object))
143129

144-
cmd.extend(self.cc_extra(base))
130+
cmd.extend(self.cc_extra(object))
145131

146132
cmd.extend(["-o", object, source])
147133

0 commit comments

Comments
 (0)