license.bbclass 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. #
  2. # Copyright OpenEmbedded Contributors
  3. #
  4. # SPDX-License-Identifier: MIT
  5. #
  6. # Populates LICENSE_DIRECTORY as set in distro config with the license files as set by
  7. # LIC_FILES_CHKSUM.
  8. # TODO:
  9. # - There is a real issue revolving around license naming standards.
  10. LICENSE_DIRECTORY ??= "${DEPLOY_DIR}/licenses"
  11. LICSSTATEDIR = "${WORKDIR}/license-destdir/"
  12. # Create extra package with license texts and add it to RRECOMMENDS:${PN}
  13. LICENSE_CREATE_PACKAGE[type] = "boolean"
  14. LICENSE_CREATE_PACKAGE ??= "0"
  15. LICENSE_PACKAGE_SUFFIX ??= "-lic"
  16. LICENSE_FILES_DIRECTORY ??= "${datadir}/licenses/"
  17. LICENSE_DEPLOY_PATHCOMPONENT = "${SSTATE_PKGARCH}"
  18. LICENSE_DEPLOY_PATHCOMPONENT:class-cross = "native"
  19. LICENSE_DEPLOY_PATHCOMPONENT:class-native = "native"
  20. # Ensure the *value* of SSTATE_PKGARCH is captured as it is used in the output paths
  21. LICENSE_DEPLOY_PATHCOMPONENT[vardepvalue] += "${LICENSE_DEPLOY_PATHCOMPONENT}"
  22. addtask populate_lic after do_patch before do_build
  23. do_populate_lic[dirs] = "${LICSSTATEDIR}/${LICENSE_DEPLOY_PATHCOMPONENT}/${PN}"
  24. do_populate_lic[cleandirs] = "${LICSSTATEDIR}"
  25. python do_populate_lic() {
  26. """
  27. Populate LICENSE_DIRECTORY with licenses.
  28. """
  29. lic_files_paths = find_license_files(d)
  30. # The base directory we wrangle licenses to
  31. destdir = os.path.join(d.getVar('LICSSTATEDIR'), d.getVar('LICENSE_DEPLOY_PATHCOMPONENT'), d.getVar('PN'))
  32. copy_license_files(lic_files_paths, destdir)
  33. info = get_recipe_info(d)
  34. with open(os.path.join(destdir, "recipeinfo"), "w") as f:
  35. for key in sorted(info.keys()):
  36. f.write("%s: %s\n" % (key, info[key]))
  37. oe.qa.exit_if_errors(d)
  38. }
  39. PSEUDO_IGNORE_PATHS .= ",${@','.join(((d.getVar('COMMON_LICENSE_DIR') or '') + ' ' + (d.getVar('LICENSE_PATH') or '') + ' ' + d.getVar('COREBASE') + '/meta/COPYING').split())}"
  40. # it would be better to copy them in do_install:append, but find_license_filesa is python
  41. python perform_packagecopy:prepend () {
  42. enabled = oe.data.typed_value('LICENSE_CREATE_PACKAGE', d)
  43. if d.getVar('CLASSOVERRIDE') == 'class-target' and enabled:
  44. lic_files_paths = find_license_files(d)
  45. # LICENSE_FILES_DIRECTORY starts with '/' so os.path.join cannot be used to join D and LICENSE_FILES_DIRECTORY
  46. destdir = d.getVar('D') + os.path.join(d.getVar('LICENSE_FILES_DIRECTORY'), d.getVar('PN'))
  47. copy_license_files(lic_files_paths, destdir)
  48. add_package_and_files(d)
  49. }
  50. perform_packagecopy[vardeps] += "LICENSE_CREATE_PACKAGE"
  51. def get_recipe_info(d):
  52. info = {}
  53. info["PV"] = d.getVar("PV")
  54. info["PR"] = d.getVar("PR")
  55. info["LICENSE"] = d.getVar("LICENSE")
  56. return info
  57. def add_package_and_files(d):
  58. packages = d.getVar('PACKAGES')
  59. files = d.getVar('LICENSE_FILES_DIRECTORY')
  60. pn = d.getVar('PN')
  61. pn_lic = "%s%s" % (pn, d.getVar('LICENSE_PACKAGE_SUFFIX', False))
  62. if pn_lic in packages.split():
  63. bb.warn("%s package already existed in %s." % (pn_lic, pn))
  64. else:
  65. # first in PACKAGES to be sure that nothing else gets LICENSE_FILES_DIRECTORY
  66. d.setVar('PACKAGES', "%s %s" % (pn_lic, packages))
  67. d.setVar('FILES:' + pn_lic, files)
  68. def copy_license_files(lic_files_paths, destdir):
  69. import shutil
  70. import errno
  71. bb.utils.mkdirhier(destdir)
  72. for (basename, path, beginline, endline) in lic_files_paths:
  73. try:
  74. src = path
  75. dst = os.path.join(destdir, basename)
  76. if os.path.exists(dst):
  77. os.remove(dst)
  78. if os.path.islink(src):
  79. src = os.path.realpath(src)
  80. 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
  81. if canlink:
  82. try:
  83. os.link(src, dst)
  84. except OSError as err:
  85. if err.errno == errno.EXDEV:
  86. # Copy license files if hardlink is not possible even if st_dev is the
  87. # same on source and destination (docker container with device-mapper?)
  88. canlink = False
  89. else:
  90. raise
  91. # Only chown if we did hardlink and we're running under pseudo
  92. if canlink and os.environ.get('PSEUDO_DISABLED') == '0':
  93. os.chown(dst,0,0)
  94. if not canlink:
  95. begin_idx = max(0, int(beginline) - 1) if beginline is not None else None
  96. end_idx = max(0, int(endline)) if endline is not None else None
  97. if begin_idx is None and end_idx is None:
  98. shutil.copyfile(src, dst)
  99. else:
  100. with open(src, 'rb') as src_f:
  101. with open(dst, 'wb') as dst_f:
  102. dst_f.write(b''.join(src_f.readlines()[begin_idx:end_idx]))
  103. except Exception as e:
  104. bb.warn("Could not copy license file %s to %s: %s" % (src, dst, e))
  105. def find_license_files(d):
  106. """
  107. Creates list of files used in LIC_FILES_CHKSUM and generic LICENSE files.
  108. """
  109. import shutil
  110. import oe.license
  111. from collections import defaultdict, OrderedDict
  112. # All the license files for the package
  113. lic_files = d.getVar('LIC_FILES_CHKSUM') or ""
  114. pn = d.getVar('PN')
  115. # The license files are located in S/LIC_FILE_CHECKSUM.
  116. srcdir = d.getVar('S')
  117. # Directory we store the generic licenses as set in the distro configuration
  118. generic_directory = d.getVar('COMMON_LICENSE_DIR')
  119. # List of basename, path tuples
  120. lic_files_paths = []
  121. # hash for keep track generic lics mappings
  122. non_generic_lics = {}
  123. # Entries from LIC_FILES_CHKSUM
  124. lic_chksums = {}
  125. license_source_dirs = []
  126. license_source_dirs.append(generic_directory)
  127. try:
  128. additional_lic_dirs = d.getVar('LICENSE_PATH').split()
  129. for lic_dir in additional_lic_dirs:
  130. license_source_dirs.append(lic_dir)
  131. except:
  132. pass
  133. class FindVisitor(oe.license.LicenseVisitor):
  134. def visit_Str(self, node):
  135. #
  136. # Until I figure out what to do with
  137. # the two modifiers I support (or greater = +
  138. # and "with exceptions" being *
  139. # we'll just strip out the modifier and put
  140. # the base license.
  141. find_license(node.s.replace("+", "").replace("*", ""))
  142. self.generic_visit(node)
  143. def visit_Constant(self, node):
  144. find_license(node.value.replace("+", "").replace("*", ""))
  145. self.generic_visit(node)
  146. def find_license(license_type):
  147. try:
  148. bb.utils.mkdirhier(gen_lic_dest)
  149. except:
  150. pass
  151. spdx_generic = None
  152. license_source = None
  153. # If the generic does not exist we need to check to see if there is an SPDX mapping to it,
  154. # unless NO_GENERIC_LICENSE is set.
  155. for lic_dir in license_source_dirs:
  156. if not os.path.isfile(os.path.join(lic_dir, license_type)):
  157. if d.getVarFlag('SPDXLICENSEMAP', license_type) != None:
  158. # Great, there is an SPDXLICENSEMAP. We can copy!
  159. bb.debug(1, "We need to use a SPDXLICENSEMAP for %s" % (license_type))
  160. spdx_generic = d.getVarFlag('SPDXLICENSEMAP', license_type)
  161. license_source = lic_dir
  162. break
  163. elif os.path.isfile(os.path.join(lic_dir, license_type)):
  164. spdx_generic = license_type
  165. license_source = lic_dir
  166. break
  167. non_generic_lic = d.getVarFlag('NO_GENERIC_LICENSE', license_type)
  168. if spdx_generic and license_source:
  169. # we really should copy to generic_ + spdx_generic, however, that ends up messing the manifest
  170. # audit up. This should be fixed in emit_pkgdata (or, we actually got and fix all the recipes)
  171. lic_files_paths.append(("generic_" + license_type, os.path.join(license_source, spdx_generic),
  172. None, None))
  173. # The user may attempt to use NO_GENERIC_LICENSE for a generic license which doesn't make sense
  174. # and should not be allowed, warn the user in this case.
  175. if d.getVarFlag('NO_GENERIC_LICENSE', license_type):
  176. oe.qa.handle_error("license-no-generic",
  177. "%s: %s is a generic license, please don't use NO_GENERIC_LICENSE for it." % (pn, license_type), d)
  178. elif non_generic_lic and non_generic_lic in lic_chksums:
  179. # if NO_GENERIC_LICENSE is set, we copy the license files from the fetched source
  180. # of the package rather than the license_source_dirs.
  181. lic_files_paths.append(("generic_" + license_type,
  182. os.path.join(srcdir, non_generic_lic), None, None))
  183. non_generic_lics[non_generic_lic] = license_type
  184. else:
  185. # Explicitly avoid the CLOSED license because this isn't generic
  186. if license_type != 'CLOSED':
  187. # And here is where we warn people that their licenses are lousy
  188. oe.qa.handle_error("license-exists",
  189. "%s: No generic license file exists for: %s in any provider" % (pn, license_type), d)
  190. pass
  191. if not generic_directory:
  192. bb.fatal("COMMON_LICENSE_DIR is unset. Please set this in your distro config")
  193. for url in lic_files.split():
  194. try:
  195. (method, host, path, user, pswd, parm) = bb.fetch.decodeurl(url)
  196. if method != "file" or not path:
  197. raise bb.fetch.MalformedUrl()
  198. except bb.fetch.MalformedUrl:
  199. bb.fatal("%s: LIC_FILES_CHKSUM contains an invalid URL: %s" % (d.getVar('PF'), url))
  200. # We want the license filename and path
  201. chksum = parm.get('md5', None)
  202. beginline = parm.get('beginline')
  203. endline = parm.get('endline')
  204. lic_chksums[path] = (chksum, beginline, endline)
  205. v = FindVisitor()
  206. try:
  207. v.visit_string(d.getVar('LICENSE'))
  208. except oe.license.InvalidLicense as exc:
  209. bb.fatal('%s: %s' % (d.getVar('PF'), exc))
  210. except SyntaxError:
  211. oe.qa.handle_error("license-syntax",
  212. "%s: Failed to parse LICENSE: %s" % (d.getVar('PF'), d.getVar('LICENSE')), d)
  213. # Add files from LIC_FILES_CHKSUM to list of license files
  214. lic_chksum_paths = defaultdict(OrderedDict)
  215. for path, data in sorted(lic_chksums.items()):
  216. lic_chksum_paths[os.path.basename(path)][data] = (os.path.join(srcdir, path), data[1], data[2])
  217. for basename, files in lic_chksum_paths.items():
  218. if len(files) == 1:
  219. # Don't copy again a LICENSE already handled as non-generic
  220. if basename in non_generic_lics:
  221. continue
  222. data = list(files.values())[0]
  223. lic_files_paths.append(tuple([basename] + list(data)))
  224. else:
  225. # If there are multiple different license files with identical
  226. # basenames we rename them to <file>.0, <file>.1, ...
  227. for i, data in enumerate(files.values()):
  228. lic_files_paths.append(tuple(["%s.%d" % (basename, i)] + list(data)))
  229. return lic_files_paths
  230. def return_spdx(d, license):
  231. """
  232. This function returns the spdx mapping of a license if it exists.
  233. """
  234. return d.getVarFlag('SPDXLICENSEMAP', license)
  235. def canonical_license(d, license):
  236. """
  237. Return the canonical (SPDX) form of the license if available (so GPLv3
  238. becomes GPL-3.0-only) or the passed license if there is no canonical form.
  239. """
  240. return d.getVarFlag('SPDXLICENSEMAP', license) or license
  241. def expand_wildcard_licenses(d, wildcard_licenses):
  242. """
  243. There are some common wildcard values users may want to use. Support them
  244. here.
  245. """
  246. licenses = set(wildcard_licenses)
  247. mapping = {
  248. "AGPL-3.0*" : ["AGPL-3.0-only", "AGPL-3.0-or-later"],
  249. "GPL-3.0*" : ["GPL-3.0-only", "GPL-3.0-or-later"],
  250. "LGPL-3.0*" : ["LGPL-3.0-only", "LGPL-3.0-or-later"],
  251. }
  252. for k in mapping:
  253. if k in wildcard_licenses:
  254. licenses.remove(k)
  255. for item in mapping[k]:
  256. licenses.add(item)
  257. for l in licenses:
  258. if l in oe.license.obsolete_license_list():
  259. bb.fatal("Error, %s is an obsolete license, please use an SPDX reference in INCOMPATIBLE_LICENSE" % l)
  260. if "*" in l:
  261. bb.fatal("Error, %s is an invalid license wildcard entry" % l)
  262. return list(licenses)
  263. def incompatible_license_contains(license, truevalue, falsevalue, d):
  264. license = canonical_license(d, license)
  265. bad_licenses = (d.getVar('INCOMPATIBLE_LICENSE') or "").split()
  266. bad_licenses = expand_wildcard_licenses(d, bad_licenses)
  267. return truevalue if license in bad_licenses else falsevalue
  268. def incompatible_pkg_license(d, dont_want_licenses, license):
  269. # Handles an "or" or two license sets provided by
  270. # flattened_licenses(), pick one that works if possible.
  271. def choose_lic_set(a, b):
  272. return a if all(oe.license.license_ok(canonical_license(d, lic),
  273. dont_want_licenses) for lic in a) else b
  274. try:
  275. licenses = oe.license.flattened_licenses(license, choose_lic_set)
  276. except oe.license.LicenseError as exc:
  277. bb.fatal('%s: %s' % (d.getVar('P'), exc))
  278. incompatible_lic = []
  279. for l in licenses:
  280. license = canonical_license(d, l)
  281. if not oe.license.license_ok(license, dont_want_licenses):
  282. incompatible_lic.append(license)
  283. return sorted(incompatible_lic)
  284. def incompatible_license(d, dont_want_licenses, package=None):
  285. """
  286. This function checks if a recipe has only incompatible licenses. It also
  287. take into consideration 'or' operand. dont_want_licenses should be passed
  288. as canonical (SPDX) names.
  289. """
  290. import oe.license
  291. license = d.getVar("LICENSE:%s" % package) if package else None
  292. if not license:
  293. license = d.getVar('LICENSE')
  294. return incompatible_pkg_license(d, dont_want_licenses, license)
  295. def check_license_flags(d):
  296. """
  297. This function checks if a recipe has any LICENSE_FLAGS that
  298. aren't acceptable.
  299. If it does, it returns the all LICENSE_FLAGS missing from the list
  300. of acceptable license flags, or all of the LICENSE_FLAGS if there
  301. is no list of acceptable flags.
  302. If everything is is acceptable, it returns None.
  303. """
  304. def license_flag_matches(flag, acceptlist, pn):
  305. """
  306. Return True if flag matches something in acceptlist, None if not.
  307. Before we test a flag against the acceptlist, we append _${PN}
  308. to it. We then try to match that string against the
  309. acceptlist. This covers the normal case, where we expect
  310. LICENSE_FLAGS to be a simple string like 'commercial', which
  311. the user typically matches exactly in the acceptlist by
  312. explicitly appending the package name e.g 'commercial_foo'.
  313. If we fail the match however, we then split the flag across
  314. '_' and append each fragment and test until we either match or
  315. run out of fragments.
  316. """
  317. flag_pn = ("%s_%s" % (flag, pn))
  318. for candidate in acceptlist:
  319. if flag_pn == candidate:
  320. return True
  321. flag_cur = ""
  322. flagments = flag_pn.split("_")
  323. flagments.pop() # we've already tested the full string
  324. for flagment in flagments:
  325. if flag_cur:
  326. flag_cur += "_"
  327. flag_cur += flagment
  328. for candidate in acceptlist:
  329. if flag_cur == candidate:
  330. return True
  331. return False
  332. def all_license_flags_match(license_flags, acceptlist):
  333. """ Return all unmatched flags, None if all flags match """
  334. pn = d.getVar('PN')
  335. split_acceptlist = acceptlist.split()
  336. flags = []
  337. for flag in license_flags.split():
  338. if not license_flag_matches(flag, split_acceptlist, pn):
  339. flags.append(flag)
  340. return flags if flags else None
  341. license_flags = d.getVar('LICENSE_FLAGS')
  342. if license_flags:
  343. acceptlist = d.getVar('LICENSE_FLAGS_ACCEPTED')
  344. if not acceptlist:
  345. return license_flags.split()
  346. unmatched_flags = all_license_flags_match(license_flags, acceptlist)
  347. if unmatched_flags:
  348. return unmatched_flags
  349. return None
  350. def check_license_format(d):
  351. """
  352. This function checks if LICENSE is well defined,
  353. Validate operators in LICENSES.
  354. No spaces are allowed between LICENSES.
  355. """
  356. pn = d.getVar('PN')
  357. licenses = d.getVar('LICENSE')
  358. from oe.license import license_operator, license_operator_chars, license_pattern
  359. elements = list(filter(lambda x: x.strip(), license_operator.split(licenses)))
  360. for pos, element in enumerate(elements):
  361. if license_pattern.match(element):
  362. if pos > 0 and license_pattern.match(elements[pos - 1]):
  363. oe.qa.handle_error('license-format',
  364. '%s: LICENSE value "%s" has an invalid format - license names ' \
  365. 'must be separated by the following characters to indicate ' \
  366. 'the license selection: %s' %
  367. (pn, licenses, license_operator_chars), d)
  368. elif not license_operator.match(element):
  369. oe.qa.handle_error('license-format',
  370. '%s: LICENSE value "%s" has an invalid separator "%s" that is not ' \
  371. 'in the valid list of separators (%s)' %
  372. (pn, licenses, element, license_operator_chars), d)
  373. SSTATETASKS += "do_populate_lic"
  374. do_populate_lic[sstate-inputdirs] = "${LICSSTATEDIR}"
  375. do_populate_lic[sstate-outputdirs] = "${LICENSE_DIRECTORY}/"
  376. IMAGE_CLASSES:append = " license_image"
  377. python do_populate_lic_setscene () {
  378. sstate_setscene(d)
  379. }
  380. addtask do_populate_lic_setscene