99Generate the TF-PSA-Crypto generated files
1010"""
1111import argparse
12+ import enum
1213import filecmp
14+ import os
1315import shutil
1416import subprocess
1517import sys
@@ -219,21 +221,39 @@ def check_generated_files(generation_scripts: List[GenerationScript], root: Path
219221 file .unlink ()
220222 bak_file .rename (file )
221223
224+
225+ class Action (enum .Enum ):
226+ CHECK = 0
227+ CLEAN = 1
228+ GENERATE = 2
229+ LIST = 3
230+
222231def main ():
223232 """
224233 Main function of this program
225234 """
226235 parser = argparse .ArgumentParser ()
227-
228- parser .add_argument ('--list' , action = 'store_true' ,
229- default = False , help = 'List generated files.' )
230236 parser .add_argument ('--root' , metavar = 'DIR' ,
231237 help = 'Root of the tree containing the generated files \
232- to check (default: Mbed TLS or TF-PSA-Cryto root.)' )
233- parser .add_argument ('--check' , action = 'store_true' ,
234- default = False , help = 'Check the generated files in root' )
238+ (default: Mbed TLS or TF-PSA-Cryto root.)' )
239+
240+ action = parser .add_mutually_exclusive_group ()
241+ action .add_argument ('--check' ,
242+ dest = 'action' , action = 'store_const' , const = Action .CHECK ,
243+ help = 'Check that the generated files are up to date.' )
244+ action .add_argument ('--clean' ,
245+ dest = 'action' , action = 'store_const' , const = Action .CLEAN ,
246+ help = 'Remove all generated files' )
247+ action .add_argument ('--generate' ,
248+ dest = 'action' , action = 'store_const' , const = Action .GENERATE ,
249+ help = 'Generate target-independent file (default mode)' )
250+ action .add_argument ('--list' ,
251+ dest = 'action' , action = 'store_const' , const = Action .LIST ,
252+ help = 'List generated files and exit' )
235253
236254 args = parser .parse_args ()
255+ if not args .action :
256+ args .action = Action .GENERATE
237257
238258 if not build_tree .looks_like_root ("." ):
239259 raise RuntimeError ("This script must be run from Mbed TLS or TF-PSA-Crypto root." )
@@ -245,13 +265,19 @@ def main():
245265 else :
246266 raise Exception ("No support for Mbed TLS 3.6" )
247267
248- if args .list :
268+ if args .action == Action . LIST :
249269 files = get_generated_files (generation_scripts )
250270 for file in files :
251271 print (str (file ))
252- elif args .check :
272+ elif args .action == Action .CLEAN :
273+ files = get_generated_files (generation_scripts )
274+ for file in files :
275+ if os .path .exists (file ):
276+ os .remove (file )
277+ elif args .action == Action .CHECK :
253278 check_generated_files (generation_scripts , Path (args .root or "." ))
254279 else :
280+ assert args .action == Action .GENERATE
255281 make_generated_files (generation_scripts )
256282
257283if __name__ == "__main__" :
0 commit comments