Skip to content

Commit 803f5fd

Browse files
author
Cruz Monrreal
authored
Merge pull request #9966 from naveenkaje/sct_fix
tools: fix the path generated to the sct file
2 parents 6dbc00d + 80b0a4c commit 803f5fd

File tree

5 files changed

+49
-24
lines changed

5 files changed

+49
-24
lines changed

tools/export/makefile/__init__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
mbed SDK
3-
Copyright (c) 2011-2016 ARM Limited
3+
Copyright (c) 2011-2019 ARM Limited
44
55
Licensed under the Apache License, Version 2.0 (the "License");
66
you may not use this file except in compliance with the License.
@@ -269,11 +269,14 @@ def generate(self):
269269
if self.resources.linker_script:
270270
sct_file = self.resources.get_file_refs(FileType.LD_SCRIPT)[-1]
271271
new_script = self.toolchain.correct_scatter_shebang(
272-
sct_file.path, join("..", dirname(sct_file.name)))
272+
sct_file, join("..", dirname(sct_file.name))
273+
)
273274
if new_script is not sct_file:
274-
self.resources.add_files_to_type(
275-
FileType.LD_SCRIPT, [new_script])
276-
self.generated_files.append(new_script)
275+
self.resources.add_file_ref(
276+
FileType.LD_SCRIPT,
277+
new_script.name,
278+
new_script.path
279+
)
277280
return super(Arm, self).generate()
278281

279282
class Armc5(Arm):

tools/export/uvision/__init__.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,14 @@ def generate(self):
244244
self.resources.inc_dirs),
245245
'device': DeviceUvision(self.target),
246246
}
247-
sct_name, sct_path = self.resources.get_file_refs(
248-
FileType.LD_SCRIPT)[0]
249-
ctx['linker_script'] = self.toolchain.correct_scatter_shebang(
250-
sct_path, dirname(sct_name))
251-
if ctx['linker_script'] != sct_path:
252-
self.generated_files.append(ctx['linker_script'])
247+
sct_file_ref = self.resources.get_file_refs(FileType.LD_SCRIPT)[0]
248+
sct_file_ref = self.toolchain.correct_scatter_shebang(
249+
sct_file_ref, dirname(sct_file_ref.name)
250+
)
251+
self.resources.add_file_ref(
252+
FileType.LD_SCRIPT, sct_file_ref.name, sct_file_ref.path
253+
)
254+
ctx['linker_script'] = sct_file_ref.name
253255
fpu_included_core_name = ctx['device'].core.replace("-NS", "")
254256
ctx['cputype'] = fpu_included_core_name.rstrip("FDE")
255257
if fpu_included_core_name.endswith("FD"):

tools/resources/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ def add_file_ref(self, file_type, file_name, file_path):
269269
if file_type:
270270
if sep != self._sep:
271271
file_name = file_name.replace(sep, self._sep)
272+
# Mbed OS projects only use one linker script at a time, so remove
273+
# any existing linker script when adding a new one
274+
if file_type == FileType.LD_SCRIPT:
275+
self._file_refs[file_type].clear()
272276
self._file_refs[file_type].add(FileRef(file_name, file_path))
273277

274278
def _include_file(self, ref):

tools/test/resources/resource_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,20 @@ def test_filter_by_bm_lib(self):
146146
exc_names = [dirname(name) or "." for name, _ in excluded_libs]
147147
assert(all(e in res.ignored_dirs for e in exc_names))
148148

149+
def test_only_one_linker_script(self):
150+
"""
151+
Verify that when multiple linker scripts are added to a resource object,
152+
only the last one added is used.
153+
"""
154+
resources = Resources(MockNotifier())
155+
linker_scripts = ["first_linker_script.sct", "second_linker_script.sct"]
156+
for linker_script in linker_scripts:
157+
resources.add_file_ref(FileType.LD_SCRIPT, linker_script, linker_script)
158+
159+
assert(len(resources.get_file_refs(FileType.LD_SCRIPT)) == 1)
160+
assert(resources.get_file_refs(FileType.LD_SCRIPT)[-1].name == linker_scripts[-1])
161+
assert(resources.get_file_refs(FileType.LD_SCRIPT)[-1].path == linker_scripts[-1])
162+
149163

150164
if __name__ == '__main__':
151165
unittest.main()

tools/toolchains/arm.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
mbed SDK
3-
Copyright (c) 2011-2013 ARM Limited
3+
Copyright (c) 2011-2019 ARM Limited
44
55
Licensed under the Apache License, Version 2.0 (the "License");
66
you may not use this file except in compliance with the License.
@@ -19,7 +19,7 @@
1919

2020
import re
2121
from copy import copy
22-
from os.path import join, dirname, splitext, basename, exists, isfile
22+
from os.path import join, dirname, splitext, basename, exists, isfile, relpath
2323
from os import makedirs, write, remove
2424
from tempfile import mkstemp
2525
from shutil import rmtree
@@ -28,6 +28,7 @@
2828
from tools.targets import CORE_ARCH
2929
from tools.toolchains.mbed_toolchain import mbedToolchain, TOOLCHAIN_PATHS
3030
from tools.utils import mkdir, NotSupportedException, run_cmd
31+
from tools.resources import FileRef
3132

3233
ARMC5_MIGRATION_WARNING = (
3334
"Warning: We noticed that you are using Arm Compiler 5. "
@@ -272,39 +273,39 @@ def compile_c(self, source, object, includes):
272273
def compile_cpp(self, source, object, includes):
273274
return self.compile(self.cppc, source, object, includes)
274275

275-
def correct_scatter_shebang(self, scatter_file, cur_dir_name=None):
276+
def correct_scatter_shebang(self, sc_fileref, cur_dir_name=None):
276277
"""Correct the shebang at the top of a scatter file.
277278
278279
Positional arguments:
279-
scatter_file -- the scatter file to correct
280+
sc_fileref -- FileRef object of the scatter file
280281
281282
Keyword arguments:
282283
cur_dir_name -- the name (not path) of the directory containing the
283284
scatter file
284285
285286
Return:
286-
The location of the correct scatter file
287+
The FileRef of the correct scatter file
287288
288289
Side Effects:
289290
This method MAY write a new scatter file to disk
290291
"""
291-
with open(scatter_file, "r") as input:
292+
with open(sc_fileref.path, "r") as input:
292293
lines = input.readlines()
293294
if (lines[0].startswith(self.SHEBANG) or
294-
not lines[0].startswith("#!")):
295-
return scatter_file
295+
not lines[0].startswith("#!")):
296+
return sc_fileref
296297
else:
297298
new_scatter = join(self.build_dir, ".link_script.sct")
298299
if cur_dir_name is None:
299-
cur_dir_name = dirname(scatter_file)
300+
cur_dir_name = dirname(sc_fileref.path)
300301
self.SHEBANG += " -I %s" % cur_dir_name
301-
if self.need_update(new_scatter, [scatter_file]):
302+
if self.need_update(new_scatter, [sc_fileref.path]):
302303
with open(new_scatter, "w") as out:
303304
out.write(self.SHEBANG)
304305
out.write("\n")
305306
out.write("".join(lines[1:]))
306307

307-
return new_scatter
308+
return FileRef(".link_script.sct", new_scatter)
308309

309310
def get_link_command(
310311
self,
@@ -322,8 +323,9 @@ def get_link_command(
322323
if lib_dirs:
323324
args.extend(["--userlibpath", ",".join(lib_dirs)])
324325
if scatter_file:
325-
new_scatter = self.correct_scatter_shebang(scatter_file)
326-
args.extend(["--scatter", new_scatter])
326+
scatter_name = relpath(scatter_file)
327+
new_scatter = self.correct_scatter_shebang(FileRef(scatter_name, scatter_file))
328+
args.extend(["--scatter", new_scatter.path])
327329

328330
cmd = self.ld + args
329331

0 commit comments

Comments
 (0)