025dfa9b7b553232c1925aeec9eddf206aa09257
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 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
31 # A file generated by a `CodeGenerator` object.
33 # A generated file has a name (influenced by the configuration's
34 # file name prefix option) and contents.
36 def __init__(self
, name
: str, contents
: str):
38 self
._contents
= contents
41 def name(self
) -> str:
45 def contents(self
) -> str:
49 # A barectf code generator.
51 # Build a code generator with a barectf configuration.
53 # A code generator can generate the TSDL `metadata` file and C source
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
65 def _barectf_header_name(self
) -> str:
66 return f
'{self._file_name_prefix}.h'
69 def _bitfield_header_name(self
) -> str:
70 return f
'{self._file_name_prefix}-bitfield.h'
72 def generate_c_headers(self
) -> List
[_GeneratedFile
]:
73 if self
._c
_headers
is None:
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()),
79 return self
._c
_headers
81 def generate_c_sources(self
) -> List
[_GeneratedFile
]:
82 if self
._c
_sources
is None:
84 _GeneratedFile(f
'{self._file_name_prefix}.c',
85 self
._c
_code
_gen
.gen_src(self
._barectf
_header
_name
,
86 self
._bitfield
_header
_name
))
89 return self
._c
_sources
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
))
96 return self
._metadata
_stream
This page took 0.030276 seconds and 3 git commands to generate.