Skip to content

tools: fix the path generated to the sct file #9966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions tools/export/makefile/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
mbed SDK
Copyright (c) 2011-2016 ARM Limited
Copyright (c) 2011-2019 ARM Limited

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -272,11 +272,14 @@ def generate(self):
if self.resources.linker_script:
sct_file = self.resources.get_file_refs(FileType.LD_SCRIPT)[-1]
new_script = self.toolchain.correct_scatter_shebang(
sct_file.path, join("..", dirname(sct_file.name)))
sct_file, join("..", dirname(sct_file.name))
)
if new_script is not sct_file:
self.resources.add_files_to_type(
FileType.LD_SCRIPT, [new_script])
self.generated_files.append(new_script)
self.resources.add_file_ref(
FileType.LD_SCRIPT,
new_script.name,
new_script.path
)
return super(Arm, self).generate()

class Armc5(Arm):
Expand Down
14 changes: 8 additions & 6 deletions tools/export/uvision/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,14 @@ def generate(self):
self.resources.inc_dirs),
'device': DeviceUvision(self.target),
}
sct_name, sct_path = self.resources.get_file_refs(
FileType.LD_SCRIPT)[0]
ctx['linker_script'] = self.toolchain.correct_scatter_shebang(
sct_path, dirname(sct_name))
if ctx['linker_script'] != sct_path:
self.generated_files.append(ctx['linker_script'])
sct_file_ref = self.resources.get_file_refs(FileType.LD_SCRIPT)[0]
sct_file_ref = self.toolchain.correct_scatter_shebang(
sct_file_ref, dirname(sct_file_ref.name)
)
self.resources.add_file_ref(
FileType.LD_SCRIPT, sct_file_ref.name, sct_file_ref.path
)
ctx['linker_script'] = sct_file_ref.name
fpu_included_core_name = ctx['device'].core.replace("-NS", "")
ctx['cputype'] = fpu_included_core_name.rstrip("FDE")
if fpu_included_core_name.endswith("FD"):
Expand Down
4 changes: 4 additions & 0 deletions tools/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ def add_file_ref(self, file_type, file_name, file_path):
if file_type:
if sep != self._sep:
file_name = file_name.replace(sep, self._sep)
# Mbed OS projects only use one linker script at a time, so remove
# any existing linker script when adding a new one
if file_type == FileType.LD_SCRIPT:
self._file_refs[file_type].clear()
self._file_refs[file_type].add(FileRef(file_name, file_path))

def _include_file(self, ref):
Expand Down
14 changes: 14 additions & 0 deletions tools/test/resources/resource_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ def test_filter_by_bm_lib(self):
exc_names = [dirname(name) or "." for name, _ in excluded_libs]
assert(all(e in res.ignored_dirs for e in exc_names))

def test_only_one_linker_script(self):
"""
Verify that when multiple linker scripts are added to a resource object,
only the last one added is used.
"""
resources = Resources(MockNotifier())
linker_scripts = ["first_linker_script.sct", "second_linker_script.sct"]
for linker_script in linker_scripts:
resources.add_file_ref(FileType.LD_SCRIPT, linker_script, linker_script)

assert(len(resources.get_file_refs(FileType.LD_SCRIPT)) == 1)
assert(resources.get_file_refs(FileType.LD_SCRIPT)[-1].name == linker_scripts[-1])
assert(resources.get_file_refs(FileType.LD_SCRIPT)[-1].path == linker_scripts[-1])


if __name__ == '__main__':
unittest.main()
28 changes: 15 additions & 13 deletions tools/toolchains/arm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
mbed SDK
Copyright (c) 2011-2013 ARM Limited
Copyright (c) 2011-2019 ARM Limited

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -19,7 +19,7 @@

import re
from copy import copy
from os.path import join, dirname, splitext, basename, exists, isfile
from os.path import join, dirname, splitext, basename, exists, isfile, relpath
from os import makedirs, write, remove
from tempfile import mkstemp
from shutil import rmtree
Expand All @@ -28,6 +28,7 @@
from tools.targets import CORE_ARCH
from tools.toolchains.mbed_toolchain import mbedToolchain, TOOLCHAIN_PATHS
from tools.utils import mkdir, NotSupportedException, run_cmd
from tools.resources import FileRef

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

def correct_scatter_shebang(self, scatter_file, cur_dir_name=None):
def correct_scatter_shebang(self, sc_fileref, cur_dir_name=None):
"""Correct the shebang at the top of a scatter file.

Positional arguments:
scatter_file -- the scatter file to correct
sc_fileref -- FileRef object of the scatter file

Keyword arguments:
cur_dir_name -- the name (not path) of the directory containing the
scatter file

Return:
The location of the correct scatter file
The FileRef of the correct scatter file

Side Effects:
This method MAY write a new scatter file to disk
"""
with open(scatter_file, "r") as input:
with open(sc_fileref.path, "r") as input:
lines = input.readlines()
if (lines[0].startswith(self.SHEBANG) or
not lines[0].startswith("#!")):
return scatter_file
not lines[0].startswith("#!")):
return sc_fileref
else:
new_scatter = join(self.build_dir, ".link_script.sct")
if cur_dir_name is None:
cur_dir_name = dirname(scatter_file)
cur_dir_name = dirname(sc_fileref.path)
self.SHEBANG += " -I %s" % cur_dir_name
if self.need_update(new_scatter, [scatter_file]):
if self.need_update(new_scatter, [sc_fileref.path]):
with open(new_scatter, "w") as out:
out.write(self.SHEBANG)
out.write("\n")
out.write("".join(lines[1:]))

return new_scatter
return FileRef(".link_script.sct", new_scatter)

def get_link_command(
self,
Expand All @@ -322,8 +323,9 @@ def get_link_command(
if lib_dirs:
args.extend(["--userlibpath", ",".join(lib_dirs)])
if scatter_file:
new_scatter = self.correct_scatter_shebang(scatter_file)
args.extend(["--scatter", new_scatter])
scatter_name = relpath(scatter_file)
new_scatter = self.correct_scatter_shebang(FileRef(scatter_name, scatter_file))
args.extend(["--scatter", new_scatter.path])

cmd = self.ld + args

Expand Down