Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # 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 | |
| 6 | LICENSE_DIRECTORY ??= "${DEPLOY_DIR}/licenses" |
| 7 | LICSSTATEDIR = "${WORKDIR}/license-destdir/" |
| 8 | |
| 9 | # Create extra package with license texts and add it to RRECOMMENDS_${PN} |
| 10 | LICENSE_CREATE_PACKAGE[type] = "boolean" |
| 11 | LICENSE_CREATE_PACKAGE ??= "0" |
| 12 | LICENSE_PACKAGE_SUFFIX ??= "-lic" |
| 13 | LICENSE_FILES_DIRECTORY ??= "${datadir}/licenses/" |
| 14 | |
| 15 | addtask populate_lic after do_patch before do_build |
| 16 | do_populate_lic[dirs] = "${LICSSTATEDIR}/${PN}" |
| 17 | do_populate_lic[cleandirs] = "${LICSSTATEDIR}" |
| 18 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 19 | python 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 26 | destdir = os.path.join(d.getVar('LICSSTATEDIR'), d.getVar('PN')) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 27 | copy_license_files(lic_files_paths, destdir) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 28 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 32 | } |
| 33 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 34 | PSEUDO_IGNORE_PATHS .= ",${@','.join(((d.getVar('COMMON_LICENSE_DIR') or '') + ' ' + (d.getVar('LICENSE_PATH') or '')).split())}" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 35 | # it would be better to copy them in do_install_append, but find_license_filesa is python |
| 36 | python perform_packagecopy_prepend () { |
| 37 | enabled = oe.data.typed_value('LICENSE_CREATE_PACKAGE', d) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 38 | if d.getVar('CLASSOVERRIDE') == 'class-target' and enabled: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 39 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 42 | destdir = d.getVar('D') + os.path.join(d.getVar('LICENSE_FILES_DIRECTORY'), d.getVar('PN')) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 43 | copy_license_files(lic_files_paths, destdir) |
| 44 | add_package_and_files(d) |
| 45 | } |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 46 | perform_packagecopy[vardeps] += "LICENSE_CREATE_PACKAGE" |
| 47 | |
| 48 | def get_recipe_info(d): |
| 49 | info = {} |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 50 | info["PV"] = d.getVar("PV") |
| 51 | info["PR"] = d.getVar("PR") |
| 52 | info["LICENSE"] = d.getVar("LICENSE") |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 53 | return info |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 54 | |
| 55 | def add_package_and_files(d): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 56 | packages = d.getVar('PACKAGES') |
| 57 | files = d.getVar('LICENSE_FILES_DIRECTORY') |
| 58 | pn = d.getVar('PN') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 59 | pn_lic = "%s%s" % (pn, d.getVar('LICENSE_PACKAGE_SUFFIX', False)) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 60 | if pn_lic in packages.split(): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 61 | 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)) |
| 65 | d.setVar('FILES_' + pn_lic, files) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 66 | for pn in packages.split(): |
| 67 | if pn == pn_lic: |
| 68 | continue |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 69 | rrecommends_pn = d.getVar('RRECOMMENDS_' + pn) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 70 | if rrecommends_pn: |
| 71 | d.setVar('RRECOMMENDS_' + pn, "%s %s" % (pn_lic, rrecommends_pn)) |
| 72 | else: |
| 73 | d.setVar('RRECOMMENDS_' + pn, "%s" % (pn_lic)) |
| 74 | |
| 75 | def copy_license_files(lic_files_paths, destdir): |
| 76 | import shutil |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 77 | import errno |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 78 | |
| 79 | bb.utils.mkdirhier(destdir) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 80 | for (basename, path, beginline, endline) in lic_files_paths: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 81 | try: |
| 82 | src = path |
| 83 | dst = os.path.join(destdir, basename) |
| 84 | if os.path.exists(dst): |
| 85 | os.remove(dst) |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 86 | if os.path.islink(src): |
| 87 | src = os.path.realpath(src) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 88 | 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 89 | if canlink: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 90 | try: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 91 | os.link(src, dst) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 92 | except OSError as err: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 93 | if err.errno == errno.EXDEV: |
| 94 | # Copy license files if hard-link is not possible even if st_dev is the |
| 95 | # same on source and destination (docker container with device-mapper?) |
| 96 | canlink = False |
| 97 | else: |
| 98 | raise |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 99 | # Only chown if we did hardling, and, we're running under pseudo |
| 100 | if canlink and os.environ.get('PSEUDO_DISABLED') == '0': |
| 101 | os.chown(dst,0,0) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 102 | if not canlink: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 103 | begin_idx = int(beginline)-1 if beginline is not None else None |
| 104 | end_idx = int(endline) if endline is not None else None |
| 105 | if begin_idx is None and end_idx is None: |
| 106 | shutil.copyfile(src, dst) |
| 107 | else: |
| 108 | with open(src, 'rb') as src_f: |
| 109 | with open(dst, 'wb') as dst_f: |
| 110 | dst_f.write(b''.join(src_f.readlines()[begin_idx:end_idx])) |
| 111 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 112 | except Exception as e: |
| 113 | bb.warn("Could not copy license file %s to %s: %s" % (src, dst, e)) |
| 114 | |
| 115 | def find_license_files(d): |
| 116 | """ |
| 117 | Creates list of files used in LIC_FILES_CHKSUM and generic LICENSE files. |
| 118 | """ |
| 119 | import shutil |
| 120 | import oe.license |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 121 | from collections import defaultdict, OrderedDict |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 122 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 123 | # All the license files for the package |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 124 | lic_files = d.getVar('LIC_FILES_CHKSUM') or "" |
| 125 | pn = d.getVar('PN') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 126 | # The license files are located in S/LIC_FILE_CHECKSUM. |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 127 | srcdir = d.getVar('S') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 128 | # Directory we store the generic licenses as set in the distro configuration |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 129 | generic_directory = d.getVar('COMMON_LICENSE_DIR') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 130 | # List of basename, path tuples |
| 131 | lic_files_paths = [] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 132 | # hash for keep track generic lics mappings |
| 133 | non_generic_lics = {} |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 134 | # Entries from LIC_FILES_CHKSUM |
| 135 | lic_chksums = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 136 | license_source_dirs = [] |
| 137 | license_source_dirs.append(generic_directory) |
| 138 | try: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 139 | additional_lic_dirs = d.getVar('LICENSE_PATH').split() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 140 | for lic_dir in additional_lic_dirs: |
| 141 | license_source_dirs.append(lic_dir) |
| 142 | except: |
| 143 | pass |
| 144 | |
| 145 | class FindVisitor(oe.license.LicenseVisitor): |
| 146 | def visit_Str(self, node): |
| 147 | # |
| 148 | # Until I figure out what to do with |
| 149 | # the two modifiers I support (or greater = + |
| 150 | # and "with exceptions" being * |
| 151 | # we'll just strip out the modifier and put |
| 152 | # the base license. |
| 153 | find_license(node.s.replace("+", "").replace("*", "")) |
| 154 | self.generic_visit(node) |
| 155 | |
| 156 | def find_license(license_type): |
| 157 | try: |
| 158 | bb.utils.mkdirhier(gen_lic_dest) |
| 159 | except: |
| 160 | pass |
| 161 | spdx_generic = None |
| 162 | license_source = None |
| 163 | # If the generic does not exist we need to check to see if there is an SPDX mapping to it, |
| 164 | # unless NO_GENERIC_LICENSE is set. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 165 | for lic_dir in license_source_dirs: |
| 166 | if not os.path.isfile(os.path.join(lic_dir, license_type)): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 167 | if d.getVarFlag('SPDXLICENSEMAP', license_type) != None: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 168 | # Great, there is an SPDXLICENSEMAP. We can copy! |
| 169 | bb.debug(1, "We need to use a SPDXLICENSEMAP for %s" % (license_type)) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 170 | spdx_generic = d.getVarFlag('SPDXLICENSEMAP', license_type) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 171 | license_source = lic_dir |
| 172 | break |
| 173 | elif os.path.isfile(os.path.join(lic_dir, license_type)): |
| 174 | spdx_generic = license_type |
| 175 | license_source = lic_dir |
| 176 | break |
| 177 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 178 | non_generic_lic = d.getVarFlag('NO_GENERIC_LICENSE', license_type) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 179 | if spdx_generic and license_source: |
| 180 | # we really should copy to generic_ + spdx_generic, however, that ends up messing the manifest |
| 181 | # audit up. This should be fixed in emit_pkgdata (or, we actually got and fix all the recipes) |
| 182 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 183 | lic_files_paths.append(("generic_" + license_type, os.path.join(license_source, spdx_generic), |
| 184 | None, None)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 185 | |
| 186 | # The user may attempt to use NO_GENERIC_LICENSE for a generic license which doesn't make sense |
| 187 | # and should not be allowed, warn the user in this case. |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 188 | if d.getVarFlag('NO_GENERIC_LICENSE', license_type): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 189 | bb.warn("%s: %s is a generic license, please don't use NO_GENERIC_LICENSE for it." % (pn, license_type)) |
| 190 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 191 | elif non_generic_lic and non_generic_lic in lic_chksums: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 192 | # if NO_GENERIC_LICENSE is set, we copy the license files from the fetched source |
| 193 | # of the package rather than the license_source_dirs. |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 194 | lic_files_paths.append(("generic_" + license_type, |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 195 | os.path.join(srcdir, non_generic_lic), None, None)) |
| 196 | non_generic_lics[non_generic_lic] = license_type |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 197 | else: |
| 198 | # Add explicity avoid of CLOSED license because this isn't generic |
| 199 | if license_type != 'CLOSED': |
| 200 | # And here is where we warn people that their licenses are lousy |
| 201 | bb.warn("%s: No generic license file exists for: %s in any provider" % (pn, license_type)) |
| 202 | pass |
| 203 | |
| 204 | if not generic_directory: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 205 | bb.fatal("COMMON_LICENSE_DIR is unset. Please set this in your distro config") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 206 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 207 | for url in lic_files.split(): |
| 208 | try: |
Brad Bishop | 220d553 | 2018-08-14 00:59:39 +0100 | [diff] [blame] | 209 | (method, host, path, user, pswd, parm) = bb.fetch.decodeurl(url) |
| 210 | if method != "file" or not path: |
| 211 | raise bb.fetch.MalformedUrl() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 212 | except bb.fetch.MalformedUrl: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 213 | bb.fatal("%s: LIC_FILES_CHKSUM contains an invalid URL: %s" % (d.getVar('PF'), url)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 214 | # We want the license filename and path |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 215 | chksum = parm.get('md5', None) |
| 216 | beginline = parm.get('beginline') |
| 217 | endline = parm.get('endline') |
| 218 | lic_chksums[path] = (chksum, beginline, endline) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 219 | |
| 220 | v = FindVisitor() |
| 221 | try: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 222 | v.visit_string(d.getVar('LICENSE')) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 223 | except oe.license.InvalidLicense as exc: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 224 | bb.fatal('%s: %s' % (d.getVar('PF'), exc)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 225 | except SyntaxError: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 226 | bb.warn("%s: Failed to parse it's LICENSE field." % (d.getVar('PF'))) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 227 | # Add files from LIC_FILES_CHKSUM to list of license files |
| 228 | lic_chksum_paths = defaultdict(OrderedDict) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 229 | for path, data in sorted(lic_chksums.items()): |
| 230 | lic_chksum_paths[os.path.basename(path)][data] = (os.path.join(srcdir, path), data[1], data[2]) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 231 | for basename, files in lic_chksum_paths.items(): |
| 232 | if len(files) == 1: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 233 | # Don't copy again a LICENSE already handled as non-generic |
| 234 | if basename in non_generic_lics: |
| 235 | continue |
| 236 | data = list(files.values())[0] |
| 237 | lic_files_paths.append(tuple([basename] + list(data))) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 238 | else: |
| 239 | # If there are multiple different license files with identical |
| 240 | # basenames we rename them to <file>.0, <file>.1, ... |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 241 | for i, data in enumerate(files.values()): |
| 242 | lic_files_paths.append(tuple(["%s.%d" % (basename, i)] + list(data))) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 243 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 244 | return lic_files_paths |
| 245 | |
| 246 | def return_spdx(d, license): |
| 247 | """ |
| 248 | This function returns the spdx mapping of a license if it exists. |
| 249 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 250 | return d.getVarFlag('SPDXLICENSEMAP', license) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 251 | |
| 252 | def canonical_license(d, license): |
| 253 | """ |
| 254 | Return the canonical (SPDX) form of the license if available (so GPLv3 |
| 255 | becomes GPL-3.0), for the license named 'X+', return canonical form of |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 256 | 'X' if available and the tailing '+' (so GPLv3+ becomes GPL-3.0+), |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 257 | or the passed license if there is no canonical form. |
| 258 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 259 | lic = d.getVarFlag('SPDXLICENSEMAP', license) or "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 260 | if not lic and license.endswith('+'): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 261 | lic = d.getVarFlag('SPDXLICENSEMAP', license.rstrip('+')) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 262 | if lic: |
| 263 | lic += '+' |
| 264 | return lic or license |
| 265 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 266 | def available_licenses(d): |
| 267 | """ |
| 268 | Return the available licenses by searching the directories specified by |
| 269 | COMMON_LICENSE_DIR and LICENSE_PATH. |
| 270 | """ |
| 271 | lic_dirs = ((d.getVar('COMMON_LICENSE_DIR') or '') + ' ' + |
| 272 | (d.getVar('LICENSE_PATH') or '')).split() |
| 273 | |
| 274 | licenses = [] |
| 275 | for lic_dir in lic_dirs: |
| 276 | licenses += os.listdir(lic_dir) |
| 277 | |
| 278 | licenses = sorted(licenses) |
| 279 | return licenses |
| 280 | |
| 281 | # Only determine the list of all available licenses once. This assumes that any |
| 282 | # additions to LICENSE_PATH have been done before this file is parsed. |
| 283 | AVAILABLE_LICENSES := "${@' '.join(available_licenses(d))}" |
| 284 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 285 | def expand_wildcard_licenses(d, wildcard_licenses): |
| 286 | """ |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 287 | Return actual spdx format license names if wildcards are used. We expand |
| 288 | wildcards from SPDXLICENSEMAP flags and AVAILABLE_LICENSES. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 289 | """ |
| 290 | import fnmatch |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 291 | licenses = wildcard_licenses[:] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 292 | spdxmapkeys = d.getVarFlags('SPDXLICENSEMAP').keys() |
| 293 | for wld_lic in wildcard_licenses: |
| 294 | spdxflags = fnmatch.filter(spdxmapkeys, wld_lic) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 295 | licenses += [d.getVarFlag('SPDXLICENSEMAP', flag) for flag in spdxflags] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 296 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 297 | spdx_lics = d.getVar('AVAILABLE_LICENSES').split() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 298 | for wld_lic in wildcard_licenses: |
| 299 | licenses += fnmatch.filter(spdx_lics, wld_lic) |
| 300 | |
| 301 | licenses = list(set(licenses)) |
| 302 | return licenses |
| 303 | |
| 304 | def incompatible_license_contains(license, truevalue, falsevalue, d): |
| 305 | license = canonical_license(d, license) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 306 | bad_licenses = (d.getVar('INCOMPATIBLE_LICENSE') or "").split() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 307 | bad_licenses = expand_wildcard_licenses(d, bad_licenses) |
| 308 | return truevalue if license in bad_licenses else falsevalue |
| 309 | |
Brad Bishop | f3f93bb | 2019-10-16 14:33:32 -0400 | [diff] [blame] | 310 | def incompatible_pkg_license(d, dont_want_licenses, license): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 311 | # Handles an "or" or two license sets provided by |
| 312 | # flattened_licenses(), pick one that works if possible. |
| 313 | def choose_lic_set(a, b): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 314 | return a if all(oe.license.license_ok(canonical_license(d, lic), |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 315 | dont_want_licenses) for lic in a) else b |
| 316 | |
| 317 | try: |
| 318 | licenses = oe.license.flattened_licenses(license, choose_lic_set) |
| 319 | except oe.license.LicenseError as exc: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 320 | bb.fatal('%s: %s' % (d.getVar('P'), exc)) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 321 | |
| 322 | incompatible_lic = [] |
| 323 | for l in licenses: |
| 324 | license = canonical_license(d, l) |
| 325 | if not oe.license.license_ok(license, dont_want_licenses): |
| 326 | incompatible_lic.append(license) |
| 327 | |
| 328 | return sorted(incompatible_lic) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 329 | |
Brad Bishop | f3f93bb | 2019-10-16 14:33:32 -0400 | [diff] [blame] | 330 | def incompatible_license(d, dont_want_licenses, package=None): |
| 331 | """ |
| 332 | This function checks if a recipe has only incompatible licenses. It also |
| 333 | take into consideration 'or' operand. dont_want_licenses should be passed |
| 334 | as canonical (SPDX) names. |
| 335 | """ |
| 336 | import oe.license |
| 337 | license = d.getVar("LICENSE_%s" % package) if package else None |
| 338 | if not license: |
| 339 | license = d.getVar('LICENSE') |
| 340 | |
| 341 | return incompatible_pkg_license(d, dont_want_licenses, license) |
| 342 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 343 | def check_license_flags(d): |
| 344 | """ |
| 345 | This function checks if a recipe has any LICENSE_FLAGS that |
| 346 | aren't whitelisted. |
| 347 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 348 | If it does, it returns the all LICENSE_FLAGS missing from the whitelist, or |
| 349 | all of the LICENSE_FLAGS if there is no whitelist. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 350 | |
| 351 | If everything is is properly whitelisted, it returns None. |
| 352 | """ |
| 353 | |
| 354 | def license_flag_matches(flag, whitelist, pn): |
| 355 | """ |
| 356 | Return True if flag matches something in whitelist, None if not. |
| 357 | |
| 358 | Before we test a flag against the whitelist, we append _${PN} |
| 359 | to it. We then try to match that string against the |
| 360 | whitelist. This covers the normal case, where we expect |
| 361 | LICENSE_FLAGS to be a simple string like 'commercial', which |
| 362 | the user typically matches exactly in the whitelist by |
| 363 | explicitly appending the package name e.g 'commercial_foo'. |
| 364 | If we fail the match however, we then split the flag across |
| 365 | '_' and append each fragment and test until we either match or |
| 366 | run out of fragments. |
| 367 | """ |
| 368 | flag_pn = ("%s_%s" % (flag, pn)) |
| 369 | for candidate in whitelist: |
| 370 | if flag_pn == candidate: |
| 371 | return True |
| 372 | |
| 373 | flag_cur = "" |
| 374 | flagments = flag_pn.split("_") |
| 375 | flagments.pop() # we've already tested the full string |
| 376 | for flagment in flagments: |
| 377 | if flag_cur: |
| 378 | flag_cur += "_" |
| 379 | flag_cur += flagment |
| 380 | for candidate in whitelist: |
| 381 | if flag_cur == candidate: |
| 382 | return True |
| 383 | return False |
| 384 | |
| 385 | def all_license_flags_match(license_flags, whitelist): |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 386 | """ Return all unmatched flags, None if all flags match """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 387 | pn = d.getVar('PN') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 388 | split_whitelist = whitelist.split() |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 389 | flags = [] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 390 | for flag in license_flags.split(): |
| 391 | if not license_flag_matches(flag, split_whitelist, pn): |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 392 | flags.append(flag) |
| 393 | return flags if flags else None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 394 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 395 | license_flags = d.getVar('LICENSE_FLAGS') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 396 | if license_flags: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 397 | whitelist = d.getVar('LICENSE_FLAGS_WHITELIST') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 398 | if not whitelist: |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 399 | return license_flags.split() |
| 400 | unmatched_flags = all_license_flags_match(license_flags, whitelist) |
| 401 | if unmatched_flags: |
| 402 | return unmatched_flags |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 403 | return None |
| 404 | |
| 405 | def check_license_format(d): |
| 406 | """ |
| 407 | This function checks if LICENSE is well defined, |
| 408 | Validate operators in LICENSES. |
| 409 | No spaces are allowed between LICENSES. |
| 410 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 411 | pn = d.getVar('PN') |
| 412 | licenses = d.getVar('LICENSE') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 413 | from oe.license import license_operator, license_operator_chars, license_pattern |
| 414 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 415 | elements = list(filter(lambda x: x.strip(), license_operator.split(licenses))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 416 | for pos, element in enumerate(elements): |
| 417 | if license_pattern.match(element): |
| 418 | if pos > 0 and license_pattern.match(elements[pos - 1]): |
| 419 | bb.warn('%s: LICENSE value "%s" has an invalid format - license names ' \ |
| 420 | 'must be separated by the following characters to indicate ' \ |
| 421 | 'the license selection: %s' % |
| 422 | (pn, licenses, license_operator_chars)) |
| 423 | elif not license_operator.match(element): |
| 424 | bb.warn('%s: LICENSE value "%s" has an invalid separator "%s" that is not ' \ |
| 425 | 'in the valid list of separators (%s)' % |
| 426 | (pn, licenses, element, license_operator_chars)) |
| 427 | |
| 428 | SSTATETASKS += "do_populate_lic" |
| 429 | do_populate_lic[sstate-inputdirs] = "${LICSSTATEDIR}" |
| 430 | do_populate_lic[sstate-outputdirs] = "${LICENSE_DIRECTORY}/" |
| 431 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 432 | IMAGE_CLASSES_append = " license_image" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 433 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 434 | python do_populate_lic_setscene () { |
| 435 | sstate_setscene(d) |
| 436 | } |
| 437 | addtask do_populate_lic_setscene |