blob: 10d6f675217419a5ffd67b6f34bf3fdf622dd4ab [file] [log] [blame]
Christoffer Jansson4e8a7732022-02-08 08:01:121#!/usr/bin/env vpython3
sakal67e414c2017-09-05 07:16:152
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.
Artem Titarenkoa9719482018-12-11 16:00:0410"""Generates license markdown for a prebuilt version of WebRTC.
sakal67e414c2017-09-05 07:16:1511
Artem Titarenkoa9719482018-12-11 16:00:0412Licenses 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:
Christoffer Jansson4e8a7732022-02-08 08:01:1216$ gn.py desc --all --format=json <out_directory> <target> | \
17 vpython3 -m json.tool
Artem Titarenkoa9719482018-12-11 16:00:0418(see "deps" subarray)
19
20Libraries are mapped to licenses via LIB_TO_LICENSES_DICT dictionary.
21
22"""
sakal67e414c2017-09-05 07:16:1523
24import sys
sakal67e414c2017-09-05 07:16:1525import argparse
sakal67e414c2017-09-05 07:16:1526import json
27import logging
28import os
29import re
30import subprocess
Christoffer Jansson4e8a7732022-02-08 08:01:1231from html import escape
sakal67e414c2017-09-05 07:16:1532
Artem Titarenkoa9719482018-12-11 16:00:0433# Third_party library to licences mapping. Keys are names of the libraries
34# (right after the `third_party/` prefix)
sakal67e414c2017-09-05 07:16:1535LIB_TO_LICENSES_DICT = {
Mirko Bonadeiafe72172018-04-13 09:11:5236 'abseil-cpp': ['third_party/abseil-cpp/LICENSE'],
Mirko Bonadeid9b62d32019-03-29 08:51:0437 'android_sdk': ['third_party/android_sdk/LICENSE'],
Prashanth Swaminathan9e639fa2023-06-02 22:52:1038 'android_toolchain': ['third_party/android_toolchain/NOTICE'],
Mirko Bonadei8cc66952020-10-30 09:13:4539 'auto': [
40 'third_party/android_deps/libs/'
41 'com_google_auto_service_auto_service/LICENSE'
42 ],
sakal67e414c2017-09-05 07:16:1543 'boringssl': ['third_party/boringssl/src/LICENSE'],
Florent Castellib531ec02021-05-03 16:22:0644 'crc32c': ['third_party/crc32c/src/LICENSE'],
Jeremy Lecontee44a1552023-06-01 09:07:2245 'cpu_features': ['third_party/cpu_features/src/LICENSE'],
philipeld44badf2022-07-04 08:44:1446 'dav1d': ['third_party/dav1d/LICENSE'],
Mirko Bonadei8cc66952020-10-30 09:13:4547 'errorprone': [
48 'third_party/android_deps/libs/'
49 'com_google_errorprone_error_prone_core/LICENSE'
50 ],
Oleh Prypind42acbb2018-01-15 19:13:1651 'fiat': ['third_party/boringssl/src/third_party/fiat/LICENSE'],
Peter Kotwicz625964f2020-11-29 01:35:4452 'guava': ['third_party/android_deps/libs/com_google_guava_guava/LICENSE'],
sakal67e414c2017-09-05 07:16:1553 'ijar': ['third_party/ijar/LICENSE'],
54 'jsoncpp': ['third_party/jsoncpp/LICENSE'],
Danil Chapovalovc46385c2020-03-11 09:45:5755 'libaom': ['third_party/libaom/source/libaom/LICENSE'],
Björn Terelius51f6a202023-08-09 15:34:1456 'libc++': ['third_party/libc++/src/LICENSE.TXT'],
57 'libc++abi': ['third_party/libc++abi/src/LICENSE.TXT'],
Byoungchan Leed69a7262022-06-23 13:06:0058 'libevent': ['third_party/libevent/LICENSE'],
sakal67e414c2017-09-05 07:16:1559 'libjpeg_turbo': ['third_party/libjpeg_turbo/LICENSE.md'],
60 'libsrtp': ['third_party/libsrtp/LICENSE'],
Björn Terelius51f6a202023-08-09 15:34:1461 'libunwind': ['third_party/libunwind/src/LICENSE.TXT'],
sakal67e414c2017-09-05 07:16:1562 'libvpx': ['third_party/libvpx/source/libvpx/LICENSE'],
63 'libyuv': ['third_party/libyuv/LICENSE'],
Mirko Bonadeib6f35a32020-04-01 17:41:0064 'nasm': ['third_party/nasm/LICENSE'],
sakal67e414c2017-09-05 07:16:1565 'opus': ['third_party/opus/src/COPYING'],
Alessio Bazzicaffb8c8c2019-04-01 08:19:2566 'pffft': ['third_party/pffft/LICENSE'],
sakal67e414c2017-09-05 07:16:1567 'protobuf': ['third_party/protobuf/LICENSE'],
Alessio Bazzicaa5b90382018-05-07 09:29:5468 'rnnoise': ['third_party/rnnoise/COPYING'],
Artem Titov29835992018-08-07 09:50:5469 'webrtc': ['LICENSE'],
Mirko Bonadeife48ee92018-03-20 15:43:2370 'zlib': ['third_party/zlib/LICENSE'],
Artem Titova76af0c2018-07-23 15:38:1271 'base64': ['rtc_base/third_party/base64/LICENSE'],
Artem Titove41c4332018-07-25 13:04:2872 'sigslot': ['rtc_base/third_party/sigslot/LICENSE'],
Artem Titov8ff433a2018-07-24 11:42:2273 'portaudio': ['modules/third_party/portaudio/LICENSE'],
Artem Titov8a838fd2018-07-24 13:37:0974 'fft': ['modules/third_party/fft/LICENSE'],
Artem Titove095b812018-07-25 10:10:2275 'g711': ['modules/third_party/g711/LICENSE'],
Artem Titov52b90002018-07-25 11:13:4476 'g722': ['modules/third_party/g722/LICENSE'],
Mirko Bonadeif0d64a52020-04-17 10:25:1977 'ooura': ['common_audio/third_party/ooura/LICENSE'],
Artem Titov29835992018-08-07 09:50:5478 'spl_sqrt_floor': ['common_audio/third_party/spl_sqrt_floor/LICENSE'],
Mirko Bonadeicd3e1d02023-02-08 09:43:1379 'kotlin_stdlib': ['third_party/kotlin_stdlib/LICENSE'],
Manashi Sarkar91a7beb2023-12-05 15:53:5080 'jni_zero': ['third_party/jni_zero/LICENSE'],
Christoffer Dewerin81a91172024-04-08 07:26:4181 'protobuf-javascript': ['third_party/protobuf-javascript/LICENSE'],
Evan Shrubsolecd098582024-04-30 07:57:4982 'perfetto': ['third_party/perfetto/LICENSE'],
Yves Gerey3f752092019-11-27 17:10:1783 # TODO(bugs.webrtc.org/1110): Remove this hack. This is not a lib.
Christoffer Dewerin81a91172024-04-08 07:26:4184 # For some reason it is listed as so in _get_third_party_libraries.
Yves Gerey3f752092019-11-27 17:10:1785 'android_deps': [],
Mirko Bonadei7e225cf2021-02-09 09:37:5286 # This is not a library but a collection of libraries.
87 'androidx': [],
Yves Gerey3f752092019-11-27 17:10:1788
sakal67e414c2017-09-05 07:16:1589 # Compile time dependencies, no license needed:
Yura Yaroshevich29c36b22018-06-12 16:59:0290 'ow2_asm': [],
Mirko Bonadei447cd3e2020-06-03 19:53:2891 'jdk': [],
sakal67e414c2017-09-05 07:16:1592}
93
Artem Titarenkoa9719482018-12-11 16:00:0494# Third_party library _regex_ to licences mapping. Keys are regular expression
95# with names of the libraries (right after the `third_party/` prefix)
96LIB_REGEX_TO_LICENSES_DICT = {
97 'android_deps:android_support_annotations.*': [
98 'third_party/android_deps/libs/' +
99 'com_android_support_support_annotations/LICENSE'
100 ],
101
Christoffer Dewerin81a91172024-04-08 07:26:41102 # Internal dependencies, licenses are already included by other deps.
Artem Titarenkoa9719482018-12-11 16:00:04103 'android_deps:com_android_support_support_annotations.*': [],
104}
105
106
hanpfeiedbaf572022-04-20 02:14:45107SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
Mirko Bonadeid8665442018-09-04 10:17:27108WEBRTC_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
hanpfeiedbaf572022-04-20 02:14:45109SRC_DIR = os.path.dirname(os.path.dirname(SCRIPT_DIR))
Alex Cooper813177d2022-08-31 17:55:37110
111# Chromium, and potentially other repositories, embed us in the location
112# "//third_party/webrtc". When this is the case, we expect that some of the
113# tools we need are *actually* in their build folder, thus we need to move up
114# to the *true* source root, when we're embedded like this.
115if SRC_DIR.endswith(os.path.join('third_party', 'webrtc')):
Christoffer Dewerin81a91172024-04-08 07:26:41116 SRC_DIR = os.path.abspath(os.path.join(SRC_DIR, os.pardir, os.pardir))
Mirko Bonadeid8665442018-09-04 10:17:27117sys.path.append(os.path.join(SRC_DIR, 'build'))
Henrik Kjellanderec57e052017-10-17 19:36:01118import find_depot_tools
119
Artem Titarenkoa9719482018-12-11 16:00:04120THIRD_PARTY_LIB_SIMPLE_NAME_REGEX = r'^.*/third_party/([\w\-+]+).*$'
121THIRD_PARTY_LIB_REGEX_TEMPLATE = r'^.*/third_party/%s$'
122
sakal67e414c2017-09-05 07:16:15123
Christoffer Jansson4e8a7732022-02-08 08:01:12124class LicenseBuilder:
sakal67e414c2017-09-05 07:16:15125
Christoffer Dewerin81a91172024-04-08 07:26:41126 def __init__(self,
127 buildfile_dirs,
128 targets,
129 lib_to_licenses_dict=None,
130 lib_regex_to_licenses_dict=None):
131 if lib_to_licenses_dict is None:
132 lib_to_licenses_dict = LIB_TO_LICENSES_DICT
Artem Titarenkoa9719482018-12-11 16:00:04133
Christoffer Dewerin81a91172024-04-08 07:26:41134 if lib_regex_to_licenses_dict is None:
135 lib_regex_to_licenses_dict = LIB_REGEX_TO_LICENSES_DICT
Artem Titarenkoa9719482018-12-11 16:00:04136
Christoffer Dewerin81a91172024-04-08 07:26:41137 self.buildfile_dirs = buildfile_dirs
138 self.targets = targets
139 self.lib_to_licenses_dict = lib_to_licenses_dict
140 self.lib_regex_to_licenses_dict = lib_regex_to_licenses_dict
Artem Titarenkoa9719482018-12-11 16:00:04141
Christoffer Dewerin81a91172024-04-08 07:26:41142 self.common_licenses_dict = self.lib_to_licenses_dict.copy()
143 self.common_licenses_dict.update(self.lib_regex_to_licenses_dict)
144
145 @staticmethod
146 def _parse_library_name(dep):
147 """Returns library name after third_party
sakal67e414c2017-09-05 07:16:15148
149 Input one of:
150 //a/b/third_party/libname:c
151 //a/b/third_party/libname:c(//d/e/f:g)
152 //a/b/third_party/libname/c:d(//e/f/g:h)
153
Artem Titarenkoa9719482018-12-11 16:00:04154 Outputs libname or None if this is not a third_party dependency.
sakal67e414c2017-09-05 07:16:15155 """
Christoffer Dewerin81a91172024-04-08 07:26:41156 groups = re.match(THIRD_PARTY_LIB_SIMPLE_NAME_REGEX, dep)
157 return groups.group(1) if groups else None
Artem Titarenkoa9719482018-12-11 16:00:04158
Christoffer Dewerin81a91172024-04-08 07:26:41159 def _parse_library(self, dep):
160 """Return library simple or regex name matching `dep` after third_party
Artem Titarenkoa9719482018-12-11 16:00:04161
162 This method matches `dep` dependency against simple names in
163 LIB_TO_LICENSES_DICT and regular expression names in
164 LIB_REGEX_TO_LICENSES_DICT keys
165
166 Outputs matched dict key or None if this is not a third_party dependency.
167 """
Christoffer Dewerin81a91172024-04-08 07:26:41168 libname = LicenseBuilder._parse_library_name(dep)
Artem Titarenkoa9719482018-12-11 16:00:04169
Christoffer Dewerin81a91172024-04-08 07:26:41170 for lib_regex in self.lib_regex_to_licenses_dict:
171 if re.match(THIRD_PARTY_LIB_REGEX_TEMPLATE % lib_regex, dep):
172 return lib_regex
Artem Titarenkoa9719482018-12-11 16:00:04173
Christoffer Dewerin81a91172024-04-08 07:26:41174 return libname
sakal67e414c2017-09-05 07:16:15175
Christoffer Dewerin81a91172024-04-08 07:26:41176 @staticmethod
177 def _run_gn(buildfile_dir, target):
178 cmd = [
179 sys.executable,
180 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py'),
181 'desc',
182 '--all',
183 '--format=json',
184 os.path.abspath(buildfile_dir),
185 target,
186 ]
187 logging.debug('Running: %r', cmd)
188 output_json = subprocess.check_output(cmd,
189 cwd=WEBRTC_ROOT).decode('UTF-8')
190 logging.debug('Output: %s', output_json)
191 return output_json
sakal67e414c2017-09-05 07:16:15192
Christoffer Dewerin81a91172024-04-08 07:26:41193 def _get_third_party_libraries(self, buildfile_dir, target):
194 output = json.loads(LicenseBuilder._run_gn(buildfile_dir, target))
195 libraries = set()
196 for described_target in list(output.values()):
197 third_party_libs = (self._parse_library(dep)
198 for dep in described_target['deps'])
199 libraries |= set(lib for lib in third_party_libs if lib)
200 return libraries
sakal67e414c2017-09-05 07:16:15201
Christoffer Dewerin81a91172024-04-08 07:26:41202 def generate_license_text(self, output_dir):
203 # Get a list of third_party libs from gn. For fat libraries we must
204 # consider all architectures, hence the multiple buildfile directories.
205 third_party_libs = set()
206 for buildfile in self.buildfile_dirs:
207 for target in self.targets:
208 third_party_libs |= self._get_third_party_libraries(
209 buildfile, target)
210 assert len(third_party_libs) > 0
sakal67e414c2017-09-05 07:16:15211
Christoffer Dewerin81a91172024-04-08 07:26:41212 missing_licenses = third_party_libs - set(
213 self.common_licenses_dict.keys())
214 if missing_licenses:
215 error_msg = 'Missing licenses for third_party targets: %s' % \
216 ', '.join(sorted(missing_licenses))
217 logging.error(error_msg)
218 raise Exception(error_msg)
sakal67e414c2017-09-05 07:16:15219
Christoffer Dewerin81a91172024-04-08 07:26:41220 # Put webrtc at the front of the list.
221 license_libs = sorted(third_party_libs)
222 license_libs.insert(0, 'webrtc')
sakal67e414c2017-09-05 07:16:15223
Christoffer Dewerin81a91172024-04-08 07:26:41224 logging.info('List of licenses: %s', ', '.join(license_libs))
sakal67e414c2017-09-05 07:16:15225
Christoffer Dewerin81a91172024-04-08 07:26:41226 # Generate markdown.
227 output_license_file = open(os.path.join(output_dir, 'LICENSE.md'),
228 'w+')
229 for license_lib in license_libs:
230 if len(self.common_licenses_dict[license_lib]) == 0:
231 logging.info(
232 'Skipping compile time or internal dependency: %s',
233 license_lib)
234 continue # Compile time dependency
sakal67e414c2017-09-05 07:16:15235
Christoffer Dewerin81a91172024-04-08 07:26:41236 output_license_file.write('# %s\n' % license_lib)
237 output_license_file.write('```\n')
238 for path in self.common_licenses_dict[license_lib]:
239 license_path = os.path.join(WEBRTC_ROOT, path)
240 with open(license_path, 'r') as license_file:
241 license_text = escape(license_file.read(), quote=True)
242 output_license_file.write(license_text)
243 output_license_file.write('\n')
244 output_license_file.write('```\n\n')
sakal67e414c2017-09-05 07:16:15245
Christoffer Dewerin81a91172024-04-08 07:26:41246 output_license_file.close()
sakal67e414c2017-09-05 07:16:15247
248
249def main():
Christoffer Dewerin81a91172024-04-08 07:26:41250 parser = argparse.ArgumentParser(description='Generate WebRTC LICENSE.md')
251 parser.add_argument('--verbose',
252 action='store_true',
253 default=False,
254 help='Debug logging.')
255 parser.add_argument('--target',
256 required=True,
257 action='append',
258 default=[],
259 help='Name of the GN target to generate a license for')
260 parser.add_argument('output_dir',
261 help='Directory to output LICENSE.md to.')
262 parser.add_argument('buildfile_dirs',
263 nargs='+',
264 help='Directories containing gn generated ninja files')
265 args = parser.parse_args()
sakal67e414c2017-09-05 07:16:15266
Christoffer Dewerin81a91172024-04-08 07:26:41267 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
sakal67e414c2017-09-05 07:16:15268
Christoffer Dewerin81a91172024-04-08 07:26:41269 builder = LicenseBuilder(args.buildfile_dirs, args.target)
270 builder.generate_license_text(args.output_dir)
sakal67e414c2017-09-05 07:16:15271
272
273if __name__ == '__main__':
Christoffer Dewerin81a91172024-04-08 07:26:41274 sys.exit(main())