-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbootstrap.py
309 lines (271 loc) · 14.9 KB
/
bootstrap.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
## Copyright [2017-2018] UMR MISTEA INRA, UMR LEPSE INRA, ##
## UMR AGAP CIRAD, EPI Virtual Plants Inria ##
## Copyright [2015-2016] UMR AGAP CIRAD, EPI Virtual Plants Inria ##
## ##
## This file is part of the AutoWIG project. More information can be ##
## found at ##
## ##
## http://autowig.rtfd.io ##
## ##
## The Apache Software Foundation (ASF) licenses this file to you under ##
## the Apache License, Version 2.0 (the "License"); you may not use this ##
## file except in compliance with the License. You should have received ##
## a copy of the Apache License, Version 2.0 along with this file; see ##
## the file LICENSE. If not, 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 ##
## mplied. See the License for the specific language governing ##
## permissions and limitations under the License. ##
import os
import platform
import uuid
import sys
import autowig
is_windows = any(platform.win32_ver())
if is_windows:
raise OSError("The file '" + __file__ + "' cannot be executed on Windows OSes")
CONDA_ENVIRONMENT = "env_" + str(uuid.uuid4())
PYTHON_VERSION = str(sys.version_info.major) + '.' + str(sys.version_info.minor)
def clanglite_controller(asg):
BOOST_PYTHON = autowig.generator.plugin.startswith('boost_python')
PYBIND11 = autowig.generator.plugin.startswith('pybind11')
LIBCLANG = autowig.parser.plugin == 'libclang'
CLANGLITE = autowig.parser.plugin == 'clanglite'
from autowig.default_controller import refactoring
asg = refactoring(asg)
if BOOST_PYTHON:
for node in asg.classes():
node.boost_python_export = False
for node in asg.functions(free=True):
node.boost_python_export = False
for node in asg.variables(free = True):
node.boost_python_export = False
for node in asg.enumerations():
node.boost_python_export = False
for node in asg.enumerators():
if node.parent.boost_python_export:
node.boost_python_export = False
for node in asg.typedefs():
node.boost_python_export = False
for node in asg['::boost::python'].classes(nested = True):
node.is_copyable = True
asg['class ::boost::python::api::object'].boost_python_export = True
asg['class ::boost::python::list'].boost_python_export = True
asg['class ::boost::python::str'].boost_python_export = True
if PYBIND11:
for node in asg.classes():
node.pybind11_export = False
for node in asg.functions(free=True):
node.pybind11_export = False
for node in asg.variables(free = True):
node.pybind11_export = False
for node in asg.enumerations():
node.pybind11_export = False
for node in asg.enumerators():
if node.parent.pybind11_export:
node.pybind11_export = False
for node in asg.typedefs():
node.pybind11_export = False
if LIBCLANG:
for fct in asg.functions(free=False):
asg._nodes[fct._node]['_is_virtual'] = False
asg._nodes[fct._node]['_is_pure'] = False
asg['class ::clang::QualType'].is_abstract = False
asg['class ::clang::QualType'].is_copyable = True
asg['class ::llvm::StringRef'].is_abstract = False
asg['class ::llvm::StringRef'].is_copyable = True
asg['class ::clang::FileID'].is_abstract = False
asg['class ::clang::FileID'].is_copyable = True
asg['class ::clang::SourceLocation'].is_abstract = False
asg['class ::clang::SourceLocation'].is_copyable = True
asg['class ::clang::TemplateArgument'].is_abstract = False
asg['class ::clang::TemplateArgument'].is_copyable = True
for cls in ['::clang::FriendDecl', '::clang::CapturedDecl', '::clang::OMPThreadPrivateDecl',
'::clang::NonTypeTemplateParmDecl', '::clang::TemplateArgumentList', '::clang::ImportDecl',
'::clang::TemplateTemplateParmDecl', '::clang::CapturedDecl', '::clang::OMPThreadPrivateDecl',
'::clang::NonTypeTemplateParmDecl', '::clang::TemplateArgumentList', '::clang::ImportDecl',
'::clang::TemplateTemplateParmDecl']:
asg['class ' + cls].is_abstract = False
subset = []
classes = [asg['class ::clang::QualType'],
asg['class ::clang::Type'],
asg['class ::clang::Decl']]
subset += classes
for cls in classes:
subset += cls.subclasses(recursive=True)
if BOOST_PYTHON:
for cls in subset:
if not cls.globalname.strip('class ') in ['::clang::QualType',
'::llvm::StringRef',
'::clang::FileID',
'::clang::SourceLocation',
'::clang::TemplateArgument',
'::clang::FriendDecl',
'::clang::CapturedDecl',
'::clang::OMPThreadPrivateDecl',
'::clang::NonTypeTemplateParmDecl',
'::clang::TemplateArgumentList',
'::clang::ImportDecl',
'::clang::TemplateTemplateParmDecl']:
cls.is_copyable = False
else:
cls.is_copyable = True
subset.append(asg['class ::llvm::StringRef'])
subset.append(asg['class ::clang::ASTUnit'])
subset.append(asg['class ::clang::ASTContext'])
subset.append(asg['class ::clang::SourceManager'])
subset.append(asg['class ::clang::FileID'])
subset.append(asg['class ::clang::SourceLocation'])
subset.append(asg['class ::clang::CXXBaseSpecifier'])
subset.append(asg['class ::clang::DeclContext'])
subset.append(asg['class ::clang::TemplateArgument'])
subset.append(asg['class ::clang::TemplateArgumentList'])
subset.append(asg['enum ::clang::Type::TypeClass'])
subset.append(asg['enum ::clang::AccessSpecifier'])
subset.append(asg['enum ::clang::LinkageSpecDecl::LanguageIDs'])
subset.append(asg['enum ::clang::BuiltinType::Kind'])
subset.append(asg['enum ::clang::TemplateArgument::ArgKind'])
subset.append(asg['enum ::clang::Decl::Kind'])
subset.extend(asg.nodes('::clanglite::build_ast_from_code_with_args'))
if BOOST_PYTHON:
for node in subset:
node.boost_python_export = True
if PYBIND11:
for node in subset:
node.pybind11_export = True
for fct in asg['::clanglite'].functions():
if not fct.localname == 'build_ast_from_code_with_args':
fct.parent = fct.parameters[0].qualified_type.desugared_type.unqualified_type
if BOOST_PYTHON:
fct.boost_python_export = True
if PYBIND11:
fct.pybind11_export = True
for mtd in asg['class ::clang::ASTContext'].methods(pattern='.*getSourceManager.*'):
if mtd.return_type.globalname == 'class ::clang::SourceManager &':
if BOOST_PYTHON:
mtd.boost_python_export = True
if PYBIND11:
mtd.pybind11_export = True
break
if LIBCLANG and BOOST_PYTHON:
for node in (asg.functions(pattern='.*(llvm|clang).*_(begin|end)')
+ asg.functions(pattern='::clang::CXXRecordDecl::getCaptureFields')
+ asg.functions(pattern='.*(llvm|clang).*getNameAsString')
+ asg.nodes('::clang::NamedDecl::getQualifiedNameAsString')
+ asg.functions(pattern='.*::clang::ObjCProtocolDecl')
+ asg.nodes('::clang::ObjCProtocolDecl::collectInheritedProtocolProperties')
+ asg.nodes('::clang::ASTUnit::LoadFromASTFile')
+ asg.nodes('::clang::ASTUnit::getCachedCompletionTypes')
+ asg.nodes('::clang::ASTUnit::getBufferForFile')
+ asg.nodes('::clang::CXXRecordDecl::getCaptureFields')
+ asg.nodes('::clang::ASTContext::SectionInfos')
+ asg.nodes('::clang::ASTContext::getAllocator')
+ asg.nodes('::clang::ASTContext::getObjCEncoding.*')
+ asg.nodes('::clang::ASTContext::getAllocator')
+ asg.nodes('::clang::QualType::getAsString')
+ asg.nodes('::clang::SourceLocation::printToString')
+ asg['class ::llvm::StringRef'].methods()):
node.boost_python_export = False
if CLANGLITE and BOOST_PYTHON:
for mtd in asg['class ::clang::Decl'].methods():
if mtd.localname == 'hasAttr':
mtd.boost_python_export = False
if BOOST_PYTHON:
if 'class ::clang::ConstructorUsingShadowDecl' in asg:
for mtd in asg["class ::clang::ConstructorUsingShadowDecl"].methods():
if mtd.localname in ['getConstructor', 'setConstructor']:
mtd.boost_python_export = False
import sys
try:
from path import path as Path
except:
from path import Path
import platform
if any(platform.win32_ver()):
for header in (Path(sys.prefix)/'Library'/'include'/'clang').walkfiles('*.h'):
asg[header.abspath()].is_external_dependency = False
else:
for header in (Path(sys.prefix)/'include'/'clang').walkfiles('*.h'):
asg[header.abspath()].is_external_dependency = False
return asg
VERSION_CHANGE=False
if not os.system("conda create -n " + CONDA_ENVIRONMENT + " python=" + PYTHON_VERSION + " statiskit-toolchain -y -c statiskit --use-local"):
try:
CONDA_PREFIX = os.path.abspath(os.environ['CONDA_PREFIX'])
if os.path.abspath(CONDA_PREFIX + '/..').endswith('envs'):
CONDA_PREFIX = os.path.abspath(os.environ['CONDA_PREFIX'] + '/..')
else:
CONDA_PREFIX = os.path.abspath(os.environ['CONDA_PREFIX'] + '/envs')
CONDA_PREFIX = os.path.join(CONDA_PREFIX, CONDA_ENVIRONMENT)
if VERSION_CHANGE:
if os.system("conda build etc/conda/llvm"):
raise OSError("Build of the llvm recipe failed !")
if os.system("conda install -n " + CONDA_ENVIRONMENT + " llvm --use-local -y"):
raise OSError("Install of llvm failed !")
else:
if os.system("conda install -n " + CONDA_ENVIRONMENT + " llvm -c statiskit -y"):
raise OSError("Install of llvm failed !")
if VERSION_CHANGE:
if os.system("conda build etc/conda/clang"):
raise OSError("Build of the clang recipe failed !")
if os.system("conda install -n " + CONDA_ENVIRONMENT + " clang --use-local -y"):
raise OSError("Install of clang failed !")
else:
if os.system("conda install -n " + CONDA_ENVIRONMENT + " clang -c statiskit -y"):
raise OSError("Install of clang failed !")
# if VERSION_CHANGE:
# if os.system("conda build etc/conda/python-clang"):
# raise OSError("Build of the python-clang recipe failed !")
# if os.system("conda install -n " + CONDA_ENVIRONMENT + " python-clang --use-local -y"):
# raise OSError("Install of python-clang failed !")
# else:
# if os.system("conda install -n " + CONDA_ENVIRONMENT + " python-clang -c statiskit -y"):
# raise OSError("Install of python-clang failed !")
if os.system("conda build etc/conda/libclanglite -c statiskit"):
raise OSError("Build of the libclanglite recipe failed !")
if os.system("conda install -n " + CONDA_ENVIRONMENT + " libclanglite --use-local -c statiskit -y"):
raise OSError("Install of libclanglite failed !")
os.system("rm src/py/*.cpp")
os.system("rm src/py/*.h")
os.system("rm src/py/clanglite/_clanglite.py")
headers = [CONDA_PREFIX + '/include/clanglite/tool.h']
flags = ['-x', 'c++', '-std=c++11',
'-D__STDC_LIMIT_MACROS',
'-D__STDC_CONSTANT_MACROS',
'-I' + CONDA_PREFIX + '/include']
if PYTHON_VERSION == '2.7':
flags += ['-I' + CONDA_PREFIX + '/include/python' + PYTHON_VERSION]
elif PYTHON_VERSION in ['3.6', '3.7']:
flags += ['-I' + CONDA_PREFIX + '/include/python' + PYTHON_VERSION + 'm']
else:
raise ValueError("Include path for python " + PYTHON_VERSION + " unknown")
_CONDA_PREFIX = os.environ['CONDA_PREFIX']
os.environ['CONDA_PREFIX'] = CONDA_PREFIX
asg = autowig.AbstractSemanticGraph()
asg = autowig.parser(asg,
headers,
flags = flags,
bootstrap = False)
autowig.controller['clanglite'] = clanglite_controller
autowig.controller.plugin = 'clanglite'
asg = autowig.controller(asg)
autowig.generator.plugin = 'boost_python_pattern'
autowig.generator.plugin = 'pybind11_pattern'
wrappers = autowig.generator(asg,
module = 'src/py/_clanglite.cpp',
decorator = 'src/py/clanglite/_clanglite.py',
closure = False)
wrappers.write()
os.environ['CONDA_PREFIX'] = _CONDA_PREFIX
if os.system("conda build bin/conda/python-clanglite -c statiskit"):
raise OSError("Build of the python-clanglite recipe failed !")
if os.system("conda install -n " + CONDA_ENVIRONMENT + " python-clanglite --use-local -c statiskit -y"):
raise OSError("Install of python-clanglite failed !")
except Exception as error:
print(error)
finally:
os.system("conda env remove -n " + CONDA_ENVIRONMENT + " -y")