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