Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame^] | 1 | #------------------------------------------------------------------------------- |
| 2 | # Copyright (c) 2018, Arm Limited. All rights reserved. |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
| 8 | import os |
| 9 | from keyword_substitution import keyword_substitute, Verbosity, log_print |
| 10 | |
| 11 | try: |
| 12 | import yaml |
| 13 | except ImportError as e: |
| 14 | print e, "To install it, type:" |
| 15 | print "pip install PyYAML" |
| 16 | exit(1) |
| 17 | |
| 18 | VERBOSITY = Verbosity.warning |
| 19 | log_print(Verbosity.debug, "Setting verbosity to", VERBOSITY, verbosity=VERBOSITY) |
| 20 | |
| 21 | controlsymbol = "@!GENERATOR" |
| 22 | controlblockstart = "@!GENERATOR_BLOCK_START!@" |
| 23 | controlblockend = "@!GENERATOR_BLOCK_END!@" |
| 24 | controlconditionstart = "@!GENERATOR_CONDITIONAL_START!@" |
| 25 | controlconditionelse = "@!GENERATOR_CONDITIONAL_ELSE!@" |
| 26 | controlconditionend = "@!GENERATOR_CONDITIONAL_END!@" |
| 27 | control_print_iteration_counter = "@!GENERATOR_ITERATION_COUNTER!@" |
| 28 | control_print_donotedit_warning = "@!GENERATOR_DONOTEDIT_WARNING!@" |
| 29 | |
| 30 | donotedit_warning = "/*********** " + \ |
| 31 | "WARNING: This is an auto-generated file. Do not edit!" + \ |
| 32 | " ***********/\n" |
| 33 | |
| 34 | # All operations assume tf-m repo root as active working directory |
| 35 | |
| 36 | # Initialisation of globals |
| 37 | part_db_basename = 'tfm_partition_list.inc' |
| 38 | part_defs_basename = 'tfm_partition_defs.inc' |
| 39 | sfid_map_basename = 'tfm_sfid_list.inc' |
| 40 | |
| 41 | # Functions |
| 42 | def load_manifest_list(file): |
| 43 | db = [] |
| 44 | manifest_list = yaml.load(file) |
| 45 | for item in manifest_list["manifest_list"]: |
| 46 | manifest_path = os.path.join('tools', item['manifest']) |
| 47 | try: |
| 48 | file = open(manifest_path) |
| 49 | manifest = yaml.load(file) |
| 50 | db.append({"manifest": manifest, "attr": item}) |
| 51 | except IOError: |
| 52 | print "Manifest for "+item['name']+" cannot be opened at path "+item['manifest'] |
| 53 | return db |
| 54 | # def load_yaml_file |
| 55 | |
| 56 | def generate(db, path, basename): |
| 57 | outfile = \ |
| 58 | open(os.path.join(path, basename), "w") |
| 59 | with open(os.path.join(path, basename+'.template'), "r") as template_file: |
| 60 | template = template_file.readlines() |
| 61 | |
| 62 | output = [] |
| 63 | blocks = [] |
| 64 | blocklines = [] |
| 65 | MISSING_KEYS_ACTION = 'replace False' |
| 66 | inblock = False |
| 67 | linecnt = len(template) |
| 68 | for lineno, line in enumerate(template): |
| 69 | if controlblockstart in line: |
| 70 | inblock = True |
| 71 | log_print(Verbosity.info, "Blockstart:", str(lineno)) |
| 72 | blocklines = [] |
| 73 | elif controlblockend in line: |
| 74 | inblock = False |
| 75 | iteration_counter = 0 |
| 76 | log_print(Verbosity.info, "Blocklines:", str(blocklines)) |
| 77 | for manifest in db: |
| 78 | print_blocked = False |
| 79 | for line in blocklines: |
| 80 | outlist = keyword_substitute(manifest, line, MISSING_KEYS_ACTION) |
| 81 | outstring = "" |
| 82 | for outline in outlist: |
| 83 | outstring += ''.join(outline) |
| 84 | log_print(Verbosity.info, outstring) |
| 85 | if controlconditionstart in outstring: |
| 86 | if 'False' in outstring: |
| 87 | log_print(Verbosity.info, "PRINT BLOCKED") |
| 88 | print_blocked = True |
| 89 | elif controlconditionend in outstring: |
| 90 | log_print(Verbosity.info, "PRINT ENABLED") |
| 91 | print_blocked = False |
| 92 | elif controlconditionelse in outstring: |
| 93 | log_print(Verbosity.info, "PRINT " + str(print_blocked)) |
| 94 | print_blocked = not print_blocked |
| 95 | else: |
| 96 | if control_print_iteration_counter in outstring: |
| 97 | outstring = outstring.replace( |
| 98 | control_print_iteration_counter, |
| 99 | str(iteration_counter)) |
| 100 | elif controlsymbol in outstring: |
| 101 | print "Invalid control symbol:", outstring |
| 102 | print "exiting" |
| 103 | exit(1) |
| 104 | if not print_blocked: |
| 105 | outfile.write(outstring) |
| 106 | iteration_counter += 1 |
| 107 | # end for manifest in db |
| 108 | blocks.append(blocklines) |
| 109 | elif inblock: |
| 110 | # inside a generator block |
| 111 | # append line to blocklines to be processed by template generator |
| 112 | blocklines.append(line) |
| 113 | else: |
| 114 | # outside a generator block |
| 115 | if control_print_donotedit_warning in line: |
| 116 | # print do not edit warning - Note: ignore rest of input line |
| 117 | line = donotedit_warning |
| 118 | elif control_print_iteration_counter in line: |
| 119 | # line contains an iteration counter request, replace with value |
| 120 | line = line.replace(control_print_iteration_counter, str(iteration_counter)) |
| 121 | elif controlsymbol in line: |
| 122 | print "Invalid control symbol:", line |
| 123 | print "exiting" |
| 124 | exit(1) |
| 125 | outfile.write(line) |
| 126 | log_print(Verbosity.info, "Blocks:" + str(blocks)) |
| 127 | outfile.close() |
| 128 | # def generate() |
| 129 | |
| 130 | # main |
| 131 | def main(): |
| 132 | with open(os.path.join('tools', 'manifest_list.yaml')) \ |
| 133 | as manifest_list_yaml_file: |
| 134 | # Read manifest list file, build database |
| 135 | db = load_manifest_list(manifest_list_yaml_file) |
| 136 | |
| 137 | path = os.path.join('secure_fw', 'services') |
| 138 | generate(db, path, part_db_basename) |
| 139 | generate(db, path, sfid_map_basename) |
| 140 | generate(db, path, part_defs_basename) |
| 141 | |
| 142 | if __name__ == "__main__": |
| 143 | main() |