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
.config
as barectf_config
26 import barectf
.cgen
as barectf_cgen
27 from typing
import List
, Optional
30 # A file generated by a `CodeGenerator` object.
32 # A generated file has a name (influenced by the configuration's
33 # file name prefix option) and contents.
35 def __init__(self
, name
: str, contents
: str):
37 self
._contents
= contents
40 def name(self
) -> str:
44 def contents(self
) -> str:
48 # A barectf code generator.
50 # Build a code generator with a barectf configuration.
52 # A code generator can generate the TSDL `metadata` file and C source
55 def __init__(self
, configuration
: barectf_config
.Configuration
):
56 self
._config
= configuration
57 self
._file
_name
_prefix
= configuration
.options
.code_generation_options
.file_name_prefix
58 self
._c
_code
_gen
= barectf_cgen
._CodeGen
(configuration
)
59 self
._c
_headers
: Optional
[List
[_GeneratedFile
]] = None
60 self
._c
_sources
: Optional
[List
[_GeneratedFile
]] = None
61 self
._metadata
_stream
: Optional
[_GeneratedFile
] = None
64 def _barectf_header_name(self
) -> str:
65 return f
'{self._file_name_prefix}.h'
68 def _bitfield_header_name(self
) -> str:
69 return f
'{self._file_name_prefix}-bitfield.h'
71 def generate_c_headers(self
) -> List
[_GeneratedFile
]:
72 if self
._c
_headers
is None:
74 _GeneratedFile(self
._barectf
_header
_name
, self
._c
_code
_gen
.gen_header()),
75 _GeneratedFile(self
._bitfield
_header
_name
, self
._c
_code
_gen
.gen_bitfield_header()),
78 return self
._c
_headers
80 def generate_c_sources(self
) -> List
[_GeneratedFile
]:
81 if self
._c
_sources
is None:
83 _GeneratedFile(f
'{self._file_name_prefix}.c',
84 self
._c
_code
_gen
.gen_src(self
._barectf
_header
_name
,
85 self
._bitfield
_header
_name
))
88 return self
._c
_sources
90 def generate_metadata_stream(self
) -> _GeneratedFile
:
91 if self
._metadata
_stream
is None:
92 self
._metadata
_stream
= _GeneratedFile('metadata',
93 barectf_tsdl182gen
._from
_config
(self
._config
))
95 return self
._metadata
_stream
This page took 0.031186 seconds and 4 git commands to generate.