blob: 83feda0f08130264a514f5d3562cb1f3f6434d33 [file] [log] [blame]
Christoffer Jansson4e8a7732022-02-08 09:01:12 +01001#!/usr/bin/env vpython3
sakal67e414c2017-09-05 00:16:15 -07002
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 17:00:04 +010010"""Generates license markdown for a prebuilt version of WebRTC.
sakal67e414c2017-09-05 00:16:15 -070011
Artem Titarenkoa9719482018-12-11 17:00:04 +010012Licenses 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 09:01:12 +010016$ gn.py desc --all --format=json <out_directory> <target> | \
17 vpython3 -m json.tool
Artem Titarenkoa9719482018-12-11 17:00:04 +010018(see "deps" subarray)
19
20Libraries are mapped to licenses via LIB_TO_LICENSES_DICT dictionary.
21
22"""
sakal67e414c2017-09-05 00:16:15 -070023
24import sys
sakal67e414c2017-09-05 00:16:15 -070025import argparse
sakal67e414c2017-09-05 00:16:15 -070026import json
27import logging
28import os
29import re
30import subprocess
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010031from html import escape
sakal67e414c2017-09-05 00:16:15 -070032
Artem Titarenkoa9719482018-12-11 17:00:04 +010033# Third_party library to licences mapping. Keys are names of the libraries
34# (right after the `third_party/` prefix)
sakal67e414c2017-09-05 00:16:15 -070035LIB_TO_LICENSES_DICT = {
Mirko Bonadeiafe72172018-04-13 11:11:52 +020036 'abseil-cpp': ['third_party/abseil-cpp/LICENSE'],
Oleksandr Iakovenko10b63612019-08-28 09:41:26 +020037 'android_ndk': ['third_party/android_ndk/NOTICE'],
Mirko Bonadeid9b62d32019-03-29 09:51:04 +010038 'android_sdk': ['third_party/android_sdk/LICENSE'],
Mirko Bonadei8cc66952020-10-30 10:13:45 +010039 'auto': [
40 'third_party/android_deps/libs/'
41 'com_google_auto_service_auto_service/LICENSE'
42 ],
sakal67e414c2017-09-05 00:16:15 -070043 'boringssl': ['third_party/boringssl/src/LICENSE'],
Florent Castellib531ec02021-05-03 18:22:06 +020044 'crc32c': ['third_party/crc32c/src/LICENSE'],
philipeld44badf2022-07-04 10:44:14 +020045 'dav1d': ['third_party/dav1d/LICENSE'],
Mirko Bonadei8cc66952020-10-30 10:13:45 +010046 'errorprone': [
47 'third_party/android_deps/libs/'
48 'com_google_errorprone_error_prone_core/LICENSE'
49 ],
Oleh Prypind42acbb2018-01-15 20:13:16 +010050 'fiat': ['third_party/boringssl/src/third_party/fiat/LICENSE'],
Peter Kotwicz625964f2020-11-28 20:35:44 -050051 'guava': ['third_party/android_deps/libs/com_google_guava_guava/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070052 'ijar': ['third_party/ijar/LICENSE'],
53 'jsoncpp': ['third_party/jsoncpp/LICENSE'],
Danil Chapovalovc46385c2020-03-11 10:45:57 +010054 'libaom': ['third_party/libaom/source/libaom/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070055 'libc++': ['buildtools/third_party/libc++/trunk/LICENSE.TXT'],
56 'libc++abi': ['buildtools/third_party/libc++abi/trunk/LICENSE.TXT'],
Byoungchan Leed69a7262022-06-23 22:06:00 +090057 'libevent': ['third_party/libevent/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070058 'libjpeg_turbo': ['third_party/libjpeg_turbo/LICENSE.md'],
59 'libsrtp': ['third_party/libsrtp/LICENSE'],
Mirko Bonadeif5ea3b92021-11-15 15:41:28 +010060 'libunwind': ['buildtools/third_party/libunwind/trunk/LICENSE.TXT'],
sakal67e414c2017-09-05 00:16:15 -070061 'libvpx': ['third_party/libvpx/source/libvpx/LICENSE'],
62 'libyuv': ['third_party/libyuv/LICENSE'],
Mirko Bonadeib6f35a32020-04-01 19:41:00 +020063 'nasm': ['third_party/nasm/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070064 'opus': ['third_party/opus/src/COPYING'],
Alessio Bazzicaffb8c8c2019-04-01 10:19:25 +020065 'pffft': ['third_party/pffft/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070066 'protobuf': ['third_party/protobuf/LICENSE'],
Alessio Bazzicaa5b90382018-05-07 09:29:54 +000067 'rnnoise': ['third_party/rnnoise/COPYING'],
Artem Titov29835992018-08-07 11:50:54 +020068 'webrtc': ['LICENSE'],
Mirko Bonadeife48ee92018-03-20 16:43:23 +010069 'zlib': ['third_party/zlib/LICENSE'],
Artem Titova76af0c2018-07-23 17:38:12 +020070 'base64': ['rtc_base/third_party/base64/LICENSE'],
Artem Titove41c4332018-07-25 15:04:28 +020071 'sigslot': ['rtc_base/third_party/sigslot/LICENSE'],
Artem Titov8ff433a2018-07-24 13:42:22 +020072 'portaudio': ['modules/third_party/portaudio/LICENSE'],
Artem Titov8a838fd2018-07-24 15:37:09 +020073 'fft': ['modules/third_party/fft/LICENSE'],
Artem Titove095b812018-07-25 12:10:22 +020074 'g711': ['modules/third_party/g711/LICENSE'],
Artem Titov52b90002018-07-25 13:13:44 +020075 'g722': ['modules/third_party/g722/LICENSE'],
Mirko Bonadeif0d64a52020-04-17 12:25:19 +020076 'ooura': ['common_audio/third_party/ooura/LICENSE'],
Artem Titov29835992018-08-07 11:50:54 +020077 'spl_sqrt_floor': ['common_audio/third_party/spl_sqrt_floor/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070078
Yves Gerey3f752092019-11-27 17:10:17 +000079 # 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': [],
Mirko Bonadei7e225cf2021-02-09 10:37:52 +010082 # This is not a library but a collection of libraries.
83 'androidx': [],
Yves Gerey3f752092019-11-27 17:10:17 +000084
sakal67e414c2017-09-05 00:16:15 -070085 # Compile time dependencies, no license needed:
86 'yasm': [],
Yura Yaroshevich29c36b22018-06-12 19:59:02 +030087 'ow2_asm': [],
Mirko Bonadei447cd3e2020-06-03 21:53:28 +020088 'jdk': [],
sakal67e414c2017-09-05 00:16:15 -070089}
90
Artem Titarenkoa9719482018-12-11 17:00:04 +010091# Third_party library _regex_ to licences mapping. Keys are regular expression
92# with names of the libraries (right after the `third_party/` prefix)
93LIB_REGEX_TO_LICENSES_DICT = {
94 'android_deps:android_support_annotations.*': [
95 'third_party/android_deps/libs/' +
96 'com_android_support_support_annotations/LICENSE'
97 ],
98
99 # Internal dependencies, licenses are already included by other dependencies
100 'android_deps:com_android_support_support_annotations.*': [],
101}
102
103
hanpfeiedbaf572022-04-20 10:14:45 +0800104SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
Mirko Bonadeid8665442018-09-04 12:17:27 +0200105WEBRTC_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
hanpfeiedbaf572022-04-20 10:14:45 +0800106SRC_DIR = os.path.dirname(os.path.dirname(SCRIPT_DIR))
Mirko Bonadeid8665442018-09-04 12:17:27 +0200107sys.path.append(os.path.join(SRC_DIR, 'build'))
Henrik Kjellanderec57e052017-10-17 21:36:01 +0200108import find_depot_tools
109
Artem Titarenkoa9719482018-12-11 17:00:04 +0100110THIRD_PARTY_LIB_SIMPLE_NAME_REGEX = r'^.*/third_party/([\w\-+]+).*$'
111THIRD_PARTY_LIB_REGEX_TEMPLATE = r'^.*/third_party/%s$'
112
sakal67e414c2017-09-05 00:16:15 -0700113
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100114class LicenseBuilder:
115 def __init__(self,
116 buildfile_dirs,
117 targets,
118 lib_to_licenses_dict=None,
119 lib_regex_to_licenses_dict=None):
120 if lib_to_licenses_dict is None:
121 lib_to_licenses_dict = LIB_TO_LICENSES_DICT
sakal67e414c2017-09-05 00:16:15 -0700122
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100123 if lib_regex_to_licenses_dict is None:
124 lib_regex_to_licenses_dict = LIB_REGEX_TO_LICENSES_DICT
Artem Titarenkoa9719482018-12-11 17:00:04 +0100125
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100126 self.buildfile_dirs = buildfile_dirs
127 self.targets = targets
128 self.lib_to_licenses_dict = lib_to_licenses_dict
129 self.lib_regex_to_licenses_dict = lib_regex_to_licenses_dict
Artem Titarenkoa9719482018-12-11 17:00:04 +0100130
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100131 self.common_licenses_dict = self.lib_to_licenses_dict.copy()
132 self.common_licenses_dict.update(self.lib_regex_to_licenses_dict)
Artem Titarenkoa9719482018-12-11 17:00:04 +0100133
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100134 @staticmethod
135 def _ParseLibraryName(dep):
136 """Returns library name after third_party
sakal67e414c2017-09-05 00:16:15 -0700137
138 Input one of:
139 //a/b/third_party/libname:c
140 //a/b/third_party/libname:c(//d/e/f:g)
141 //a/b/third_party/libname/c:d(//e/f/g:h)
142
Artem Titarenkoa9719482018-12-11 17:00:04 +0100143 Outputs libname or None if this is not a third_party dependency.
sakal67e414c2017-09-05 00:16:15 -0700144 """
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100145 groups = re.match(THIRD_PARTY_LIB_SIMPLE_NAME_REGEX, dep)
146 return groups.group(1) if groups else None
Artem Titarenkoa9719482018-12-11 17:00:04 +0100147
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100148 def _ParseLibrary(self, dep):
149 """Returns library simple or regex name that matches `dep` after third_party
Artem Titarenkoa9719482018-12-11 17:00:04 +0100150
151 This method matches `dep` dependency against simple names in
152 LIB_TO_LICENSES_DICT and regular expression names in
153 LIB_REGEX_TO_LICENSES_DICT keys
154
155 Outputs matched dict key or None if this is not a third_party dependency.
156 """
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100157 libname = LicenseBuilder._ParseLibraryName(dep)
Artem Titarenkoa9719482018-12-11 17:00:04 +0100158
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100159 for lib_regex in self.lib_regex_to_licenses_dict:
160 if re.match(THIRD_PARTY_LIB_REGEX_TEMPLATE % lib_regex, dep):
161 return lib_regex
Artem Titarenkoa9719482018-12-11 17:00:04 +0100162
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100163 return libname
sakal67e414c2017-09-05 00:16:15 -0700164
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100165 @staticmethod
166 def _RunGN(buildfile_dir, target):
167 cmd = [
168 sys.executable,
169 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py'),
170 'desc',
171 '--all',
172 '--format=json',
173 os.path.abspath(buildfile_dir),
174 target,
175 ]
176 logging.debug('Running: %r', cmd)
177 output_json = subprocess.check_output(cmd, cwd=WEBRTC_ROOT).decode('UTF-8')
178 logging.debug('Output: %s', output_json)
179 return output_json
sakal67e414c2017-09-05 00:16:15 -0700180
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100181 def _GetThirdPartyLibraries(self, buildfile_dir, target):
182 output = json.loads(LicenseBuilder._RunGN(buildfile_dir, target))
183 libraries = set()
184 for described_target in list(output.values()):
185 third_party_libs = (self._ParseLibrary(dep)
186 for dep in described_target['deps'])
187 libraries |= set(lib for lib in third_party_libs if lib)
188 return libraries
sakal67e414c2017-09-05 00:16:15 -0700189
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100190 def GenerateLicenseText(self, output_dir):
191 # Get a list of third_party libs from gn. For fat libraries we must consider
192 # all architectures, hence the multiple buildfile directories.
193 third_party_libs = set()
194 for buildfile in self.buildfile_dirs:
195 for target in self.targets:
196 third_party_libs |= self._GetThirdPartyLibraries(buildfile, target)
197 assert len(third_party_libs) > 0
sakal67e414c2017-09-05 00:16:15 -0700198
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100199 missing_licenses = third_party_libs - set(self.common_licenses_dict.keys())
200 if missing_licenses:
201 error_msg = 'Missing licenses for following third_party targets: %s' % \
202 ', '.join(sorted(missing_licenses))
203 logging.error(error_msg)
204 raise Exception(error_msg)
sakal67e414c2017-09-05 00:16:15 -0700205
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100206 # Put webrtc at the front of the list.
207 license_libs = sorted(third_party_libs)
208 license_libs.insert(0, 'webrtc')
sakal67e414c2017-09-05 00:16:15 -0700209
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100210 logging.info('List of licenses: %s', ', '.join(license_libs))
sakal67e414c2017-09-05 00:16:15 -0700211
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100212 # Generate markdown.
213 output_license_file = open(os.path.join(output_dir, 'LICENSE.md'), 'w+')
214 for license_lib in license_libs:
215 if len(self.common_licenses_dict[license_lib]) == 0:
216 logging.info('Skipping compile time or internal dependency: %s',
217 license_lib)
218 continue # Compile time dependency
sakal67e414c2017-09-05 00:16:15 -0700219
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100220 output_license_file.write('# %s\n' % license_lib)
221 output_license_file.write('```\n')
222 for path in self.common_licenses_dict[license_lib]:
223 license_path = os.path.join(WEBRTC_ROOT, path)
224 with open(license_path, 'r') as license_file:
225 license_text = escape(license_file.read(), quote=True)
226 output_license_file.write(license_text)
227 output_license_file.write('\n')
228 output_license_file.write('```\n\n')
sakal67e414c2017-09-05 00:16:15 -0700229
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100230 output_license_file.close()
sakal67e414c2017-09-05 00:16:15 -0700231
232
233def main():
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100234 parser = argparse.ArgumentParser(description='Generate WebRTC LICENSE.md')
235 parser.add_argument('--verbose',
236 action='store_true',
237 default=False,
238 help='Debug logging.')
239 parser.add_argument('--target',
240 required=True,
241 action='append',
242 default=[],
243 help='Name of the GN target to generate a license for')
244 parser.add_argument('output_dir', help='Directory to output LICENSE.md to.')
245 parser.add_argument('buildfile_dirs',
246 nargs='+',
247 help='Directories containing gn generated ninja files')
248 args = parser.parse_args()
sakal67e414c2017-09-05 00:16:15 -0700249
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100250 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
sakal67e414c2017-09-05 00:16:15 -0700251
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100252 builder = LicenseBuilder(args.buildfile_dirs, args.target)
253 builder.GenerateLicenseText(args.output_dir)
sakal67e414c2017-09-05 00:16:15 -0700254
255
256if __name__ == '__main__':
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100257 sys.exit(main())