1 # The MIT License (MIT)
3 # Copyright (c) 2014-2020 Philippe Proulx <pproulx@efficios.com>
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 from pkg_resources
import resource_filename
25 from termcolor
import cprint
26 import barectf
.tsdl182gen
37 cprint('Error: ', 'red', end
='', file=sys
.stderr
)
38 cprint(msg
, 'red', attrs
=['bold'], file=sys
.stderr
)
42 def _pconfig_error(exc
):
43 cprint('Error:', 'red', file=sys
.stderr
)
45 for ctx
in reversed(exc
.ctx
):
46 if ctx
.msg
is not None:
51 cprint(f
' {ctx.name}:{msg}', 'red', attrs
=['bold'], file=sys
.stderr
)
57 cprint(msg
, 'green', attrs
=['bold'])
61 ap
= argparse
.ArgumentParser()
63 ap
.add_argument('-c', '--code-dir', metavar
='DIR', action
='store',
65 help='output directory of C source file')
66 ap
.add_argument('--dump-config', action
='store_true',
67 help='also dump the effective YAML configuration file used for generation')
68 ap
.add_argument('-H', '--headers-dir', metavar
='DIR', action
='store',
70 help='output directory of C header files')
71 ap
.add_argument('-I', '--include-dir', metavar
='DIR', action
='append',
73 help='add directory DIR to the list of directories to be searched for include files')
74 ap
.add_argument('--ignore-include-not-found', action
='store_true',
75 help='continue to process the configuration file when included files are not found')
76 ap
.add_argument('-m', '--metadata-dir', metavar
='DIR', action
='store',
78 help='output directory of CTF metadata')
79 ap
.add_argument('-p', '--prefix', metavar
='PREFIX', action
='store',
80 help='override configuration\'s prefix')
81 ap
.add_argument('-V', '--version', action
='version',
82 version
='%(prog)s {}'.format(barectf
.__version
__))
83 ap
.add_argument('config', metavar
='CONFIG', action
='store',
84 help='barectf YAML configuration file')
87 args
= ap
.parse_args()
89 # validate output directories
90 for d
in [args
.code_dir
, args
.headers_dir
, args
.metadata_dir
] + args
.include_dir
:
91 if not os
.path
.isdir(d
):
92 _perror(f
'`{d}` is not an existing directory')
94 # validate that configuration file exists
95 if not os
.path
.isfile(args
.config
):
96 _perror(f
'`{args.config}` is not an existing, regular file')
98 # append current working directory and provided include directory
99 args
.include_dir
+= [os
.getcwd(), resource_filename(__name__
, 'include')]
104 def _write_file(d
, name
, content
):
105 with
open(os
.path
.join(d
, name
), 'w') as f
:
113 # create configuration
115 config
= barectf
.config
.from_file(args
.config
, args
.include_dir
,
116 args
.ignore_include_not_found
,
118 except barectf
.config
._ConfigParseError
as exc
:
120 except Exception as exc
:
123 traceback
.print_exc()
124 _perror(f
'Unknown exception: {exc}')
126 # replace prefix if needed
128 config
= barectf
.config
.Config(config
.metadata
, args
.prefix
,
132 metadata
= barectf
.tsdl182gen
.from_metadata(config
.metadata
)
135 _write_file(args
.metadata_dir
, 'metadata', metadata
)
136 except Exception as exc
:
137 _perror(f
'Cannot write metadata file: {exc}')
140 generator
= barectf
.gen
.CCodeGenerator(config
)
143 header
= generator
.generate_header()
144 bitfield_header
= generator
.generate_bitfield_header()
147 _write_file(args
.headers_dir
, generator
.get_header_filename(), header
)
148 _write_file(args
.headers_dir
, generator
.get_bitfield_header_filename(),
150 except Exception as exc
:
151 _perror(f
'Cannot write header files: {exc}')
154 c_src
= generator
.generate_c_src()
157 _write_file(args
.code_dir
, '{}.c'.format(config
.prefix
.rstrip('_')),
159 except Exception as exc
:
160 _perror(f
'Cannot write C source file: {exc}')
This page took 0.04744 seconds and 5 git commands to generate.