2018-04-17 22:57:45 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
|
|
|
import argparse
|
2019-06-27 06:30:42 -04:00
|
|
|
import json
|
2018-11-26 20:45:21 -05:00
|
|
|
import sys
|
2018-11-28 19:46:54 -05:00
|
|
|
import tempfile
|
2019-04-26 01:42:10 -04:00
|
|
|
import subprocess
|
2019-05-30 23:30:44 -04:00
|
|
|
import os
|
|
|
|
import errno
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
from fragments import FragmentFile
|
2018-04-17 22:57:45 -04:00
|
|
|
from sdkconfig import SDKConfig
|
|
|
|
from generation import GenerationModel, TemplateModel, SectionsInfo
|
2019-04-12 20:59:32 -04:00
|
|
|
from ldgen_common import LdGenFailure
|
2019-03-19 02:35:47 -04:00
|
|
|
from pyparsing import ParseException, ParseFatalException
|
2019-04-26 01:42:10 -04:00
|
|
|
from io import StringIO
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
|
2019-06-27 06:30:42 -04:00
|
|
|
def _update_environment(args):
|
|
|
|
env = [(name, value) for (name,value) in (e.split("=",1) for e in args.env)]
|
|
|
|
for name, value in env:
|
|
|
|
value = " ".join(value.split())
|
|
|
|
os.environ[name] = value
|
|
|
|
|
|
|
|
if args.env_file is not None:
|
|
|
|
env = json.load(args.env_file)
|
|
|
|
os.environ.update(env)
|
|
|
|
|
|
|
|
|
2018-04-17 22:57:45 -04:00
|
|
|
def main():
|
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
argparser = argparse.ArgumentParser(description="ESP-IDF linker script generator")
|
2018-04-17 22:57:45 -04:00
|
|
|
|
|
|
|
argparser.add_argument(
|
|
|
|
"--input", "-i",
|
2018-12-04 07:46:48 -05:00
|
|
|
help="Linker template file",
|
|
|
|
type=argparse.FileType("r"))
|
2018-04-17 22:57:45 -04:00
|
|
|
|
|
|
|
argparser.add_argument(
|
|
|
|
"--fragments", "-f",
|
2018-12-04 07:46:48 -05:00
|
|
|
type=argparse.FileType("r"),
|
|
|
|
help="Input fragment files",
|
|
|
|
nargs="+")
|
2018-04-17 22:57:45 -04:00
|
|
|
|
|
|
|
argparser.add_argument(
|
2019-04-26 01:42:10 -04:00
|
|
|
"--libraries-file",
|
2018-12-04 07:46:48 -05:00
|
|
|
type=argparse.FileType("r"),
|
2019-04-26 01:42:10 -04:00
|
|
|
help="File that contains the list of libraries in the build")
|
2018-04-17 22:57:45 -04:00
|
|
|
|
|
|
|
argparser.add_argument(
|
|
|
|
"--output", "-o",
|
2018-12-04 07:46:48 -05:00
|
|
|
help="Output linker script",
|
|
|
|
type=str)
|
2018-04-17 22:57:45 -04:00
|
|
|
|
|
|
|
argparser.add_argument(
|
|
|
|
"--config", "-c",
|
2019-03-19 02:35:47 -04:00
|
|
|
help="Project configuration")
|
2018-04-17 22:57:45 -04:00
|
|
|
|
|
|
|
argparser.add_argument(
|
|
|
|
"--kconfig", "-k",
|
2019-03-19 02:35:47 -04:00
|
|
|
help="IDF Kconfig file")
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2020-04-29 06:26:52 -04:00
|
|
|
argparser.add_argument(
|
|
|
|
"--check-mapping",
|
|
|
|
help="Perform a check if a mapping (archive, obj, symbol) exists",
|
|
|
|
action='store_true'
|
|
|
|
)
|
|
|
|
|
|
|
|
argparser.add_argument(
|
|
|
|
"--check-mapping-exceptions",
|
|
|
|
help="Mappings exempted from check",
|
|
|
|
type=argparse.FileType("r")
|
|
|
|
)
|
|
|
|
|
2018-04-17 22:57:45 -04:00
|
|
|
argparser.add_argument(
|
|
|
|
"--env", "-e",
|
|
|
|
action='append', default=[],
|
|
|
|
help='Environment to set when evaluating the config file', metavar='NAME=VAL')
|
|
|
|
|
2019-06-27 06:30:42 -04:00
|
|
|
argparser.add_argument('--env-file', type=argparse.FileType('r'),
|
|
|
|
help='Optional file to load environment variables from. Contents '
|
|
|
|
'should be a JSON object where each key/value pair is a variable.')
|
|
|
|
|
2019-04-26 01:42:10 -04:00
|
|
|
argparser.add_argument(
|
|
|
|
"--objdump",
|
|
|
|
help="Path to toolchain objdump")
|
|
|
|
|
2018-04-17 22:57:45 -04:00
|
|
|
args = argparser.parse_args()
|
|
|
|
|
|
|
|
input_file = args.input
|
|
|
|
fragment_files = [] if not args.fragments else args.fragments
|
2019-04-26 01:42:10 -04:00
|
|
|
libraries_file = args.libraries_file
|
2018-04-17 22:57:45 -04:00
|
|
|
config_file = args.config
|
2018-11-28 19:46:54 -05:00
|
|
|
output_path = args.output
|
2018-04-17 22:57:45 -04:00
|
|
|
kconfig_file = args.kconfig
|
2019-04-26 01:42:10 -04:00
|
|
|
objdump = args.objdump
|
2018-11-28 19:46:54 -05:00
|
|
|
|
2020-04-29 06:26:52 -04:00
|
|
|
check_mapping = args.check_mapping
|
|
|
|
if args.check_mapping_exceptions:
|
|
|
|
check_mapping_exceptions = [line.strip() for line in args.check_mapping_exceptions]
|
|
|
|
else:
|
|
|
|
check_mapping_exceptions = None
|
|
|
|
|
2018-04-17 22:57:45 -04:00
|
|
|
try:
|
|
|
|
sections_infos = SectionsInfo()
|
2019-04-26 01:42:10 -04:00
|
|
|
for library in libraries_file:
|
|
|
|
library = library.strip()
|
|
|
|
if library:
|
|
|
|
dump = StringIO(subprocess.check_output([objdump, "-h", library]).decode())
|
|
|
|
dump.name = library
|
|
|
|
sections_infos.add_sections_info(dump)
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2020-04-29 06:26:52 -04:00
|
|
|
generation_model = GenerationModel(check_mapping, check_mapping_exceptions)
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2019-06-27 06:30:42 -04:00
|
|
|
_update_environment(args) # assign args.env and args.env_file to os.environ
|
|
|
|
|
|
|
|
sdkconfig = SDKConfig(kconfig_file, config_file)
|
2019-03-19 02:35:47 -04:00
|
|
|
|
2018-04-17 22:57:45 -04:00
|
|
|
for fragment_file in fragment_files:
|
2019-03-19 02:35:47 -04:00
|
|
|
try:
|
|
|
|
fragment_file = FragmentFile(fragment_file, sdkconfig)
|
|
|
|
except (ParseException, ParseFatalException) as e:
|
|
|
|
# ParseException is raised on incorrect grammar
|
|
|
|
# ParseFatalException is raised on correct grammar, but inconsistent contents (ex. duplicate
|
|
|
|
# keys, key unsupported by fragment, unexpected number of values, etc.)
|
|
|
|
raise LdGenFailure("failed to parse %s\n%s" % (fragment_file.name, str(e)))
|
2018-04-17 22:57:45 -04:00
|
|
|
generation_model.add_fragments_from_file(fragment_file)
|
|
|
|
|
2019-03-19 02:35:47 -04:00
|
|
|
mapping_rules = generation_model.generate_rules(sections_infos)
|
2018-04-17 22:57:45 -04:00
|
|
|
|
|
|
|
script_model = TemplateModel(input_file)
|
2019-03-19 02:35:47 -04:00
|
|
|
script_model.fill(mapping_rules)
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2018-11-28 19:46:54 -05:00
|
|
|
with tempfile.TemporaryFile("w+") as output:
|
|
|
|
script_model.write(output)
|
|
|
|
output.seek(0)
|
2019-05-30 23:30:44 -04:00
|
|
|
|
|
|
|
if not os.path.exists(os.path.dirname(output_path)):
|
|
|
|
try:
|
|
|
|
os.makedirs(os.path.dirname(output_path))
|
|
|
|
except OSError as exc:
|
|
|
|
if exc.errno != errno.EEXIST:
|
|
|
|
raise
|
|
|
|
|
2018-11-28 19:46:54 -05:00
|
|
|
with open(output_path, "w") as f: # only create output file after generation has suceeded
|
|
|
|
f.write(output.read())
|
|
|
|
except LdGenFailure as e:
|
|
|
|
print("linker script generation failed for %s\nERROR: %s" % (input_file.name, e))
|
2018-11-26 20:45:21 -05:00
|
|
|
sys.exit(1)
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
|
2018-04-17 22:57:45 -04:00
|
|
|
if __name__ == "__main__":
|
2018-11-28 19:46:54 -05:00
|
|
|
main()
|