025dfa9b7b553232c1925aeec9eddf206aa09257
[deliverable/barectf.git] / barectf / codegen.py
1 # The MIT License (MIT)
2 #
3 # Copyright (c) 2014-2020 Philippe Proulx <pproulx@efficios.com>
4 #
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:
12 #
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
15 #
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.
23
24 import barectf.tsdl182gen as barectf_tsdl182gen
25 import barectf.template as barectf_template
26 import barectf.config as barectf_config
27 import barectf.cgen as barectf_cgen
28 from typing import List, Optional
29
30
31 # A file generated by a `CodeGenerator` object.
32 #
33 # A generated file has a name (influenced by the configuration's
34 # file name prefix option) and contents.
35 class _GeneratedFile:
36 def __init__(self, name: str, contents: str):
37 self._name = name
38 self._contents = contents
39
40 @property
41 def name(self) -> str:
42 return self._name
43
44 @property
45 def contents(self) -> str:
46 return self._contents
47
48
49 # A barectf code generator.
50 #
51 # Build a code generator with a barectf configuration.
52 #
53 # A code generator can generate the TSDL `metadata` file and C source
54 # and header files.
55 class CodeGenerator:
56 def __init__(self, configuration: barectf_config.Configuration):
57 self._config = configuration
58 self._file_name_prefix = configuration.options.code_generation_options.file_name_prefix
59 self._c_code_gen = barectf_cgen._CodeGen(configuration)
60 self._c_headers: Optional[List[_GeneratedFile]] = None
61 self._c_sources: Optional[List[_GeneratedFile]] = None
62 self._metadata_stream: Optional[_GeneratedFile] = None
63
64 @property
65 def _barectf_header_name(self) -> str:
66 return f'{self._file_name_prefix}.h'
67
68 @property
69 def _bitfield_header_name(self) -> str:
70 return f'{self._file_name_prefix}-bitfield.h'
71
72 def generate_c_headers(self) -> List[_GeneratedFile]:
73 if self._c_headers is None:
74 self._c_headers = [
75 _GeneratedFile(self._barectf_header_name, self._c_code_gen.gen_header()),
76 _GeneratedFile(self._bitfield_header_name, self._c_code_gen.gen_bitfield_header()),
77 ]
78
79 return self._c_headers
80
81 def generate_c_sources(self) -> List[_GeneratedFile]:
82 if self._c_sources is None:
83 self._c_sources = [
84 _GeneratedFile(f'{self._file_name_prefix}.c',
85 self._c_code_gen.gen_src(self._barectf_header_name,
86 self._bitfield_header_name))
87 ]
88
89 return self._c_sources
90
91 def generate_metadata_stream(self) -> _GeneratedFile:
92 if self._metadata_stream is None:
93 self._metadata_stream = _GeneratedFile('metadata',
94 barectf_tsdl182gen._from_config(self._config))
95
96 return self._metadata_stream
This page took 0.030276 seconds and 3 git commands to generate.