blob: a4f9cbf1ebd5538e17f4ef3b93a4a04444f8aecc [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.
10
11"""Generates license markdown for a prebuilt version of WebRTC."""
12
13import sys
14
15import argparse
16import cgi
17import json
18import logging
19import os
20import re
21import subprocess
22
23
24LIB_TO_LICENSES_DICT = {
Mirko Bonadeiafe72172018-04-13 11:11:52 +020025 'abseil-cpp': ['third_party/abseil-cpp/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070026 'android_tools': ['third_party/android_tools/LICENSE'],
Edward Lemur5c24c672017-11-06 20:29:00 +010027 'auto': ['third_party/auto/src/LICENSE.txt'],
Sami Kalliomäkie7fac682018-03-20 16:32:49 +010028 'bazel': ['third_party/bazel/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070029 'boringssl': ['third_party/boringssl/src/LICENSE'],
Mirko Bonadeif91ec562017-10-13 13:58:37 +020030 'errorprone': ['third_party/errorprone/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070031 'expat': ['third_party/expat/files/COPYING'],
Oleh Prypind42acbb2018-01-15 20:13:16 +010032 'fiat': ['third_party/boringssl/src/third_party/fiat/LICENSE'],
Edward Lemur5c24c672017-11-06 20:29:00 +010033 'guava': ['third_party/guava/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070034 'ijar': ['third_party/ijar/LICENSE'],
35 'jsoncpp': ['third_party/jsoncpp/LICENSE'],
Sami Kalliomäkie7fac682018-03-20 16:32:49 +010036 'jsr-305': ['third_party/jsr-305/src/ri/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070037 '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'],
sakal67e414c2017-09-05 00:16:15 -070044 'opus': ['third_party/opus/src/COPYING'],
45 'protobuf': ['third_party/protobuf/LICENSE'],
Alessio Bazzicaa5b90382018-05-07 09:29:54 +000046 'rnnoise': ['third_party/rnnoise/COPYING'],
sakal67e414c2017-09-05 00:16:15 -070047 'usrsctp': ['third_party/usrsctp/LICENSE'],
Henrik Kjellander0e8f0532017-09-15 08:57:50 +020048 'webrtc': ['LICENSE', 'LICENSE_THIRD_PARTY'],
Mirko Bonadeife48ee92018-03-20 16:43:23 +010049 'zlib': ['third_party/zlib/LICENSE'],
Artem Titova76af0c2018-07-23 17:38:12 +020050 'base64': ['rtc_base/third_party/base64/LICENSE'],
Artem Titove41c4332018-07-25 15:04:28 +020051 'sigslot': ['rtc_base/third_party/sigslot/LICENSE'],
Artem Titov8ff433a2018-07-24 13:42:22 +020052 'portaudio': ['modules/third_party/portaudio/LICENSE'],
Artem Titov8a838fd2018-07-24 15:37:09 +020053 'fft': ['modules/third_party/fft/LICENSE'],
Artem Titove095b812018-07-25 12:10:22 +020054 'g711': ['modules/third_party/g711/LICENSE'],
Artem Titov52b90002018-07-25 13:13:44 +020055 'g722': ['modules/third_party/g722/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070056
57 # Compile time dependencies, no license needed:
58 'yasm': [],
Yura Yaroshevich29c36b22018-06-12 19:59:02 +030059 'ow2_asm': [],
sakal67e414c2017-09-05 00:16:15 -070060}
61
62SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
63CHECKOUT_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
Henrik Kjellanderec57e052017-10-17 21:36:01 +020064sys.path.append(os.path.join(CHECKOUT_ROOT, 'build'))
65import find_depot_tools
66
Sami Kalliomäkie7fac682018-03-20 16:32:49 +010067THIRD_PARTY_LIB_REGEX = r'^.*/third_party/([\w\-+]+).*$'
sakal67e414c2017-09-05 00:16:15 -070068
69class LicenseBuilder(object):
70
71 def __init__(self, buildfile_dirs, targets):
72 self.buildfile_dirs = buildfile_dirs
73 self.targets = targets
74
75 @staticmethod
76 def _ParseLibrary(dep):
77 """
78 Returns a regex match containing library name after third_party
79
80 Input one of:
81 //a/b/third_party/libname:c
82 //a/b/third_party/libname:c(//d/e/f:g)
83 //a/b/third_party/libname/c:d(//e/f/g:h)
84
85 Outputs match with libname in group 1 or None if this is not a third_party
86 dependency.
87 """
88 return re.match(THIRD_PARTY_LIB_REGEX, dep)
89
90 @staticmethod
91 def _RunGN(buildfile_dir, target):
Henrik Kjellanderec57e052017-10-17 21:36:01 +020092 cmd = [
93 sys.executable,
94 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py'),
95 'desc',
96 '--all',
97 '--format=json',
98 os.path.abspath(buildfile_dir),
99 target,
100 ]
sakal67e414c2017-09-05 00:16:15 -0700101 logging.debug("Running: %r", cmd)
102 output_json = subprocess.check_output(cmd, cwd=CHECKOUT_ROOT)
103 logging.debug("Output: %s", output_json)
104 return output_json
105
106 @staticmethod
107 def _GetThirdPartyLibraries(buildfile_dir, target):
108 output = json.loads(LicenseBuilder._RunGN(buildfile_dir, target))
109 libraries = set()
110 for target in output.values():
111 third_party_matches = (
112 LicenseBuilder._ParseLibrary(dep) for dep in target['deps'])
113 libraries |= set(match.group(1) for match in third_party_matches if match)
114 return libraries
115
116 def GenerateLicenseText(self, output_dir):
117 # Get a list of third_party libs from gn. For fat libraries we must consider
118 # all architectures, hence the multiple buildfile directories.
119 third_party_libs = set()
120 for buildfile in self.buildfile_dirs:
121 for target in self.targets:
122 third_party_libs |= LicenseBuilder._GetThirdPartyLibraries(
123 buildfile, target)
124 assert len(third_party_libs) > 0
125
126 missing_licenses = third_party_libs - set(LIB_TO_LICENSES_DICT.keys())
127 if missing_licenses:
128 error_msg = 'Missing licenses: %s' % ', '.join(missing_licenses)
129 logging.error(error_msg)
130 raise Exception(error_msg)
131
132 # Put webrtc at the front of the list.
133 license_libs = sorted(third_party_libs)
134 license_libs.insert(0, 'webrtc')
135
136 logging.info("List of licenses: %s", ', '.join(license_libs))
137
138 # Generate markdown.
139 output_license_file = open(os.path.join(output_dir, 'LICENSE.md'), 'w+')
140 for license_lib in license_libs:
141 if len(LIB_TO_LICENSES_DICT[license_lib]) == 0:
142 logging.info("Skipping compile time dependency: %s", license_lib)
143 continue # Compile time dependency
144
145 output_license_file.write('# %s\n' % license_lib)
146 output_license_file.write('```\n')
147 for path in LIB_TO_LICENSES_DICT[license_lib]:
148 license_path = os.path.join(CHECKOUT_ROOT, path)
149 with open(license_path, 'r') as license_file:
150 license_text = cgi.escape(license_file.read(), quote=True)
151 output_license_file.write(license_text)
152 output_license_file.write('\n')
153 output_license_file.write('```\n\n')
154
155 output_license_file.close()
156
157
158def main():
159 parser = argparse.ArgumentParser(description='Generate WebRTC LICENSE.md')
160 parser.add_argument('--verbose', action='store_true', default=False,
161 help='Debug logging.')
162 parser.add_argument('--target', required=True, action='append', default=[],
163 help='Name of the GN target to generate a license for')
164 parser.add_argument('output_dir',
165 help='Directory to output LICENSE.md to.')
166 parser.add_argument('buildfile_dirs', nargs="+",
167 help='Directories containing gn generated ninja files')
168 args = parser.parse_args()
169
170 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
171
172 builder = LicenseBuilder(args.buildfile_dirs, args.target)
173 builder.GenerateLicenseText(args.output_dir)
174
175
176if __name__ == '__main__':
177 sys.exit(main())