blob: 6a9d8a90c1e7cf9cf0a1772fc157281413da125c [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'],
Mirko Bonadeid9b62d32019-03-29 09:51:04 +010037 'android_sdk': ['third_party/android_sdk/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070038 'android_tools': ['third_party/android_tools/LICENSE'],
Edward Lemur5c24c672017-11-06 20:29:00 +010039 'auto': ['third_party/auto/src/LICENSE.txt'],
Sami Kalliomäkie7fac682018-03-20 16:32:49 +010040 'bazel': ['third_party/bazel/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070041 'boringssl': ['third_party/boringssl/src/LICENSE'],
Mirko Bonadeif91ec562017-10-13 13:58:37 +020042 'errorprone': ['third_party/errorprone/LICENSE'],
Oleh Prypind42acbb2018-01-15 20:13:16 +010043 'fiat': ['third_party/boringssl/src/third_party/fiat/LICENSE'],
Edward Lemur5c24c672017-11-06 20:29:00 +010044 'guava': ['third_party/guava/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070045 'ijar': ['third_party/ijar/LICENSE'],
46 'jsoncpp': ['third_party/jsoncpp/LICENSE'],
47 'libc++': ['buildtools/third_party/libc++/trunk/LICENSE.TXT'],
48 'libc++abi': ['buildtools/third_party/libc++abi/trunk/LICENSE.TXT'],
49 'libevent': ['base/third_party/libevent/LICENSE'],
50 'libjpeg_turbo': ['third_party/libjpeg_turbo/LICENSE.md'],
51 'libsrtp': ['third_party/libsrtp/LICENSE'],
52 'libvpx': ['third_party/libvpx/source/libvpx/LICENSE'],
53 'libyuv': ['third_party/libyuv/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070054 'opus': ['third_party/opus/src/COPYING'],
Alessio Bazzicaffb8c8c2019-04-01 10:19:25 +020055 'pffft': ['third_party/pffft/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070056 'protobuf': ['third_party/protobuf/LICENSE'],
Alessio Bazzicaa5b90382018-05-07 09:29:54 +000057 'rnnoise': ['third_party/rnnoise/COPYING'],
sakal67e414c2017-09-05 00:16:15 -070058 'usrsctp': ['third_party/usrsctp/LICENSE'],
Artem Titov29835992018-08-07 11:50:54 +020059 'webrtc': ['LICENSE'],
Mirko Bonadeife48ee92018-03-20 16:43:23 +010060 'zlib': ['third_party/zlib/LICENSE'],
Artem Titova76af0c2018-07-23 17:38:12 +020061 'base64': ['rtc_base/third_party/base64/LICENSE'],
Artem Titove41c4332018-07-25 15:04:28 +020062 'sigslot': ['rtc_base/third_party/sigslot/LICENSE'],
Artem Titov8ff433a2018-07-24 13:42:22 +020063 'portaudio': ['modules/third_party/portaudio/LICENSE'],
Artem Titov8a838fd2018-07-24 15:37:09 +020064 'fft': ['modules/third_party/fft/LICENSE'],
Artem Titove095b812018-07-25 12:10:22 +020065 'g711': ['modules/third_party/g711/LICENSE'],
Artem Titov52b90002018-07-25 13:13:44 +020066 'g722': ['modules/third_party/g722/LICENSE'],
Artem Titov333a5052018-07-25 16:56:18 +020067 'fft4g': ['common_audio/third_party/fft4g/LICENSE'],
Artem Titov29835992018-08-07 11:50:54 +020068 'spl_sqrt_floor': ['common_audio/third_party/spl_sqrt_floor/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070069
70 # Compile time dependencies, no license needed:
71 'yasm': [],
Yura Yaroshevich29c36b22018-06-12 19:59:02 +030072 'ow2_asm': [],
sakal67e414c2017-09-05 00:16:15 -070073}
74
Artem Titarenkoa9719482018-12-11 17:00:04 +010075# Third_party library _regex_ to licences mapping. Keys are regular expression
76# with names of the libraries (right after the `third_party/` prefix)
77LIB_REGEX_TO_LICENSES_DICT = {
78 'android_deps:android_support_annotations.*': [
79 'third_party/android_deps/libs/' +
80 'com_android_support_support_annotations/LICENSE'
81 ],
82
83 # Internal dependencies, licenses are already included by other dependencies
84 'android_deps:com_android_support_support_annotations.*': [],
85}
86
87
88def FindSrcDirPath():
89 """Returns the abs path to the src/ dir of the project."""
90 src_dir = os.path.dirname(os.path.abspath(__file__))
91 while os.path.basename(src_dir) != 'src':
92 src_dir = os.path.normpath(os.path.join(src_dir, os.pardir))
93 return src_dir
94
95
sakal67e414c2017-09-05 00:16:15 -070096SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
Mirko Bonadeid8665442018-09-04 12:17:27 +020097WEBRTC_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
98SRC_DIR = FindSrcDirPath()
99sys.path.append(os.path.join(SRC_DIR, 'build'))
Henrik Kjellanderec57e052017-10-17 21:36:01 +0200100import find_depot_tools
101
Artem Titarenkoa9719482018-12-11 17:00:04 +0100102THIRD_PARTY_LIB_SIMPLE_NAME_REGEX = r'^.*/third_party/([\w\-+]+).*$'
103THIRD_PARTY_LIB_REGEX_TEMPLATE = r'^.*/third_party/%s$'
104
sakal67e414c2017-09-05 00:16:15 -0700105
106class LicenseBuilder(object):
107
Artem Titarenkoa9719482018-12-11 17:00:04 +0100108 def __init__(self,
109 buildfile_dirs,
110 targets,
111 lib_to_licenses_dict=None,
112 lib_regex_to_licenses_dict=None):
113 if lib_to_licenses_dict is None:
114 lib_to_licenses_dict = LIB_TO_LICENSES_DICT
115
116 if lib_regex_to_licenses_dict is None:
117 lib_regex_to_licenses_dict = LIB_REGEX_TO_LICENSES_DICT
118
sakal67e414c2017-09-05 00:16:15 -0700119 self.buildfile_dirs = buildfile_dirs
120 self.targets = targets
Artem Titarenkoa9719482018-12-11 17:00:04 +0100121 self.lib_to_licenses_dict = lib_to_licenses_dict
122 self.lib_regex_to_licenses_dict = lib_regex_to_licenses_dict
123
124 self.common_licenses_dict = self.lib_to_licenses_dict.copy()
125 self.common_licenses_dict.update(self.lib_regex_to_licenses_dict)
sakal67e414c2017-09-05 00:16:15 -0700126
127 @staticmethod
Artem Titarenkoa9719482018-12-11 17:00:04 +0100128 def _ParseLibraryName(dep):
129 """Returns library name after third_party
sakal67e414c2017-09-05 00:16:15 -0700130
131 Input one of:
132 //a/b/third_party/libname:c
133 //a/b/third_party/libname:c(//d/e/f:g)
134 //a/b/third_party/libname/c:d(//e/f/g:h)
135
Artem Titarenkoa9719482018-12-11 17:00:04 +0100136 Outputs libname or None if this is not a third_party dependency.
sakal67e414c2017-09-05 00:16:15 -0700137 """
Artem Titarenkoa9719482018-12-11 17:00:04 +0100138 groups = re.match(THIRD_PARTY_LIB_SIMPLE_NAME_REGEX, dep)
139 return groups.group(1) if groups else None
140
141 def _ParseLibrary(self, dep):
142 """Returns library simple or regex name that matches `dep` after third_party
143
144 This method matches `dep` dependency against simple names in
145 LIB_TO_LICENSES_DICT and regular expression names in
146 LIB_REGEX_TO_LICENSES_DICT keys
147
148 Outputs matched dict key or None if this is not a third_party dependency.
149 """
150 libname = LicenseBuilder._ParseLibraryName(dep)
151
152 for lib_regex in self.lib_regex_to_licenses_dict:
153 if re.match(THIRD_PARTY_LIB_REGEX_TEMPLATE % lib_regex, dep):
154 return lib_regex
155
156 return libname
sakal67e414c2017-09-05 00:16:15 -0700157
158 @staticmethod
159 def _RunGN(buildfile_dir, target):
Henrik Kjellanderec57e052017-10-17 21:36:01 +0200160 cmd = [
Artem Titarenkoa9719482018-12-11 17:00:04 +0100161 sys.executable,
162 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py'),
163 'desc',
164 '--all',
165 '--format=json',
166 os.path.abspath(buildfile_dir),
167 target,
Henrik Kjellanderec57e052017-10-17 21:36:01 +0200168 ]
Artem Titarenkoa9719482018-12-11 17:00:04 +0100169 logging.debug('Running: %r', cmd)
Mirko Bonadeid8665442018-09-04 12:17:27 +0200170 output_json = subprocess.check_output(cmd, cwd=WEBRTC_ROOT)
Artem Titarenkoa9719482018-12-11 17:00:04 +0100171 logging.debug('Output: %s', output_json)
sakal67e414c2017-09-05 00:16:15 -0700172 return output_json
173
Artem Titarenkoa9719482018-12-11 17:00:04 +0100174 def _GetThirdPartyLibraries(self, buildfile_dir, target):
sakal67e414c2017-09-05 00:16:15 -0700175 output = json.loads(LicenseBuilder._RunGN(buildfile_dir, target))
176 libraries = set()
Artem Titarenkoa9719482018-12-11 17:00:04 +0100177 for described_target in output.values():
178 third_party_libs = (
179 self._ParseLibrary(dep) for dep in described_target['deps'])
180 libraries |= set(lib for lib in third_party_libs if lib)
sakal67e414c2017-09-05 00:16:15 -0700181 return libraries
182
183 def GenerateLicenseText(self, output_dir):
184 # Get a list of third_party libs from gn. For fat libraries we must consider
185 # all architectures, hence the multiple buildfile directories.
186 third_party_libs = set()
187 for buildfile in self.buildfile_dirs:
188 for target in self.targets:
Artem Titarenkoa9719482018-12-11 17:00:04 +0100189 third_party_libs |= self._GetThirdPartyLibraries(buildfile, target)
sakal67e414c2017-09-05 00:16:15 -0700190 assert len(third_party_libs) > 0
191
Artem Titarenkoa9719482018-12-11 17:00:04 +0100192 missing_licenses = third_party_libs - set(self.common_licenses_dict.keys())
sakal67e414c2017-09-05 00:16:15 -0700193 if missing_licenses:
Artem Titarenkoa9719482018-12-11 17:00:04 +0100194 error_msg = 'Missing licenses for following third_party targets: %s' % \
195 ', '.join(missing_licenses)
sakal67e414c2017-09-05 00:16:15 -0700196 logging.error(error_msg)
197 raise Exception(error_msg)
198
199 # Put webrtc at the front of the list.
200 license_libs = sorted(third_party_libs)
201 license_libs.insert(0, 'webrtc')
202
Artem Titarenkoa9719482018-12-11 17:00:04 +0100203 logging.info('List of licenses: %s', ', '.join(license_libs))
sakal67e414c2017-09-05 00:16:15 -0700204
205 # Generate markdown.
206 output_license_file = open(os.path.join(output_dir, 'LICENSE.md'), 'w+')
207 for license_lib in license_libs:
Artem Titarenkoa9719482018-12-11 17:00:04 +0100208 if len(self.common_licenses_dict[license_lib]) == 0:
209 logging.info('Skipping compile time or internal dependency: %s',
210 license_lib)
211 continue # Compile time dependency
sakal67e414c2017-09-05 00:16:15 -0700212
213 output_license_file.write('# %s\n' % license_lib)
214 output_license_file.write('```\n')
Artem Titarenkoa9719482018-12-11 17:00:04 +0100215 for path in self.common_licenses_dict[license_lib]:
Mirko Bonadeid8665442018-09-04 12:17:27 +0200216 license_path = os.path.join(WEBRTC_ROOT, path)
sakal67e414c2017-09-05 00:16:15 -0700217 with open(license_path, 'r') as license_file:
218 license_text = cgi.escape(license_file.read(), quote=True)
219 output_license_file.write(license_text)
220 output_license_file.write('\n')
221 output_license_file.write('```\n\n')
222
223 output_license_file.close()
224
225
226def main():
227 parser = argparse.ArgumentParser(description='Generate WebRTC LICENSE.md')
Artem Titarenkoa9719482018-12-11 17:00:04 +0100228 parser.add_argument(
229 '--verbose', action='store_true', default=False, help='Debug logging.')
230 parser.add_argument(
231 '--target',
232 required=True,
233 action='append',
234 default=[],
235 help='Name of the GN target to generate a license for')
236 parser.add_argument('output_dir', help='Directory to output LICENSE.md to.')
237 parser.add_argument(
238 'buildfile_dirs',
239 nargs='+',
240 help='Directories containing gn generated ninja files')
sakal67e414c2017-09-05 00:16:15 -0700241 args = parser.parse_args()
242
243 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
244
245 builder = LicenseBuilder(args.buildfile_dirs, args.target)
246 builder.GenerateLicenseText(args.output_dir)
247
248
249if __name__ == '__main__':
250 sys.exit(main())