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