Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 1 | ROOTFS_LICENSE_DIR = "${IMAGE_ROOTFS}/usr/share/common-licenses" |
| 2 | |
Andrew Geissler | 5f35090 | 2021-07-23 13:09:54 -0400 | [diff] [blame] | 3 | # This requires LICENSE_CREATE_PACKAGE=1 to work too |
| 4 | COMPLEMENTARY_GLOB[lic-pkgs] = "*-lic" |
| 5 | |
| 6 | python() { |
| 7 | if not oe.data.typed_value('LICENSE_CREATE_PACKAGE', d): |
| 8 | features = set(oe.data.typed_value('IMAGE_FEATURES', d)) |
| 9 | if 'lic-pkgs' in features: |
| 10 | bb.error("'lic-pkgs' in IMAGE_FEATURES but LICENSE_CREATE_PACKAGE not enabled to generate -lic packages") |
| 11 | } |
| 12 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 13 | python write_package_manifest() { |
| 14 | # Get list of installed packages |
| 15 | license_image_dir = d.expand('${LICENSE_DIRECTORY}/${IMAGE_NAME}') |
| 16 | bb.utils.mkdirhier(license_image_dir) |
| 17 | from oe.rootfs import image_list_installed_packages |
| 18 | from oe.utils import format_pkg_list |
| 19 | |
| 20 | pkgs = image_list_installed_packages(d) |
| 21 | output = format_pkg_list(pkgs) |
| 22 | open(os.path.join(license_image_dir, 'package.manifest'), |
| 23 | 'w+').write(output) |
| 24 | } |
| 25 | |
| 26 | python license_create_manifest() { |
| 27 | import oe.packagedata |
| 28 | from oe.rootfs import image_list_installed_packages |
| 29 | |
| 30 | build_images_from_feeds = d.getVar('BUILD_IMAGES_FROM_FEEDS') |
| 31 | if build_images_from_feeds == "1": |
| 32 | return 0 |
| 33 | |
| 34 | pkg_dic = {} |
| 35 | for pkg in sorted(image_list_installed_packages(d)): |
| 36 | pkg_info = os.path.join(d.getVar('PKGDATA_DIR'), |
| 37 | 'runtime-reverse', pkg) |
| 38 | pkg_name = os.path.basename(os.readlink(pkg_info)) |
| 39 | |
| 40 | pkg_dic[pkg_name] = oe.packagedata.read_pkgdatafile(pkg_info) |
| 41 | if not "LICENSE" in pkg_dic[pkg_name].keys(): |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 42 | pkg_lic_name = "LICENSE:" + pkg_name |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 43 | pkg_dic[pkg_name]["LICENSE"] = pkg_dic[pkg_name][pkg_lic_name] |
| 44 | |
| 45 | rootfs_license_manifest = os.path.join(d.getVar('LICENSE_DIRECTORY'), |
| 46 | d.getVar('IMAGE_NAME'), 'license.manifest') |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 47 | write_license_files(d, rootfs_license_manifest, pkg_dic, rootfs=True) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 50 | def write_license_files(d, license_manifest, pkg_dic, rootfs=True): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 51 | import re |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 52 | import stat |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 53 | |
| 54 | bad_licenses = (d.getVar("INCOMPATIBLE_LICENSE") or "").split() |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 55 | bad_licenses = expand_wildcard_licenses(d, bad_licenses) |
| 56 | |
Brad Bishop | f3f93bb | 2019-10-16 14:33:32 -0400 | [diff] [blame] | 57 | whitelist = [] |
| 58 | for lic in bad_licenses: |
| 59 | whitelist.extend((d.getVar("WHITELIST_" + lic) or "").split()) |
| 60 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 61 | with open(license_manifest, "w") as license_file: |
| 62 | for pkg in sorted(pkg_dic): |
Brad Bishop | f3f93bb | 2019-10-16 14:33:32 -0400 | [diff] [blame] | 63 | if bad_licenses and pkg not in whitelist: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 64 | try: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 65 | licenses = incompatible_pkg_license(d, bad_licenses, pkg_dic[pkg]["LICENSE"]) |
| 66 | if licenses: |
| 67 | bb.fatal("Package %s cannot be installed into the image because it has incompatible license(s): %s" %(pkg, ' '.join(licenses))) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 68 | (pkg_dic[pkg]["LICENSE"], pkg_dic[pkg]["LICENSES"]) = \ |
| 69 | oe.license.manifest_licenses(pkg_dic[pkg]["LICENSE"], |
| 70 | bad_licenses, canonical_license, d) |
| 71 | except oe.license.LicenseError as exc: |
| 72 | bb.fatal('%s: %s' % (d.getVar('P'), exc)) |
| 73 | else: |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 74 | pkg_dic[pkg]["LICENSES"] = re.sub(r'[|&()*]', ' ', pkg_dic[pkg]["LICENSE"]) |
| 75 | pkg_dic[pkg]["LICENSES"] = re.sub(r' *', ' ', pkg_dic[pkg]["LICENSES"]) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 76 | pkg_dic[pkg]["LICENSES"] = pkg_dic[pkg]["LICENSES"].split() |
Brad Bishop | f3f93bb | 2019-10-16 14:33:32 -0400 | [diff] [blame] | 77 | if pkg in whitelist: |
| 78 | bb.warn("Including %s with an incompatible license %s into the image, because it has been whitelisted." %(pkg, pkg_dic[pkg]["LICENSE"])) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 79 | |
| 80 | if not "IMAGE_MANIFEST" in pkg_dic[pkg]: |
| 81 | # Rootfs manifest |
| 82 | license_file.write("PACKAGE NAME: %s\n" % pkg) |
| 83 | license_file.write("PACKAGE VERSION: %s\n" % pkg_dic[pkg]["PV"]) |
| 84 | license_file.write("RECIPE NAME: %s\n" % pkg_dic[pkg]["PN"]) |
| 85 | license_file.write("LICENSE: %s\n\n" % pkg_dic[pkg]["LICENSE"]) |
| 86 | |
| 87 | # If the package doesn't contain any file, that is, its size is 0, the license |
| 88 | # isn't relevant as far as the final image is concerned. So doing license check |
| 89 | # doesn't make much sense, skip it. |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 90 | if pkg_dic[pkg]["PKGSIZE:%s" % pkg] == "0": |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 91 | continue |
| 92 | else: |
| 93 | # Image manifest |
| 94 | license_file.write("RECIPE NAME: %s\n" % pkg_dic[pkg]["PN"]) |
| 95 | license_file.write("VERSION: %s\n" % pkg_dic[pkg]["PV"]) |
| 96 | license_file.write("LICENSE: %s\n" % pkg_dic[pkg]["LICENSE"]) |
| 97 | license_file.write("FILES: %s\n\n" % pkg_dic[pkg]["FILES"]) |
| 98 | |
| 99 | for lic in pkg_dic[pkg]["LICENSES"]: |
| 100 | lic_file = os.path.join(d.getVar('LICENSE_DIRECTORY'), |
| 101 | pkg_dic[pkg]["PN"], "generic_%s" % |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 102 | re.sub(r'\+', '', lic)) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 103 | # add explicity avoid of CLOSED license because isn't generic |
| 104 | if lic == "CLOSED": |
| 105 | continue |
| 106 | |
| 107 | if not os.path.exists(lic_file): |
| 108 | bb.warn("The license listed %s was not in the "\ |
| 109 | "licenses collected for recipe %s" |
| 110 | % (lic, pkg_dic[pkg]["PN"])) |
| 111 | |
| 112 | # Two options here: |
| 113 | # - Just copy the manifest |
| 114 | # - Copy the manifest and the license directories |
| 115 | # With both options set we see a .5 M increase in core-image-minimal |
| 116 | copy_lic_manifest = d.getVar('COPY_LIC_MANIFEST') |
| 117 | copy_lic_dirs = d.getVar('COPY_LIC_DIRS') |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 118 | if rootfs and copy_lic_manifest == "1": |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 119 | rootfs_license_dir = d.getVar('ROOTFS_LICENSE_DIR') |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 120 | bb.utils.mkdirhier(rootfs_license_dir) |
| 121 | rootfs_license_manifest = os.path.join(rootfs_license_dir, |
| 122 | os.path.split(license_manifest)[1]) |
| 123 | if not os.path.exists(rootfs_license_manifest): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 124 | oe.path.copyhardlink(license_manifest, rootfs_license_manifest) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 125 | |
| 126 | if copy_lic_dirs == "1": |
| 127 | for pkg in sorted(pkg_dic): |
| 128 | pkg_rootfs_license_dir = os.path.join(rootfs_license_dir, pkg) |
| 129 | bb.utils.mkdirhier(pkg_rootfs_license_dir) |
| 130 | pkg_license_dir = os.path.join(d.getVar('LICENSE_DIRECTORY'), |
| 131 | pkg_dic[pkg]["PN"]) |
| 132 | |
| 133 | pkg_manifest_licenses = [canonical_license(d, lic) \ |
| 134 | for lic in pkg_dic[pkg]["LICENSES"]] |
| 135 | |
| 136 | licenses = os.listdir(pkg_license_dir) |
| 137 | for lic in licenses: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 138 | pkg_license = os.path.join(pkg_license_dir, lic) |
| 139 | pkg_rootfs_license = os.path.join(pkg_rootfs_license_dir, lic) |
| 140 | |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 141 | if re.match(r"^generic_.*$", lic): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 142 | generic_lic = canonical_license(d, |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 143 | re.search(r"^generic_(.*)$", lic).group(1)) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 144 | |
| 145 | # Do not copy generic license into package if isn't |
| 146 | # declared into LICENSES of the package. |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 147 | if not re.sub(r'\+$', '', generic_lic) in \ |
| 148 | [re.sub(r'\+', '', lic) for lic in \ |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 149 | pkg_manifest_licenses]: |
| 150 | continue |
| 151 | |
| 152 | if oe.license.license_ok(generic_lic, |
| 153 | bad_licenses) == False: |
| 154 | continue |
| 155 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 156 | # Make sure we use only canonical name for the license file |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 157 | generic_lic_file = "generic_%s" % generic_lic |
| 158 | rootfs_license = os.path.join(rootfs_license_dir, generic_lic_file) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 159 | if not os.path.exists(rootfs_license): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 160 | oe.path.copyhardlink(pkg_license, rootfs_license) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 161 | |
| 162 | if not os.path.exists(pkg_rootfs_license): |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 163 | os.symlink(os.path.join('..', generic_lic_file), pkg_rootfs_license) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 164 | else: |
| 165 | if (oe.license.license_ok(canonical_license(d, |
| 166 | lic), bad_licenses) == False or |
| 167 | os.path.exists(pkg_rootfs_license)): |
| 168 | continue |
| 169 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 170 | oe.path.copyhardlink(pkg_license, pkg_rootfs_license) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 171 | # Fixup file ownership and permissions |
| 172 | for walkroot, dirs, files in os.walk(rootfs_license_dir): |
| 173 | for f in files: |
| 174 | p = os.path.join(walkroot, f) |
| 175 | os.lchown(p, 0, 0) |
| 176 | if not os.path.islink(p): |
| 177 | os.chmod(p, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH) |
| 178 | for dir in dirs: |
| 179 | p = os.path.join(walkroot, dir) |
| 180 | os.lchown(p, 0, 0) |
| 181 | os.chmod(p, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH) |
| 182 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 183 | |
| 184 | |
| 185 | def license_deployed_manifest(d): |
| 186 | """ |
| 187 | Write the license manifest for the deployed recipes. |
| 188 | The deployed recipes usually includes the bootloader |
| 189 | and extra files to boot the target. |
| 190 | """ |
| 191 | |
| 192 | dep_dic = {} |
| 193 | man_dic = {} |
| 194 | lic_dir = d.getVar("LICENSE_DIRECTORY") |
| 195 | |
| 196 | dep_dic = get_deployed_dependencies(d) |
| 197 | for dep in dep_dic.keys(): |
| 198 | man_dic[dep] = {} |
| 199 | # It is necessary to mark this will be used for image manifest |
| 200 | man_dic[dep]["IMAGE_MANIFEST"] = True |
| 201 | man_dic[dep]["PN"] = dep |
| 202 | man_dic[dep]["FILES"] = \ |
| 203 | " ".join(get_deployed_files(dep_dic[dep])) |
| 204 | with open(os.path.join(lic_dir, dep, "recipeinfo"), "r") as f: |
| 205 | for line in f.readlines(): |
| 206 | key,val = line.split(": ", 1) |
| 207 | man_dic[dep][key] = val[:-1] |
| 208 | |
| 209 | lic_manifest_dir = os.path.join(d.getVar('LICENSE_DIRECTORY'), |
| 210 | d.getVar('IMAGE_NAME')) |
| 211 | bb.utils.mkdirhier(lic_manifest_dir) |
| 212 | image_license_manifest = os.path.join(lic_manifest_dir, 'image_license.manifest') |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 213 | write_license_files(d, image_license_manifest, man_dic, rootfs=False) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 214 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 215 | link_name = d.getVar('IMAGE_LINK_NAME') |
| 216 | if link_name: |
| 217 | lic_manifest_symlink_dir = os.path.join(d.getVar('LICENSE_DIRECTORY'), |
| 218 | link_name) |
| 219 | # remove old symlink |
| 220 | if os.path.islink(lic_manifest_symlink_dir): |
| 221 | os.unlink(lic_manifest_symlink_dir) |
| 222 | |
| 223 | # create the image dir symlink |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 224 | if lic_manifest_dir != lic_manifest_symlink_dir: |
| 225 | os.symlink(lic_manifest_dir, lic_manifest_symlink_dir) |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 226 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 227 | def get_deployed_dependencies(d): |
| 228 | """ |
| 229 | Get all the deployed dependencies of an image |
| 230 | """ |
| 231 | |
| 232 | deploy = {} |
| 233 | # Get all the dependencies for the current task (rootfs). |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 234 | taskdata = d.getVar("BB_TASKDEPDATA", False) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 235 | pn = d.getVar("PN", True) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 236 | depends = list(set([dep[0] for dep |
| 237 | in list(taskdata.values()) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 238 | if not dep[0].endswith("-native") and not dep[0] == pn])) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 239 | |
| 240 | # To verify what was deployed it checks the rootfs dependencies against |
| 241 | # the SSTATE_MANIFESTS for "deploy" task. |
| 242 | # The manifest file name contains the arch. Because we are not running |
| 243 | # in the recipe context it is necessary to check every arch used. |
| 244 | sstate_manifest_dir = d.getVar("SSTATE_MANIFESTS") |
| 245 | archs = list(set(d.getVar("SSTATE_ARCHS").split())) |
| 246 | for dep in depends: |
| 247 | for arch in archs: |
| 248 | sstate_manifest_file = os.path.join(sstate_manifest_dir, |
| 249 | "manifest-%s-%s.deploy" % (arch, dep)) |
| 250 | if os.path.exists(sstate_manifest_file): |
| 251 | deploy[dep] = sstate_manifest_file |
| 252 | break |
| 253 | |
| 254 | return deploy |
| 255 | get_deployed_dependencies[vardepsexclude] = "BB_TASKDEPDATA" |
| 256 | |
| 257 | def get_deployed_files(man_file): |
| 258 | """ |
| 259 | Get the files deployed from the sstate manifest |
| 260 | """ |
| 261 | |
| 262 | dep_files = [] |
| 263 | excluded_files = [] |
| 264 | with open(man_file, "r") as manifest: |
| 265 | all_files = manifest.read() |
| 266 | for f in all_files.splitlines(): |
| 267 | if ((not (os.path.islink(f) or os.path.isdir(f))) and |
| 268 | not os.path.basename(f) in excluded_files): |
| 269 | dep_files.append(os.path.basename(f)) |
| 270 | return dep_files |
| 271 | |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 272 | ROOTFS_POSTPROCESS_COMMAND:prepend = "write_package_manifest; license_create_manifest; " |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 273 | do_rootfs[recrdeptask] += "do_populate_lic" |
| 274 | |
| 275 | python do_populate_lic_deploy() { |
| 276 | license_deployed_manifest(d) |
| 277 | } |
| 278 | |
| 279 | addtask populate_lic_deploy before do_build after do_image_complete |
| 280 | do_populate_lic_deploy[recrdeptask] += "do_populate_lic do_deploy" |
| 281 | |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 282 | python license_qa_dead_symlink() { |
| 283 | import os |
| 284 | |
| 285 | for root, dirs, files in os.walk(d.getVar('ROOTFS_LICENSE_DIR')): |
| 286 | for file in files: |
| 287 | full_path = root + "/" + file |
| 288 | if os.path.islink(full_path) and not os.path.exists(full_path): |
| 289 | bb.error("broken symlink: " + full_path) |
| 290 | } |
| 291 | IMAGE_QA_COMMANDS += "license_qa_dead_symlink" |