-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathconanfile.py
365 lines (321 loc) · 12.9 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout, CMakeDeps
from conan.tools.build import check_min_cppstd
from conan.tools.files import (
load,
save,
copy,
rm,
)
from conan.tools.scm import Git
from os.path import join, isdir, exists
import re
from pathlib import Path, PurePosixPath
import textwrap
import json
import os
from conan.errors import ConanException
required_conan_version = ">=2.0"
def components_from_dotfile(dotfile):
def node_labels(dot):
# here we are mapping dependencies visible to cmake to component dependencies in conan
label_replacements = {
"LibXml2::LibXml2": "libxml2::libxml2",
"ZLIB::ZLIB": "zlib::zlib",
"zstd::libzstd_static": "zstd::zstdlib",
"-lpthread": "pthread",
"curl": "libcurl::libcurl",
"nlohmann_json_schema_validator": "json-schema-validator::json-schema-validator",
"clangCodeGen": "clang::clangCodeGen",
"clangTooling": "clang::clangTooling",
"SQLite::SQLite3": "sqlite3::sqlite3",
}
for row in dot:
# e.g. "node0" [ label = "phasar\n(phasar::phasar)", shape = octagon ];
match_label = re.match(r'''^\s*"(node[0-9]+)"\s*\[\s*label\s*=\s*"([^\\"]+)''', row)
if match_label:
node = match_label.group(1)
label = match_label.group(2)
if label.startswith("LLVM"):
yield node, f"llvm-core::{label}"
# XXX find_library adds direct filepath -> imho a flaw in current cmake files
elif label.endswith("libsqlite3.a"):
yield node, "sqlite3::sqlite3"
elif label.endswith("libclang-cpp.so"):
yield node, "clang::clang"
elif label.endswith("libclangCodeGen.a"):
yield node, "clang::clangCodeGen"
elif label.endswith("libclangTooling.a"):
yield node, "clang::clangTooling"
else:
yield node, label_replacements.get(label, label)
def node_dependencies(dot):
ignore_deps = [
]
labels = {k: v for k, v in node_labels(dot)}
for row in dot:
# "node0" -> "node1" [ style = dashed ] // phasar -> LLVMAnalysis
match_dep = re.match(r'''^\s*"(node[0-9]+)"\s*->\s*"(node[0-9]+)".*''', row)
if match_dep:
node_label = labels[match_dep.group(1)]
dependency = labels[match_dep.group(2)]
if node_label.startswith("phasar") and PurePosixPath(dependency).parts[-1] not in ignore_deps:
yield node_label, labels[match_dep.group(2)]
# some components don't have dependencies
for label in labels.values():
if label.startswith("phasar"):
yield label, None
system_libs = {
"ole32",
"delayimp",
"shell32",
"advapi32",
"-delayload:shell32.dll",
"uuid",
"psapi",
"-delayload:ole32.dll",
"ntdll",
"ws2_32",
"rt",
"m",
"dl",
"pthread",
"stdc++fs"
}
components = {}
dotfile_rows = dotfile.split("\n")
for node, dependency in node_dependencies(dotfile_rows):
if dependency in system_libs:
key = "system_libs"
elif dependency is not None and (dependency.startswith("phasar") or "::" in dependency):
key = "requires"
else:
key = "unknown"
if node not in components:
components[node] = { "system_libs": [], "requires": [], "unknown": [] }
if dependency is not None:
components[node][key] = [dependency]
elif dependency is not None:
components[node][key].append(dependency)
return components
class PhasarRecipe(ConanFile):
name = "phasar"
package_type = "library"
# Optional metadata
license = "MIT license"
url = "https://github.com/secure-software-engineering/phasar"
description = "A LLVM-based static analysis framework. "
topics = ("LLVM", "PhASAR", "SAST")
# Binary configuration
settings = "os", "compiler", "build_type", "arch"
options = {
"with_z3": [True, False],
"shared": [True, False],
"fPIC": [True, False],
"tests": [True, False],
"llvm_version": ["ANY"],
}
default_options = {
"with_z3": True,
"shared": False,
"fPIC": True,
"tests": False,
"llvm_version": "15.0.7"
}
def _parse_gitignore(self, folder, additional_exclusions = [], invert=False):
exclusions = []
inclusions = []
if invert:
for exc in additional_exclusions:
if exc.startswith("!"):
inclusions = exc[1:]
else:
inclusions = f"!{exc}"
else:
exclusions = additional_exclusions
with open(f'{folder}/.gitignore', 'r') as file:
for line in file:
line = line.strip()
if line.startswith("#") or not line:
continue
if invert:
if line.startswith("!"):
inclusions.append(line[1:])
else:
inclusions.append("!" + line)
else:
exclusions.append(line)
if invert:
return inclusions
else:
return exclusions
def export_sources(self):
exclusions = self._parse_gitignore(".", [
"test_package",
"utils",
"img",
"githooks",
"external"
])
for tlf in os.listdir("."):
if isdir(tlf):
copy(self, f"{tlf}/*", src=".", dst=self.export_sources_folder, excludes=exclusions)
else:
copy(self, tlf, src=".", dst=self.export_sources_folder, excludes=exclusions)
@property
def _graphviz_file(self):
return PurePosixPath(self.build_folder) / "graph" / "phasar.dot"
@property
def _info_file(self):
# this is called very early where folders aren't set but this is fine
if self.export_folder is None:
return None
return PurePosixPath(self.export_folder) / "info.json"
def _read_info(self):
if self._info_file is not None and exists(self._info_file):
with open(self._info_file, encoding="utf-8") as fp:
return json.load(fp)
else:
return {
"version": None,
}
def _write_info(self, info):
if self._info_file is not None:
with open(self._info_file, "w", encoding="utf-8") as fp:
json.dump(info, fp, indent=2)
def set_version(self):
if self.version is not None:
return
info = self._read_info()
version = info["version"]
if version is None:
git = Git(self, self.recipe_folder)
# XXX consider git.coordinates_to_conandata()
if git.is_dirty():
raise ConanException("Repository is dirty. I can't calculate a correct version and this is a potential leak because all files visible to git will be exported. Please stash or commit, to skip this for local testing use \"--version dev\".")
self.output.info("No version information set, retrieving from git.")
calver = git.run("show -s --date=format:'%Y.%m.%d' --format='%cd'")
short_hash = git.run("show -s --format='%h'")
version = f"{calver}+{short_hash}"
if info["version"] != version:
info["version"] = version
self._write_info(info)
self.version = version
def layout(self):
cmake_layout(self)
def config_options(self):
if self.settings.os == "Windows":
self.options.rm_safe("fPIC")
def requirements(self):
self.requires("boost/[>1.72.0 <=1.86.0]")
self.requires("sqlite3/[>=3 <4]")
self.requires(f"clang/{self.options.llvm_version}@secure-software-engineering", transitive_libs=True, transitive_headers=True)
self.requires("nlohmann_json/3.11.3", transitive_headers=True)
self.requires("json-schema-validator/2.3.0", transitive_libs=True, transitive_headers=True)
llvm_options={
"rtti": True,
}
if self.options.with_z3:
self.requires("z3/[>=4.7.1 <5]")
llvm_options["with_z3"] = True
self.requires(f"llvm-core/{self.options.llvm_version}@secure-software-engineering", transitive_libs=True, transitive_headers=True, options=llvm_options)
def build_requirements(self):
self.tool_requires("cmake/[>=3.25.0 <4.0.0]") # find_program validator
self.tool_requires("ninja/[>=1.9.0 <2.0.0]")
if self.options.tests:
self.test_requires("openssl/[>2 <4]")
self.test_requires("gtest/1.14.0")
def configure(self):
if self.options.shared:
self.options.rm_safe("fPIC")
def validate(self):
check_min_cppstd(self, '17')
def generate(self):
deps = CMakeDeps(self)
deps.generate()
tc = CMakeToolchain(self, 'Ninja')
tc.generate()
def _cmake_configure(self):
cmake = CMake(self)
self._handle_graphviz()
cmake.configure(
variables={
'PHASAR_ENABLE_PIC': self.options.get_safe("fPIC", False),
'PHASAR_USE_CONAN': True,
'BUILD_SHARED_LIBS': self.options.shared,
'PHASAR_BUILD_UNITTESTS': self.options.tests,
'PHASAR_BUILD_IR': self.options.tests,
'PHASAR_BUILD_DOC': False,
'PHASAR_USE_Z3': self.options.with_z3,
'USE_LLVM_FAT_LIB': False,
'BUILD_PHASAR_CLANG': True,
'PHASAR_BUILD_TOOLS': True,
},
cli_args=[
f"--graphviz={self._graphviz_file}"
]
)
return cmake
def _handle_graphviz(self):
exclude_patterns = [
"LLVMTableGenGlobalISel.*",
"CONAN_LIB.*",
"LLVMExegesis.*",
"LLVMCFIVerify.*"
]
graphviz_options = textwrap.dedent(f"""
set(GRAPHVIZ_EXECUTABLES OFF)
set(GRAPHVIZ_MODULE_LIBS OFF)
set(GRAPHVIZ_OBJECT_LIBS OFF)
set(GRAPHVIZ_IGNORE_TARGETS "{';'.join(exclude_patterns)}")
""")
save(self, PurePosixPath(self.build_folder) / "CMakeGraphVizOptions.cmake", graphviz_options)
def build(self):
cmake = self._cmake_configure()
cmake.build()
if self.options.tests:
cmake.ctest(cli_args=[
"--exclude-regex 'IDEExtendedTaintAnalysisTest.*'", # known flaky test
"--no-tests=error",
"--output-on-failure",
"--test-dir",
self.build_folder
])
@property
def _cmake_module_path(self):
return PurePosixPath("lib") / "cmake" / "phasar"
@property
def _build_info_file(self):
return PurePosixPath(self.package_folder) / self._cmake_module_path / "conan_phasar_build_info.json"
def _write_build_info(self):
# maybe process original config
cmake_config = Path(self.package_folder / self._cmake_module_path / "phasarConfig.cmake").read_text("utf-8")
components = components_from_dotfile(load(self, self._graphviz_file))
build_info = {
"components": components,
}
with open(self._build_info_file, "w", encoding="utf-8") as fp:
json.dump(build_info, fp, indent=2)
return build_info
def _read_build_info(self) -> dict:
with open(self._build_info_file, encoding="utf-8") as fp:
return json.load(fp)
def package_id(self):
del self.info.options.tests
def package(self):
copy(self, "LICENSE.txt", self.source_folder, join(self.package_folder, "licenses"))
cmake = self._cmake_configure()
cmake.install()
rm(self, "phasarConfig*.cmake", join("lib", "cmake", "phasar"))
rm(self, "*target*.cmake", join("lib", "cmake", "phasar"))
self._write_build_info()
def package_info(self):
self.cpp_info.set_property("cmake_file_name", "phasar")
interfaces = ["phasar_interface"]
build_info = self._read_build_info()
components = build_info["components"]
for component_name, data in components.items():
self.cpp_info.components[component_name].set_property("cmake_target_name", component_name)
self.cpp_info.components[component_name].libs = [component_name] if component_name not in interfaces else []
self.cpp_info.components[component_name].requires = data["requires"]
self.cpp_info.components[component_name].system_libs = data["system_libs"]