forked from conan-io/conan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.py
412 lines (343 loc) · 14.4 KB
/
info.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import os
from conans.client.build.cppstd_flags import cppstd_default
from conans.client.tools.win import MSVS_DEFAULT_TOOLSETS_INVERSE
from conans.errors import ConanException
from conans.model.env_info import EnvValues
from conans.model.options import OptionsValues
from conans.model.ref import PackageReference
from conans.model.values import Values
from conans.paths import CONANINFO
from conans.util.config_parser import ConfigParser
from conans.util.files import load
from conans.util.sha import sha1
class RequirementInfo(object):
def __init__(self, pref, default_package_id_mode, indirect=False):
self.package = pref
self.full_name = pref.ref.name
self.full_version = pref.ref.version
self.full_user = pref.ref.user
self.full_channel = pref.ref.channel
self.full_package_id = pref.id
self._indirect = indirect
try:
getattr(self, default_package_id_mode)()
except AttributeError:
raise ConanException("'%s' is not a known package_id_mode" % default_package_id_mode)
def copy(self):
# Useful for build_id()
result = RequirementInfo(self.package, "unrelated_mode")
for f in ("name", "version", "user", "channel", "package_id"):
setattr(result, f, getattr(self, f))
f = "full_%s" % f
setattr(result, f, getattr(self, f))
return result
def dumps(self):
if not self.name:
return ""
result = ["%s/%s" % (self.name, self.version)]
if self.user or self.channel:
result.append("@%s/%s" % (self.user, self.channel))
if self.package_id:
result.append(":%s" % self.package_id)
return "".join(result)
@property
def sha(self):
vals = [str(n) for n in (self.name, self.version, self.user, self.channel, self.package_id)]
# This is done later to NOT affect existing package-IDs (before revisions)
return "/".join(vals)
def unrelated_mode(self):
self.name = self.version = self.user = self.channel = self.package_id = None
def semver_direct_mode(self):
if self._indirect:
self.unrelated_mode()
else:
self.semver_mode()
def semver_mode(self):
self.name = self.full_name
self.version = self.full_version.stable()
self.user = self.channel = self.package_id = None
semver = semver_mode # Remove Conan 2.0
def full_version_mode(self):
self.name = self.full_name
self.version = self.full_version
self.user = self.channel = self.package_id = None
def patch_mode(self):
self.name = self.full_name
self.version = self.full_version.patch()
self.user = self.channel = self.package_id = None
def base_mode(self):
self.name = self.full_name
self.version = self.full_version.base
self.user = self.channel = self.package_id = None
def minor_mode(self):
self.name = self.full_name
self.version = self.full_version.minor()
self.user = self.channel = self.package_id = None
def major_mode(self):
self.name = self.full_name
self.version = self.full_version.major()
self.user = self.channel = self.package_id = None
def full_recipe_mode(self):
self.name = self.full_name
self.version = self.full_version
self.user = self.full_user
self.channel = self.full_channel
self.package_id = None
def full_package_mode(self):
self.name = self.full_name
self.version = self.full_version
self.user = self.full_user
self.channel = self.full_channel
self.package_id = self.full_package_id
class RequirementsInfo(object):
def __init__(self, prefs, default_package_id_mode):
# {PackageReference: RequirementInfo}
self._data = {pref: RequirementInfo(pref, default_package_id_mode=default_package_id_mode)
for pref in prefs}
def copy(self):
# For build_id() implementation
result = RequirementsInfo([], None)
result._data = {pref: req_info.copy() for pref, req_info in self._data.items()}
return result
def clear(self):
self._data = {}
def remove(self, *args):
for name in args:
del self._data[self._get_key(name)]
def add(self, prefs_indirect, default_package_id_mode):
""" necessary to propagate from upstream the real
package requirements
"""
for r in prefs_indirect:
self._data[r] = RequirementInfo(r, indirect=True,
default_package_id_mode=default_package_id_mode)
def refs(self):
""" used for updating downstream requirements with this
"""
return list(self._data.keys())
def _get_key(self, item):
for reference in self._data:
if reference.ref.name == item:
return reference
raise ConanException("No requirement matching for %s" % (item))
def __getitem__(self, item):
"""get by package name
Necessary to access from conaninfo
self.requires["Boost"].version = "2.X"
"""
return self._data[self._get_key(item)]
@property
def pkg_names(self):
return [r.ref.name for r in self._data.keys()]
@property
def sha(self):
result = []
# Remove requirements without a name, i.e. indirect transitive requirements
data = {k: v for k, v in self._data.items() if v.name}
for key in sorted(data):
s = data[key].sha
result.append(s)
return sha1('\n'.join(result).encode())
def dumps(self):
result = []
for ref in sorted(self._data):
dumped = self._data[ref].dumps()
if dumped:
result.append(dumped)
return "\n".join(result)
def unrelated_mode(self):
self.clear()
def semver_direct_mode(self):
for r in self._data.values():
r.semver_direct_mode()
def semver_mode(self):
for r in self._data.values():
r.semver_mode()
def patch_mode(self):
for r in self._data.values():
r.patch_mode()
def minor_mode(self):
for r in self._data.values():
r.minor_mode()
def major_mode(self):
for r in self._data.values():
r.major_mode()
def base_mode(self):
for r in self._data.values():
r.base_mode()
def full_version_mode(self):
for r in self._data.values():
r.full_version_mode()
def full_recipe_mode(self):
for r in self._data.values():
r.full_recipe_mode()
def full_package_mode(self):
for r in self._data.values():
r.full_package_mode()
class _PackageReferenceList(list):
@staticmethod
def loads(text):
return _PackageReferenceList([PackageReference.loads(package_reference)
for package_reference in text.splitlines()])
def dumps(self):
return "\n".join(self.serialize())
def serialize(self):
return [str(r) for r in sorted(self)]
class ConanInfo(object):
def copy(self):
""" Useful for build_id implementation
"""
result = ConanInfo()
result.settings = self.settings.copy()
result.options = self.options.copy()
result.requires = self.requires.copy()
return result
@staticmethod
def create(settings, options, prefs_direct, prefs_indirect, default_package_id_mode):
result = ConanInfo()
result.full_settings = settings
result.settings = settings.copy()
result.full_options = options
result.options = options.copy()
result.options.clear_indirect()
result.full_requires = _PackageReferenceList(prefs_direct)
result.requires = RequirementsInfo(prefs_direct, default_package_id_mode)
result.requires.add(prefs_indirect, default_package_id_mode)
result.full_requires.extend(prefs_indirect)
result.recipe_hash = None
result.env_values = EnvValues()
result.vs_toolset_compatible()
result.discard_build_settings()
result.default_std_matching()
return result
@staticmethod
def loads(text):
# This is used for search functionality, search prints info from this file
# Other use is from the BinariesAnalyzer, to get the recipe_hash and know
# if package is outdated
parser = ConfigParser(text, ["settings", "full_settings", "options", "full_options",
"requires", "full_requires", "scope", "recipe_hash", "env"],
raise_unexpected_field=False)
result = ConanInfo()
result.settings = Values.loads(parser.settings)
result.full_settings = Values.loads(parser.full_settings)
result.options = OptionsValues.loads(parser.options)
result.full_options = OptionsValues.loads(parser.full_options)
result.full_requires = _PackageReferenceList.loads(parser.full_requires)
# Requires after load are not used for any purpose, CAN'T be used, they are not correct
result.requires = RequirementsInfo(result.full_requires, "semver_direct_mode")
result.recipe_hash = parser.recipe_hash or None
# TODO: Missing handling paring of requires, but not necessary now
result.env_values = EnvValues.loads(parser.env)
return result
def dumps(self):
def indent(text):
if not text:
return ""
return '\n'.join(" " + line for line in text.splitlines())
result = list()
result.append("[settings]")
result.append(indent(self.settings.dumps()))
result.append("\n[requires]")
result.append(indent(self.requires.dumps()))
result.append("\n[options]")
result.append(indent(self.options.dumps()))
result.append("\n[full_settings]")
result.append(indent(self.full_settings.dumps()))
result.append("\n[full_requires]")
result.append(indent(self.full_requires.dumps()))
result.append("\n[full_options]")
result.append(indent(self.full_options.dumps()))
result.append("\n[recipe_hash]\n%s" % indent(self.recipe_hash))
result.append("\n[env]")
result.append(indent(self.env_values.dumps()))
return '\n'.join(result) + "\n"
def __eq__(self, other):
""" currently just for testing purposes
"""
return self.dumps() == other.dumps()
def __ne__(self, other):
return not self.__eq__(other)
@staticmethod
def load_file(conan_info_path):
""" load from file
"""
try:
config_text = load(conan_info_path)
except IOError:
raise ConanException("Does not exist %s" % conan_info_path)
else:
return ConanInfo.loads(config_text)
@staticmethod
def load_from_package(package_folder):
info_path = os.path.join(package_folder, CONANINFO)
return ConanInfo.load_file(info_path)
def package_id(self):
""" The package_id of a conans is the sha1 of its specific requirements,
options and settings
"""
result = []
result.append(self.settings.sha)
# Only are valid requires for OPtions those Non-Dev who are still in requires
self.options.filter_used(self.requires.pkg_names)
result.append(self.options.sha)
requires_sha = self.requires.sha
result.append(requires_sha)
package_id = sha1('\n'.join(result).encode())
return package_id
def serialize_min(self):
"""
This info will be shown in search results.
"""
conan_info_json = {"settings": dict(self.settings.serialize()),
"options": dict(self.options.serialize()["options"]),
"full_requires": self.full_requires.serialize(),
"recipe_hash": self.recipe_hash}
return conan_info_json
def header_only(self):
self.settings.clear()
self.options.clear()
self.requires.clear()
def vs_toolset_compatible(self):
"""Default behaviour, same package for toolset v140 with compiler=Visual Studio 15 than
using Visual Studio 14"""
if self.full_settings.compiler != "Visual Studio":
return
toolset = str(self.full_settings.compiler.toolset)
version = MSVS_DEFAULT_TOOLSETS_INVERSE.get(toolset)
if version is not None:
self.settings.compiler.version = version
del self.settings.compiler.toolset
def vs_toolset_incompatible(self):
"""Will generate different packages for v140 and visual 15 than the visual 14"""
if self.full_settings.compiler != "Visual Studio":
return
self.settings.compiler.version = self.full_settings.compiler.version
self.settings.compiler.toolset = self.full_settings.compiler.toolset
def discard_build_settings(self):
# When os is defined, os_build is irrelevant for the consumer.
# only when os_build is alone (installers, etc) it has to be present in the package_id
if self.full_settings.os and self.full_settings.os_build:
del self.settings.os_build
if self.full_settings.arch and self.full_settings.arch_build:
del self.settings.arch_build
def include_build_settings(self):
self.settings.os_build = self.full_settings.os_build
self.settings.arch_build = self.full_settings.arch_build
def default_std_matching(self):
"""
If we are building with gcc 7, and we specify -s cppstd=gnu14, it's the default, so the
same as specifying None, packages are the same
"""
if self.full_settings.compiler and \
self.full_settings.compiler.version and \
self.full_settings.compiler.cppstd:
default = cppstd_default(str(self.full_settings.compiler),
str(self.full_settings.compiler.version))
if default == str(self.full_settings.compiler.cppstd):
self.settings.compiler.cppstd = None
self.settings.cppstd = None
def default_std_non_matching(self):
if self.full_settings.compiler.cppstd:
self.settings.compiler.cppstd = self.full_settings.compiler.cppstd
self.settings.cppstd = self.full_settings.compiler.cppstd