tests/tracing/support/test-platform.c: clear buffer at every packet opening
[deliverable/barectf.git] / barectf / codegen.py
CommitLineData
728fc4a7
PP
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
24import barectf.tsdl182gen as barectf_tsdl182gen
728fc4a7
PP
25import barectf.config as barectf_config
26import barectf.cgen as barectf_cgen
c4b0c6f2 27from typing import List, Optional
728fc4a7
PP
28
29
30# A file generated by a `CodeGenerator` object.
31#
32# A generated file has a name (influenced by the configuration's
33# file name prefix option) and contents.
34class _GeneratedFile:
c4b0c6f2 35 def __init__(self, name: str, contents: str):
728fc4a7
PP
36 self._name = name
37 self._contents = contents
38
39 @property
c4b0c6f2 40 def name(self) -> str:
728fc4a7
PP
41 return self._name
42
43 @property
c4b0c6f2 44 def contents(self) -> str:
728fc4a7
PP
45 return self._contents
46
47
48# A barectf code generator.
49#
50# Build a code generator with a barectf configuration.
51#
52# A code generator can generate the TSDL `metadata` file and C source
53# and header files.
54class CodeGenerator:
c4b0c6f2 55 def __init__(self, configuration: barectf_config.Configuration):
728fc4a7
PP
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)
c4b0c6f2
PP
59 self._c_headers: Optional[List[_GeneratedFile]] = None
60 self._c_sources: Optional[List[_GeneratedFile]] = None
61 self._metadata_stream: Optional[_GeneratedFile] = None
728fc4a7
PP
62
63 @property
c4b0c6f2 64 def _barectf_header_name(self) -> str:
728fc4a7
PP
65 return f'{self._file_name_prefix}.h'
66
67 @property
c4b0c6f2 68 def _bitfield_header_name(self) -> str:
728fc4a7
PP
69 return f'{self._file_name_prefix}-bitfield.h'
70
c4b0c6f2 71 def generate_c_headers(self) -> List[_GeneratedFile]:
728fc4a7
PP
72 if self._c_headers is None:
73 self._c_headers = [
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()),
76 ]
77
78 return self._c_headers
79
c4b0c6f2 80 def generate_c_sources(self) -> List[_GeneratedFile]:
728fc4a7
PP
81 if self._c_sources is None:
82 self._c_sources = [
83 _GeneratedFile(f'{self._file_name_prefix}.c',
84 self._c_code_gen.gen_src(self._barectf_header_name,
85 self._bitfield_header_name))
86 ]
87
88 return self._c_sources
89
c4b0c6f2 90 def generate_metadata_stream(self) -> _GeneratedFile:
728fc4a7
PP
91 if self._metadata_stream is None:
92 self._metadata_stream = _GeneratedFile('metadata',
93 barectf_tsdl182gen._from_config(self._config))
94
95 return self._metadata_stream
This page took 0.03138 seconds and 4 git commands to generate.