blob: a23123e7603a5d564d54396907dffb8bd85ad699 [file] [log] [blame]
sakal67e414c2017-09-05 00:16:15 -07001#!/usr/bin/env python
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.
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
26import cgi
27import json
28import logging
29import os
30import re
31import subprocess
32
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 ],
Sami Kalliomäkie7fac682018-03-20 16:32:49 +010043 'bazel': ['third_party/bazel/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070044 'boringssl': ['third_party/boringssl/src/LICENSE'],
Florent Castellib531ec02021-05-03 18:22:06 +020045 'crc32c': ['third_party/crc32c/src/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'],
57 'libevent': ['base/third_party/libevent/LICENSE'],
58 'libjpeg_turbo': ['third_party/libjpeg_turbo/LICENSE.md'],
59 'libsrtp': ['third_party/libsrtp/LICENSE'],
60 'libvpx': ['third_party/libvpx/source/libvpx/LICENSE'],
61 'libyuv': ['third_party/libyuv/LICENSE'],
Mirko Bonadeib6f35a32020-04-01 19:41:00 +020062 'nasm': ['third_party/nasm/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070063 'opus': ['third_party/opus/src/COPYING'],
Alessio Bazzicaffb8c8c2019-04-01 10:19:25 +020064 'pffft': ['third_party/pffft/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070065 'protobuf': ['third_party/protobuf/LICENSE'],
Alessio Bazzicaa5b90382018-05-07 09:29:54 +000066 'rnnoise': ['third_party/rnnoise/COPYING'],
sakal67e414c2017-09-05 00:16:15 -070067 'usrsctp': ['third_party/usrsctp/LICENSE'],
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
104def FindSrcDirPath():
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100105 """Returns the abs path to the src/ dir of the project."""
106 src_dir = os.path.dirname(os.path.abspath(__file__))
107 while os.path.basename(src_dir) != 'src':
108 src_dir = os.path.normpath(os.path.join(src_dir, os.pardir))
109 return src_dir
Artem Titarenkoa9719482018-12-11 17:00:04 +0100110
111
sakal67e414c2017-09-05 00:16:15 -0700112SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
Mirko Bonadeid8665442018-09-04 12:17:27 +0200113WEBRTC_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
114SRC_DIR = FindSrcDirPath()
115sys.path.append(os.path.join(SRC_DIR, 'build'))
Henrik Kjellanderec57e052017-10-17 21:36:01 +0200116import find_depot_tools
117
Artem Titarenkoa9719482018-12-11 17:00:04 +0100118THIRD_PARTY_LIB_SIMPLE_NAME_REGEX = r'^.*/third_party/([\w\-+]+).*$'
119THIRD_PARTY_LIB_REGEX_TEMPLATE = r'^.*/third_party/%s$'
120
sakal67e414c2017-09-05 00:16:15 -0700121
122class LicenseBuilder(object):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100123 def __init__(self,
124 buildfile_dirs,
125 targets,
126 lib_to_licenses_dict=None,
127 lib_regex_to_licenses_dict=None):
128 if lib_to_licenses_dict is None:
129 lib_to_licenses_dict = LIB_TO_LICENSES_DICT
sakal67e414c2017-09-05 00:16:15 -0700130
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100131 if lib_regex_to_licenses_dict is None:
132 lib_regex_to_licenses_dict = LIB_REGEX_TO_LICENSES_DICT
Artem Titarenkoa9719482018-12-11 17:00:04 +0100133
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100134 self.buildfile_dirs = buildfile_dirs
135 self.targets = targets
136 self.lib_to_licenses_dict = lib_to_licenses_dict
137 self.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.common_licenses_dict = self.lib_to_licenses_dict.copy()
140 self.common_licenses_dict.update(self.lib_regex_to_licenses_dict)
Artem Titarenkoa9719482018-12-11 17:00:04 +0100141
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100142 @staticmethod
143 def _ParseLibraryName(dep):
144 """Returns library name after third_party
sakal67e414c2017-09-05 00:16:15 -0700145
146 Input one of:
147 //a/b/third_party/libname:c
148 //a/b/third_party/libname:c(//d/e/f:g)
149 //a/b/third_party/libname/c:d(//e/f/g:h)
150
Artem Titarenkoa9719482018-12-11 17:00:04 +0100151 Outputs libname or None if this is not a third_party dependency.
sakal67e414c2017-09-05 00:16:15 -0700152 """
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100153 groups = re.match(THIRD_PARTY_LIB_SIMPLE_NAME_REGEX, dep)
154 return groups.group(1) if groups else None
Artem Titarenkoa9719482018-12-11 17:00:04 +0100155
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100156 def _ParseLibrary(self, dep):
157 """Returns library simple or regex name that matches `dep` after third_party
Artem Titarenkoa9719482018-12-11 17:00:04 +0100158
159 This method matches `dep` dependency against simple names in
160 LIB_TO_LICENSES_DICT and regular expression names in
161 LIB_REGEX_TO_LICENSES_DICT keys
162
163 Outputs matched dict key or None if this is not a third_party dependency.
164 """
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100165 libname = LicenseBuilder._ParseLibraryName(dep)
Artem Titarenkoa9719482018-12-11 17:00:04 +0100166
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100167 for lib_regex in self.lib_regex_to_licenses_dict:
168 if re.match(THIRD_PARTY_LIB_REGEX_TEMPLATE % lib_regex, dep):
169 return lib_regex
Artem Titarenkoa9719482018-12-11 17:00:04 +0100170
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100171 return libname
sakal67e414c2017-09-05 00:16:15 -0700172
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100173 @staticmethod
174 def _RunGN(buildfile_dir, target):
175 cmd = [
176 sys.executable,
177 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py'),
178 'desc',
179 '--all',
180 '--format=json',
181 os.path.abspath(buildfile_dir),
182 target,
183 ]
184 logging.debug('Running: %r', cmd)
185 output_json = subprocess.check_output(cmd, cwd=WEBRTC_ROOT)
186 logging.debug('Output: %s', output_json)
187 return output_json
sakal67e414c2017-09-05 00:16:15 -0700188
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100189 def _GetThirdPartyLibraries(self, buildfile_dir, target):
190 output = json.loads(LicenseBuilder._RunGN(buildfile_dir, target))
191 libraries = set()
192 for described_target in output.values():
193 third_party_libs = (self._ParseLibrary(dep)
194 for dep in described_target['deps'])
195 libraries |= set(lib for lib in third_party_libs if lib)
196 return libraries
sakal67e414c2017-09-05 00:16:15 -0700197
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100198 def GenerateLicenseText(self, output_dir):
199 # Get a list of third_party libs from gn. For fat libraries we must consider
200 # all architectures, hence the multiple buildfile directories.
201 third_party_libs = set()
202 for buildfile in self.buildfile_dirs:
203 for target in self.targets:
204 third_party_libs |= self._GetThirdPartyLibraries(
205 buildfile, target)
206 assert len(third_party_libs) > 0
sakal67e414c2017-09-05 00:16:15 -0700207
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100208 missing_licenses = third_party_libs - set(
209 self.common_licenses_dict.keys())
210 if missing_licenses:
211 error_msg = 'Missing licenses for following third_party targets: %s' % \
212 ', '.join(missing_licenses)
213 logging.error(error_msg)
214 raise Exception(error_msg)
sakal67e414c2017-09-05 00:16:15 -0700215
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100216 # Put webrtc at the front of the list.
217 license_libs = sorted(third_party_libs)
218 license_libs.insert(0, 'webrtc')
sakal67e414c2017-09-05 00:16:15 -0700219
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100220 logging.info('List of licenses: %s', ', '.join(license_libs))
sakal67e414c2017-09-05 00:16:15 -0700221
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100222 # Generate markdown.
223 output_license_file = open(os.path.join(output_dir, 'LICENSE.md'),
224 'w+')
225 for license_lib in license_libs:
226 if len(self.common_licenses_dict[license_lib]) == 0:
227 logging.info(
228 'Skipping compile time or internal dependency: %s',
229 license_lib)
230 continue # Compile time dependency
sakal67e414c2017-09-05 00:16:15 -0700231
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100232 output_license_file.write('# %s\n' % license_lib)
233 output_license_file.write('```\n')
234 for path in self.common_licenses_dict[license_lib]:
235 license_path = os.path.join(WEBRTC_ROOT, path)
236 with open(license_path, 'r') as license_file:
237 license_text = cgi.escape(license_file.read(), quote=True)
238 output_license_file.write(license_text)
239 output_license_file.write('\n')
240 output_license_file.write('```\n\n')
sakal67e414c2017-09-05 00:16:15 -0700241
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100242 output_license_file.close()
sakal67e414c2017-09-05 00:16:15 -0700243
244
245def main():
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100246 parser = argparse.ArgumentParser(description='Generate WebRTC LICENSE.md')
247 parser.add_argument('--verbose',
248 action='store_true',
249 default=False,
250 help='Debug logging.')
251 parser.add_argument('--target',
252 required=True,
253 action='append',
254 default=[],
255 help='Name of the GN target to generate a license for')
256 parser.add_argument('output_dir',
257 help='Directory to output LICENSE.md to.')
258 parser.add_argument('buildfile_dirs',
259 nargs='+',
260 help='Directories containing gn generated ninja files')
261 args = parser.parse_args()
sakal67e414c2017-09-05 00:16:15 -0700262
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100263 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
sakal67e414c2017-09-05 00:16:15 -0700264
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100265 builder = LicenseBuilder(args.buildfile_dirs, args.target)
266 builder.GenerateLicenseText(args.output_dir)
sakal67e414c2017-09-05 00:16:15 -0700267
268
269if __name__ == '__main__':
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100270 sys.exit(main())