2018-04-18 02:57:45 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
2021-12-24 01:41:47 +00:00
|
|
|
# SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
2021-11-15 17:03:33 +00:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2018-04-18 02:57:45 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
import argparse
|
2021-01-26 02:49:01 +00:00
|
|
|
import errno
|
2019-06-27 10:30:42 +00:00
|
|
|
import json
|
2021-01-26 02:49:01 +00:00
|
|
|
import os
|
|
|
|
import subprocess
|
2018-11-27 01:45:21 +00:00
|
|
|
import sys
|
2018-11-29 00:46:54 +00:00
|
|
|
import tempfile
|
2021-01-26 02:49:01 +00:00
|
|
|
from io import StringIO
|
2018-04-18 02:57:45 +00:00
|
|
|
|
2021-12-24 01:43:06 +00:00
|
|
|
from ldgen.entity import EntityDB
|
2021-12-24 01:41:47 +00:00
|
|
|
from ldgen.fragments import parse_fragment_file
|
2021-12-24 01:43:06 +00:00
|
|
|
from ldgen.generation import Generation
|
|
|
|
from ldgen.ldgen_common import LdGenFailure
|
|
|
|
from ldgen.linker_script import LinkerScript
|
|
|
|
from ldgen.sdkconfig import SDKConfig
|
2019-03-19 06:35:47 +00:00
|
|
|
from pyparsing import ParseException, ParseFatalException
|
2018-04-18 02:57:45 +00:00
|
|
|
|
2019-09-10 07:58:52 +00:00
|
|
|
try:
|
|
|
|
import confgen
|
|
|
|
except Exception:
|
|
|
|
parent_dir_name = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
|
2021-01-26 02:49:01 +00:00
|
|
|
kconfig_new_dir = os.path.abspath(parent_dir_name + '/kconfig_new')
|
2019-09-10 07:58:52 +00:00
|
|
|
sys.path.insert(0, kconfig_new_dir)
|
|
|
|
import confgen
|
|
|
|
|
2018-12-04 12:46:48 +00:00
|
|
|
|
2019-06-27 10:30:42 +00:00
|
|
|
def _update_environment(args):
|
2021-01-26 02:49:01 +00:00
|
|
|
env = [(name, value) for (name,value) in (e.split('=',1) for e in args.env)]
|
2019-06-27 10:30:42 +00:00
|
|
|
for name, value in env:
|
2021-01-26 02:49:01 +00:00
|
|
|
value = ' '.join(value.split())
|
2019-06-27 10:30:42 +00:00
|
|
|
os.environ[name] = value
|
|
|
|
|
|
|
|
if args.env_file is not None:
|
|
|
|
env = json.load(args.env_file)
|
2019-09-10 07:58:52 +00:00
|
|
|
os.environ.update(confgen.dict_enc_for_env(env))
|
2019-06-27 10:30:42 +00:00
|
|
|
|
|
|
|
|
2018-04-18 02:57:45 +00:00
|
|
|
def main():
|
|
|
|
|
2021-01-26 02:49:01 +00:00
|
|
|
argparser = argparse.ArgumentParser(description='ESP-IDF linker script generator')
|
2018-04-18 02:57:45 +00:00
|
|
|
|
|
|
|
argparser.add_argument(
|
2021-01-26 02:49:01 +00:00
|
|
|
'--input', '-i',
|
|
|
|
help='Linker template file',
|
|
|
|
type=argparse.FileType('r'))
|
2018-04-18 02:57:45 +00:00
|
|
|
|
2020-10-09 17:06:33 +00:00
|
|
|
fragments_group = argparser.add_mutually_exclusive_group()
|
|
|
|
|
|
|
|
fragments_group.add_argument(
|
2021-01-26 02:49:01 +00:00
|
|
|
'--fragments', '-f',
|
|
|
|
type=argparse.FileType('r'),
|
|
|
|
help='Input fragment files',
|
2020-10-09 17:06:33 +00:00
|
|
|
nargs='+'
|
|
|
|
)
|
|
|
|
|
|
|
|
fragments_group.add_argument(
|
|
|
|
'--fragments-list',
|
|
|
|
help='Input fragment files as a semicolon-separated list',
|
|
|
|
type=str
|
|
|
|
)
|
2018-04-18 02:57:45 +00:00
|
|
|
|
|
|
|
argparser.add_argument(
|
2021-01-26 02:49:01 +00:00
|
|
|
'--libraries-file',
|
|
|
|
type=argparse.FileType('r'),
|
|
|
|
help='File that contains the list of libraries in the build')
|
2018-04-18 02:57:45 +00:00
|
|
|
|
|
|
|
argparser.add_argument(
|
2021-01-26 02:49:01 +00:00
|
|
|
'--output', '-o',
|
|
|
|
help='Output linker script',
|
2018-12-04 12:46:48 +00:00
|
|
|
type=str)
|
2018-04-18 02:57:45 +00:00
|
|
|
|
|
|
|
argparser.add_argument(
|
2021-01-26 02:49:01 +00:00
|
|
|
'--config', '-c',
|
|
|
|
help='Project configuration')
|
2018-04-18 02:57:45 +00:00
|
|
|
|
|
|
|
argparser.add_argument(
|
2021-01-26 02:49:01 +00:00
|
|
|
'--kconfig', '-k',
|
|
|
|
help='IDF Kconfig file')
|
2018-04-18 02:57:45 +00:00
|
|
|
|
2020-04-29 10:26:52 +00:00
|
|
|
argparser.add_argument(
|
2021-01-26 02:49:01 +00:00
|
|
|
'--check-mapping',
|
|
|
|
help='Perform a check if a mapping (archive, obj, symbol) exists',
|
2020-04-29 10:26:52 +00:00
|
|
|
action='store_true'
|
|
|
|
)
|
|
|
|
|
|
|
|
argparser.add_argument(
|
2021-01-26 02:49:01 +00:00
|
|
|
'--check-mapping-exceptions',
|
|
|
|
help='Mappings exempted from check',
|
|
|
|
type=argparse.FileType('r')
|
2020-04-29 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
2018-04-18 02:57:45 +00:00
|
|
|
argparser.add_argument(
|
2021-01-26 02:49:01 +00:00
|
|
|
'--env', '-e',
|
2018-04-18 02:57:45 +00:00
|
|
|
action='append', default=[],
|
|
|
|
help='Environment to set when evaluating the config file', metavar='NAME=VAL')
|
|
|
|
|
2019-06-27 10:30:42 +00: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 05:42:10 +00:00
|
|
|
argparser.add_argument(
|
2021-01-26 02:49:01 +00:00
|
|
|
'--objdump',
|
|
|
|
help='Path to toolchain objdump')
|
2019-04-26 05:42:10 +00:00
|
|
|
|
2018-04-18 02:57:45 +00:00
|
|
|
args = argparser.parse_args()
|
|
|
|
|
|
|
|
input_file = args.input
|
2019-04-26 05:42:10 +00:00
|
|
|
libraries_file = args.libraries_file
|
2018-04-18 02:57:45 +00:00
|
|
|
config_file = args.config
|
2018-11-29 00:46:54 +00:00
|
|
|
output_path = args.output
|
2018-04-18 02:57:45 +00:00
|
|
|
kconfig_file = args.kconfig
|
2019-04-26 05:42:10 +00:00
|
|
|
objdump = args.objdump
|
2018-11-29 00:46:54 +00:00
|
|
|
|
2020-10-09 17:06:33 +00:00
|
|
|
fragment_files = []
|
|
|
|
if args.fragments_list:
|
|
|
|
fragment_files = args.fragments_list.split(';')
|
|
|
|
elif args.fragments:
|
|
|
|
fragment_files = args.fragments
|
|
|
|
|
2020-04-29 10:26:52 +00: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-18 02:57:45 +00:00
|
|
|
try:
|
2021-01-27 08:00:19 +00:00
|
|
|
sections_infos = EntityDB()
|
2019-04-26 05:42:10 +00:00
|
|
|
for library in libraries_file:
|
|
|
|
library = library.strip()
|
|
|
|
if library:
|
2021-11-15 07:06:18 +00:00
|
|
|
new_env = os.environ.copy()
|
2021-11-15 17:03:33 +00:00
|
|
|
new_env['LC_ALL'] = 'C'
|
2021-11-15 07:06:18 +00:00
|
|
|
dump = StringIO(subprocess.check_output([objdump, '-h', library], env=new_env).decode())
|
2019-04-26 05:42:10 +00:00
|
|
|
dump.name = library
|
|
|
|
sections_infos.add_sections_info(dump)
|
2018-04-18 02:57:45 +00:00
|
|
|
|
2021-01-27 08:00:19 +00:00
|
|
|
generation_model = Generation(check_mapping, check_mapping_exceptions)
|
2018-04-18 02:57:45 +00:00
|
|
|
|
2019-06-27 10:30:42 +00:00
|
|
|
_update_environment(args) # assign args.env and args.env_file to os.environ
|
|
|
|
|
|
|
|
sdkconfig = SDKConfig(kconfig_file, config_file)
|
2019-03-19 06:35:47 +00:00
|
|
|
|
2018-04-18 02:57:45 +00:00
|
|
|
for fragment_file in fragment_files:
|
2019-03-19 06:35:47 +00:00
|
|
|
try:
|
2021-12-24 01:41:47 +00:00
|
|
|
fragment_file = parse_fragment_file(fragment_file, sdkconfig)
|
2019-03-19 06:35:47 +00:00
|
|
|
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.)
|
2021-12-24 01:41:47 +00:00
|
|
|
raise LdGenFailure('failed to parse %s\n%s' % (fragment_file, str(e)))
|
2018-04-18 02:57:45 +00:00
|
|
|
generation_model.add_fragments_from_file(fragment_file)
|
|
|
|
|
2021-02-03 10:19:09 +00:00
|
|
|
mapping_rules = generation_model.generate(sections_infos)
|
2018-04-18 02:57:45 +00:00
|
|
|
|
2021-01-27 08:00:19 +00:00
|
|
|
script_model = LinkerScript(input_file)
|
2019-03-19 06:35:47 +00:00
|
|
|
script_model.fill(mapping_rules)
|
2018-04-18 02:57:45 +00:00
|
|
|
|
2021-01-26 02:49:01 +00:00
|
|
|
with tempfile.TemporaryFile('w+') as output:
|
2018-11-29 00:46:54 +00:00
|
|
|
script_model.write(output)
|
|
|
|
output.seek(0)
|
2019-05-31 03:30:44 +00: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
|
|
|
|
|
2021-01-26 02:49:01 +00:00
|
|
|
with open(output_path, 'w') as f: # only create output file after generation has suceeded
|
2018-11-29 00:46:54 +00:00
|
|
|
f.write(output.read())
|
|
|
|
except LdGenFailure as e:
|
2021-01-26 02:49:01 +00:00
|
|
|
print('linker script generation failed for %s\nERROR: %s' % (input_file.name, e))
|
2018-11-27 01:45:21 +00:00
|
|
|
sys.exit(1)
|
2018-04-18 02:57:45 +00:00
|
|
|
|
2018-12-04 12:46:48 +00:00
|
|
|
|
2021-01-26 02:49:01 +00:00
|
|
|
if __name__ == '__main__':
|
2018-11-29 00:46:54 +00:00
|
|
|
main()
|