1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7# Filter the license, the copyleft_should_include returns True for the
8# COPYLEFT_LICENSE_INCLUDE recipe, and False for the
9# COPYLEFT_LICENSE_EXCLUDE.
10#
11# By default, includes all GPL and LGPL, and excludes CLOSED and Proprietary.
12
13COPYLEFT_LICENSE_INCLUDE ?= 'GPL* LGPL* AGPL*'
14COPYLEFT_LICENSE_INCLUDE[type] = 'list'
15COPYLEFT_LICENSE_INCLUDE[doc] = 'Space separated list of globs which include licenses'
16
17COPYLEFT_LICENSE_EXCLUDE ?= 'CLOSED Proprietary'
18COPYLEFT_LICENSE_EXCLUDE[type] = 'list'
19COPYLEFT_LICENSE_EXCLUDE[doc] = 'Space separated list of globs which exclude licenses'
20
21COPYLEFT_RECIPE_TYPE ?= '${@copyleft_recipe_type(d)}'
22COPYLEFT_RECIPE_TYPE[doc] = 'The "type" of the current recipe (e.g. target, native, cross)'
23
24COPYLEFT_RECIPE_TYPES ?= 'target'
25COPYLEFT_RECIPE_TYPES[type] = 'list'
26COPYLEFT_RECIPE_TYPES[doc] = 'Space separated list of recipe types to include'
27
28COPYLEFT_AVAILABLE_RECIPE_TYPES = 'target native nativesdk cross crosssdk cross-canadian'
29COPYLEFT_AVAILABLE_RECIPE_TYPES[type] = 'list'
30COPYLEFT_AVAILABLE_RECIPE_TYPES[doc] = 'Space separated list of available recipe types'
31
32COPYLEFT_PN_INCLUDE ?= ''
33COPYLEFT_PN_INCLUDE[type] = 'list'
34COPYLEFT_PN_INCLUDE[doc] = 'Space separated list of recipe names to include'
35
36COPYLEFT_PN_EXCLUDE ?= ''
37COPYLEFT_PN_EXCLUDE[type] = 'list'
38COPYLEFT_PN_EXCLUDE[doc] = 'Space separated list of recipe names to exclude'
39
40def copyleft_recipe_type(d):
41    for recipe_type in oe.data.typed_value('COPYLEFT_AVAILABLE_RECIPE_TYPES', d):
42        if oe.utils.inherits(d, recipe_type):
43            return recipe_type
44    return 'target'
45
46def copyleft_should_include(d):
47    """
48    Determine if this recipe's sources should be deployed for compliance
49    """
50    import ast
51    import oe.license
52    from fnmatch import fnmatchcase as fnmatch
53
54    recipe_type = d.getVar('COPYLEFT_RECIPE_TYPE')
55    if recipe_type not in oe.data.typed_value('COPYLEFT_RECIPE_TYPES', d):
56        included, motive = False, 'recipe type "%s" is excluded' % recipe_type
57    else:
58        included, motive = False, 'recipe did not match anything'
59
60        include = oe.data.typed_value('COPYLEFT_LICENSE_INCLUDE', d)
61        exclude = oe.data.typed_value('COPYLEFT_LICENSE_EXCLUDE', d)
62
63        try:
64            is_included, reason = oe.license.is_included(d.getVar('LICENSE'), include, exclude)
65        except oe.license.LicenseError as exc:
66            bb.fatal('%s: %s' % (d.getVar('PF'), exc))
67        else:
68            if is_included:
69                if reason:
70                    included, motive = True, 'recipe has included licenses: %s' % ', '.join(reason)
71                else:
72                    included, motive = False, 'recipe does not include a copyleft license'
73            else:
74                included, motive = False, 'recipe has excluded licenses: %s' % ', '.join(reason)
75
76    if any(fnmatch(d.getVar('PN'), name) \
77            for name in oe.data.typed_value('COPYLEFT_PN_INCLUDE', d)):
78        included, motive =  True, 'recipe included by name'
79    if any(fnmatch(d.getVar('PN'), name) \
80            for name in oe.data.typed_value('COPYLEFT_PN_EXCLUDE', d)):
81        included, motive = False, 'recipe excluded by name'
82
83    return included, motive
84