diff options
Diffstat (limited to 'meta/classes/license.bbclass')
-rw-r--r-- | meta/classes/license.bbclass | 373 |
1 files changed, 373 insertions, 0 deletions
diff --git a/meta/classes/license.bbclass b/meta/classes/license.bbclass new file mode 100644 index 0000000000..08f0665e7d --- /dev/null +++ b/meta/classes/license.bbclass | |||
@@ -0,0 +1,373 @@ | |||
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 | |||
145 | bb.utils.mkdirhier(destdir) | ||
146 | for (basename, path) in lic_files_paths: | ||
147 | try: | ||
148 | ret = shutil.copyfile(path, os.path.join(destdir, basename)) | ||
149 | except Exception as e: | ||
150 | bb.warn("Could not copy license file %s: %s" % (basename, e)) | ||
151 | |||
152 | def find_license_files(d): | ||
153 | """ | ||
154 | Creates list of files used in LIC_FILES_CHKSUM and generic LICENSE files. | ||
155 | """ | ||
156 | import shutil | ||
157 | import oe.license | ||
158 | |||
159 | pn = d.getVar('PN', True) | ||
160 | for package in d.getVar('PACKAGES', True): | ||
161 | if d.getVar('LICENSE_' + package, True): | ||
162 | license_types = license_types + ' & ' + \ | ||
163 | d.getVar('LICENSE_' + package, True) | ||
164 | |||
165 | #If we get here with no license types, then that means we have a recipe | ||
166 | #level license. If so, we grab only those. | ||
167 | try: | ||
168 | license_types | ||
169 | except NameError: | ||
170 | # All the license types at the recipe level | ||
171 | license_types = d.getVar('LICENSE', True) | ||
172 | |||
173 | # All the license files for the package | ||
174 | lic_files = d.getVar('LIC_FILES_CHKSUM', True) | ||
175 | pn = d.getVar('PN', True) | ||
176 | # The license files are located in S/LIC_FILE_CHECKSUM. | ||
177 | srcdir = d.getVar('S', True) | ||
178 | # Directory we store the generic licenses as set in the distro configuration | ||
179 | generic_directory = d.getVar('COMMON_LICENSE_DIR', True) | ||
180 | # List of basename, path tuples | ||
181 | lic_files_paths = [] | ||
182 | license_source_dirs = [] | ||
183 | license_source_dirs.append(generic_directory) | ||
184 | try: | ||
185 | additional_lic_dirs = d.getVar('LICENSE_PATH', True).split() | ||
186 | for lic_dir in additional_lic_dirs: | ||
187 | license_source_dirs.append(lic_dir) | ||
188 | except: | ||
189 | pass | ||
190 | |||
191 | class FindVisitor(oe.license.LicenseVisitor): | ||
192 | def visit_Str(self, node): | ||
193 | # | ||
194 | # Until I figure out what to do with | ||
195 | # the two modifiers I support (or greater = + | ||
196 | # and "with exceptions" being * | ||
197 | # we'll just strip out the modifier and put | ||
198 | # the base license. | ||
199 | find_license(node.s.replace("+", "").replace("*", "")) | ||
200 | self.generic_visit(node) | ||
201 | |||
202 | def find_license(license_type): | ||
203 | try: | ||
204 | bb.utils.mkdirhier(gen_lic_dest) | ||
205 | except: | ||
206 | pass | ||
207 | spdx_generic = None | ||
208 | license_source = None | ||
209 | # If the generic does not exist we need to check to see if there is an SPDX mapping to it | ||
210 | for lic_dir in license_source_dirs: | ||
211 | if not os.path.isfile(os.path.join(lic_dir, license_type)): | ||
212 | if d.getVarFlag('SPDXLICENSEMAP', license_type) != None: | ||
213 | # Great, there is an SPDXLICENSEMAP. We can copy! | ||
214 | bb.debug(1, "We need to use a SPDXLICENSEMAP for %s" % (license_type)) | ||
215 | spdx_generic = d.getVarFlag('SPDXLICENSEMAP', license_type) | ||
216 | license_source = lic_dir | ||
217 | break | ||
218 | elif os.path.isfile(os.path.join(lic_dir, license_type)): | ||
219 | spdx_generic = license_type | ||
220 | license_source = lic_dir | ||
221 | break | ||
222 | |||
223 | if spdx_generic and license_source: | ||
224 | # we really should copy to generic_ + spdx_generic, however, that ends up messing the manifest | ||
225 | # audit up. This should be fixed in emit_pkgdata (or, we actually got and fix all the recipes) | ||
226 | |||
227 | lic_files_paths.append(("generic_" + license_type, os.path.join(license_source, spdx_generic))) | ||
228 | else: | ||
229 | # And here is where we warn people that their licenses are lousy | ||
230 | bb.warn("%s: No generic license file exists for: %s in any provider" % (pn, license_type)) | ||
231 | pass | ||
232 | |||
233 | if not generic_directory: | ||
234 | raise bb.build.FuncFailed("COMMON_LICENSE_DIR is unset. Please set this in your distro config") | ||
235 | |||
236 | if not lic_files: | ||
237 | # No recipe should have an invalid license file. This is checked else | ||
238 | # where, but let's be pedantic | ||
239 | bb.note(pn + ": Recipe file does not have license file information.") | ||
240 | return lic_files_paths | ||
241 | |||
242 | for url in lic_files.split(): | ||
243 | try: | ||
244 | (type, host, path, user, pswd, parm) = bb.fetch.decodeurl(url) | ||
245 | except bb.fetch.MalformedUrl: | ||
246 | raise bb.build.FuncFailed("%s: LIC_FILES_CHKSUM contains an invalid URL: %s" % (d.getVar('PF', True), url)) | ||
247 | # We want the license filename and path | ||
248 | srclicfile = os.path.join(srcdir, path) | ||
249 | lic_files_paths.append((os.path.basename(path), srclicfile)) | ||
250 | |||
251 | v = FindVisitor() | ||
252 | try: | ||
253 | v.visit_string(license_types) | ||
254 | except oe.license.InvalidLicense as exc: | ||
255 | bb.fatal('%s: %s' % (d.getVar('PF', True), exc)) | ||
256 | except SyntaxError: | ||
257 | bb.warn("%s: Failed to parse it's LICENSE field." % (d.getVar('PF', True))) | ||
258 | |||
259 | return lic_files_paths | ||
260 | |||
261 | def return_spdx(d, license): | ||
262 | """ | ||
263 | This function returns the spdx mapping of a license if it exists. | ||
264 | """ | ||
265 | return d.getVarFlag('SPDXLICENSEMAP', license, True) | ||
266 | |||
267 | def incompatible_license(d, dont_want_licenses, package=None): | ||
268 | """ | ||
269 | This function checks if a recipe has only incompatible licenses. It also take into consideration 'or' | ||
270 | operand. | ||
271 | """ | ||
272 | import re | ||
273 | import oe.license | ||
274 | from fnmatch import fnmatchcase as fnmatch | ||
275 | license = d.getVar("LICENSE_%s" % package, True) if package else None | ||
276 | if not license: | ||
277 | license = d.getVar('LICENSE', True) | ||
278 | |||
279 | def license_ok(license): | ||
280 | for dwl in dont_want_licenses: | ||
281 | # If you want to exclude license named generically 'X', we | ||
282 | # surely want to exclude 'X+' as well. In consequence, we | ||
283 | # will exclude a trailing '+' character from LICENSE in | ||
284 | # case INCOMPATIBLE_LICENSE is not a 'X+' license. | ||
285 | lic = license | ||
286 | if not re.search('\+$', dwl): | ||
287 | lic = re.sub('\+', '', license) | ||
288 | if fnmatch(lic, dwl): | ||
289 | return False | ||
290 | return True | ||
291 | |||
292 | # Handles an "or" or two license sets provided by | ||
293 | # flattened_licenses(), pick one that works if possible. | ||
294 | def choose_lic_set(a, b): | ||
295 | return a if all(license_ok(lic) for lic in a) else b | ||
296 | |||
297 | try: | ||
298 | licenses = oe.license.flattened_licenses(license, choose_lic_set) | ||
299 | except oe.license.LicenseError as exc: | ||
300 | bb.fatal('%s: %s' % (d.getVar('P', True), exc)) | ||
301 | return any(not license_ok(l) for l in licenses) | ||
302 | |||
303 | def check_license_flags(d): | ||
304 | """ | ||
305 | This function checks if a recipe has any LICENSE_FLAGS that | ||
306 | aren't whitelisted. | ||
307 | |||
308 | If it does, it returns the first LICENSE_FLAGS item missing from the | ||
309 | whitelist, or all of the LICENSE_FLAGS if there is no whitelist. | ||
310 | |||
311 | If everything is is properly whitelisted, it returns None. | ||
312 | """ | ||
313 | |||
314 | def license_flag_matches(flag, whitelist, pn): | ||
315 | """ | ||
316 | Return True if flag matches something in whitelist, None if not. | ||
317 | |||
318 | Before we test a flag against the whitelist, we append _${PN} | ||
319 | to it. We then try to match that string against the | ||
320 | whitelist. This covers the normal case, where we expect | ||
321 | LICENSE_FLAGS to be a simple string like 'commercial', which | ||
322 | the user typically matches exactly in the whitelist by | ||
323 | explicitly appending the package name e.g 'commercial_foo'. | ||
324 | If we fail the match however, we then split the flag across | ||
325 | '_' and append each fragment and test until we either match or | ||
326 | run out of fragments. | ||
327 | """ | ||
328 | flag_pn = ("%s_%s" % (flag, pn)) | ||
329 | for candidate in whitelist: | ||
330 | if flag_pn == candidate: | ||
331 | return True | ||
332 | |||
333 | flag_cur = "" | ||
334 | flagments = flag_pn.split("_") | ||
335 | flagments.pop() # we've already tested the full string | ||
336 | for flagment in flagments: | ||
337 | if flag_cur: | ||
338 | flag_cur += "_" | ||
339 | flag_cur += flagment | ||
340 | for candidate in whitelist: | ||
341 | if flag_cur == candidate: | ||
342 | return True | ||
343 | return False | ||
344 | |||
345 | def all_license_flags_match(license_flags, whitelist): | ||
346 | """ Return first unmatched flag, None if all flags match """ | ||
347 | pn = d.getVar('PN', True) | ||
348 | split_whitelist = whitelist.split() | ||
349 | for flag in license_flags.split(): | ||
350 | if not license_flag_matches(flag, split_whitelist, pn): | ||
351 | return flag | ||
352 | return None | ||
353 | |||
354 | license_flags = d.getVar('LICENSE_FLAGS', True) | ||
355 | if license_flags: | ||
356 | whitelist = d.getVar('LICENSE_FLAGS_WHITELIST', True) | ||
357 | if not whitelist: | ||
358 | return license_flags | ||
359 | unmatched_flag = all_license_flags_match(license_flags, whitelist) | ||
360 | if unmatched_flag: | ||
361 | return unmatched_flag | ||
362 | return None | ||
363 | |||
364 | SSTATETASKS += "do_populate_lic" | ||
365 | do_populate_lic[sstate-inputdirs] = "${LICSSTATEDIR}" | ||
366 | do_populate_lic[sstate-outputdirs] = "${LICENSE_DIRECTORY}/" | ||
367 | |||
368 | ROOTFS_POSTPROCESS_COMMAND_prepend = "write_package_manifest; license_create_manifest; " | ||
369 | |||
370 | python do_populate_lic_setscene () { | ||
371 | sstate_setscene(d) | ||
372 | } | ||
373 | addtask do_populate_lic_setscene | ||