1*d9f75844SAndroid Build Coastguard Worker#!/usr/bin/env vpython3 2*d9f75844SAndroid Build Coastguard Worker 3*d9f75844SAndroid Build Coastguard Worker# Copyright 2016 The WebRTC project authors. All Rights Reserved. 4*d9f75844SAndroid Build Coastguard Worker# 5*d9f75844SAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license 6*d9f75844SAndroid Build Coastguard Worker# that can be found in the LICENSE file in the root of the source 7*d9f75844SAndroid Build Coastguard Worker# tree. An additional intellectual property rights grant can be found 8*d9f75844SAndroid Build Coastguard Worker# in the file PATENTS. All contributing project authors may 9*d9f75844SAndroid Build Coastguard Worker# be found in the AUTHORS file in the root of the source tree. 10*d9f75844SAndroid Build Coastguard Worker"""Generates license markdown for a prebuilt version of WebRTC. 11*d9f75844SAndroid Build Coastguard Worker 12*d9f75844SAndroid Build Coastguard WorkerLicenses are taken from dependent libraries which are determined by 13*d9f75844SAndroid Build Coastguard WorkerGN desc command `gn desc` on all targets specified via `--target` argument. 14*d9f75844SAndroid Build Coastguard Worker 15*d9f75844SAndroid Build Coastguard WorkerOne can see all dependencies by invoking this command: 16*d9f75844SAndroid Build Coastguard Worker$ gn.py desc --all --format=json <out_directory> <target> | \ 17*d9f75844SAndroid Build Coastguard Worker vpython3 -m json.tool 18*d9f75844SAndroid Build Coastguard Worker(see "deps" subarray) 19*d9f75844SAndroid Build Coastguard Worker 20*d9f75844SAndroid Build Coastguard WorkerLibraries are mapped to licenses via LIB_TO_LICENSES_DICT dictionary. 21*d9f75844SAndroid Build Coastguard Worker 22*d9f75844SAndroid Build Coastguard Worker""" 23*d9f75844SAndroid Build Coastguard Worker 24*d9f75844SAndroid Build Coastguard Workerimport sys 25*d9f75844SAndroid Build Coastguard Workerimport argparse 26*d9f75844SAndroid Build Coastguard Workerimport json 27*d9f75844SAndroid Build Coastguard Workerimport logging 28*d9f75844SAndroid Build Coastguard Workerimport os 29*d9f75844SAndroid Build Coastguard Workerimport re 30*d9f75844SAndroid Build Coastguard Workerimport subprocess 31*d9f75844SAndroid Build Coastguard Workerfrom html import escape 32*d9f75844SAndroid Build Coastguard Worker 33*d9f75844SAndroid Build Coastguard Worker# Third_party library to licences mapping. Keys are names of the libraries 34*d9f75844SAndroid Build Coastguard Worker# (right after the `third_party/` prefix) 35*d9f75844SAndroid Build Coastguard WorkerLIB_TO_LICENSES_DICT = { 36*d9f75844SAndroid Build Coastguard Worker 'abseil-cpp': ['third_party/abseil-cpp/LICENSE'], 37*d9f75844SAndroid Build Coastguard Worker 'android_ndk': ['third_party/android_ndk/NOTICE'], 38*d9f75844SAndroid Build Coastguard Worker 'android_sdk': ['third_party/android_sdk/LICENSE'], 39*d9f75844SAndroid Build Coastguard Worker 'auto': [ 40*d9f75844SAndroid Build Coastguard Worker 'third_party/android_deps/libs/' 41*d9f75844SAndroid Build Coastguard Worker 'com_google_auto_service_auto_service/LICENSE' 42*d9f75844SAndroid Build Coastguard Worker ], 43*d9f75844SAndroid Build Coastguard Worker 'boringssl': ['third_party/boringssl/src/LICENSE'], 44*d9f75844SAndroid Build Coastguard Worker 'crc32c': ['third_party/crc32c/src/LICENSE'], 45*d9f75844SAndroid Build Coastguard Worker 'dav1d': ['third_party/dav1d/LICENSE'], 46*d9f75844SAndroid Build Coastguard Worker 'errorprone': [ 47*d9f75844SAndroid Build Coastguard Worker 'third_party/android_deps/libs/' 48*d9f75844SAndroid Build Coastguard Worker 'com_google_errorprone_error_prone_core/LICENSE' 49*d9f75844SAndroid Build Coastguard Worker ], 50*d9f75844SAndroid Build Coastguard Worker 'fiat': ['third_party/boringssl/src/third_party/fiat/LICENSE'], 51*d9f75844SAndroid Build Coastguard Worker 'guava': ['third_party/android_deps/libs/com_google_guava_guava/LICENSE'], 52*d9f75844SAndroid Build Coastguard Worker 'ijar': ['third_party/ijar/LICENSE'], 53*d9f75844SAndroid Build Coastguard Worker 'jsoncpp': ['third_party/jsoncpp/LICENSE'], 54*d9f75844SAndroid Build Coastguard Worker 'libaom': ['third_party/libaom/source/libaom/LICENSE'], 55*d9f75844SAndroid Build Coastguard Worker 'libc++': ['buildtools/third_party/libc++/trunk/LICENSE.TXT'], 56*d9f75844SAndroid Build Coastguard Worker 'libc++abi': ['buildtools/third_party/libc++abi/trunk/LICENSE.TXT'], 57*d9f75844SAndroid Build Coastguard Worker 'libevent': ['third_party/libevent/LICENSE'], 58*d9f75844SAndroid Build Coastguard Worker 'libjpeg_turbo': ['third_party/libjpeg_turbo/LICENSE.md'], 59*d9f75844SAndroid Build Coastguard Worker 'libsrtp': ['third_party/libsrtp/LICENSE'], 60*d9f75844SAndroid Build Coastguard Worker 'libunwind': ['buildtools/third_party/libunwind/trunk/LICENSE.TXT'], 61*d9f75844SAndroid Build Coastguard Worker 'libvpx': ['third_party/libvpx/source/libvpx/LICENSE'], 62*d9f75844SAndroid Build Coastguard Worker 'libyuv': ['third_party/libyuv/LICENSE'], 63*d9f75844SAndroid Build Coastguard Worker 'nasm': ['third_party/nasm/LICENSE'], 64*d9f75844SAndroid Build Coastguard Worker 'opus': ['third_party/opus/src/COPYING'], 65*d9f75844SAndroid Build Coastguard Worker 'pffft': ['third_party/pffft/LICENSE'], 66*d9f75844SAndroid Build Coastguard Worker 'protobuf': ['third_party/protobuf/LICENSE'], 67*d9f75844SAndroid Build Coastguard Worker 'rnnoise': ['third_party/rnnoise/COPYING'], 68*d9f75844SAndroid Build Coastguard Worker 'webrtc': ['LICENSE'], 69*d9f75844SAndroid Build Coastguard Worker 'zlib': ['third_party/zlib/LICENSE'], 70*d9f75844SAndroid Build Coastguard Worker 'base64': ['rtc_base/third_party/base64/LICENSE'], 71*d9f75844SAndroid Build Coastguard Worker 'sigslot': ['rtc_base/third_party/sigslot/LICENSE'], 72*d9f75844SAndroid Build Coastguard Worker 'portaudio': ['modules/third_party/portaudio/LICENSE'], 73*d9f75844SAndroid Build Coastguard Worker 'fft': ['modules/third_party/fft/LICENSE'], 74*d9f75844SAndroid Build Coastguard Worker 'g711': ['modules/third_party/g711/LICENSE'], 75*d9f75844SAndroid Build Coastguard Worker 'g722': ['modules/third_party/g722/LICENSE'], 76*d9f75844SAndroid Build Coastguard Worker 'ooura': ['common_audio/third_party/ooura/LICENSE'], 77*d9f75844SAndroid Build Coastguard Worker 'spl_sqrt_floor': ['common_audio/third_party/spl_sqrt_floor/LICENSE'], 78*d9f75844SAndroid Build Coastguard Worker 79*d9f75844SAndroid Build Coastguard Worker # TODO(bugs.webrtc.org/1110): Remove this hack. This is not a lib. 80*d9f75844SAndroid Build Coastguard Worker # For some reason it is listed as so in _GetThirdPartyLibraries. 81*d9f75844SAndroid Build Coastguard Worker 'android_deps': [], 82*d9f75844SAndroid Build Coastguard Worker # This is not a library but a collection of libraries. 83*d9f75844SAndroid Build Coastguard Worker 'androidx': [], 84*d9f75844SAndroid Build Coastguard Worker 85*d9f75844SAndroid Build Coastguard Worker # Compile time dependencies, no license needed: 86*d9f75844SAndroid Build Coastguard Worker 'ow2_asm': [], 87*d9f75844SAndroid Build Coastguard Worker 'jdk': [], 88*d9f75844SAndroid Build Coastguard Worker} 89*d9f75844SAndroid Build Coastguard Worker 90*d9f75844SAndroid Build Coastguard Worker# Third_party library _regex_ to licences mapping. Keys are regular expression 91*d9f75844SAndroid Build Coastguard Worker# with names of the libraries (right after the `third_party/` prefix) 92*d9f75844SAndroid Build Coastguard WorkerLIB_REGEX_TO_LICENSES_DICT = { 93*d9f75844SAndroid Build Coastguard Worker 'android_deps:android_support_annotations.*': [ 94*d9f75844SAndroid Build Coastguard Worker 'third_party/android_deps/libs/' + 95*d9f75844SAndroid Build Coastguard Worker 'com_android_support_support_annotations/LICENSE' 96*d9f75844SAndroid Build Coastguard Worker ], 97*d9f75844SAndroid Build Coastguard Worker 98*d9f75844SAndroid Build Coastguard Worker # Internal dependencies, licenses are already included by other dependencies 99*d9f75844SAndroid Build Coastguard Worker 'android_deps:com_android_support_support_annotations.*': [], 100*d9f75844SAndroid Build Coastguard Worker} 101*d9f75844SAndroid Build Coastguard Worker 102*d9f75844SAndroid Build Coastguard Worker 103*d9f75844SAndroid Build Coastguard WorkerSCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) 104*d9f75844SAndroid Build Coastguard WorkerWEBRTC_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir)) 105*d9f75844SAndroid Build Coastguard WorkerSRC_DIR = os.path.dirname(os.path.dirname(SCRIPT_DIR)) 106*d9f75844SAndroid Build Coastguard Worker 107*d9f75844SAndroid Build Coastguard Worker# Chromium, and potentially other repositories, embed us in the location 108*d9f75844SAndroid Build Coastguard Worker# "//third_party/webrtc". When this is the case, we expect that some of the 109*d9f75844SAndroid Build Coastguard Worker# tools we need are *actually* in their build folder, thus we need to move up 110*d9f75844SAndroid Build Coastguard Worker# to the *true* source root, when we're embedded like this. 111*d9f75844SAndroid Build Coastguard Workerif SRC_DIR.endswith(os.path.join('third_party', 'webrtc')): 112*d9f75844SAndroid Build Coastguard Worker SRC_DIR = os.path.abspath(os.path.join(SRC_DIR, os.pardir, os.pardir)) 113*d9f75844SAndroid Build Coastguard Workersys.path.append(os.path.join(SRC_DIR, 'build')) 114*d9f75844SAndroid Build Coastguard Workerimport find_depot_tools 115*d9f75844SAndroid Build Coastguard Worker 116*d9f75844SAndroid Build Coastguard WorkerTHIRD_PARTY_LIB_SIMPLE_NAME_REGEX = r'^.*/third_party/([\w\-+]+).*$' 117*d9f75844SAndroid Build Coastguard WorkerTHIRD_PARTY_LIB_REGEX_TEMPLATE = r'^.*/third_party/%s$' 118*d9f75844SAndroid Build Coastguard Worker 119*d9f75844SAndroid Build Coastguard Worker 120*d9f75844SAndroid Build Coastguard Workerclass LicenseBuilder: 121*d9f75844SAndroid Build Coastguard Worker def __init__(self, 122*d9f75844SAndroid Build Coastguard Worker buildfile_dirs, 123*d9f75844SAndroid Build Coastguard Worker targets, 124*d9f75844SAndroid Build Coastguard Worker lib_to_licenses_dict=None, 125*d9f75844SAndroid Build Coastguard Worker lib_regex_to_licenses_dict=None): 126*d9f75844SAndroid Build Coastguard Worker if lib_to_licenses_dict is None: 127*d9f75844SAndroid Build Coastguard Worker lib_to_licenses_dict = LIB_TO_LICENSES_DICT 128*d9f75844SAndroid Build Coastguard Worker 129*d9f75844SAndroid Build Coastguard Worker if lib_regex_to_licenses_dict is None: 130*d9f75844SAndroid Build Coastguard Worker lib_regex_to_licenses_dict = LIB_REGEX_TO_LICENSES_DICT 131*d9f75844SAndroid Build Coastguard Worker 132*d9f75844SAndroid Build Coastguard Worker self.buildfile_dirs = buildfile_dirs 133*d9f75844SAndroid Build Coastguard Worker self.targets = targets 134*d9f75844SAndroid Build Coastguard Worker self.lib_to_licenses_dict = lib_to_licenses_dict 135*d9f75844SAndroid Build Coastguard Worker self.lib_regex_to_licenses_dict = lib_regex_to_licenses_dict 136*d9f75844SAndroid Build Coastguard Worker 137*d9f75844SAndroid Build Coastguard Worker self.common_licenses_dict = self.lib_to_licenses_dict.copy() 138*d9f75844SAndroid Build Coastguard Worker self.common_licenses_dict.update(self.lib_regex_to_licenses_dict) 139*d9f75844SAndroid Build Coastguard Worker 140*d9f75844SAndroid Build Coastguard Worker @staticmethod 141*d9f75844SAndroid Build Coastguard Worker def _ParseLibraryName(dep): 142*d9f75844SAndroid Build Coastguard Worker """Returns library name after third_party 143*d9f75844SAndroid Build Coastguard Worker 144*d9f75844SAndroid Build Coastguard Worker Input one of: 145*d9f75844SAndroid Build Coastguard Worker //a/b/third_party/libname:c 146*d9f75844SAndroid Build Coastguard Worker //a/b/third_party/libname:c(//d/e/f:g) 147*d9f75844SAndroid Build Coastguard Worker //a/b/third_party/libname/c:d(//e/f/g:h) 148*d9f75844SAndroid Build Coastguard Worker 149*d9f75844SAndroid Build Coastguard Worker Outputs libname or None if this is not a third_party dependency. 150*d9f75844SAndroid Build Coastguard Worker """ 151*d9f75844SAndroid Build Coastguard Worker groups = re.match(THIRD_PARTY_LIB_SIMPLE_NAME_REGEX, dep) 152*d9f75844SAndroid Build Coastguard Worker return groups.group(1) if groups else None 153*d9f75844SAndroid Build Coastguard Worker 154*d9f75844SAndroid Build Coastguard Worker def _ParseLibrary(self, dep): 155*d9f75844SAndroid Build Coastguard Worker """Returns library simple or regex name that matches `dep` after third_party 156*d9f75844SAndroid Build Coastguard Worker 157*d9f75844SAndroid Build Coastguard Worker This method matches `dep` dependency against simple names in 158*d9f75844SAndroid Build Coastguard Worker LIB_TO_LICENSES_DICT and regular expression names in 159*d9f75844SAndroid Build Coastguard Worker LIB_REGEX_TO_LICENSES_DICT keys 160*d9f75844SAndroid Build Coastguard Worker 161*d9f75844SAndroid Build Coastguard Worker Outputs matched dict key or None if this is not a third_party dependency. 162*d9f75844SAndroid Build Coastguard Worker """ 163*d9f75844SAndroid Build Coastguard Worker libname = LicenseBuilder._ParseLibraryName(dep) 164*d9f75844SAndroid Build Coastguard Worker 165*d9f75844SAndroid Build Coastguard Worker for lib_regex in self.lib_regex_to_licenses_dict: 166*d9f75844SAndroid Build Coastguard Worker if re.match(THIRD_PARTY_LIB_REGEX_TEMPLATE % lib_regex, dep): 167*d9f75844SAndroid Build Coastguard Worker return lib_regex 168*d9f75844SAndroid Build Coastguard Worker 169*d9f75844SAndroid Build Coastguard Worker return libname 170*d9f75844SAndroid Build Coastguard Worker 171*d9f75844SAndroid Build Coastguard Worker @staticmethod 172*d9f75844SAndroid Build Coastguard Worker def _RunGN(buildfile_dir, target): 173*d9f75844SAndroid Build Coastguard Worker cmd = [ 174*d9f75844SAndroid Build Coastguard Worker sys.executable, 175*d9f75844SAndroid Build Coastguard Worker os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py'), 176*d9f75844SAndroid Build Coastguard Worker 'desc', 177*d9f75844SAndroid Build Coastguard Worker '--all', 178*d9f75844SAndroid Build Coastguard Worker '--format=json', 179*d9f75844SAndroid Build Coastguard Worker os.path.abspath(buildfile_dir), 180*d9f75844SAndroid Build Coastguard Worker target, 181*d9f75844SAndroid Build Coastguard Worker ] 182*d9f75844SAndroid Build Coastguard Worker logging.debug('Running: %r', cmd) 183*d9f75844SAndroid Build Coastguard Worker output_json = subprocess.check_output(cmd, cwd=WEBRTC_ROOT).decode('UTF-8') 184*d9f75844SAndroid Build Coastguard Worker logging.debug('Output: %s', output_json) 185*d9f75844SAndroid Build Coastguard Worker return output_json 186*d9f75844SAndroid Build Coastguard Worker 187*d9f75844SAndroid Build Coastguard Worker def _GetThirdPartyLibraries(self, buildfile_dir, target): 188*d9f75844SAndroid Build Coastguard Worker output = json.loads(LicenseBuilder._RunGN(buildfile_dir, target)) 189*d9f75844SAndroid Build Coastguard Worker libraries = set() 190*d9f75844SAndroid Build Coastguard Worker for described_target in list(output.values()): 191*d9f75844SAndroid Build Coastguard Worker third_party_libs = (self._ParseLibrary(dep) 192*d9f75844SAndroid Build Coastguard Worker for dep in described_target['deps']) 193*d9f75844SAndroid Build Coastguard Worker libraries |= set(lib for lib in third_party_libs if lib) 194*d9f75844SAndroid Build Coastguard Worker return libraries 195*d9f75844SAndroid Build Coastguard Worker 196*d9f75844SAndroid Build Coastguard Worker def GenerateLicenseText(self, output_dir): 197*d9f75844SAndroid Build Coastguard Worker # Get a list of third_party libs from gn. For fat libraries we must consider 198*d9f75844SAndroid Build Coastguard Worker # all architectures, hence the multiple buildfile directories. 199*d9f75844SAndroid Build Coastguard Worker third_party_libs = set() 200*d9f75844SAndroid Build Coastguard Worker for buildfile in self.buildfile_dirs: 201*d9f75844SAndroid Build Coastguard Worker for target in self.targets: 202*d9f75844SAndroid Build Coastguard Worker third_party_libs |= self._GetThirdPartyLibraries(buildfile, target) 203*d9f75844SAndroid Build Coastguard Worker assert len(third_party_libs) > 0 204*d9f75844SAndroid Build Coastguard Worker 205*d9f75844SAndroid Build Coastguard Worker missing_licenses = third_party_libs - set(self.common_licenses_dict.keys()) 206*d9f75844SAndroid Build Coastguard Worker if missing_licenses: 207*d9f75844SAndroid Build Coastguard Worker error_msg = 'Missing licenses for following third_party targets: %s' % \ 208*d9f75844SAndroid Build Coastguard Worker ', '.join(sorted(missing_licenses)) 209*d9f75844SAndroid Build Coastguard Worker logging.error(error_msg) 210*d9f75844SAndroid Build Coastguard Worker raise Exception(error_msg) 211*d9f75844SAndroid Build Coastguard Worker 212*d9f75844SAndroid Build Coastguard Worker # Put webrtc at the front of the list. 213*d9f75844SAndroid Build Coastguard Worker license_libs = sorted(third_party_libs) 214*d9f75844SAndroid Build Coastguard Worker license_libs.insert(0, 'webrtc') 215*d9f75844SAndroid Build Coastguard Worker 216*d9f75844SAndroid Build Coastguard Worker logging.info('List of licenses: %s', ', '.join(license_libs)) 217*d9f75844SAndroid Build Coastguard Worker 218*d9f75844SAndroid Build Coastguard Worker # Generate markdown. 219*d9f75844SAndroid Build Coastguard Worker output_license_file = open(os.path.join(output_dir, 'LICENSE.md'), 'w+') 220*d9f75844SAndroid Build Coastguard Worker for license_lib in license_libs: 221*d9f75844SAndroid Build Coastguard Worker if len(self.common_licenses_dict[license_lib]) == 0: 222*d9f75844SAndroid Build Coastguard Worker logging.info('Skipping compile time or internal dependency: %s', 223*d9f75844SAndroid Build Coastguard Worker license_lib) 224*d9f75844SAndroid Build Coastguard Worker continue # Compile time dependency 225*d9f75844SAndroid Build Coastguard Worker 226*d9f75844SAndroid Build Coastguard Worker output_license_file.write('# %s\n' % license_lib) 227*d9f75844SAndroid Build Coastguard Worker output_license_file.write('```\n') 228*d9f75844SAndroid Build Coastguard Worker for path in self.common_licenses_dict[license_lib]: 229*d9f75844SAndroid Build Coastguard Worker license_path = os.path.join(WEBRTC_ROOT, path) 230*d9f75844SAndroid Build Coastguard Worker with open(license_path, 'r') as license_file: 231*d9f75844SAndroid Build Coastguard Worker license_text = escape(license_file.read(), quote=True) 232*d9f75844SAndroid Build Coastguard Worker output_license_file.write(license_text) 233*d9f75844SAndroid Build Coastguard Worker output_license_file.write('\n') 234*d9f75844SAndroid Build Coastguard Worker output_license_file.write('```\n\n') 235*d9f75844SAndroid Build Coastguard Worker 236*d9f75844SAndroid Build Coastguard Worker output_license_file.close() 237*d9f75844SAndroid Build Coastguard Worker 238*d9f75844SAndroid Build Coastguard Worker 239*d9f75844SAndroid Build Coastguard Workerdef main(): 240*d9f75844SAndroid Build Coastguard Worker parser = argparse.ArgumentParser(description='Generate WebRTC LICENSE.md') 241*d9f75844SAndroid Build Coastguard Worker parser.add_argument('--verbose', 242*d9f75844SAndroid Build Coastguard Worker action='store_true', 243*d9f75844SAndroid Build Coastguard Worker default=False, 244*d9f75844SAndroid Build Coastguard Worker help='Debug logging.') 245*d9f75844SAndroid Build Coastguard Worker parser.add_argument('--target', 246*d9f75844SAndroid Build Coastguard Worker required=True, 247*d9f75844SAndroid Build Coastguard Worker action='append', 248*d9f75844SAndroid Build Coastguard Worker default=[], 249*d9f75844SAndroid Build Coastguard Worker help='Name of the GN target to generate a license for') 250*d9f75844SAndroid Build Coastguard Worker parser.add_argument('output_dir', help='Directory to output LICENSE.md to.') 251*d9f75844SAndroid Build Coastguard Worker parser.add_argument('buildfile_dirs', 252*d9f75844SAndroid Build Coastguard Worker nargs='+', 253*d9f75844SAndroid Build Coastguard Worker help='Directories containing gn generated ninja files') 254*d9f75844SAndroid Build Coastguard Worker args = parser.parse_args() 255*d9f75844SAndroid Build Coastguard Worker 256*d9f75844SAndroid Build Coastguard Worker logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO) 257*d9f75844SAndroid Build Coastguard Worker 258*d9f75844SAndroid Build Coastguard Worker builder = LicenseBuilder(args.buildfile_dirs, args.target) 259*d9f75844SAndroid Build Coastguard Worker builder.GenerateLicenseText(args.output_dir) 260*d9f75844SAndroid Build Coastguard Worker 261*d9f75844SAndroid Build Coastguard Worker 262*d9f75844SAndroid Build Coastguard Workerif __name__ == '__main__': 263*d9f75844SAndroid Build Coastguard Worker sys.exit(main()) 264