sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 1 | #!/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 Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 10 | """Generates license markdown for a prebuilt version of WebRTC. |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 11 | |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 12 | Licenses are taken from dependent libraries which are determined by |
| 13 | GN desc command `gn desc` on all targets specified via `--target` argument. |
| 14 | |
| 15 | One 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 | |
| 19 | Libraries are mapped to licenses via LIB_TO_LICENSES_DICT dictionary. |
| 20 | |
| 21 | """ |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 22 | |
| 23 | import sys |
| 24 | |
| 25 | import argparse |
| 26 | import cgi |
| 27 | import json |
| 28 | import logging |
| 29 | import os |
| 30 | import re |
| 31 | import subprocess |
| 32 | |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 33 | # Third_party library to licences mapping. Keys are names of the libraries |
| 34 | # (right after the `third_party/` prefix) |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 35 | LIB_TO_LICENSES_DICT = { |
Mirko Bonadei | afe7217 | 2018-04-13 11:11:52 +0200 | [diff] [blame] | 36 | 'abseil-cpp': ['third_party/abseil-cpp/LICENSE'], |
Oleksandr Iakovenko | 10b6361 | 2019-08-28 09:41:26 +0200 | [diff] [blame] | 37 | 'android_ndk': ['third_party/android_ndk/NOTICE'], |
Mirko Bonadei | d9b62d3 | 2019-03-29 09:51:04 +0100 | [diff] [blame] | 38 | 'android_sdk': ['third_party/android_sdk/LICENSE'], |
Yves Gerey | 2f385d2 | 2019-11-20 12:10:08 +0100 | [diff] [blame] | 39 | 'auto': ['third_party/android_deps/libs/' |
| 40 | 'com_google_auto_service_auto_service/LICENSE'], |
Sami Kalliomäki | e7fac68 | 2018-03-20 16:32:49 +0100 | [diff] [blame] | 41 | 'bazel': ['third_party/bazel/LICENSE'], |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 42 | 'boringssl': ['third_party/boringssl/src/LICENSE'], |
Yves Gerey | 2f385d2 | 2019-11-20 12:10:08 +0100 | [diff] [blame] | 43 | 'errorprone': ['third_party/android_deps/libs/' |
| 44 | 'com_google_errorprone_error_prone_core/LICENSE'], |
Oleh Prypin | d42acbb | 2018-01-15 20:13:16 +0100 | [diff] [blame] | 45 | 'fiat': ['third_party/boringssl/src/third_party/fiat/LICENSE'], |
Edward Lemur | 5c24c67 | 2017-11-06 20:29:00 +0100 | [diff] [blame] | 46 | 'guava': ['third_party/guava/LICENSE'], |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 47 | 'ijar': ['third_party/ijar/LICENSE'], |
| 48 | 'jsoncpp': ['third_party/jsoncpp/LICENSE'], |
| 49 | 'libc++': ['buildtools/third_party/libc++/trunk/LICENSE.TXT'], |
| 50 | 'libc++abi': ['buildtools/third_party/libc++abi/trunk/LICENSE.TXT'], |
| 51 | 'libevent': ['base/third_party/libevent/LICENSE'], |
| 52 | 'libjpeg_turbo': ['third_party/libjpeg_turbo/LICENSE.md'], |
| 53 | 'libsrtp': ['third_party/libsrtp/LICENSE'], |
| 54 | 'libvpx': ['third_party/libvpx/source/libvpx/LICENSE'], |
| 55 | 'libyuv': ['third_party/libyuv/LICENSE'], |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 56 | 'opus': ['third_party/opus/src/COPYING'], |
Alessio Bazzica | ffb8c8c | 2019-04-01 10:19:25 +0200 | [diff] [blame] | 57 | 'pffft': ['third_party/pffft/LICENSE'], |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 58 | 'protobuf': ['third_party/protobuf/LICENSE'], |
Alessio Bazzica | a5b9038 | 2018-05-07 09:29:54 +0000 | [diff] [blame] | 59 | 'rnnoise': ['third_party/rnnoise/COPYING'], |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 60 | 'usrsctp': ['third_party/usrsctp/LICENSE'], |
Artem Titov | 2983599 | 2018-08-07 11:50:54 +0200 | [diff] [blame] | 61 | 'webrtc': ['LICENSE'], |
Mirko Bonadei | fe48ee9 | 2018-03-20 16:43:23 +0100 | [diff] [blame] | 62 | 'zlib': ['third_party/zlib/LICENSE'], |
Artem Titov | a76af0c | 2018-07-23 17:38:12 +0200 | [diff] [blame] | 63 | 'base64': ['rtc_base/third_party/base64/LICENSE'], |
Artem Titov | e41c433 | 2018-07-25 15:04:28 +0200 | [diff] [blame] | 64 | 'sigslot': ['rtc_base/third_party/sigslot/LICENSE'], |
Artem Titov | 8ff433a | 2018-07-24 13:42:22 +0200 | [diff] [blame] | 65 | 'portaudio': ['modules/third_party/portaudio/LICENSE'], |
Artem Titov | 8a838fd | 2018-07-24 15:37:09 +0200 | [diff] [blame] | 66 | 'fft': ['modules/third_party/fft/LICENSE'], |
Artem Titov | e095b81 | 2018-07-25 12:10:22 +0200 | [diff] [blame] | 67 | 'g711': ['modules/third_party/g711/LICENSE'], |
Artem Titov | 52b9000 | 2018-07-25 13:13:44 +0200 | [diff] [blame] | 68 | 'g722': ['modules/third_party/g722/LICENSE'], |
Artem Titov | 333a505 | 2018-07-25 16:56:18 +0200 | [diff] [blame] | 69 | 'fft4g': ['common_audio/third_party/fft4g/LICENSE'], |
Artem Titov | 2983599 | 2018-08-07 11:50:54 +0200 | [diff] [blame] | 70 | 'spl_sqrt_floor': ['common_audio/third_party/spl_sqrt_floor/LICENSE'], |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 71 | |
Yves Gerey | 2f385d2 | 2019-11-20 12:10:08 +0100 | [diff] [blame] | 72 | # TODO(bugs.webrtc.org/1110): Remove this hack. This is not a lib. |
| 73 | # For some reason it is listed as so in _GetThirdPartyLibraries. |
| 74 | 'android_deps': [], |
| 75 | |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 76 | # Compile time dependencies, no license needed: |
| 77 | 'yasm': [], |
Yura Yaroshevich | 29c36b2 | 2018-06-12 19:59:02 +0300 | [diff] [blame] | 78 | 'ow2_asm': [], |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 81 | # Third_party library _regex_ to licences mapping. Keys are regular expression |
| 82 | # with names of the libraries (right after the `third_party/` prefix) |
| 83 | LIB_REGEX_TO_LICENSES_DICT = { |
| 84 | 'android_deps:android_support_annotations.*': [ |
| 85 | 'third_party/android_deps/libs/' + |
| 86 | 'com_android_support_support_annotations/LICENSE' |
| 87 | ], |
| 88 | |
| 89 | # Internal dependencies, licenses are already included by other dependencies |
| 90 | 'android_deps:com_android_support_support_annotations.*': [], |
| 91 | } |
| 92 | |
| 93 | |
| 94 | def FindSrcDirPath(): |
| 95 | """Returns the abs path to the src/ dir of the project.""" |
| 96 | src_dir = os.path.dirname(os.path.abspath(__file__)) |
| 97 | while os.path.basename(src_dir) != 'src': |
| 98 | src_dir = os.path.normpath(os.path.join(src_dir, os.pardir)) |
| 99 | return src_dir |
| 100 | |
| 101 | |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 102 | SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0])) |
Mirko Bonadei | d866544 | 2018-09-04 12:17:27 +0200 | [diff] [blame] | 103 | WEBRTC_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir)) |
| 104 | SRC_DIR = FindSrcDirPath() |
| 105 | sys.path.append(os.path.join(SRC_DIR, 'build')) |
Henrik Kjellander | ec57e05 | 2017-10-17 21:36:01 +0200 | [diff] [blame] | 106 | import find_depot_tools |
| 107 | |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 108 | THIRD_PARTY_LIB_SIMPLE_NAME_REGEX = r'^.*/third_party/([\w\-+]+).*$' |
| 109 | THIRD_PARTY_LIB_REGEX_TEMPLATE = r'^.*/third_party/%s$' |
| 110 | |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 111 | |
| 112 | class LicenseBuilder(object): |
| 113 | |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 114 | def __init__(self, |
| 115 | buildfile_dirs, |
| 116 | targets, |
| 117 | lib_to_licenses_dict=None, |
| 118 | lib_regex_to_licenses_dict=None): |
| 119 | if lib_to_licenses_dict is None: |
| 120 | lib_to_licenses_dict = LIB_TO_LICENSES_DICT |
| 121 | |
| 122 | if lib_regex_to_licenses_dict is None: |
| 123 | lib_regex_to_licenses_dict = LIB_REGEX_TO_LICENSES_DICT |
| 124 | |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 125 | self.buildfile_dirs = buildfile_dirs |
| 126 | self.targets = targets |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 127 | self.lib_to_licenses_dict = lib_to_licenses_dict |
| 128 | self.lib_regex_to_licenses_dict = lib_regex_to_licenses_dict |
| 129 | |
| 130 | self.common_licenses_dict = self.lib_to_licenses_dict.copy() |
| 131 | self.common_licenses_dict.update(self.lib_regex_to_licenses_dict) |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 132 | |
| 133 | @staticmethod |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 134 | def _ParseLibraryName(dep): |
| 135 | """Returns library name after third_party |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 136 | |
| 137 | Input one of: |
| 138 | //a/b/third_party/libname:c |
| 139 | //a/b/third_party/libname:c(//d/e/f:g) |
| 140 | //a/b/third_party/libname/c:d(//e/f/g:h) |
| 141 | |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 142 | Outputs libname or None if this is not a third_party dependency. |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 143 | """ |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 144 | groups = re.match(THIRD_PARTY_LIB_SIMPLE_NAME_REGEX, dep) |
| 145 | return groups.group(1) if groups else None |
| 146 | |
| 147 | def _ParseLibrary(self, dep): |
| 148 | """Returns library simple or regex name that matches `dep` after third_party |
| 149 | |
| 150 | This method matches `dep` dependency against simple names in |
| 151 | LIB_TO_LICENSES_DICT and regular expression names in |
| 152 | LIB_REGEX_TO_LICENSES_DICT keys |
| 153 | |
| 154 | Outputs matched dict key or None if this is not a third_party dependency. |
| 155 | """ |
| 156 | libname = LicenseBuilder._ParseLibraryName(dep) |
| 157 | |
| 158 | for lib_regex in self.lib_regex_to_licenses_dict: |
| 159 | if re.match(THIRD_PARTY_LIB_REGEX_TEMPLATE % lib_regex, dep): |
| 160 | return lib_regex |
| 161 | |
| 162 | return libname |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 163 | |
| 164 | @staticmethod |
| 165 | def _RunGN(buildfile_dir, target): |
Henrik Kjellander | ec57e05 | 2017-10-17 21:36:01 +0200 | [diff] [blame] | 166 | cmd = [ |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 167 | sys.executable, |
| 168 | os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py'), |
| 169 | 'desc', |
| 170 | '--all', |
| 171 | '--format=json', |
| 172 | os.path.abspath(buildfile_dir), |
| 173 | target, |
Henrik Kjellander | ec57e05 | 2017-10-17 21:36:01 +0200 | [diff] [blame] | 174 | ] |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 175 | logging.debug('Running: %r', cmd) |
Mirko Bonadei | d866544 | 2018-09-04 12:17:27 +0200 | [diff] [blame] | 176 | output_json = subprocess.check_output(cmd, cwd=WEBRTC_ROOT) |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 177 | logging.debug('Output: %s', output_json) |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 178 | return output_json |
| 179 | |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 180 | def _GetThirdPartyLibraries(self, buildfile_dir, target): |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 181 | output = json.loads(LicenseBuilder._RunGN(buildfile_dir, target)) |
| 182 | libraries = set() |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 183 | for described_target in output.values(): |
| 184 | third_party_libs = ( |
| 185 | self._ParseLibrary(dep) for dep in described_target['deps']) |
| 186 | libraries |= set(lib for lib in third_party_libs if lib) |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 187 | return libraries |
| 188 | |
| 189 | def GenerateLicenseText(self, output_dir): |
| 190 | # Get a list of third_party libs from gn. For fat libraries we must consider |
| 191 | # all architectures, hence the multiple buildfile directories. |
| 192 | third_party_libs = set() |
| 193 | for buildfile in self.buildfile_dirs: |
| 194 | for target in self.targets: |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 195 | third_party_libs |= self._GetThirdPartyLibraries(buildfile, target) |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 196 | assert len(third_party_libs) > 0 |
| 197 | |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 198 | missing_licenses = third_party_libs - set(self.common_licenses_dict.keys()) |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 199 | if missing_licenses: |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 200 | error_msg = 'Missing licenses for following third_party targets: %s' % \ |
| 201 | ', '.join(missing_licenses) |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 202 | logging.error(error_msg) |
| 203 | raise Exception(error_msg) |
| 204 | |
| 205 | # Put webrtc at the front of the list. |
| 206 | license_libs = sorted(third_party_libs) |
| 207 | license_libs.insert(0, 'webrtc') |
| 208 | |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 209 | logging.info('List of licenses: %s', ', '.join(license_libs)) |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 210 | |
| 211 | # Generate markdown. |
| 212 | output_license_file = open(os.path.join(output_dir, 'LICENSE.md'), 'w+') |
| 213 | for license_lib in license_libs: |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 214 | if len(self.common_licenses_dict[license_lib]) == 0: |
| 215 | logging.info('Skipping compile time or internal dependency: %s', |
| 216 | license_lib) |
| 217 | continue # Compile time dependency |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 218 | |
| 219 | output_license_file.write('# %s\n' % license_lib) |
| 220 | output_license_file.write('```\n') |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 221 | for path in self.common_licenses_dict[license_lib]: |
Mirko Bonadei | d866544 | 2018-09-04 12:17:27 +0200 | [diff] [blame] | 222 | license_path = os.path.join(WEBRTC_ROOT, path) |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 223 | with open(license_path, 'r') as license_file: |
| 224 | license_text = cgi.escape(license_file.read(), quote=True) |
| 225 | output_license_file.write(license_text) |
| 226 | output_license_file.write('\n') |
| 227 | output_license_file.write('```\n\n') |
| 228 | |
| 229 | output_license_file.close() |
| 230 | |
| 231 | |
| 232 | def main(): |
| 233 | parser = argparse.ArgumentParser(description='Generate WebRTC LICENSE.md') |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 234 | parser.add_argument( |
| 235 | '--verbose', action='store_true', default=False, help='Debug logging.') |
| 236 | parser.add_argument( |
| 237 | '--target', |
| 238 | required=True, |
| 239 | action='append', |
| 240 | default=[], |
| 241 | help='Name of the GN target to generate a license for') |
| 242 | parser.add_argument('output_dir', help='Directory to output LICENSE.md to.') |
| 243 | parser.add_argument( |
| 244 | 'buildfile_dirs', |
| 245 | nargs='+', |
| 246 | help='Directories containing gn generated ninja files') |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 247 | args = parser.parse_args() |
| 248 | |
| 249 | logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO) |
| 250 | |
| 251 | builder = LicenseBuilder(args.buildfile_dirs, args.target) |
| 252 | builder.GenerateLicenseText(args.output_dir) |
| 253 | |
| 254 | |
| 255 | if __name__ == '__main__': |
| 256 | sys.exit(main()) |