blob: f33c05029150313b113774972ccc24ccbe622cc0 [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'],
Mirko Bonadei8cc66952020-10-30 10:13:45 +010045 'errorprone': [
46 'third_party/android_deps/libs/'
47 'com_google_errorprone_error_prone_core/LICENSE'
48 ],
Oleh Prypind42acbb2018-01-15 20:13:16 +010049 'fiat': ['third_party/boringssl/src/third_party/fiat/LICENSE'],
Peter Kotwicz625964f2020-11-28 20:35:44 -050050 'guava': ['third_party/android_deps/libs/com_google_guava_guava/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070051 'ijar': ['third_party/ijar/LICENSE'],
52 'jsoncpp': ['third_party/jsoncpp/LICENSE'],
Danil Chapovalovc46385c2020-03-11 10:45:57 +010053 'libaom': ['third_party/libaom/source/libaom/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070054 'libc++': ['buildtools/third_party/libc++/trunk/LICENSE.TXT'],
55 'libc++abi': ['buildtools/third_party/libc++abi/trunk/LICENSE.TXT'],
56 'libevent': ['base/third_party/libevent/LICENSE'],
57 'libjpeg_turbo': ['third_party/libjpeg_turbo/LICENSE.md'],
58 'libsrtp': ['third_party/libsrtp/LICENSE'],
59 'libvpx': ['third_party/libvpx/source/libvpx/LICENSE'],
60 'libyuv': ['third_party/libyuv/LICENSE'],
Mirko Bonadeib6f35a32020-04-01 19:41:00 +020061 'nasm': ['third_party/nasm/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070062 'opus': ['third_party/opus/src/COPYING'],
Alessio Bazzicaffb8c8c2019-04-01 10:19:25 +020063 'pffft': ['third_party/pffft/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070064 'protobuf': ['third_party/protobuf/LICENSE'],
Alessio Bazzicaa5b90382018-05-07 09:29:54 +000065 'rnnoise': ['third_party/rnnoise/COPYING'],
sakal67e414c2017-09-05 00:16:15 -070066 'usrsctp': ['third_party/usrsctp/LICENSE'],
Artem Titov29835992018-08-07 11:50:54 +020067 'webrtc': ['LICENSE'],
Mirko Bonadeife48ee92018-03-20 16:43:23 +010068 'zlib': ['third_party/zlib/LICENSE'],
Artem Titova76af0c2018-07-23 17:38:12 +020069 'base64': ['rtc_base/third_party/base64/LICENSE'],
Artem Titove41c4332018-07-25 15:04:28 +020070 'sigslot': ['rtc_base/third_party/sigslot/LICENSE'],
Artem Titov8ff433a2018-07-24 13:42:22 +020071 'portaudio': ['modules/third_party/portaudio/LICENSE'],
Artem Titov8a838fd2018-07-24 15:37:09 +020072 'fft': ['modules/third_party/fft/LICENSE'],
Artem Titove095b812018-07-25 12:10:22 +020073 'g711': ['modules/third_party/g711/LICENSE'],
Artem Titov52b90002018-07-25 13:13:44 +020074 'g722': ['modules/third_party/g722/LICENSE'],
Mirko Bonadeif0d64a52020-04-17 12:25:19 +020075 'ooura': ['common_audio/third_party/ooura/LICENSE'],
Artem Titov29835992018-08-07 11:50:54 +020076 'spl_sqrt_floor': ['common_audio/third_party/spl_sqrt_floor/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070077
Yves Gerey3f752092019-11-27 17:10:17 +000078 # TODO(bugs.webrtc.org/1110): Remove this hack. This is not a lib.
79 # For some reason it is listed as so in _GetThirdPartyLibraries.
80 'android_deps': [],
81
sakal67e414c2017-09-05 00:16:15 -070082 # Compile time dependencies, no license needed:
83 'yasm': [],
Yura Yaroshevich29c36b22018-06-12 19:59:02 +030084 'ow2_asm': [],
Mirko Bonadei447cd3e2020-06-03 21:53:28 +020085 'jdk': [],
sakal67e414c2017-09-05 00:16:15 -070086}
87
Artem Titarenkoa9719482018-12-11 17:00:04 +010088# Third_party library _regex_ to licences mapping. Keys are regular expression
89# with names of the libraries (right after the `third_party/` prefix)
90LIB_REGEX_TO_LICENSES_DICT = {
91 'android_deps:android_support_annotations.*': [
92 'third_party/android_deps/libs/' +
93 'com_android_support_support_annotations/LICENSE'
94 ],
95
96 # Internal dependencies, licenses are already included by other dependencies
97 'android_deps:com_android_support_support_annotations.*': [],
98}
99
100
101def FindSrcDirPath():
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100102 """Returns the abs path to the src/ dir of the project."""
103 src_dir = os.path.dirname(os.path.abspath(__file__))
104 while os.path.basename(src_dir) != 'src':
105 src_dir = os.path.normpath(os.path.join(src_dir, os.pardir))
106 return src_dir
Artem Titarenkoa9719482018-12-11 17:00:04 +0100107
108
sakal67e414c2017-09-05 00:16:15 -0700109SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
Mirko Bonadeid8665442018-09-04 12:17:27 +0200110WEBRTC_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
111SRC_DIR = FindSrcDirPath()
112sys.path.append(os.path.join(SRC_DIR, 'build'))
Henrik Kjellanderec57e052017-10-17 21:36:01 +0200113import find_depot_tools
114
Artem Titarenkoa9719482018-12-11 17:00:04 +0100115THIRD_PARTY_LIB_SIMPLE_NAME_REGEX = r'^.*/third_party/([\w\-+]+).*$'
116THIRD_PARTY_LIB_REGEX_TEMPLATE = r'^.*/third_party/%s$'
117
sakal67e414c2017-09-05 00:16:15 -0700118
119class LicenseBuilder(object):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100120 def __init__(self,
121 buildfile_dirs,
122 targets,
123 lib_to_licenses_dict=None,
124 lib_regex_to_licenses_dict=None):
125 if lib_to_licenses_dict is None:
126 lib_to_licenses_dict = LIB_TO_LICENSES_DICT
sakal67e414c2017-09-05 00:16:15 -0700127
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100128 if lib_regex_to_licenses_dict is None:
129 lib_regex_to_licenses_dict = LIB_REGEX_TO_LICENSES_DICT
Artem Titarenkoa9719482018-12-11 17:00:04 +0100130
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100131 self.buildfile_dirs = buildfile_dirs
132 self.targets = targets
133 self.lib_to_licenses_dict = lib_to_licenses_dict
134 self.lib_regex_to_licenses_dict = lib_regex_to_licenses_dict
Artem Titarenkoa9719482018-12-11 17:00:04 +0100135
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100136 self.common_licenses_dict = self.lib_to_licenses_dict.copy()
137 self.common_licenses_dict.update(self.lib_regex_to_licenses_dict)
Artem Titarenkoa9719482018-12-11 17:00:04 +0100138
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100139 @staticmethod
140 def _ParseLibraryName(dep):
141 """Returns library name after third_party
sakal67e414c2017-09-05 00:16:15 -0700142
143 Input one of:
144 //a/b/third_party/libname:c
145 //a/b/third_party/libname:c(//d/e/f:g)
146 //a/b/third_party/libname/c:d(//e/f/g:h)
147
Artem Titarenkoa9719482018-12-11 17:00:04 +0100148 Outputs libname or None if this is not a third_party dependency.
sakal67e414c2017-09-05 00:16:15 -0700149 """
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100150 groups = re.match(THIRD_PARTY_LIB_SIMPLE_NAME_REGEX, dep)
151 return groups.group(1) if groups else None
Artem Titarenkoa9719482018-12-11 17:00:04 +0100152
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100153 def _ParseLibrary(self, dep):
154 """Returns library simple or regex name that matches `dep` after third_party
Artem Titarenkoa9719482018-12-11 17:00:04 +0100155
156 This method matches `dep` dependency against simple names in
157 LIB_TO_LICENSES_DICT and regular expression names in
158 LIB_REGEX_TO_LICENSES_DICT keys
159
160 Outputs matched dict key or None if this is not a third_party dependency.
161 """
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100162 libname = LicenseBuilder._ParseLibraryName(dep)
Artem Titarenkoa9719482018-12-11 17:00:04 +0100163
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100164 for lib_regex in self.lib_regex_to_licenses_dict:
165 if re.match(THIRD_PARTY_LIB_REGEX_TEMPLATE % lib_regex, dep):
166 return lib_regex
Artem Titarenkoa9719482018-12-11 17:00:04 +0100167
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100168 return libname
sakal67e414c2017-09-05 00:16:15 -0700169
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100170 @staticmethod
171 def _RunGN(buildfile_dir, target):
172 cmd = [
173 sys.executable,
174 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py'),
175 'desc',
176 '--all',
177 '--format=json',
178 os.path.abspath(buildfile_dir),
179 target,
180 ]
181 logging.debug('Running: %r', cmd)
182 output_json = subprocess.check_output(cmd, cwd=WEBRTC_ROOT)
183 logging.debug('Output: %s', output_json)
184 return output_json
sakal67e414c2017-09-05 00:16:15 -0700185
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100186 def _GetThirdPartyLibraries(self, buildfile_dir, target):
187 output = json.loads(LicenseBuilder._RunGN(buildfile_dir, target))
188 libraries = set()
189 for described_target in output.values():
190 third_party_libs = (self._ParseLibrary(dep)
191 for dep in described_target['deps'])
192 libraries |= set(lib for lib in third_party_libs if lib)
193 return libraries
sakal67e414c2017-09-05 00:16:15 -0700194
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100195 def GenerateLicenseText(self, output_dir):
196 # Get a list of third_party libs from gn. For fat libraries we must consider
197 # all architectures, hence the multiple buildfile directories.
198 third_party_libs = set()
199 for buildfile in self.buildfile_dirs:
200 for target in self.targets:
201 third_party_libs |= self._GetThirdPartyLibraries(
202 buildfile, target)
203 assert len(third_party_libs) > 0
sakal67e414c2017-09-05 00:16:15 -0700204
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100205 missing_licenses = third_party_libs - set(
206 self.common_licenses_dict.keys())
207 if missing_licenses:
208 error_msg = 'Missing licenses for following third_party targets: %s' % \
209 ', '.join(missing_licenses)
210 logging.error(error_msg)
211 raise Exception(error_msg)
sakal67e414c2017-09-05 00:16:15 -0700212
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100213 # Put webrtc at the front of the list.
214 license_libs = sorted(third_party_libs)
215 license_libs.insert(0, 'webrtc')
sakal67e414c2017-09-05 00:16:15 -0700216
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100217 logging.info('List of licenses: %s', ', '.join(license_libs))
sakal67e414c2017-09-05 00:16:15 -0700218
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100219 # Generate markdown.
220 output_license_file = open(os.path.join(output_dir, 'LICENSE.md'),
221 'w+')
222 for license_lib in license_libs:
223 if len(self.common_licenses_dict[license_lib]) == 0:
224 logging.info(
225 'Skipping compile time or internal dependency: %s',
226 license_lib)
227 continue # Compile time dependency
sakal67e414c2017-09-05 00:16:15 -0700228
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100229 output_license_file.write('# %s\n' % license_lib)
230 output_license_file.write('```\n')
231 for path in self.common_licenses_dict[license_lib]:
232 license_path = os.path.join(WEBRTC_ROOT, path)
233 with open(license_path, 'r') as license_file:
234 license_text = cgi.escape(license_file.read(), quote=True)
235 output_license_file.write(license_text)
236 output_license_file.write('\n')
237 output_license_file.write('```\n\n')
sakal67e414c2017-09-05 00:16:15 -0700238
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100239 output_license_file.close()
sakal67e414c2017-09-05 00:16:15 -0700240
241
242def main():
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100243 parser = argparse.ArgumentParser(description='Generate WebRTC LICENSE.md')
244 parser.add_argument('--verbose',
245 action='store_true',
246 default=False,
247 help='Debug logging.')
248 parser.add_argument('--target',
249 required=True,
250 action='append',
251 default=[],
252 help='Name of the GN target to generate a license for')
253 parser.add_argument('output_dir',
254 help='Directory to output LICENSE.md to.')
255 parser.add_argument('buildfile_dirs',
256 nargs='+',
257 help='Directories containing gn generated ninja files')
258 args = parser.parse_args()
sakal67e414c2017-09-05 00:16:15 -0700259
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100260 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
sakal67e414c2017-09-05 00:16:15 -0700261
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100262 builder = LicenseBuilder(args.buildfile_dirs, args.target)
263 builder.GenerateLicenseText(args.output_dir)
sakal67e414c2017-09-05 00:16:15 -0700264
265
266if __name__ == '__main__':
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100267 sys.exit(main())