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. |
| 10 | |
| 11 | """Generates license markdown for a prebuilt version of WebRTC.""" |
| 12 | |
| 13 | import sys |
| 14 | |
| 15 | import argparse |
| 16 | import cgi |
| 17 | import json |
| 18 | import logging |
| 19 | import os |
| 20 | import re |
| 21 | import subprocess |
| 22 | |
| 23 | |
| 24 | LIB_TO_LICENSES_DICT = { |
Mirko Bonadei | afe7217 | 2018-04-13 11:11:52 +0200 | [diff] [blame] | 25 | 'abseil-cpp': ['third_party/abseil-cpp/LICENSE'], |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 26 | 'android_tools': ['third_party/android_tools/LICENSE'], |
Edward Lemur | 5c24c67 | 2017-11-06 20:29:00 +0100 | [diff] [blame] | 27 | 'auto': ['third_party/auto/src/LICENSE.txt'], |
Sami Kalliomäki | e7fac68 | 2018-03-20 16:32:49 +0100 | [diff] [blame] | 28 | 'bazel': ['third_party/bazel/LICENSE'], |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 29 | 'boringssl': ['third_party/boringssl/src/LICENSE'], |
Mirko Bonadei | f91ec56 | 2017-10-13 13:58:37 +0200 | [diff] [blame] | 30 | 'errorprone': ['third_party/errorprone/LICENSE'], |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 31 | 'expat': ['third_party/expat/files/COPYING'], |
Oleh Prypin | d42acbb | 2018-01-15 20:13:16 +0100 | [diff] [blame] | 32 | 'fiat': ['third_party/boringssl/src/third_party/fiat/LICENSE'], |
Edward Lemur | 5c24c67 | 2017-11-06 20:29:00 +0100 | [diff] [blame] | 33 | 'guava': ['third_party/guava/LICENSE'], |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 34 | 'ijar': ['third_party/ijar/LICENSE'], |
| 35 | 'jsoncpp': ['third_party/jsoncpp/LICENSE'], |
Sami Kalliomäki | e7fac68 | 2018-03-20 16:32:49 +0100 | [diff] [blame] | 36 | 'jsr-305': ['third_party/jsr-305/src/ri/LICENSE'], |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 37 | 'libc++': ['buildtools/third_party/libc++/trunk/LICENSE.TXT'], |
| 38 | 'libc++abi': ['buildtools/third_party/libc++abi/trunk/LICENSE.TXT'], |
| 39 | 'libevent': ['base/third_party/libevent/LICENSE'], |
| 40 | 'libjpeg_turbo': ['third_party/libjpeg_turbo/LICENSE.md'], |
| 41 | 'libsrtp': ['third_party/libsrtp/LICENSE'], |
| 42 | 'libvpx': ['third_party/libvpx/source/libvpx/LICENSE'], |
| 43 | 'libyuv': ['third_party/libyuv/LICENSE'], |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 44 | 'opus': ['third_party/opus/src/COPYING'], |
| 45 | 'protobuf': ['third_party/protobuf/LICENSE'], |
Alessio Bazzica | a5b9038 | 2018-05-07 09:29:54 +0000 | [diff] [blame] | 46 | 'rnnoise': ['third_party/rnnoise/COPYING'], |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 47 | 'usrsctp': ['third_party/usrsctp/LICENSE'], |
Henrik Kjellander | 0e8f053 | 2017-09-15 08:57:50 +0200 | [diff] [blame] | 48 | 'webrtc': ['LICENSE', 'LICENSE_THIRD_PARTY'], |
Mirko Bonadei | fe48ee9 | 2018-03-20 16:43:23 +0100 | [diff] [blame] | 49 | 'zlib': ['third_party/zlib/LICENSE'], |
Artem Titov | a76af0c | 2018-07-23 17:38:12 +0200 | [diff] [blame] | 50 | 'base64': ['rtc_base/third_party/base64/LICENSE'], |
Artem Titov | e41c433 | 2018-07-25 15:04:28 +0200 | [diff] [blame] | 51 | 'sigslot': ['rtc_base/third_party/sigslot/LICENSE'], |
Artem Titov | 8ff433a | 2018-07-24 13:42:22 +0200 | [diff] [blame] | 52 | 'portaudio': ['modules/third_party/portaudio/LICENSE'], |
Artem Titov | 8a838fd | 2018-07-24 15:37:09 +0200 | [diff] [blame] | 53 | 'fft': ['modules/third_party/fft/LICENSE'], |
Artem Titov | e095b81 | 2018-07-25 12:10:22 +0200 | [diff] [blame] | 54 | 'g711': ['modules/third_party/g711/LICENSE'], |
Artem Titov | 52b9000 | 2018-07-25 13:13:44 +0200 | [diff] [blame] | 55 | 'g722': ['modules/third_party/g722/LICENSE'], |
Artem Titov | 333a505 | 2018-07-25 16:56:18 +0200 | [diff] [blame] | 56 | 'fft4g': ['common_audio/third_party/fft4g/LICENSE'], |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 57 | |
| 58 | # Compile time dependencies, no license needed: |
| 59 | 'yasm': [], |
Yura Yaroshevich | 29c36b2 | 2018-06-12 19:59:02 +0300 | [diff] [blame] | 60 | 'ow2_asm': [], |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0])) |
| 64 | CHECKOUT_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir)) |
Henrik Kjellander | ec57e05 | 2017-10-17 21:36:01 +0200 | [diff] [blame] | 65 | sys.path.append(os.path.join(CHECKOUT_ROOT, 'build')) |
| 66 | import find_depot_tools |
| 67 | |
Sami Kalliomäki | e7fac68 | 2018-03-20 16:32:49 +0100 | [diff] [blame] | 68 | THIRD_PARTY_LIB_REGEX = r'^.*/third_party/([\w\-+]+).*$' |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 69 | |
| 70 | class LicenseBuilder(object): |
| 71 | |
| 72 | def __init__(self, buildfile_dirs, targets): |
| 73 | self.buildfile_dirs = buildfile_dirs |
| 74 | self.targets = targets |
| 75 | |
| 76 | @staticmethod |
| 77 | def _ParseLibrary(dep): |
| 78 | """ |
| 79 | Returns a regex match containing library name after third_party |
| 80 | |
| 81 | Input one of: |
| 82 | //a/b/third_party/libname:c |
| 83 | //a/b/third_party/libname:c(//d/e/f:g) |
| 84 | //a/b/third_party/libname/c:d(//e/f/g:h) |
| 85 | |
| 86 | Outputs match with libname in group 1 or None if this is not a third_party |
| 87 | dependency. |
| 88 | """ |
| 89 | return re.match(THIRD_PARTY_LIB_REGEX, dep) |
| 90 | |
| 91 | @staticmethod |
| 92 | def _RunGN(buildfile_dir, target): |
Henrik Kjellander | ec57e05 | 2017-10-17 21:36:01 +0200 | [diff] [blame] | 93 | cmd = [ |
| 94 | sys.executable, |
| 95 | os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py'), |
| 96 | 'desc', |
| 97 | '--all', |
| 98 | '--format=json', |
| 99 | os.path.abspath(buildfile_dir), |
| 100 | target, |
| 101 | ] |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 102 | logging.debug("Running: %r", cmd) |
| 103 | output_json = subprocess.check_output(cmd, cwd=CHECKOUT_ROOT) |
| 104 | logging.debug("Output: %s", output_json) |
| 105 | return output_json |
| 106 | |
| 107 | @staticmethod |
| 108 | def _GetThirdPartyLibraries(buildfile_dir, target): |
| 109 | output = json.loads(LicenseBuilder._RunGN(buildfile_dir, target)) |
| 110 | libraries = set() |
| 111 | for target in output.values(): |
| 112 | third_party_matches = ( |
| 113 | LicenseBuilder._ParseLibrary(dep) for dep in target['deps']) |
| 114 | libraries |= set(match.group(1) for match in third_party_matches if match) |
| 115 | return libraries |
| 116 | |
| 117 | def GenerateLicenseText(self, output_dir): |
| 118 | # Get a list of third_party libs from gn. For fat libraries we must consider |
| 119 | # all architectures, hence the multiple buildfile directories. |
| 120 | third_party_libs = set() |
| 121 | for buildfile in self.buildfile_dirs: |
| 122 | for target in self.targets: |
| 123 | third_party_libs |= LicenseBuilder._GetThirdPartyLibraries( |
| 124 | buildfile, target) |
| 125 | assert len(third_party_libs) > 0 |
| 126 | |
| 127 | missing_licenses = third_party_libs - set(LIB_TO_LICENSES_DICT.keys()) |
| 128 | if missing_licenses: |
| 129 | error_msg = 'Missing licenses: %s' % ', '.join(missing_licenses) |
| 130 | logging.error(error_msg) |
| 131 | raise Exception(error_msg) |
| 132 | |
| 133 | # Put webrtc at the front of the list. |
| 134 | license_libs = sorted(third_party_libs) |
| 135 | license_libs.insert(0, 'webrtc') |
| 136 | |
| 137 | logging.info("List of licenses: %s", ', '.join(license_libs)) |
| 138 | |
| 139 | # Generate markdown. |
| 140 | output_license_file = open(os.path.join(output_dir, 'LICENSE.md'), 'w+') |
| 141 | for license_lib in license_libs: |
| 142 | if len(LIB_TO_LICENSES_DICT[license_lib]) == 0: |
| 143 | logging.info("Skipping compile time dependency: %s", license_lib) |
| 144 | continue # Compile time dependency |
| 145 | |
| 146 | output_license_file.write('# %s\n' % license_lib) |
| 147 | output_license_file.write('```\n') |
| 148 | for path in LIB_TO_LICENSES_DICT[license_lib]: |
| 149 | license_path = os.path.join(CHECKOUT_ROOT, path) |
| 150 | with open(license_path, 'r') as license_file: |
| 151 | license_text = cgi.escape(license_file.read(), quote=True) |
| 152 | output_license_file.write(license_text) |
| 153 | output_license_file.write('\n') |
| 154 | output_license_file.write('```\n\n') |
| 155 | |
| 156 | output_license_file.close() |
| 157 | |
| 158 | |
| 159 | def main(): |
| 160 | parser = argparse.ArgumentParser(description='Generate WebRTC LICENSE.md') |
| 161 | parser.add_argument('--verbose', action='store_true', default=False, |
| 162 | help='Debug logging.') |
| 163 | parser.add_argument('--target', required=True, action='append', default=[], |
| 164 | help='Name of the GN target to generate a license for') |
| 165 | parser.add_argument('output_dir', |
| 166 | help='Directory to output LICENSE.md to.') |
| 167 | parser.add_argument('buildfile_dirs', nargs="+", |
| 168 | help='Directories containing gn generated ninja files') |
| 169 | args = parser.parse_args() |
| 170 | |
| 171 | logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO) |
| 172 | |
| 173 | builder = LicenseBuilder(args.buildfile_dirs, args.target) |
| 174 | builder.GenerateLicenseText(args.output_dir) |
| 175 | |
| 176 | |
| 177 | if __name__ == '__main__': |
| 178 | sys.exit(main()) |