Commit | Line | Data |
---|---|---|
dcdbb941 PP |
1 | # The MIT License (MIT) |
2 | # | |
3 | # Copyright (c) 2014 Philippe Proulx <philippe.proulx@efficios.com> | |
4 | # | |
5 | # Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | # of this software and associated documentation files (the "Software"), to deal | |
7 | # in the Software without restriction, including without limitation the rights | |
8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | # copies of the Software, and to permit persons to whom the Software is | |
10 | # furnished to do so, subject to the following conditions: | |
11 | # | |
12 | # The above copyright notice and this permission notice shall be included in | |
13 | # all copies or substantial portions of the Software. | |
14 | # | |
15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
21 | # THE SOFTWARE. | |
22 | from termcolor import cprint, colored | |
23 | import argparse | |
24 | import sys | |
25 | import os | |
26 | import re | |
27 | ||
28 | ||
29 | def _perror(msg, exit_code=1): | |
30 | cprint('Error: {}'.format(msg), 'red', attrs=['bold'], file=sys.stderr) | |
31 | sys.exit(exit_code) | |
32 | ||
33 | ||
34 | def _pinfo(msg): | |
35 | cprint(':: {}'.format(msg), 'blue', attrs=['bold'], file=sys.stderr) | |
36 | ||
37 | ||
38 | def _parse_args(): | |
39 | ap = argparse.ArgumentParser() | |
40 | ||
41 | ap.add_argument('-O', '--output', metavar='OUTPUT', action='store', | |
42 | default=os.getcwd(), | |
43 | help='output directory of C files') | |
44 | ap.add_argument('-p', '--prefix', metavar='PREFIX', action='store', | |
45 | default='barectf', | |
46 | help='custom prefix for C function and structure names') | |
47 | ap.add_argument('-s', '--static-inline', action='store_true', | |
48 | help='generate static inline C functions') | |
49 | ap.add_argument('-c', '--manual-clock', action='store_true', | |
50 | help='do not use a clock callback: pass clock value to tracing functions') | |
51 | ap.add_argument('metadata', metavar='METADATA', action='store', | |
52 | help='CTF metadata input file') | |
53 | ||
54 | # parse args | |
55 | args = ap.parse_args() | |
56 | ||
57 | # validate output directory | |
58 | if not os.path.isdir(args.output): | |
59 | _perror('"{}" is not an existing directory'.format(args.output)) | |
60 | ||
61 | # validate prefix | |
62 | if not re.match(r'^[a-zA-Z_][a-zA-Z0-9_]*$', args.prefix): | |
63 | _perror('"{}" is not a valid C identifier'.format(args.prefix)) | |
64 | ||
65 | # validate that metadata file exists | |
66 | if not os.path.isfile(args.metadata): | |
67 | _perror('"{}" is not an existing file'.format(args.metadata)) | |
68 | ||
69 | return args | |
70 | ||
71 | ||
72 | def gen_barectf(output, prefix, static_inline, manual_clock): | |
73 | _pinfo(output) | |
74 | _pinfo(prefix) | |
75 | _pinfo(static_inline) | |
76 | _pinfo(manual_clock) | |
77 | ||
78 | ||
79 | def run(): | |
80 | args = _parse_args() | |
81 | gen_barectf(args.output, args.prefix, args.static_inline, | |
82 | args.manual_clock) |