blob: 86b4cd01f0b7a931977b14e1f8bf677f718661ac [file] [log] [blame]
Byoungchan Lee5be2aa12021-05-29 13:50:31 +09001#!/usr/bin/env python3
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:
16$ gn.py desc --all --format=json <out_directory> <target> | python -m json.tool
17(see "deps" subarray)
18
19Libraries are mapped to licenses via LIB_TO_LICENSES_DICT dictionary.
20
21"""
sakal67e414c2017-09-05 00:16:15 -070022
23import sys
24
25import argparse
sakal67e414c2017-09-05 00:16:15 -070026import json
27import logging
28import os
29import re
30import subprocess
Byoungchan Lee5be2aa12021-05-29 13:50:31 +090031try:
32 # python 3.2+
33 from html import escape
34except ImportError:
35 from cgi import escape
sakal67e414c2017-09-05 00:16:15 -070036
Artem Titarenkoa9719482018-12-11 17:00:04 +010037# Third_party library to licences mapping. Keys are names of the libraries
38# (right after the `third_party/` prefix)
sakal67e414c2017-09-05 00:16:15 -070039LIB_TO_LICENSES_DICT = {
Mirko Bonadeiafe72172018-04-13 11:11:52 +020040 'abseil-cpp': ['third_party/abseil-cpp/LICENSE'],
Oleksandr Iakovenko10b63612019-08-28 09:41:26 +020041 'android_ndk': ['third_party/android_ndk/NOTICE'],
Mirko Bonadeid9b62d32019-03-29 09:51:04 +010042 'android_sdk': ['third_party/android_sdk/LICENSE'],
Mirko Bonadei8cc66952020-10-30 10:13:45 +010043 'auto': [
44 'third_party/android_deps/libs/'
45 'com_google_auto_service_auto_service/LICENSE'
46 ],
Sami Kalliomäkie7fac682018-03-20 16:32:49 +010047 'bazel': ['third_party/bazel/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070048 'boringssl': ['third_party/boringssl/src/LICENSE'],
Florent Castellib531ec02021-05-03 18:22:06 +020049 'crc32c': ['third_party/crc32c/src/LICENSE'],
Mirko Bonadei8cc66952020-10-30 10:13:45 +010050 'errorprone': [
51 'third_party/android_deps/libs/'
52 'com_google_errorprone_error_prone_core/LICENSE'
53 ],
Oleh Prypind42acbb2018-01-15 20:13:16 +010054 'fiat': ['third_party/boringssl/src/third_party/fiat/LICENSE'],
Peter Kotwicz625964f2020-11-28 20:35:44 -050055 'guava': ['third_party/android_deps/libs/com_google_guava_guava/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070056 'ijar': ['third_party/ijar/LICENSE'],
57 'jsoncpp': ['third_party/jsoncpp/LICENSE'],
Danil Chapovalovc46385c2020-03-11 10:45:57 +010058 'libaom': ['third_party/libaom/source/libaom/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070059 'libc++': ['buildtools/third_party/libc++/trunk/LICENSE.TXT'],
60 'libc++abi': ['buildtools/third_party/libc++abi/trunk/LICENSE.TXT'],
61 'libevent': ['base/third_party/libevent/LICENSE'],
62 'libjpeg_turbo': ['third_party/libjpeg_turbo/LICENSE.md'],
63 'libsrtp': ['third_party/libsrtp/LICENSE'],
Mirko Bonadeif5ea3b92021-11-15 15:41:28 +010064 'libunwind': ['buildtools/third_party/libunwind/trunk/LICENSE.TXT'],
sakal67e414c2017-09-05 00:16:15 -070065 'libvpx': ['third_party/libvpx/source/libvpx/LICENSE'],
66 'libyuv': ['third_party/libyuv/LICENSE'],
Mirko Bonadeib6f35a32020-04-01 19:41:00 +020067 'nasm': ['third_party/nasm/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070068 'opus': ['third_party/opus/src/COPYING'],
Alessio Bazzicaffb8c8c2019-04-01 10:19:25 +020069 'pffft': ['third_party/pffft/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070070 'protobuf': ['third_party/protobuf/LICENSE'],
Alessio Bazzicaa5b90382018-05-07 09:29:54 +000071 'rnnoise': ['third_party/rnnoise/COPYING'],
sakal67e414c2017-09-05 00:16:15 -070072 'usrsctp': ['third_party/usrsctp/LICENSE'],
Artem Titov29835992018-08-07 11:50:54 +020073 'webrtc': ['LICENSE'],
Mirko Bonadeife48ee92018-03-20 16:43:23 +010074 'zlib': ['third_party/zlib/LICENSE'],
Artem Titova76af0c2018-07-23 17:38:12 +020075 'base64': ['rtc_base/third_party/base64/LICENSE'],
Artem Titove41c4332018-07-25 15:04:28 +020076 'sigslot': ['rtc_base/third_party/sigslot/LICENSE'],
Artem Titov8ff433a2018-07-24 13:42:22 +020077 'portaudio': ['modules/third_party/portaudio/LICENSE'],
Artem Titov8a838fd2018-07-24 15:37:09 +020078 'fft': ['modules/third_party/fft/LICENSE'],
Artem Titove095b812018-07-25 12:10:22 +020079 'g711': ['modules/third_party/g711/LICENSE'],
Artem Titov52b90002018-07-25 13:13:44 +020080 'g722': ['modules/third_party/g722/LICENSE'],
Mirko Bonadeif0d64a52020-04-17 12:25:19 +020081 'ooura': ['common_audio/third_party/ooura/LICENSE'],
Artem Titov29835992018-08-07 11:50:54 +020082 'spl_sqrt_floor': ['common_audio/third_party/spl_sqrt_floor/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070083
Yves Gerey3f752092019-11-27 17:10:17 +000084 # TODO(bugs.webrtc.org/1110): Remove this hack. This is not a lib.
85 # For some reason it is listed as so in _GetThirdPartyLibraries.
86 'android_deps': [],
Mirko Bonadei7e225cf2021-02-09 10:37:52 +010087 # This is not a library but a collection of libraries.
88 'androidx': [],
Yves Gerey3f752092019-11-27 17:10:17 +000089
sakal67e414c2017-09-05 00:16:15 -070090 # Compile time dependencies, no license needed:
91 'yasm': [],
Yura Yaroshevich29c36b22018-06-12 19:59:02 +030092 'ow2_asm': [],
Mirko Bonadei447cd3e2020-06-03 21:53:28 +020093 'jdk': [],
sakal67e414c2017-09-05 00:16:15 -070094}
95
Artem Titarenkoa9719482018-12-11 17:00:04 +010096# Third_party library _regex_ to licences mapping. Keys are regular expression
97# with names of the libraries (right after the `third_party/` prefix)
98LIB_REGEX_TO_LICENSES_DICT = {
99 'android_deps:android_support_annotations.*': [
100 'third_party/android_deps/libs/' +
101 'com_android_support_support_annotations/LICENSE'
102 ],
103
104 # Internal dependencies, licenses are already included by other dependencies
105 'android_deps:com_android_support_support_annotations.*': [],
106}
107
108
109def FindSrcDirPath():
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100110 """Returns the abs path to the src/ dir of the project."""
111 src_dir = os.path.dirname(os.path.abspath(__file__))
112 while os.path.basename(src_dir) != 'src':
113 src_dir = os.path.normpath(os.path.join(src_dir, os.pardir))
114 return src_dir
Artem Titarenkoa9719482018-12-11 17:00:04 +0100115
116
sakal67e414c2017-09-05 00:16:15 -0700117SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
Mirko Bonadeid8665442018-09-04 12:17:27 +0200118WEBRTC_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
119SRC_DIR = FindSrcDirPath()
120sys.path.append(os.path.join(SRC_DIR, 'build'))
Henrik Kjellanderec57e052017-10-17 21:36:01 +0200121import find_depot_tools
122
Artem Titarenkoa9719482018-12-11 17:00:04 +0100123THIRD_PARTY_LIB_SIMPLE_NAME_REGEX = r'^.*/third_party/([\w\-+]+).*$'
124THIRD_PARTY_LIB_REGEX_TEMPLATE = r'^.*/third_party/%s$'
125
sakal67e414c2017-09-05 00:16:15 -0700126
127class LicenseBuilder(object):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100128 def __init__(self,
129 buildfile_dirs,
130 targets,
131 lib_to_licenses_dict=None,
132 lib_regex_to_licenses_dict=None):
133 if lib_to_licenses_dict is None:
134 lib_to_licenses_dict = LIB_TO_LICENSES_DICT
sakal67e414c2017-09-05 00:16:15 -0700135
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100136 if lib_regex_to_licenses_dict is None:
137 lib_regex_to_licenses_dict = LIB_REGEX_TO_LICENSES_DICT
Artem Titarenkoa9719482018-12-11 17:00:04 +0100138
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100139 self.buildfile_dirs = buildfile_dirs
140 self.targets = targets
141 self.lib_to_licenses_dict = lib_to_licenses_dict
142 self.lib_regex_to_licenses_dict = lib_regex_to_licenses_dict
Artem Titarenkoa9719482018-12-11 17:00:04 +0100143
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100144 self.common_licenses_dict = self.lib_to_licenses_dict.copy()
145 self.common_licenses_dict.update(self.lib_regex_to_licenses_dict)
Artem Titarenkoa9719482018-12-11 17:00:04 +0100146
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100147 @staticmethod
148 def _ParseLibraryName(dep):
149 """Returns library name after third_party
sakal67e414c2017-09-05 00:16:15 -0700150
151 Input one of:
152 //a/b/third_party/libname:c
153 //a/b/third_party/libname:c(//d/e/f:g)
154 //a/b/third_party/libname/c:d(//e/f/g:h)
155
Artem Titarenkoa9719482018-12-11 17:00:04 +0100156 Outputs libname or None if this is not a third_party dependency.
sakal67e414c2017-09-05 00:16:15 -0700157 """
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100158 groups = re.match(THIRD_PARTY_LIB_SIMPLE_NAME_REGEX, dep)
159 return groups.group(1) if groups else None
Artem Titarenkoa9719482018-12-11 17:00:04 +0100160
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100161 def _ParseLibrary(self, dep):
162 """Returns library simple or regex name that matches `dep` after third_party
Artem Titarenkoa9719482018-12-11 17:00:04 +0100163
164 This method matches `dep` dependency against simple names in
165 LIB_TO_LICENSES_DICT and regular expression names in
166 LIB_REGEX_TO_LICENSES_DICT keys
167
168 Outputs matched dict key or None if this is not a third_party dependency.
169 """
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100170 libname = LicenseBuilder._ParseLibraryName(dep)
Artem Titarenkoa9719482018-12-11 17:00:04 +0100171
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100172 for lib_regex in self.lib_regex_to_licenses_dict:
173 if re.match(THIRD_PARTY_LIB_REGEX_TEMPLATE % lib_regex, dep):
174 return lib_regex
Artem Titarenkoa9719482018-12-11 17:00:04 +0100175
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100176 return libname
sakal67e414c2017-09-05 00:16:15 -0700177
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100178 @staticmethod
179 def _RunGN(buildfile_dir, target):
180 cmd = [
181 sys.executable,
182 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py'),
183 'desc',
184 '--all',
185 '--format=json',
186 os.path.abspath(buildfile_dir),
187 target,
188 ]
189 logging.debug('Running: %r', cmd)
Byoungchan Lee5be2aa12021-05-29 13:50:31 +0900190 output_json = subprocess.check_output(cmd, cwd=WEBRTC_ROOT).decode('UTF-8')
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100191 logging.debug('Output: %s', output_json)
192 return output_json
sakal67e414c2017-09-05 00:16:15 -0700193
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100194 def _GetThirdPartyLibraries(self, buildfile_dir, target):
195 output = json.loads(LicenseBuilder._RunGN(buildfile_dir, target))
196 libraries = set()
197 for described_target in output.values():
198 third_party_libs = (self._ParseLibrary(dep)
199 for dep in described_target['deps'])
200 libraries |= set(lib for lib in third_party_libs if lib)
201 return libraries
sakal67e414c2017-09-05 00:16:15 -0700202
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100203 def GenerateLicenseText(self, output_dir):
204 # Get a list of third_party libs from gn. For fat libraries we must consider
205 # all architectures, hence the multiple buildfile directories.
206 third_party_libs = set()
207 for buildfile in self.buildfile_dirs:
208 for target in self.targets:
209 third_party_libs |= self._GetThirdPartyLibraries(
210 buildfile, target)
211 assert len(third_party_libs) > 0
sakal67e414c2017-09-05 00:16:15 -0700212
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100213 missing_licenses = third_party_libs - set(
214 self.common_licenses_dict.keys())
215 if missing_licenses:
216 error_msg = 'Missing licenses for following third_party targets: %s' % \
Byoungchan Lee5be2aa12021-05-29 13:50:31 +0900217 ', '.join(sorted(missing_licenses))
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100218 logging.error(error_msg)
219 raise Exception(error_msg)
sakal67e414c2017-09-05 00:16:15 -0700220
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100221 # Put webrtc at the front of the list.
222 license_libs = sorted(third_party_libs)
223 license_libs.insert(0, 'webrtc')
sakal67e414c2017-09-05 00:16:15 -0700224
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100225 logging.info('List of licenses: %s', ', '.join(license_libs))
sakal67e414c2017-09-05 00:16:15 -0700226
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100227 # Generate markdown.
228 output_license_file = open(os.path.join(output_dir, 'LICENSE.md'),
229 'w+')
230 for license_lib in license_libs:
231 if len(self.common_licenses_dict[license_lib]) == 0:
232 logging.info(
233 'Skipping compile time or internal dependency: %s',
234 license_lib)
235 continue # Compile time dependency
sakal67e414c2017-09-05 00:16:15 -0700236
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100237 output_license_file.write('# %s\n' % license_lib)
238 output_license_file.write('```\n')
239 for path in self.common_licenses_dict[license_lib]:
240 license_path = os.path.join(WEBRTC_ROOT, path)
241 with open(license_path, 'r') as license_file:
Byoungchan Lee5be2aa12021-05-29 13:50:31 +0900242 license_text = escape(license_file.read(), quote=True)
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100243 output_license_file.write(license_text)
244 output_license_file.write('\n')
245 output_license_file.write('```\n\n')
sakal67e414c2017-09-05 00:16:15 -0700246
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100247 output_license_file.close()
sakal67e414c2017-09-05 00:16:15 -0700248
249
250def main():
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100251 parser = argparse.ArgumentParser(description='Generate WebRTC LICENSE.md')
252 parser.add_argument('--verbose',
253 action='store_true',
254 default=False,
255 help='Debug logging.')
256 parser.add_argument('--target',
257 required=True,
258 action='append',
259 default=[],
260 help='Name of the GN target to generate a license for')
261 parser.add_argument('output_dir',
262 help='Directory to output LICENSE.md to.')
263 parser.add_argument('buildfile_dirs',
264 nargs='+',
265 help='Directories containing gn generated ninja files')
266 args = parser.parse_args()
sakal67e414c2017-09-05 00:16:15 -0700267
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100268 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
sakal67e414c2017-09-05 00:16:15 -0700269
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100270 builder = LicenseBuilder(args.buildfile_dirs, args.target)
271 builder.GenerateLicenseText(args.output_dir)
sakal67e414c2017-09-05 00:16:15 -0700272
273
274if __name__ == '__main__':
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100275 sys.exit(main())