-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathc.py
183 lines (146 loc) · 6.7 KB
/
c.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2022 Couchbase, Inc All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from datetime import datetime, timedelta
from typing import List, Dict, cast
from defs import DefaultGenerator, DefaultEntry, Constant, ConstantValue, ConstantType, make_c_style_varname
license="""//
// {filename}
// CouchbaseLite
//
// Copyright (c) {year}-present Couchbase, Inc All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// THIS IS AN AUTOGENERATED FILE, MANUAL CHANGES SHOULD BE EXPECTED TO
// BE OVERWRITTEN
"""
OUTPUT_ID = "c"
top_level_format_header = license + """
#pragma once
#include "CBL_Compat.h"
#include "CBLReplicator.h"
CBL_CAPI_BEGIN
/** \defgroup constants Constants
@{{
Constants for default configuration values. */
{generated}
/** @}} */
CBL_CAPI_END
"""
top_level_format_impl = license + """
#include "CBLDefaults.h"
#include <climits>
{generated}
"""
class CDefaultGenerator(DefaultGenerator):
_type_mapping: Dict[str, str] = {
ConstantType.BOOLEAN_TYPE_ID: "bool",
ConstantType.TIMESPAN_TYPE_ID: "unsigned",
ConstantType.INT_TYPE_ID: "int",
ConstantType.LONG_TYPE_ID: "int64_t",
ConstantType.UINT_TYPE_ID: "unsigned",
ConstantType.USHORT_TYPE_ID: "unsigned short",
"ReplicatorType": "CBLReplicatorType",
"DistanceMetric": "CBLDistanceMetric"
}
def transform_var_value(self, type: ConstantType, value: ConstantValue) -> str:
if type.subset == "enum":
return f"kCBL{type.id}{value}"
if type.id == ConstantType.BOOLEAN_TYPE_ID:
return str(value.val).lower()
if type.id == ConstantType.TIMESPAN_TYPE_ID:
if value.unit == "seconds":
return str(cast(timedelta, value.val).seconds)
else:
raise Exception(f"Unknown unit '{value.unit}'")
if type.id == ConstantType.UINT_TYPE_ID:
if value.val == "max":
return "UINT_MAX"
if type.id == ConstantType.INT_TYPE_ID:
if value.val == "max":
return "INT_MAX"
return str(value)
def compute_header_line(self, prefix_name: str, constant: Constant) -> str:
platform_type = constant.type(OUTPUT_ID)
platform_value = constant.value(OUTPUT_ID)
value = self.transform_var_value(platform_type, platform_value)
ret_val = f"/** [{value}] {constant.description} */\n"
type = self._type_mapping[platform_type.id] if platform_type.id in self._type_mapping else platform_type
var_name = make_c_style_varname(prefix_name, constant.name)
ret_val += f"CBL_PUBLIC extern const {type} {var_name};\n\n"
return ret_val
def compute_impl_line(self, prefix_name: str, constant: Constant) -> str:
platform_type = constant.type(OUTPUT_ID)
platform_value = constant.value(OUTPUT_ID)
type = self._type_mapping[platform_type.id] if platform_type.id in self._type_mapping else platform_type
value = self.transform_var_value(platform_type, platform_value)
var_name = make_c_style_varname(prefix_name, constant.name)
return f"CBL_PUBLIC const {type} {var_name} = {value};\n"
def compute_export_line(self, prefix_name: str, constant: Constant) -> str:
var_name = make_c_style_varname(prefix_name, constant.name)
return var_name + "\n"
def compute_doc_comment_header(self, long_name: str) -> str:
return f"/** \\name CBL{long_name}\n\t@{{\n*/\n\n"
def generate(self, input: List[DefaultEntry]) -> Dict[str, str]:
generated: Dict[str, str] = {}
generated_header = ""
generated_impl = ""
generated_exports = ""
writing_ee = False
input = sorted(input, key=lambda x: x.ee)
for entry in input:
if len(entry.only_on) > 0 and not OUTPUT_ID in entry.only_on:
continue
if not writing_ee and entry.ee:
generated_header += "#ifdef COUCHBASE_ENTERPRISE\n\n"
generated_impl += "#ifdef COUCHBASE_ENTERPRISE\n\n"
generated_exports += "#ifdef COUCHBASE_ENTERPRISE\n\n"
writing_ee = True
generated_header += self.compute_doc_comment_header(entry.long_name)
generated_impl += f"#pragma mark - CBL{entry.long_name}\n\n"
generated_exports += f"### CBL{entry.long_name}\n\n"
for c in entry.constants:
if len(c.only_on) > 0 and not OUTPUT_ID in c.only_on:
continue
generated_header += self.compute_header_line(entry.name, c)
generated_impl += self.compute_impl_line(entry.name, c)
generated_exports += self.compute_export_line(entry.name, c)
generated_header += "/** @} */\n\n"
generated_impl += "\n"
generated_exports += "\n"
if writing_ee:
generated_header += "#endif"
generated_impl += "#endif"
generated_exports += "#endif"
generated["CBLDefaults.h"] = top_level_format_header.format(year = datetime.now().year, filename = "CBLDefaults.h", generated = generated_header)
generated["CBLDefaults_CAPI.cc"] = top_level_format_impl.format(year = datetime.now().year, filename = "CBLDefaults_CAPI.cc", generated = generated_impl)
generated["CBLDefaults_Exports.txt"] = (license + generated_exports).format(year = datetime.now().year, filename = "CBLDefaults_Exports.txt",
generated = generated_exports)
return generated
if __name__ == "__main__":
raise Exception("This script is not standalone, it is used with gen_defaults.py")