diff options
Diffstat (limited to 'meta/classes/license.bbclass')
-rw-r--r-- | meta/classes/license.bbclass | 397 |
1 files changed, 397 insertions, 0 deletions
diff --git a/meta/classes/license.bbclass b/meta/classes/license.bbclass new file mode 100644 index 0000000000..69e8f12cba --- /dev/null +++ b/meta/classes/license.bbclass | |||
@@ -0,0 +1,397 @@ | |||
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 | |||
19 | python write_package_manifest() { | ||
20 | # Get list of installed packages | ||
21 | license_image_dir = d.expand('${LICENSE_DIRECTORY}/${IMAGE_NAME}') | ||
22 | bb.utils.mkdirhier(license_image_dir) | ||
23 | from oe.rootfs import image_list_installed_packages | ||
24 | open(os.path.join(license_image_dir, 'package.manifest'), | ||
25 | 'w+').write(image_list_installed_packages(d)) | ||
26 | } | ||
27 | |||
28 | license_create_manifest() { | ||
29 | # Test if BUILD_IMAGES_FROM_FEEDS is defined in env | ||
30 | if [ -n "${BUILD_IMAGES_FROM_FEEDS}" ]; then | ||
31 | exit 0 | ||
32 | fi | ||
33 | |||
34 | INSTALLED_PKGS=`cat ${LICENSE_DIRECTORY}/${IMAGE_NAME}/package.manifest` | ||
35 | LICENSE_MANIFEST="${LICENSE_DIRECTORY}/${IMAGE_NAME}/license.manifest" | ||
36 | # remove existing license.manifest file | ||
37 | if [ -f ${LICENSE_MANIFEST} ]; then | ||
38 | rm ${LICENSE_MANIFEST} | ||
39 | fi | ||
40 | touch ${LICENSE_MANIFEST} | ||
41 | for pkg in ${INSTALLED_PKGS}; do | ||
42 | filename=`ls ${PKGDATA_DIR}/runtime-reverse/${pkg}| head -1` | ||
43 | pkged_pn="$(sed -n 's/^PN: //p' ${filename})" | ||
44 | |||
45 | # check to see if the package name exists in the manifest. if so, bail. | ||
46 | if grep -q "^PACKAGE NAME: ${pkg}" ${LICENSE_MANIFEST}; then | ||
47 | continue | ||
48 | fi | ||
49 | |||
50 | pkged_pv="$(sed -n 's/^PV: //p' ${filename})" | ||
51 | pkged_name="$(basename $(readlink ${filename}))" | ||
52 | pkged_lic="$(sed -n "/^LICENSE_${pkged_name}: /{ s/^LICENSE_${pkged_name}: //; s/[|&()*]/ /g; s/ */ /g; p }" ${filename})" | ||
53 | if [ -z ${pkged_lic} ]; then | ||
54 | # fallback checking value of LICENSE | ||
55 | pkged_lic="$(sed -n "/^LICENSE: /{ s/^LICENSE: //; s/[|&()*]/ /g; s/ */ /g; p }" ${filename})" | ||
56 | fi | ||
57 | |||
58 | echo "PACKAGE NAME:" ${pkg} >> ${LICENSE_MANIFEST} | ||
59 | echo "PACKAGE VERSION:" ${pkged_pv} >> ${LICENSE_MANIFEST} | ||
60 | echo "RECIPE NAME:" ${pkged_pn} >> ${LICENSE_MANIFEST} | ||
61 | printf "LICENSE:" >> ${LICENSE_MANIFEST} | ||
62 | for lic in ${pkged_lic}; do | ||
63 | # to reference a license file trim trailing + symbol | ||
64 | if ! [ -e "${LICENSE_DIRECTORY}/${pkged_pn}/generic_${lic%+}" ]; then | ||
65 | bbwarn "The license listed ${lic} was not in the licenses collected for ${pkged_pn}" | ||
66 | fi | ||
67 | printf " ${lic}" >> ${LICENSE_MANIFEST} | ||
68 | done | ||
69 | printf "\n\n" >> ${LICENSE_MANIFEST} | ||
70 | done | ||
71 | |||
72 | # Two options here: | ||
73 | # - Just copy the manifest | ||
74 | # - Copy the manifest and the license directories | ||
75 | # With both options set we see a .5 M increase in core-image-minimal | ||
76 | if [ "${COPY_LIC_MANIFEST}" = "1" ]; then | ||
77 | mkdir -p ${IMAGE_ROOTFS}/usr/share/common-licenses/ | ||
78 | cp ${LICENSE_MANIFEST} ${IMAGE_ROOTFS}/usr/share/common-licenses/license.manifest | ||
79 | if [ "${COPY_LIC_DIRS}" = "1" ]; then | ||
80 | for pkg in ${INSTALLED_PKGS}; do | ||
81 | mkdir -p ${IMAGE_ROOTFS}/usr/share/common-licenses/${pkg} | ||
82 | pkged_pn="$(oe-pkgdata-util lookup-recipe ${PKGDATA_DIR} ${pkg})" | ||
83 | for lic in `ls ${LICENSE_DIRECTORY}/${pkged_pn}`; do | ||
84 | # Really don't need to copy the generics as they're | ||
85 | # represented in the manifest and in the actual pkg licenses | ||
86 | # Doing so would make your image quite a bit larger | ||
87 | if [ "${lic#generic_}" = "${lic}" ]; then | ||
88 | cp ${LICENSE_DIRECTORY}/${pkged_pn}/${lic} ${IMAGE_ROOTFS}/usr/share/common-licenses/${pkg}/${lic} | ||
89 | else | ||
90 | if [ ! -f ${IMAGE_ROOTFS}/usr/share/common-licenses/${lic} ]; then | ||
91 | cp ${LICENSE_DIRECTORY}/${pkged_pn}/${lic} ${IMAGE_ROOTFS}/usr/share/common-licenses/ | ||
92 | fi | ||
93 | ln -sf ../${lic} ${IMAGE_ROOTFS}/usr/share/common-licenses/${pkg}/${lic} | ||
94 | fi | ||
95 | done | ||
96 | done | ||
97 | fi | ||
98 | fi | ||
99 | |||
100 | } | ||
101 | |||
102 | python do_populate_lic() { | ||
103 | """ | ||
104 | Populate LICENSE_DIRECTORY with licenses. | ||
105 | """ | ||
106 | lic_files_paths = find_license_files(d) | ||
107 | |||
108 | # The base directory we wrangle licenses to | ||
109 | destdir = os.path.join(d.getVar('LICSSTATEDIR', True), d.getVar('PN', True)) | ||
110 | copy_license_files(lic_files_paths, destdir) | ||
111 | } | ||
112 | |||
113 | # it would be better to copy them in do_install_append, but find_license_filesa is python | ||
114 | python perform_packagecopy_prepend () { | ||
115 | enabled = oe.data.typed_value('LICENSE_CREATE_PACKAGE', d) | ||
116 | if d.getVar('CLASSOVERRIDE', True) == 'class-target' and enabled: | ||
117 | lic_files_paths = find_license_files(d) | ||
118 | |||
119 | # LICENSE_FILES_DIRECTORY starts with '/' so os.path.join cannot be used to join D and LICENSE_FILES_DIRECTORY | ||
120 | destdir = d.getVar('D', True) + os.path.join(d.getVar('LICENSE_FILES_DIRECTORY', True), d.getVar('PN', True)) | ||
121 | copy_license_files(lic_files_paths, destdir) | ||
122 | add_package_and_files(d) | ||
123 | } | ||
124 | |||
125 | def add_package_and_files(d): | ||
126 | packages = d.getVar('PACKAGES', True) | ||
127 | files = d.getVar('LICENSE_FILES_DIRECTORY', True) | ||
128 | pn = d.getVar('PN', True) | ||
129 | pn_lic = "%s%s" % (pn, d.getVar('LICENSE_PACKAGE_SUFFIX')) | ||
130 | if pn_lic in packages: | ||
131 | bb.warn("%s package already existed in %s." % (pn_lic, pn)) | ||
132 | else: | ||
133 | # first in PACKAGES to be sure that nothing else gets LICENSE_FILES_DIRECTORY | ||
134 | d.setVar('PACKAGES', "%s %s" % (pn_lic, packages)) | ||
135 | d.setVar('FILES_' + pn_lic, files) | ||
136 | rrecommends_pn = d.getVar('RRECOMMENDS_' + pn, True) | ||
137 | if rrecommends_pn: | ||
138 | d.setVar('RRECOMMENDS_' + pn, "%s %s" % (pn_lic, rrecommends_pn)) | ||
139 | else: | ||
140 | d.setVar('RRECOMMENDS_' + pn, "%s" % (pn_lic)) | ||
141 | |||
142 | def copy_license_files(lic_files_paths, destdir): | ||
143 | import shutil | ||
144 | import stat | ||
145 | |||
146 | bb.utils.mkdirhier(destdir) | ||
147 | for (basename, path) in lic_files_paths: | ||
148 | try: | ||
149 | src = path | ||
150 | dst = os.path.join(destdir, basename) | ||
151 | if os.path.exists(dst): | ||
152 | os.remove(dst) | ||
153 | if (os.stat(src).st_dev == os.stat(destdir).st_dev): | ||
154 | os.link(src, dst) | ||
155 | else: | ||
156 | shutil.copyfile(src, dst) | ||
157 | os.chmod(dst, os.stat(dst).st_mode | stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH); | ||
158 | except Exception as e: | ||
159 | bb.warn("Could not copy license file %s: %s" % (basename, e)) | ||
160 | |||
161 | def find_license_files(d): | ||
162 | """ | ||
163 | Creates list of files used in LIC_FILES_CHKSUM and generic LICENSE files. | ||
164 | """ | ||
165 | import shutil | ||
166 | import oe.license | ||
167 | |||
168 | pn = d.getVar('PN', True) | ||
169 | for package in d.getVar('PACKAGES', True): | ||
170 | if d.getVar('LICENSE_' + package, True): | ||
171 | license_types = license_types + ' & ' + \ | ||
172 | d.getVar('LICENSE_' + package, True) | ||
173 | |||
174 | #If we get here with no license types, then that means we have a recipe | ||
175 | #level license. If so, we grab only those. | ||
176 | try: | ||
177 | license_types | ||
178 | except NameError: | ||
179 | # All the license types at the recipe level | ||
180 | license_types = d.getVar('LICENSE', True) | ||
181 | |||
182 | # All the license files for the package | ||
183 | lic_files = d.getVar('LIC_FILES_CHKSUM', True) | ||
184 | pn = d.getVar('PN', True) | ||
185 | # The license files are located in S/LIC_FILE_CHECKSUM. | ||
186 | srcdir = d.getVar('S', True) | ||
187 | # Directory we store the generic licenses as set in the distro configuration | ||
188 | generic_directory = d.getVar('COMMON_LICENSE_DIR', True) | ||
189 | # List of basename, path tuples | ||
190 | lic_files_paths = [] | ||
191 | license_source_dirs = [] | ||
192 | license_source_dirs.append(generic_directory) | ||
193 | try: | ||
194 | additional_lic_dirs = d.getVar('LICENSE_PATH', True).split() | ||
195 | for lic_dir in additional_lic_dirs: | ||
196 | license_source_dirs.append(lic_dir) | ||
197 | except: | ||
198 | pass | ||
199 | |||
200 | class FindVisitor(oe.license.LicenseVisitor): | ||
201 | def visit_Str(self, node): | ||
202 | # | ||
203 | # Until I figure out what to do with | ||
204 | # the two modifiers I support (or greater = + | ||
205 | # and "with exceptions" being * | ||
206 | # we'll just strip out the modifier and put | ||
207 | # the base license. | ||
208 | find_license(node.s.replace("+", "").replace("*", "")) | ||
209 | self.generic_visit(node) | ||
210 | |||
211 | def find_license(license_type): | ||
212 | try: | ||
213 | bb.utils.mkdirhier(gen_lic_dest) | ||
214 | except: | ||
215 | pass | ||
216 | spdx_generic = None | ||
217 | license_source = None | ||
218 | # If the generic does not exist we need to check to see if there is an SPDX mapping to it | ||
219 | for lic_dir in license_source_dirs: | ||
220 | if not os.path.isfile(os.path.join(lic_dir, license_type)): | ||
221 | if d.getVarFlag('SPDXLICENSEMAP', license_type) != None: | ||
222 | # Great, there is an SPDXLICENSEMAP. We can copy! | ||
223 | bb.debug(1, "We need to use a SPDXLICENSEMAP for %s" % (license_type)) | ||
224 | spdx_generic = d.getVarFlag('SPDXLICENSEMAP', license_type) | ||
225 | license_source = lic_dir | ||
226 | break | ||
227 | elif os.path.isfile(os.path.join(lic_dir, license_type)): | ||
228 | spdx_generic = license_type | ||
229 | license_source = lic_dir | ||
230 | break | ||
231 | |||
232 | if spdx_generic and license_source: | ||
233 | # we really should copy to generic_ + spdx_generic, however, that ends up messing the manifest | ||
234 | # audit up. This should be fixed in emit_pkgdata (or, we actually got and fix all the recipes) | ||
235 | |||
236 | lic_files_paths.append(("generic_" + license_type, os.path.join(license_source, spdx_generic))) | ||
237 | else: | ||
238 | # And here is where we warn people that their licenses are lousy | ||
239 | bb.warn("%s: No generic license file exists for: %s in any provider" % (pn, license_type)) | ||
240 | pass | ||
241 | |||
242 | if not generic_directory: | ||
243 | raise bb.build.FuncFailed("COMMON_LICENSE_DIR is unset. Please set this in your distro config") | ||
244 | |||
245 | if not lic_files: | ||
246 | # No recipe should have an invalid license file. This is checked else | ||
247 | # where, but let's be pedantic | ||
248 | bb.note(pn + ": Recipe file does not have license file information.") | ||
249 | return lic_files_paths | ||
250 | |||
251 | for url in lic_files.split(): | ||
252 | try: | ||
253 | (type, host, path, user, pswd, parm) = bb.fetch.decodeurl(url) | ||
254 | except bb.fetch.MalformedUrl: | ||
255 | raise bb.build.FuncFailed("%s: LIC_FILES_CHKSUM contains an invalid URL: %s" % (d.getVar('PF', True), url)) | ||
256 | # We want the license filename and path | ||
257 | srclicfile = os.path.join(srcdir, path) | ||
258 | lic_files_paths.append((os.path.basename(path), srclicfile)) | ||
259 | |||
260 | v = FindVisitor() | ||
261 | try: | ||
262 | v.visit_string(license_types) | ||
263 | except oe.license.InvalidLicense as exc: | ||
264 | bb.fatal('%s: %s' % (d.getVar('PF', True), exc)) | ||
265 | except SyntaxError: | ||
266 | bb.warn("%s: Failed to parse it's LICENSE field." % (d.getVar('PF', True))) | ||
267 | |||
268 | return lic_files_paths | ||
269 | |||
270 | def return_spdx(d, license): | ||
271 | """ | ||
272 | This function returns the spdx mapping of a license if it exists. | ||
273 | """ | ||
274 | return d.getVarFlag('SPDXLICENSEMAP', license, True) | ||
275 | |||
276 | def canonical_license(d, license): | ||
277 | """ | ||
278 | Return the canonical (SPDX) form of the license if available (so GPLv3 | ||
279 | becomes GPL-3.0), for the license named 'X+', return canonical form of | ||
280 | 'X' if availabel and the tailing '+' (so GPLv3+ becomes GPL-3.0+), | ||
281 | or the passed license if there is no canonical form. | ||
282 | """ | ||
283 | lic = d.getVarFlag('SPDXLICENSEMAP', license, True) or "" | ||
284 | if not lic and license.endswith('+'): | ||
285 | lic = d.getVarFlag('SPDXLICENSEMAP', license.rstrip('+'), True) | ||
286 | if lic: | ||
287 | lic += '+' | ||
288 | return lic or license | ||
289 | |||
290 | def incompatible_license(d, dont_want_licenses, package=None): | ||
291 | """ | ||
292 | This function checks if a recipe has only incompatible licenses. It also | ||
293 | take into consideration 'or' operand. dont_want_licenses should be passed | ||
294 | as canonical (SPDX) names. | ||
295 | """ | ||
296 | import re | ||
297 | import oe.license | ||
298 | from fnmatch import fnmatchcase as fnmatch | ||
299 | license = d.getVar("LICENSE_%s" % package, True) if package else None | ||
300 | if not license: | ||
301 | license = d.getVar('LICENSE', True) | ||
302 | |||
303 | def license_ok(license): | ||
304 | for dwl in dont_want_licenses: | ||
305 | # If you want to exclude license named generically 'X', we | ||
306 | # surely want to exclude 'X+' as well. In consequence, we | ||
307 | # will exclude a trailing '+' character from LICENSE in | ||
308 | # case INCOMPATIBLE_LICENSE is not a 'X+' license. | ||
309 | lic = license | ||
310 | if not re.search('\+$', dwl): | ||
311 | lic = re.sub('\+', '', license) | ||
312 | if fnmatch(lic, dwl): | ||
313 | return False | ||
314 | return True | ||
315 | |||
316 | # Handles an "or" or two license sets provided by | ||
317 | # flattened_licenses(), pick one that works if possible. | ||
318 | def choose_lic_set(a, b): | ||
319 | return a if all(license_ok(lic) for lic in a) else b | ||
320 | |||
321 | try: | ||
322 | licenses = oe.license.flattened_licenses(license, choose_lic_set) | ||
323 | except oe.license.LicenseError as exc: | ||
324 | bb.fatal('%s: %s' % (d.getVar('P', True), exc)) | ||
325 | return any(not license_ok(canonical_license(d, l)) for l in licenses) | ||
326 | |||
327 | def check_license_flags(d): | ||
328 | """ | ||
329 | This function checks if a recipe has any LICENSE_FLAGS that | ||
330 | aren't whitelisted. | ||
331 | |||
332 | If it does, it returns the first LICENSE_FLAGS item missing from the | ||
333 | whitelist, or all of the LICENSE_FLAGS if there is no whitelist. | ||
334 | |||
335 | If everything is is properly whitelisted, it returns None. | ||
336 | """ | ||
337 | |||
338 | def license_flag_matches(flag, whitelist, pn): | ||
339 | """ | ||
340 | Return True if flag matches something in whitelist, None if not. | ||
341 | |||
342 | Before we test a flag against the whitelist, we append _${PN} | ||
343 | to it. We then try to match that string against the | ||
344 | whitelist. This covers the normal case, where we expect | ||
345 | LICENSE_FLAGS to be a simple string like 'commercial', which | ||
346 | the user typically matches exactly in the whitelist by | ||
347 | explicitly appending the package name e.g 'commercial_foo'. | ||
348 | If we fail the match however, we then split the flag across | ||
349 | '_' and append each fragment and test until we either match or | ||
350 | run out of fragments. | ||
351 | """ | ||
352 | flag_pn = ("%s_%s" % (flag, pn)) | ||
353 | for candidate in whitelist: | ||
354 | if flag_pn == candidate: | ||
355 | return True | ||
356 | |||
357 | flag_cur = "" | ||
358 | flagments = flag_pn.split("_") | ||
359 | flagments.pop() # we've already tested the full string | ||
360 | for flagment in flagments: | ||
361 | if flag_cur: | ||
362 | flag_cur += "_" | ||
363 | flag_cur += flagment | ||
364 | for candidate in whitelist: | ||
365 | if flag_cur == candidate: | ||
366 | return True | ||
367 | return False | ||
368 | |||
369 | def all_license_flags_match(license_flags, whitelist): | ||
370 | """ Return first unmatched flag, None if all flags match """ | ||
371 | pn = d.getVar('PN', True) | ||
372 | split_whitelist = whitelist.split() | ||
373 | for flag in license_flags.split(): | ||
374 | if not license_flag_matches(flag, split_whitelist, pn): | ||
375 | return flag | ||
376 | return None | ||
377 | |||
378 | license_flags = d.getVar('LICENSE_FLAGS', True) | ||
379 | if license_flags: | ||
380 | whitelist = d.getVar('LICENSE_FLAGS_WHITELIST', True) | ||
381 | if not whitelist: | ||
382 | return license_flags | ||
383 | unmatched_flag = all_license_flags_match(license_flags, whitelist) | ||
384 | if unmatched_flag: | ||
385 | return unmatched_flag | ||
386 | return None | ||
387 | |||
388 | SSTATETASKS += "do_populate_lic" | ||
389 | do_populate_lic[sstate-inputdirs] = "${LICSSTATEDIR}" | ||
390 | do_populate_lic[sstate-outputdirs] = "${LICENSE_DIRECTORY}/" | ||
391 | |||
392 | ROOTFS_POSTPROCESS_COMMAND_prepend = "write_package_manifest; license_create_manifest; " | ||
393 | |||
394 | python do_populate_lic_setscene () { | ||
395 | sstate_setscene(d) | ||
396 | } | ||
397 | addtask do_populate_lic_setscene | ||