blob: 45d912741d27db4edd664624d2879b0e4759ef6e [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# Populates LICENSE_DIRECTORY as set in distro config with the license files as set by
2# LIC_FILES_CHKSUM.
3# TODO:
4# - There is a real issue revolving around license naming standards.
5
6LICENSE_DIRECTORY ??= "${DEPLOY_DIR}/licenses"
7LICSSTATEDIR = "${WORKDIR}/license-destdir/"
8
Patrick Williams213cb262021-08-07 19:21:33 -05009# Create extra package with license texts and add it to RRECOMMENDS:${PN}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050010LICENSE_CREATE_PACKAGE[type] = "boolean"
11LICENSE_CREATE_PACKAGE ??= "0"
12LICENSE_PACKAGE_SUFFIX ??= "-lic"
13LICENSE_FILES_DIRECTORY ??= "${datadir}/licenses/"
14
15addtask populate_lic after do_patch before do_build
16do_populate_lic[dirs] = "${LICSSTATEDIR}/${PN}"
17do_populate_lic[cleandirs] = "${LICSSTATEDIR}"
18
Patrick Williamsc124f4f2015-09-15 14:41:29 -050019python do_populate_lic() {
20 """
21 Populate LICENSE_DIRECTORY with licenses.
22 """
23 lic_files_paths = find_license_files(d)
24
25 # The base directory we wrangle licenses to
Brad Bishop6e60e8b2018-02-01 10:27:11 -050026 destdir = os.path.join(d.getVar('LICSSTATEDIR'), d.getVar('PN'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027 copy_license_files(lic_files_paths, destdir)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050028 info = get_recipe_info(d)
29 with open(os.path.join(destdir, "recipeinfo"), "w") as f:
30 for key in sorted(info.keys()):
31 f.write("%s: %s\n" % (key, info[key]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050032}
33
Patrick Williams213cb262021-08-07 19:21:33 -050034PSEUDO_IGNORE_PATHS .= ",${@','.join(((d.getVar('COMMON_LICENSE_DIR') or '') + ' ' + (d.getVar('LICENSE_PATH') or '') + ' ' + d.getVar('COREBASE') + '/meta/COPYING').split())}"
35# it would be better to copy them in do_install:append, but find_license_filesa is python
36python perform_packagecopy:prepend () {
Patrick Williamsc124f4f2015-09-15 14:41:29 -050037 enabled = oe.data.typed_value('LICENSE_CREATE_PACKAGE', d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050038 if d.getVar('CLASSOVERRIDE') == 'class-target' and enabled:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039 lic_files_paths = find_license_files(d)
40
41 # LICENSE_FILES_DIRECTORY starts with '/' so os.path.join cannot be used to join D and LICENSE_FILES_DIRECTORY
Brad Bishop6e60e8b2018-02-01 10:27:11 -050042 destdir = d.getVar('D') + os.path.join(d.getVar('LICENSE_FILES_DIRECTORY'), d.getVar('PN'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050043 copy_license_files(lic_files_paths, destdir)
44 add_package_and_files(d)
45}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050046perform_packagecopy[vardeps] += "LICENSE_CREATE_PACKAGE"
47
48def get_recipe_info(d):
49 info = {}
Brad Bishop6e60e8b2018-02-01 10:27:11 -050050 info["PV"] = d.getVar("PV")
51 info["PR"] = d.getVar("PR")
52 info["LICENSE"] = d.getVar("LICENSE")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050053 return info
Patrick Williamsc124f4f2015-09-15 14:41:29 -050054
55def add_package_and_files(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050056 packages = d.getVar('PACKAGES')
57 files = d.getVar('LICENSE_FILES_DIRECTORY')
58 pn = d.getVar('PN')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059 pn_lic = "%s%s" % (pn, d.getVar('LICENSE_PACKAGE_SUFFIX', False))
Brad Bishop316dfdd2018-06-25 12:45:53 -040060 if pn_lic in packages.split():
Patrick Williamsc124f4f2015-09-15 14:41:29 -050061 bb.warn("%s package already existed in %s." % (pn_lic, pn))
62 else:
63 # first in PACKAGES to be sure that nothing else gets LICENSE_FILES_DIRECTORY
64 d.setVar('PACKAGES', "%s %s" % (pn_lic, packages))
Patrick Williams213cb262021-08-07 19:21:33 -050065 d.setVar('FILES:' + pn_lic, files)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050066
67def copy_license_files(lic_files_paths, destdir):
68 import shutil
Patrick Williamsc0f7c042017-02-23 20:41:17 -060069 import errno
Patrick Williamsc124f4f2015-09-15 14:41:29 -050070
71 bb.utils.mkdirhier(destdir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050072 for (basename, path, beginline, endline) in lic_files_paths:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073 try:
74 src = path
75 dst = os.path.join(destdir, basename)
76 if os.path.exists(dst):
77 os.remove(dst)
Brad Bishop37a0e4d2017-12-04 01:01:44 -050078 if os.path.islink(src):
79 src = os.path.realpath(src)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050080 canlink = os.access(src, os.W_OK) and (os.stat(src).st_dev == os.stat(destdir).st_dev) and beginline is None and endline is None
Patrick Williamsc0f7c042017-02-23 20:41:17 -060081 if canlink:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050082 try:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060083 os.link(src, dst)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050084 except OSError as err:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060085 if err.errno == errno.EXDEV:
86 # Copy license files if hard-link is not possible even if st_dev is the
87 # same on source and destination (docker container with device-mapper?)
88 canlink = False
89 else:
90 raise
Brad Bishop6e60e8b2018-02-01 10:27:11 -050091 # Only chown if we did hardling, and, we're running under pseudo
92 if canlink and os.environ.get('PSEUDO_DISABLED') == '0':
93 os.chown(dst,0,0)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060094 if not canlink:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050095 begin_idx = int(beginline)-1 if beginline is not None else None
96 end_idx = int(endline) if endline is not None else None
97 if begin_idx is None and end_idx is None:
98 shutil.copyfile(src, dst)
99 else:
100 with open(src, 'rb') as src_f:
101 with open(dst, 'wb') as dst_f:
102 dst_f.write(b''.join(src_f.readlines()[begin_idx:end_idx]))
103
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104 except Exception as e:
105 bb.warn("Could not copy license file %s to %s: %s" % (src, dst, e))
106
107def find_license_files(d):
108 """
109 Creates list of files used in LIC_FILES_CHKSUM and generic LICENSE files.
110 """
111 import shutil
112 import oe.license
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600113 from collections import defaultdict, OrderedDict
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500114
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500115 # All the license files for the package
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500116 lic_files = d.getVar('LIC_FILES_CHKSUM') or ""
117 pn = d.getVar('PN')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500118 # The license files are located in S/LIC_FILE_CHECKSUM.
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500119 srcdir = d.getVar('S')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500120 # Directory we store the generic licenses as set in the distro configuration
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500121 generic_directory = d.getVar('COMMON_LICENSE_DIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500122 # List of basename, path tuples
123 lic_files_paths = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500124 # hash for keep track generic lics mappings
125 non_generic_lics = {}
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600126 # Entries from LIC_FILES_CHKSUM
127 lic_chksums = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500128 license_source_dirs = []
129 license_source_dirs.append(generic_directory)
130 try:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500131 additional_lic_dirs = d.getVar('LICENSE_PATH').split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500132 for lic_dir in additional_lic_dirs:
133 license_source_dirs.append(lic_dir)
134 except:
135 pass
136
137 class FindVisitor(oe.license.LicenseVisitor):
138 def visit_Str(self, node):
139 #
140 # Until I figure out what to do with
141 # the two modifiers I support (or greater = +
142 # and "with exceptions" being *
143 # we'll just strip out the modifier and put
144 # the base license.
145 find_license(node.s.replace("+", "").replace("*", ""))
146 self.generic_visit(node)
147
148 def find_license(license_type):
149 try:
150 bb.utils.mkdirhier(gen_lic_dest)
151 except:
152 pass
153 spdx_generic = None
154 license_source = None
155 # If the generic does not exist we need to check to see if there is an SPDX mapping to it,
156 # unless NO_GENERIC_LICENSE is set.
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500157 for lic_dir in license_source_dirs:
158 if not os.path.isfile(os.path.join(lic_dir, license_type)):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500159 if d.getVarFlag('SPDXLICENSEMAP', license_type) != None:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500160 # Great, there is an SPDXLICENSEMAP. We can copy!
161 bb.debug(1, "We need to use a SPDXLICENSEMAP for %s" % (license_type))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500162 spdx_generic = d.getVarFlag('SPDXLICENSEMAP', license_type)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500163 license_source = lic_dir
164 break
165 elif os.path.isfile(os.path.join(lic_dir, license_type)):
166 spdx_generic = license_type
167 license_source = lic_dir
168 break
169
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500170 non_generic_lic = d.getVarFlag('NO_GENERIC_LICENSE', license_type)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500171 if spdx_generic and license_source:
172 # we really should copy to generic_ + spdx_generic, however, that ends up messing the manifest
173 # audit up. This should be fixed in emit_pkgdata (or, we actually got and fix all the recipes)
174
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500175 lic_files_paths.append(("generic_" + license_type, os.path.join(license_source, spdx_generic),
176 None, None))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500177
178 # The user may attempt to use NO_GENERIC_LICENSE for a generic license which doesn't make sense
179 # and should not be allowed, warn the user in this case.
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500180 if d.getVarFlag('NO_GENERIC_LICENSE', license_type):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500181 bb.warn("%s: %s is a generic license, please don't use NO_GENERIC_LICENSE for it." % (pn, license_type))
182
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600183 elif non_generic_lic and non_generic_lic in lic_chksums:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500184 # if NO_GENERIC_LICENSE is set, we copy the license files from the fetched source
185 # of the package rather than the license_source_dirs.
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600186 lic_files_paths.append(("generic_" + license_type,
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500187 os.path.join(srcdir, non_generic_lic), None, None))
188 non_generic_lics[non_generic_lic] = license_type
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500189 else:
190 # Add explicity avoid of CLOSED license because this isn't generic
191 if license_type != 'CLOSED':
192 # And here is where we warn people that their licenses are lousy
193 bb.warn("%s: No generic license file exists for: %s in any provider" % (pn, license_type))
194 pass
195
196 if not generic_directory:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600197 bb.fatal("COMMON_LICENSE_DIR is unset. Please set this in your distro config")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500198
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500199 for url in lic_files.split():
200 try:
Brad Bishop220d5532018-08-14 00:59:39 +0100201 (method, host, path, user, pswd, parm) = bb.fetch.decodeurl(url)
202 if method != "file" or not path:
203 raise bb.fetch.MalformedUrl()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500204 except bb.fetch.MalformedUrl:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500205 bb.fatal("%s: LIC_FILES_CHKSUM contains an invalid URL: %s" % (d.getVar('PF'), url))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500206 # We want the license filename and path
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500207 chksum = parm.get('md5', None)
208 beginline = parm.get('beginline')
209 endline = parm.get('endline')
210 lic_chksums[path] = (chksum, beginline, endline)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500211
212 v = FindVisitor()
213 try:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500214 v.visit_string(d.getVar('LICENSE'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500215 except oe.license.InvalidLicense as exc:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500216 bb.fatal('%s: %s' % (d.getVar('PF'), exc))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500217 except SyntaxError:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500218 bb.warn("%s: Failed to parse it's LICENSE field." % (d.getVar('PF')))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600219 # Add files from LIC_FILES_CHKSUM to list of license files
220 lic_chksum_paths = defaultdict(OrderedDict)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500221 for path, data in sorted(lic_chksums.items()):
222 lic_chksum_paths[os.path.basename(path)][data] = (os.path.join(srcdir, path), data[1], data[2])
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600223 for basename, files in lic_chksum_paths.items():
224 if len(files) == 1:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500225 # Don't copy again a LICENSE already handled as non-generic
226 if basename in non_generic_lics:
227 continue
228 data = list(files.values())[0]
229 lic_files_paths.append(tuple([basename] + list(data)))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600230 else:
231 # If there are multiple different license files with identical
232 # basenames we rename them to <file>.0, <file>.1, ...
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500233 for i, data in enumerate(files.values()):
234 lic_files_paths.append(tuple(["%s.%d" % (basename, i)] + list(data)))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600235
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500236 return lic_files_paths
237
238def return_spdx(d, license):
239 """
240 This function returns the spdx mapping of a license if it exists.
241 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500242 return d.getVarFlag('SPDXLICENSEMAP', license)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500243
244def canonical_license(d, license):
245 """
246 Return the canonical (SPDX) form of the license if available (so GPLv3
Andrew Geissler90fd73c2021-03-05 15:25:55 -0600247 becomes GPL-3.0) or the passed license if there is no canonical form.
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500248 """
Andrew Geissler90fd73c2021-03-05 15:25:55 -0600249 return d.getVarFlag('SPDXLICENSEMAP', license) or license
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500250
Andrew Geissler82c905d2020-04-13 13:39:40 -0500251def available_licenses(d):
252 """
253 Return the available licenses by searching the directories specified by
254 COMMON_LICENSE_DIR and LICENSE_PATH.
255 """
256 lic_dirs = ((d.getVar('COMMON_LICENSE_DIR') or '') + ' ' +
257 (d.getVar('LICENSE_PATH') or '')).split()
258
259 licenses = []
260 for lic_dir in lic_dirs:
261 licenses += os.listdir(lic_dir)
262
263 licenses = sorted(licenses)
264 return licenses
265
266# Only determine the list of all available licenses once. This assumes that any
267# additions to LICENSE_PATH have been done before this file is parsed.
268AVAILABLE_LICENSES := "${@' '.join(available_licenses(d))}"
269
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500270def expand_wildcard_licenses(d, wildcard_licenses):
271 """
Andrew Geissler82c905d2020-04-13 13:39:40 -0500272 Return actual spdx format license names if wildcards are used. We expand
273 wildcards from SPDXLICENSEMAP flags and AVAILABLE_LICENSES.
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500274 """
275 import fnmatch
Andrew Geissler90fd73c2021-03-05 15:25:55 -0600276
Brad Bishop15ae2502019-06-18 21:44:24 -0400277 licenses = wildcard_licenses[:]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500278 spdxmapkeys = d.getVarFlags('SPDXLICENSEMAP').keys()
279 for wld_lic in wildcard_licenses:
280 spdxflags = fnmatch.filter(spdxmapkeys, wld_lic)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500281 licenses += [d.getVarFlag('SPDXLICENSEMAP', flag) for flag in spdxflags]
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500282 # Assume if we're passed "GPLv3" or "*GPLv3" it means -or-later as well
283 if not wld_lic.endswith(("-or-later", "-only", "*", "+")):
284 spdxflags = fnmatch.filter(spdxmapkeys, wld_lic + "+")
285 licenses += [d.getVarFlag('SPDXLICENSEMAP', flag) for flag in spdxflags]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500286
Andrew Geissler82c905d2020-04-13 13:39:40 -0500287 spdx_lics = d.getVar('AVAILABLE_LICENSES').split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500288 for wld_lic in wildcard_licenses:
289 licenses += fnmatch.filter(spdx_lics, wld_lic)
290
291 licenses = list(set(licenses))
292 return licenses
293
294def incompatible_license_contains(license, truevalue, falsevalue, d):
295 license = canonical_license(d, license)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500296 bad_licenses = (d.getVar('INCOMPATIBLE_LICENSE') or "").split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500297 bad_licenses = expand_wildcard_licenses(d, bad_licenses)
298 return truevalue if license in bad_licenses else falsevalue
299
Brad Bishopf3f93bb2019-10-16 14:33:32 -0400300def incompatible_pkg_license(d, dont_want_licenses, license):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500301 # Handles an "or" or two license sets provided by
302 # flattened_licenses(), pick one that works if possible.
303 def choose_lic_set(a, b):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500304 return a if all(oe.license.license_ok(canonical_license(d, lic),
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500305 dont_want_licenses) for lic in a) else b
306
307 try:
308 licenses = oe.license.flattened_licenses(license, choose_lic_set)
309 except oe.license.LicenseError as exc:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500310 bb.fatal('%s: %s' % (d.getVar('P'), exc))
Andrew Geissler82c905d2020-04-13 13:39:40 -0500311
312 incompatible_lic = []
313 for l in licenses:
314 license = canonical_license(d, l)
315 if not oe.license.license_ok(license, dont_want_licenses):
316 incompatible_lic.append(license)
317
318 return sorted(incompatible_lic)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500319
Brad Bishopf3f93bb2019-10-16 14:33:32 -0400320def incompatible_license(d, dont_want_licenses, package=None):
321 """
322 This function checks if a recipe has only incompatible licenses. It also
323 take into consideration 'or' operand. dont_want_licenses should be passed
324 as canonical (SPDX) names.
325 """
326 import oe.license
Patrick Williams213cb262021-08-07 19:21:33 -0500327 license = d.getVar("LICENSE:%s" % package) if package else None
Brad Bishopf3f93bb2019-10-16 14:33:32 -0400328 if not license:
329 license = d.getVar('LICENSE')
330
331 return incompatible_pkg_license(d, dont_want_licenses, license)
332
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500333def check_license_flags(d):
334 """
335 This function checks if a recipe has any LICENSE_FLAGS that
336 aren't whitelisted.
337
Brad Bishop19323692019-04-05 15:28:33 -0400338 If it does, it returns the all LICENSE_FLAGS missing from the whitelist, or
339 all of the LICENSE_FLAGS if there is no whitelist.
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500340
341 If everything is is properly whitelisted, it returns None.
342 """
343
344 def license_flag_matches(flag, whitelist, pn):
345 """
346 Return True if flag matches something in whitelist, None if not.
347
348 Before we test a flag against the whitelist, we append _${PN}
349 to it. We then try to match that string against the
350 whitelist. This covers the normal case, where we expect
351 LICENSE_FLAGS to be a simple string like 'commercial', which
352 the user typically matches exactly in the whitelist by
353 explicitly appending the package name e.g 'commercial_foo'.
354 If we fail the match however, we then split the flag across
355 '_' and append each fragment and test until we either match or
356 run out of fragments.
357 """
358 flag_pn = ("%s_%s" % (flag, pn))
359 for candidate in whitelist:
360 if flag_pn == candidate:
361 return True
362
363 flag_cur = ""
364 flagments = flag_pn.split("_")
365 flagments.pop() # we've already tested the full string
366 for flagment in flagments:
367 if flag_cur:
368 flag_cur += "_"
369 flag_cur += flagment
370 for candidate in whitelist:
371 if flag_cur == candidate:
372 return True
373 return False
374
375 def all_license_flags_match(license_flags, whitelist):
Brad Bishop19323692019-04-05 15:28:33 -0400376 """ Return all unmatched flags, None if all flags match """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500377 pn = d.getVar('PN')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500378 split_whitelist = whitelist.split()
Brad Bishop19323692019-04-05 15:28:33 -0400379 flags = []
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500380 for flag in license_flags.split():
381 if not license_flag_matches(flag, split_whitelist, pn):
Brad Bishop19323692019-04-05 15:28:33 -0400382 flags.append(flag)
383 return flags if flags else None
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500384
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500385 license_flags = d.getVar('LICENSE_FLAGS')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500386 if license_flags:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500387 whitelist = d.getVar('LICENSE_FLAGS_WHITELIST')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500388 if not whitelist:
Brad Bishop19323692019-04-05 15:28:33 -0400389 return license_flags.split()
390 unmatched_flags = all_license_flags_match(license_flags, whitelist)
391 if unmatched_flags:
392 return unmatched_flags
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500393 return None
394
395def check_license_format(d):
396 """
397 This function checks if LICENSE is well defined,
398 Validate operators in LICENSES.
399 No spaces are allowed between LICENSES.
400 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500401 pn = d.getVar('PN')
402 licenses = d.getVar('LICENSE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500403 from oe.license import license_operator, license_operator_chars, license_pattern
404
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600405 elements = list(filter(lambda x: x.strip(), license_operator.split(licenses)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500406 for pos, element in enumerate(elements):
407 if license_pattern.match(element):
408 if pos > 0 and license_pattern.match(elements[pos - 1]):
409 bb.warn('%s: LICENSE value "%s" has an invalid format - license names ' \
410 'must be separated by the following characters to indicate ' \
411 'the license selection: %s' %
412 (pn, licenses, license_operator_chars))
413 elif not license_operator.match(element):
414 bb.warn('%s: LICENSE value "%s" has an invalid separator "%s" that is not ' \
415 'in the valid list of separators (%s)' %
416 (pn, licenses, element, license_operator_chars))
417
418SSTATETASKS += "do_populate_lic"
419do_populate_lic[sstate-inputdirs] = "${LICSSTATEDIR}"
420do_populate_lic[sstate-outputdirs] = "${LICENSE_DIRECTORY}/"
421
Patrick Williams213cb262021-08-07 19:21:33 -0500422IMAGE_CLASSES:append = " license_image"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500423
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500424python do_populate_lic_setscene () {
425 sstate_setscene(d)
426}
427addtask do_populate_lic_setscene