-
Notifications
You must be signed in to change notification settings - Fork 3
/
conanfile.py
260 lines (206 loc) · 8.97 KB
/
conanfile.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
from conans import ConanFile, tools
from conans.client.output import ConanOutput
from conans.model import Generator
from functools import wraps
import io
import os
import sys
import textwrap
##################
# Local indenter #
##################
def prepend_2s(s):
return textwrap.indent(s, prefix=' ' * 2)
def prepender(s_function):
@wraps(s_function)
def wrapped_s_function(*args, **kwargs):
args = list(args)
prepended_s = prepend_2s(args[0])
args = tuple([prepended_s] + args[1:])
return s_function(*args, **kwargs)
return wrapped_s_function
class StringIOClsWrapper(object):
def __init__(self, instance, wrapped_funcs):
self._instance = instance
self._wrapped_funcs = wrapped_funcs
def __getattribute__(self, name):
try:
attr = super(StringIOClsWrapper, self).__getattribute__(name)
except AttributeError:
pass
else:
return attr
attr = self._instance.__getattribute__(name)
if attr.__name__ in self._wrapped_funcs.keys():
return self._wrapped_funcs[attr.__name__](attr)
else:
return attr
class StringIO_wrapper(object):
def __init__(self, instance, write_method):
self._instance = instance
self._write_method = write_method
def __enter__(self):
return StringIOClsWrapper(self._instance, {"write": self._write_method})
def __exit__(self, *args, **kwargs):
pass
##############
# GN Grammar #
##############
class GNIdentifier(object):
def __init__(self, name):
self._name = name
def __str__(self):
return self._name
class GNBool(object):
def __init__(self, value):
self._b = value
def __str__(self):
return self._b.__str__().lower()
class GNString(str):
def __str__(self):
return'"' + super().__str__() + '"'
class GNList(list):
def __str__(self):
if len(self) == 0:
return "[]"
else:
output = io.StringIO()
if len(self) > 3:
output.write('[\n')
with StringIO_wrapper(output, prepender) as output_:
output_.write(',\n'.join(map(str, self)))
output.write('\n]')
else:
output.write('[ ')
output.write(', '.join(map(str, self)))
output.write(' ]')
return output.getvalue()
class GNScope(dict):
def __str__(self):
if len(self) == 0:
return "{}"
else:
output = io.StringIO()
output.write("{\n")
with StringIO_wrapper(output, prepender) as output_:
for key, value in self.items():
output_.write("{key} = {value},\n".format(key=key, value=str(value)))
output.write("}")
return output.getvalue()
class GNVarStatement(object):
def __init__(self, name, value):
self._name = name
self._value = value
def __str__(self):
return "{name} = {value}".format(name=self._name, value=str(self._value))
class GNCallStatement(object):
def __init__(self, name, parameters=None, block=None):
self._name = name
self._parameters = parameters
self._block = block
def __str__(self):
output = io.StringIO()
output.write("{name}(".format(name=self._name))
if self._parameters:
output.write(', '.join(map(str, self._parameters)))
output.write(")")
if self._block:
output.write(" {\n")
with StringIO_wrapper(output, prepender) as output_:
output_.write('\n'.join(map(str, self._block)))
output.write("\n}")
return output.getvalue()
#############
# Generator #
#############
class GNGenerator(Generator):
_output = ConanOutput(sys.stdout, True)
@staticmethod
def generateGNBuildFile(dep_name, dep_cpp_info):
configs = list()
tree = list()
#tree.append(GNVarStatement("_root", GNString(dep_cpp_info.rootpath)))
config_name = "{dep_name}_include".format(dep_name=dep_name)
visibility = "include"
configs.append(tuple([config_name, visibility]))
tree.append(GNCallStatement(name="config",
parameters=[GNString(config_name)],
block=[
GNVarStatement("include_dirs",
GNList(
[GNString(include_path)
for include_path in dep_cpp_info.include_paths])),
GNVarStatement("defines",
GNList(
[GNString(flag)
for flag in dep_cpp_info.defines])),
GNVarStatement("cflags_c",
GNList(
[GNString(flag)
for flag in dep_cpp_info.cflags])),
GNVarStatement("cflags_cc",
GNList(
[GNString(flag)
for flag in dep_cpp_info.cppflags])),
GNVarStatement("visibility",
GNList([GNString(":{}".format(visibility))]))
]))
config_name = "{dep_name}_runtime_path".format(dep_name=dep_name)
visibility = "runtime_path"
configs.append(tuple([config_name, visibility]))
tree.append(GNCallStatement(name="config",
parameters=[GNString(config_name)],
block=[
GNVarStatement("lib_dirs",
GNList([GNString(lib_path)
for lib_path in dep_cpp_info.lib_paths])),
GNVarStatement("ldflags",
GNList(
[GNString(flag)
for flag in dep_cpp_info.sharedlinkflags] +
[GNString(flag)
for flag in dep_cpp_info.exelinkflags])),
GNVarStatement("visibility",
GNList([GNString(":{}".format(visibility))]))
]))
for lib_name in dep_cpp_info.libs:
config_name = "{}_lib_{}".format(dep_name, lib_name)
configs.append(tuple([config_name, lib_name]))
tree.append(GNCallStatement(name="config",
parameters=[GNString(config_name)],
block=[
GNVarStatement("libs",
GNList([GNString(lib_name)])),
GNVarStatement("visibility",
GNList([GNString(":{}".format(lib_name))]))
]))
for config_name, visibility in configs:
tree.append(GNCallStatement(name="group",
parameters=[GNString(visibility)],
block=[
GNVarStatement("public_configs",
GNList([GNString(":{}".format(config_name))]))
]))
return '\n\n'.join(map(str, tree)) + '\n'
@property
def filename(self):
return False
@property
def content(self):
gn_build_files = dict()
for dep_name, dep_cpp_info in self.deps_build_info.dependencies:
if dep_name != GNGeneratorConanFile.name:
content = self.generateGNBuildFile(dep_name, dep_cpp_info)
gn_build_files[os.path.join(dep_name, "BUILD.gn")] = content
return gn_build_files
class GNGeneratorConanFile(ConanFile):
name = "GNGenerator"
version = "0.1"
url = "https://github.com/matlo607/conan-gn-generator.git"
licence = "MIT License (https://github.com/matlo607/conan-gn-generator/blob/master/LICENSE)"
exports = "*.py"
settings = "os", "compiler", "build_type", "arch"
def build(self):
pass
def package_info(self):
pass