xref: /aosp_15_r20/external/webrtc/tools_webrtc/libs/generate_licenses.py (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1#!/usr/bin/env vpython3
2
3#  Copyright 2016 The WebRTC project authors. All Rights Reserved.
4#
5#  Use of this source code is governed by a BSD-style license
6#  that can be found in the LICENSE file in the root of the source
7#  tree. An additional intellectual property rights grant can be found
8#  in the file PATENTS.  All contributing project authors may
9#  be found in the AUTHORS file in the root of the source tree.
10"""Generates license markdown for a prebuilt version of WebRTC.
11
12Licenses are taken from dependent libraries which are determined by
13GN desc command `gn desc` on all targets specified via `--target` argument.
14
15One can see all dependencies by invoking this command:
16$ gn.py desc --all --format=json <out_directory> <target> | \
17        vpython3 -m json.tool
18(see "deps" subarray)
19
20Libraries are mapped to licenses via LIB_TO_LICENSES_DICT dictionary.
21
22"""
23
24import sys
25import argparse
26import json
27import logging
28import os
29import re
30import subprocess
31from html import escape
32
33# Third_party library to licences mapping. Keys are names of the libraries
34# (right after the `third_party/` prefix)
35LIB_TO_LICENSES_DICT = {
36    'abseil-cpp': ['third_party/abseil-cpp/LICENSE'],
37    'android_ndk': ['third_party/android_ndk/NOTICE'],
38    'android_sdk': ['third_party/android_sdk/LICENSE'],
39    'auto': [
40        'third_party/android_deps/libs/'
41        'com_google_auto_service_auto_service/LICENSE'
42    ],
43    'boringssl': ['third_party/boringssl/src/LICENSE'],
44    'crc32c': ['third_party/crc32c/src/LICENSE'],
45    'dav1d': ['third_party/dav1d/LICENSE'],
46    'errorprone': [
47        'third_party/android_deps/libs/'
48        'com_google_errorprone_error_prone_core/LICENSE'
49    ],
50    'fiat': ['third_party/boringssl/src/third_party/fiat/LICENSE'],
51    'guava': ['third_party/android_deps/libs/com_google_guava_guava/LICENSE'],
52    'ijar': ['third_party/ijar/LICENSE'],
53    'jsoncpp': ['third_party/jsoncpp/LICENSE'],
54    'libaom': ['third_party/libaom/source/libaom/LICENSE'],
55    'libc++': ['buildtools/third_party/libc++/trunk/LICENSE.TXT'],
56    'libc++abi': ['buildtools/third_party/libc++abi/trunk/LICENSE.TXT'],
57    'libevent': ['third_party/libevent/LICENSE'],
58    'libjpeg_turbo': ['third_party/libjpeg_turbo/LICENSE.md'],
59    'libsrtp': ['third_party/libsrtp/LICENSE'],
60    'libunwind': ['buildtools/third_party/libunwind/trunk/LICENSE.TXT'],
61    'libvpx': ['third_party/libvpx/source/libvpx/LICENSE'],
62    'libyuv': ['third_party/libyuv/LICENSE'],
63    'nasm': ['third_party/nasm/LICENSE'],
64    'opus': ['third_party/opus/src/COPYING'],
65    'pffft': ['third_party/pffft/LICENSE'],
66    'protobuf': ['third_party/protobuf/LICENSE'],
67    'rnnoise': ['third_party/rnnoise/COPYING'],
68    'webrtc': ['LICENSE'],
69    'zlib': ['third_party/zlib/LICENSE'],
70    'base64': ['rtc_base/third_party/base64/LICENSE'],
71    'sigslot': ['rtc_base/third_party/sigslot/LICENSE'],
72    'portaudio': ['modules/third_party/portaudio/LICENSE'],
73    'fft': ['modules/third_party/fft/LICENSE'],
74    'g711': ['modules/third_party/g711/LICENSE'],
75    'g722': ['modules/third_party/g722/LICENSE'],
76    'ooura': ['common_audio/third_party/ooura/LICENSE'],
77    'spl_sqrt_floor': ['common_audio/third_party/spl_sqrt_floor/LICENSE'],
78
79    # TODO(bugs.webrtc.org/1110): Remove this hack. This is not a lib.
80    # For some reason it is listed as so in _GetThirdPartyLibraries.
81    'android_deps': [],
82    # This is not a library but a collection of libraries.
83    'androidx': [],
84
85    # Compile time dependencies, no license needed:
86    'ow2_asm': [],
87    'jdk': [],
88}
89
90# Third_party library _regex_ to licences mapping. Keys are regular expression
91# with names of the libraries (right after the `third_party/` prefix)
92LIB_REGEX_TO_LICENSES_DICT = {
93    'android_deps:android_support_annotations.*': [
94        'third_party/android_deps/libs/' +
95        'com_android_support_support_annotations/LICENSE'
96    ],
97
98    # Internal dependencies, licenses are already included by other dependencies
99    'android_deps:com_android_support_support_annotations.*': [],
100}
101
102
103SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
104WEBRTC_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
105SRC_DIR = os.path.dirname(os.path.dirname(SCRIPT_DIR))
106
107# Chromium, and potentially other repositories, embed us in the location
108# "//third_party/webrtc". When this is the case, we expect that some of the
109# tools we need are *actually* in their build folder, thus we need to move up
110# to the *true* source root, when we're embedded like this.
111if SRC_DIR.endswith(os.path.join('third_party', 'webrtc')):
112  SRC_DIR = os.path.abspath(os.path.join(SRC_DIR, os.pardir, os.pardir))
113sys.path.append(os.path.join(SRC_DIR, 'build'))
114import find_depot_tools
115
116THIRD_PARTY_LIB_SIMPLE_NAME_REGEX = r'^.*/third_party/([\w\-+]+).*$'
117THIRD_PARTY_LIB_REGEX_TEMPLATE = r'^.*/third_party/%s$'
118
119
120class LicenseBuilder:
121  def __init__(self,
122               buildfile_dirs,
123               targets,
124               lib_to_licenses_dict=None,
125               lib_regex_to_licenses_dict=None):
126    if lib_to_licenses_dict is None:
127      lib_to_licenses_dict = LIB_TO_LICENSES_DICT
128
129    if lib_regex_to_licenses_dict is None:
130      lib_regex_to_licenses_dict = LIB_REGEX_TO_LICENSES_DICT
131
132    self.buildfile_dirs = buildfile_dirs
133    self.targets = targets
134    self.lib_to_licenses_dict = lib_to_licenses_dict
135    self.lib_regex_to_licenses_dict = lib_regex_to_licenses_dict
136
137    self.common_licenses_dict = self.lib_to_licenses_dict.copy()
138    self.common_licenses_dict.update(self.lib_regex_to_licenses_dict)
139
140  @staticmethod
141  def _ParseLibraryName(dep):
142    """Returns library name after third_party
143
144    Input one of:
145    //a/b/third_party/libname:c
146    //a/b/third_party/libname:c(//d/e/f:g)
147    //a/b/third_party/libname/c:d(//e/f/g:h)
148
149    Outputs libname or None if this is not a third_party dependency.
150    """
151    groups = re.match(THIRD_PARTY_LIB_SIMPLE_NAME_REGEX, dep)
152    return groups.group(1) if groups else None
153
154  def _ParseLibrary(self, dep):
155    """Returns library simple or regex name that matches `dep` after third_party
156
157    This method matches `dep` dependency against simple names in
158    LIB_TO_LICENSES_DICT and regular expression names in
159    LIB_REGEX_TO_LICENSES_DICT keys
160
161    Outputs matched dict key or None if this is not a third_party dependency.
162    """
163    libname = LicenseBuilder._ParseLibraryName(dep)
164
165    for lib_regex in self.lib_regex_to_licenses_dict:
166      if re.match(THIRD_PARTY_LIB_REGEX_TEMPLATE % lib_regex, dep):
167        return lib_regex
168
169    return libname
170
171  @staticmethod
172  def _RunGN(buildfile_dir, target):
173    cmd = [
174        sys.executable,
175        os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py'),
176        'desc',
177        '--all',
178        '--format=json',
179        os.path.abspath(buildfile_dir),
180        target,
181    ]
182    logging.debug('Running: %r', cmd)
183    output_json = subprocess.check_output(cmd, cwd=WEBRTC_ROOT).decode('UTF-8')
184    logging.debug('Output: %s', output_json)
185    return output_json
186
187  def _GetThirdPartyLibraries(self, buildfile_dir, target):
188    output = json.loads(LicenseBuilder._RunGN(buildfile_dir, target))
189    libraries = set()
190    for described_target in list(output.values()):
191      third_party_libs = (self._ParseLibrary(dep)
192                          for dep in described_target['deps'])
193      libraries |= set(lib for lib in third_party_libs if lib)
194    return libraries
195
196  def GenerateLicenseText(self, output_dir):
197    # Get a list of third_party libs from gn. For fat libraries we must consider
198    # all architectures, hence the multiple buildfile directories.
199    third_party_libs = set()
200    for buildfile in self.buildfile_dirs:
201      for target in self.targets:
202        third_party_libs |= self._GetThirdPartyLibraries(buildfile, target)
203    assert len(third_party_libs) > 0
204
205    missing_licenses = third_party_libs - set(self.common_licenses_dict.keys())
206    if missing_licenses:
207      error_msg = 'Missing licenses for following third_party targets: %s' % \
208                  ', '.join(sorted(missing_licenses))
209      logging.error(error_msg)
210      raise Exception(error_msg)
211
212    # Put webrtc at the front of the list.
213    license_libs = sorted(third_party_libs)
214    license_libs.insert(0, 'webrtc')
215
216    logging.info('List of licenses: %s', ', '.join(license_libs))
217
218    # Generate markdown.
219    output_license_file = open(os.path.join(output_dir, 'LICENSE.md'), 'w+')
220    for license_lib in license_libs:
221      if len(self.common_licenses_dict[license_lib]) == 0:
222        logging.info('Skipping compile time or internal dependency: %s',
223                     license_lib)
224        continue  # Compile time dependency
225
226      output_license_file.write('# %s\n' % license_lib)
227      output_license_file.write('```\n')
228      for path in self.common_licenses_dict[license_lib]:
229        license_path = os.path.join(WEBRTC_ROOT, path)
230        with open(license_path, 'r') as license_file:
231          license_text = escape(license_file.read(), quote=True)
232          output_license_file.write(license_text)
233          output_license_file.write('\n')
234      output_license_file.write('```\n\n')
235
236    output_license_file.close()
237
238
239def main():
240  parser = argparse.ArgumentParser(description='Generate WebRTC LICENSE.md')
241  parser.add_argument('--verbose',
242                      action='store_true',
243                      default=False,
244                      help='Debug logging.')
245  parser.add_argument('--target',
246                      required=True,
247                      action='append',
248                      default=[],
249                      help='Name of the GN target to generate a license for')
250  parser.add_argument('output_dir', help='Directory to output LICENSE.md to.')
251  parser.add_argument('buildfile_dirs',
252                      nargs='+',
253                      help='Directories containing gn generated ninja files')
254  args = parser.parse_args()
255
256  logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
257
258  builder = LicenseBuilder(args.buildfile_dirs, args.target)
259  builder.GenerateLicenseText(args.output_dir)
260
261
262if __name__ == '__main__':
263  sys.exit(main())
264