2018-04-18 02:57:45 +00: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
|
2018-11-27 01:45:21 +00:00
|
|
|
import sys
|
2018-11-29 00:46:54 +00:00
|
|
|
import tempfile
|
2019-04-26 05:42:10 +00:00
|
|
|
import subprocess
|
2019-05-31 03:30:44 +00:00
|
|
|
import os
|
|
|
|
import errno
|
2018-04-18 02:57:45 +00:00
|
|
|
|
2019-03-19 06:35:47 +00:00
|
|
|
from fragments import FragmentFile
|
2018-04-18 02:57:45 +00:00
|
|
|
from sdkconfig import SDKConfig
|
|
|
|
from generation import GenerationModel, TemplateModel, SectionsInfo
|
2019-04-13 00:59:32 +00:00
|
|
|
from ldgen_common import LdGenFailure
|
2019-03-19 06:35:47 +00:00
|
|
|
from pyparsing import ParseException, ParseFatalException
|
2019-04-26 05:42:10 +00:00
|
|
|
from io import StringIO
|
2018-04-18 02:57:45 +00:00
|
|
|
|
2018-12-04 12:46:48 +00:00
|
|
|
|
2018-04-18 02:57:45 +00:00
|
|
|
def main():
|
|
|
|
|
2018-12-04 12:46:48 +00:00
|
|
|
argparser = argparse.ArgumentParser(description="ESP-IDF linker script generator")
|
2018-04-18 02:57:45 +00:00
|
|
|
|
|
|
|
argparser.add_argument(
|
|
|
|
"--input", "-i",
|
2018-12-04 12:46:48 +00:00
|
|
|
help="Linker template file",
|
|
|
|
type=argparse.FileType("r"))
|
2018-04-18 02:57:45 +00:00
|
|
|
|
|
|
|
argparser.add_argument(
|
|
|
|
"--fragments", "-f",
|
2018-12-04 12:46:48 +00:00
|
|
|
type=argparse.FileType("r"),
|
|
|
|
help="Input fragment files",
|
|
|
|
nargs="+")
|
2018-04-18 02:57:45 +00:00
|
|
|
|
|
|
|
argparser.add_argument(
|
2019-04-26 05:42:10 +00:00
|
|
|
"--libraries-file",
|
2018-12-04 12:46:48 +00:00
|
|
|
type=argparse.FileType("r"),
|
2019-04-26 05:42:10 +00:00
|
|
|
help="File that contains the list of libraries in the build")
|
2018-04-18 02:57:45 +00:00
|
|
|
|
|
|
|
argparser.add_argument(
|
|
|
|
"--output", "-o",
|
2018-12-04 12:46:48 +00:00
|
|
|
help="Output linker script",
|
|
|
|
type=str)
|
2018-04-18 02:57:45 +00:00
|
|
|
|
|
|
|
argparser.add_argument(
|
|
|
|
"--config", "-c",
|
2019-03-19 06:35:47 +00:00
|
|
|
help="Project configuration")
|
2018-04-18 02:57:45 +00:00
|
|
|
|
|
|
|
argparser.add_argument(
|
|
|
|
"--kconfig", "-k",
|
2019-03-19 06:35:47 +00:00
|
|
|
help="IDF Kconfig file")
|
2018-04-18 02:57:45 +00:00
|
|
|
|
|
|
|
argparser.add_argument(
|
|
|
|
"--env", "-e",
|
|
|
|
action='append', default=[],
|
|
|
|
help='Environment to set when evaluating the config file', metavar='NAME=VAL')
|
|
|
|
|
2019-04-26 05:42:10 +00:00
|
|
|
argparser.add_argument(
|
|
|
|
"--objdump",
|
|
|
|
help="Path to toolchain objdump")
|
|
|
|
|
2018-04-18 02:57:45 +00:00
|
|
|
args = argparser.parse_args()
|
|
|
|
|
|
|
|
input_file = args.input
|
|
|
|
fragment_files = [] if not args.fragments else args.fragments
|
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
|
|
|
|
2018-04-18 02:57:45 +00:00
|
|
|
try:
|
|
|
|
sections_infos = SectionsInfo()
|
2019-04-26 05:42:10 +00: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-18 02:57:45 +00:00
|
|
|
|
|
|
|
generation_model = GenerationModel()
|
|
|
|
|
2019-03-19 06:35:47 +00:00
|
|
|
sdkconfig = SDKConfig(kconfig_file, config_file, args.env)
|
|
|
|
|
2018-04-18 02:57:45 +00:00
|
|
|
for fragment_file in fragment_files:
|
2019-03-19 06:35:47 +00: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-18 02:57:45 +00:00
|
|
|
generation_model.add_fragments_from_file(fragment_file)
|
|
|
|
|
2019-03-19 06:35:47 +00:00
|
|
|
mapping_rules = generation_model.generate_rules(sections_infos)
|
2018-04-18 02:57:45 +00:00
|
|
|
|
|
|
|
script_model = TemplateModel(input_file)
|
2019-03-19 06:35:47 +00:00
|
|
|
script_model.fill(mapping_rules)
|
2018-04-18 02:57:45 +00:00
|
|
|
|
2018-11-29 00:46:54 +00:00
|
|
|
with tempfile.TemporaryFile("w+") as output:
|
|
|
|
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
|
|
|
|
|
2018-11-29 00:46:54 +00: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-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
|
|
|
|
2018-04-18 02:57:45 +00:00
|
|
|
if __name__ == "__main__":
|
2018-11-29 00:46:54 +00:00
|
|
|
main()
|