Skip to content

Commit

Permalink
add license_files kwarg to project
Browse files Browse the repository at this point in the history
Hook this up to installed dependency manifests. This is often needed
above and beyond just an SPDX string -- e.g. many licenses have custom
copyright lines.
  • Loading branch information
eli-schwartz committed Dec 28, 2022
1 parent 26b83ee commit 2fa0749
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 8 deletions.
5 changes: 5 additions & 0 deletions docs/markdown/snippets/license_files_project_info.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## The project function now supports setting the project license files

This goes together with the license name. The license files can be
automatically installed via [[meson.install_dependency_manifest]],
or queried via [[meson.project_license_files]].
12 changes: 10 additions & 2 deletions docs/yaml/builtins/meson.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,11 @@ methods:
returns: void
description: |
Installs a manifest file
containing a list of all subprojects, their versions and license
files to the file name given as the argument.
containing a list of all subprojects, their versions and license names
to the file name given as the argument.
If license files are defined as well, they will be copied next to the
manifest and referenced in it.
posargs:
output_name:
Expand Down Expand Up @@ -406,6 +409,11 @@ methods:
since: 0.45.0
description: Returns the array of licenses specified in [[project]] function call.

- name: project_license_files
returns: list[file]
since: 1.1.0
description: Returns the array of license files specified in the [[project]] function call.

- name: project_name
returns: str
description: Returns the project name specified in the [[project]] function call.
Expand Down
8 changes: 8 additions & 0 deletions mesonbuild/backend/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -1191,14 +1191,22 @@ def generate_depmf_install(self, d: InstallData) -> None:
return
ifilename = os.path.join(self.environment.get_build_dir(), 'depmf.json')
ofilename = os.path.join(self.environment.get_prefix(), self.build.dep_manifest_name)
odirname = os.path.join(self.environment.get_prefix(), os.path.dirname(self.build.dep_manifest_name))
out_name = os.path.join('{prefix}', self.build.dep_manifest_name)
out_dir = os.path.join('{prefix}', os.path.dirname(self.build.dep_manifest_name))
mfobj = {'type': 'dependency manifest', 'version': '1.0',
'projects': {k: v.to_json() for k, v in self.build.dep_manifest.items()}}
with open(ifilename, 'w', encoding='utf-8') as f:
f.write(json.dumps(mfobj))
# Copy file from, to, and with mode unchanged
d.data.append(InstallDataBase(ifilename, ofilename, out_name, None, '',
tag='devel', data_type='depmf'))
for m in self.build.dep_manifest.values():
for ifilename, name in m.license_files:
ofilename = os.path.join(odirname, name.relative_name())
out_name = os.path.join(out_dir, name.relative_name())
d.data.append(InstallDataBase(ifilename, ofilename, out_name, None,
m.subproject, tag='devel', data_type='depmf'))

def get_regen_filelist(self) -> T.List[str]:
'''List of all files whose alteration means that the build
Expand Down
3 changes: 3 additions & 0 deletions mesonbuild/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,14 @@ class InstallDir(HoldableObject):
class DepManifest:
version: str
license: T.List[str]
license_files: T.List[T.Tuple[str, File]]
subproject: str

def to_json(self) -> T.Dict[str, T.Union[str, T.List[str]]]:
return {
'version': self.version,
'license': self.license,
'license_files': [l[1].relative_name() for l in self.license_files],
}


Expand Down
19 changes: 16 additions & 3 deletions mesonbuild/interpreter/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,8 @@ def set_backend(self) -> None:
validator=_project_version_validator,
convertor=lambda x: x[0] if isinstance(x, list) else x,
),
KwargInfo('license', ContainerTypeInfo(list, str), default=['unknown'], listify=True),
KwargInfo('license', (ContainerTypeInfo(list, str), NoneType), default=None, listify=True),
KwargInfo('license_files', ContainerTypeInfo(list, str), default=[], listify=True, since='1.1.0'),
KwargInfo('subproject_dir', str, default='subprojects'),
)
def func_project(self, node: mparser.FunctionNode, args: T.Tuple[str, T.List[str]], kwargs: 'kwtypes.Project') -> None:
Expand Down Expand Up @@ -1198,8 +1199,20 @@ def func_project(self, node: mparser.FunctionNode, args: T.Tuple[str, T.List[str

if self.build.project_version is None:
self.build.project_version = self.project_version
proj_license = kwargs['license']
self.build.dep_manifest[proj_name] = build.DepManifest(self.project_version, proj_license)

if kwargs['license'] is None:
proj_license = ['unknown']
if kwargs['license_files']:
raise InvalidArguments('Project `license` name must be specified when `license_files` is set')
else:
proj_license = kwargs['license']
proj_license_files = []
for i in self.source_strings_to_files(kwargs['license_files']):
ifname = i.absolute_path(self.environment.source_dir,
self.environment.build_dir)
proj_license_files.append((ifname, i))
self.build.dep_manifest[proj_name] = build.DepManifest(self.project_version, proj_license,
proj_license_files, self.subproject)
if self.subproject in self.build.projects:
raise InvalidCode('Second call to project().')

Expand Down
8 changes: 8 additions & 0 deletions mesonbuild/interpreter/mesonmain.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2012-2021 The Meson development team
# Copyright © 2021 Intel Corporation
from __future__ import annotations

import os
import typing as T
Expand Down Expand Up @@ -74,6 +75,7 @@ def __init__(self, build: 'build.Build', interpreter: 'Interpreter'):
'override_find_program': self.override_find_program_method,
'project_version': self.project_version_method,
'project_license': self.project_license_method,
'project_license_files': self.project_license_files_method,
'version': self.version_method,
'project_name': self.project_name_method,
'get_cross_property': self.get_cross_property_method,
Expand Down Expand Up @@ -400,6 +402,12 @@ def project_version_method(self, args: T.List['TYPE_var'], kwargs: 'TYPE_kwargs'
def project_license_method(self, args: T.List['TYPE_var'], kwargs: 'TYPE_kwargs') -> T.List[str]:
return self.build.dep_manifest[self.interpreter.active_projectname].license

@FeatureNew('meson.project_license_files()', '1.1.0')
@noPosargs
@noKwargs
def project_license_files_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> T.List[mesonlib.File]:
return [l[1] for l in self.build.dep_manifest[self.interpreter.active_projectname].license_files]

@noPosargs
@noKwargs
def version_method(self, args: T.List['TYPE_var'], kwargs: 'TYPE_kwargs') -> MesonVersionString:
Expand Down
4 changes: 3 additions & 1 deletion test cases/common/42 subproject/meson.build
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
project('subproj user', 'c',
version : '2.3.4',
license : 'mylicense')
license : 'mylicense',
license_files: 'mylicense.txt',
)

assert(meson.project_name() == 'subproj user', 'Incorrect project name')

Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
project('subproject', 'c',
version : '1.0.0',
license : ['sublicense1', 'sublicense2'])
license : ['sublicense1', 'sublicense2'],
license_files: ['sublicense1.txt', 'sublicense2.txt'],
)

if not meson.is_subproject()
error('Claimed to be master project even though we are a subproject.')
Expand Down
Empty file.
Empty file.
5 changes: 4 additions & 1 deletion test cases/common/42 subproject/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"installed": [
{"type": "exe", "file": "usr/bin/user"},
{"type": "pdb", "file": "usr/bin/user"},
{"type": "file", "file": "usr/share/sublib/sublib.depmf"}
{"type": "file", "file": "usr/share/sublib/sublib.depmf"},
{"type": "file", "file": "usr/share/sublib/mylicense.txt"},
{"type": "file", "file": "usr/share/sublib/subprojects/sublib/sublicense1.txt"},
{"type": "file", "file": "usr/share/sublib/subprojects/sublib/sublicense2.txt"}
]
}

0 comments on commit 2fa0749

Please sign in to comment.