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
|
2018-11-26 20:45:21 -05:00
|
|
|
import sys
|
2018-11-28 19:46:54 -05:00
|
|
|
import tempfile
|
2018-04-17 22:57:45 -04:00
|
|
|
|
|
|
|
from fragments import FragmentFileModel
|
|
|
|
from sdkconfig import SDKConfig
|
|
|
|
from generation import GenerationModel, TemplateModel, SectionsInfo
|
2018-11-28 19:46:54 -05:00
|
|
|
from common import LdGenFailure
|
2018-04-17 22:57:45 -04:00
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
|
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(
|
|
|
|
"--sections", "-s",
|
2018-12-04 07:46:48 -05:00
|
|
|
type=argparse.FileType("r"),
|
|
|
|
help="Library sections info")
|
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",
|
2018-12-04 07:46:48 -05:00
|
|
|
help="Project configuration",
|
|
|
|
type=argparse.FileType("r"))
|
2018-04-17 22:57:45 -04:00
|
|
|
|
|
|
|
argparser.add_argument(
|
|
|
|
"--kconfig", "-k",
|
2018-12-04 07:46:48 -05:00
|
|
|
help="IDF Kconfig file",
|
|
|
|
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')
|
|
|
|
|
|
|
|
args = argparser.parse_args()
|
|
|
|
|
|
|
|
input_file = args.input
|
|
|
|
fragment_files = [] if not args.fragments else args.fragments
|
|
|
|
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
|
2018-12-05 02:29:28 -05:00
|
|
|
sections = args.sections
|
2018-11-28 19:46:54 -05:00
|
|
|
|
2018-04-17 22:57:45 -04:00
|
|
|
try:
|
|
|
|
sections_infos = SectionsInfo()
|
|
|
|
|
2018-12-05 02:29:28 -05:00
|
|
|
if sections:
|
|
|
|
section_info_contents = [s.strip() for s in sections.read().split("\n")]
|
|
|
|
section_info_contents = [s for s in section_info_contents if s]
|
|
|
|
else:
|
|
|
|
section_info_contents = []
|
2018-12-01 09:22:45 -05:00
|
|
|
|
|
|
|
for sections_info_file in section_info_contents:
|
|
|
|
with open(sections_info_file) as sections_info_file_obj:
|
|
|
|
sections_infos.add_sections_info(sections_info_file_obj)
|
2018-04-17 22:57:45 -04:00
|
|
|
|
|
|
|
generation_model = GenerationModel()
|
|
|
|
|
|
|
|
for fragment_file in fragment_files:
|
|
|
|
fragment_file = FragmentFileModel(fragment_file)
|
|
|
|
generation_model.add_fragments_from_file(fragment_file)
|
|
|
|
|
|
|
|
sdkconfig = SDKConfig(kconfig_file, config_file, args.env)
|
|
|
|
mapping_rules = generation_model.generate_rules(sdkconfig, sections_infos)
|
|
|
|
|
|
|
|
script_model = TemplateModel(input_file)
|
|
|
|
script_model.fill(mapping_rules, sdkconfig)
|
|
|
|
|
2018-11-28 19:46:54 -05:00
|
|
|
with tempfile.TemporaryFile("w+") as output:
|
|
|
|
script_model.write(output)
|
|
|
|
output.seek(0)
|
|
|
|
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()
|