blob: d1a76a64eec1a35467f1ede98eef7f9627339dc9 [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
Mirko Bonadeid8665442018-09-04 12:17:27 +020024def FindSrcDirPath():
25 """Returns the abs path to the src/ dir of the project."""
26 src_dir = os.path.dirname(os.path.abspath(__file__))
27 while os.path.basename(src_dir) != 'src':
28 src_dir = os.path.normpath(os.path.join(src_dir, os.pardir))
29 return src_dir
30
31
sakal67e414c2017-09-05 00:16:15 -070032LIB_TO_LICENSES_DICT = {
Mirko Bonadeiafe72172018-04-13 11:11:52 +020033 'abseil-cpp': ['third_party/abseil-cpp/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070034 'android_tools': ['third_party/android_tools/LICENSE'],
Edward Lemur5c24c672017-11-06 20:29:00 +010035 'auto': ['third_party/auto/src/LICENSE.txt'],
Sami Kalliomäkie7fac682018-03-20 16:32:49 +010036 'bazel': ['third_party/bazel/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070037 'boringssl': ['third_party/boringssl/src/LICENSE'],
Mirko Bonadeif91ec562017-10-13 13:58:37 +020038 'errorprone': ['third_party/errorprone/LICENSE'],
Oleh Prypind42acbb2018-01-15 20:13:16 +010039 'fiat': ['third_party/boringssl/src/third_party/fiat/LICENSE'],
Edward Lemur5c24c672017-11-06 20:29:00 +010040 'guava': ['third_party/guava/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070041 'ijar': ['third_party/ijar/LICENSE'],
42 'jsoncpp': ['third_party/jsoncpp/LICENSE'],
Sami Kalliomäkie7fac682018-03-20 16:32:49 +010043 'jsr-305': ['third_party/jsr-305/src/ri/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070044 'libc++': ['buildtools/third_party/libc++/trunk/LICENSE.TXT'],
45 'libc++abi': ['buildtools/third_party/libc++abi/trunk/LICENSE.TXT'],
46 'libevent': ['base/third_party/libevent/LICENSE'],
47 'libjpeg_turbo': ['third_party/libjpeg_turbo/LICENSE.md'],
48 'libsrtp': ['third_party/libsrtp/LICENSE'],
49 'libvpx': ['third_party/libvpx/source/libvpx/LICENSE'],
50 'libyuv': ['third_party/libyuv/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070051 'opus': ['third_party/opus/src/COPYING'],
52 'protobuf': ['third_party/protobuf/LICENSE'],
Alessio Bazzicaa5b90382018-05-07 09:29:54 +000053 'rnnoise': ['third_party/rnnoise/COPYING'],
sakal67e414c2017-09-05 00:16:15 -070054 'usrsctp': ['third_party/usrsctp/LICENSE'],
Artem Titov29835992018-08-07 11:50:54 +020055 'webrtc': ['LICENSE'],
Mirko Bonadeife48ee92018-03-20 16:43:23 +010056 'zlib': ['third_party/zlib/LICENSE'],
Artem Titova76af0c2018-07-23 17:38:12 +020057 'base64': ['rtc_base/third_party/base64/LICENSE'],
Artem Titove41c4332018-07-25 15:04:28 +020058 'sigslot': ['rtc_base/third_party/sigslot/LICENSE'],
Artem Titov8ff433a2018-07-24 13:42:22 +020059 'portaudio': ['modules/third_party/portaudio/LICENSE'],
Artem Titov8a838fd2018-07-24 15:37:09 +020060 'fft': ['modules/third_party/fft/LICENSE'],
Artem Titove095b812018-07-25 12:10:22 +020061 'g711': ['modules/third_party/g711/LICENSE'],
Artem Titov52b90002018-07-25 13:13:44 +020062 'g722': ['modules/third_party/g722/LICENSE'],
Artem Titov333a5052018-07-25 16:56:18 +020063 'fft4g': ['common_audio/third_party/fft4g/LICENSE'],
Artem Titov29835992018-08-07 11:50:54 +020064 'spl_sqrt_floor': ['common_audio/third_party/spl_sqrt_floor/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070065
66 # Compile time dependencies, no license needed:
67 'yasm': [],
Yura Yaroshevich29c36b22018-06-12 19:59:02 +030068 'ow2_asm': [],
sakal67e414c2017-09-05 00:16:15 -070069}
70
71SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
Mirko Bonadeid8665442018-09-04 12:17:27 +020072WEBRTC_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
73SRC_DIR = FindSrcDirPath()
74sys.path.append(os.path.join(SRC_DIR, 'build'))
Henrik Kjellanderec57e052017-10-17 21:36:01 +020075import find_depot_tools
76
Sami Kalliomäkie7fac682018-03-20 16:32:49 +010077THIRD_PARTY_LIB_REGEX = r'^.*/third_party/([\w\-+]+).*$'
sakal67e414c2017-09-05 00:16:15 -070078
79class LicenseBuilder(object):
80
81 def __init__(self, buildfile_dirs, targets):
82 self.buildfile_dirs = buildfile_dirs
83 self.targets = targets
84
85 @staticmethod
86 def _ParseLibrary(dep):
87 """
88 Returns a regex match containing library name after third_party
89
90 Input one of:
91 //a/b/third_party/libname:c
92 //a/b/third_party/libname:c(//d/e/f:g)
93 //a/b/third_party/libname/c:d(//e/f/g:h)
94
95 Outputs match with libname in group 1 or None if this is not a third_party
96 dependency.
97 """
98 return re.match(THIRD_PARTY_LIB_REGEX, dep)
99
100 @staticmethod
101 def _RunGN(buildfile_dir, target):
Henrik Kjellanderec57e052017-10-17 21:36:01 +0200102 cmd = [
103 sys.executable,
104 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py'),
105 'desc',
106 '--all',
107 '--format=json',
108 os.path.abspath(buildfile_dir),
109 target,
110 ]
sakal67e414c2017-09-05 00:16:15 -0700111 logging.debug("Running: %r", cmd)
Mirko Bonadeid8665442018-09-04 12:17:27 +0200112 output_json = subprocess.check_output(cmd, cwd=WEBRTC_ROOT)
sakal67e414c2017-09-05 00:16:15 -0700113 logging.debug("Output: %s", output_json)
114 return output_json
115
116 @staticmethod
117 def _GetThirdPartyLibraries(buildfile_dir, target):
118 output = json.loads(LicenseBuilder._RunGN(buildfile_dir, target))
119 libraries = set()
120 for target in output.values():
121 third_party_matches = (
122 LicenseBuilder._ParseLibrary(dep) for dep in target['deps'])
123 libraries |= set(match.group(1) for match in third_party_matches if match)
124 return libraries
125
126 def GenerateLicenseText(self, output_dir):
127 # Get a list of third_party libs from gn. For fat libraries we must consider
128 # all architectures, hence the multiple buildfile directories.
129 third_party_libs = set()
130 for buildfile in self.buildfile_dirs:
131 for target in self.targets:
132 third_party_libs |= LicenseBuilder._GetThirdPartyLibraries(
133 buildfile, target)
134 assert len(third_party_libs) > 0
135
136 missing_licenses = third_party_libs - set(LIB_TO_LICENSES_DICT.keys())
137 if missing_licenses:
138 error_msg = 'Missing licenses: %s' % ', '.join(missing_licenses)
139 logging.error(error_msg)
140 raise Exception(error_msg)
141
142 # Put webrtc at the front of the list.
143 license_libs = sorted(third_party_libs)
144 license_libs.insert(0, 'webrtc')
145
146 logging.info("List of licenses: %s", ', '.join(license_libs))
147
148 # Generate markdown.
149 output_license_file = open(os.path.join(output_dir, 'LICENSE.md'), 'w+')
150 for license_lib in license_libs:
151 if len(LIB_TO_LICENSES_DICT[license_lib]) == 0:
152 logging.info("Skipping compile time dependency: %s", license_lib)
153 continue # Compile time dependency
154
155 output_license_file.write('# %s\n' % license_lib)
156 output_license_file.write('```\n')
157 for path in LIB_TO_LICENSES_DICT[license_lib]:
Mirko Bonadeid8665442018-09-04 12:17:27 +0200158 license_path = os.path.join(WEBRTC_ROOT, path)
sakal67e414c2017-09-05 00:16:15 -0700159 with open(license_path, 'r') as license_file:
160 license_text = cgi.escape(license_file.read(), quote=True)
161 output_license_file.write(license_text)
162 output_license_file.write('\n')
163 output_license_file.write('```\n\n')
164
165 output_license_file.close()
166
167
168def main():
169 parser = argparse.ArgumentParser(description='Generate WebRTC LICENSE.md')
170 parser.add_argument('--verbose', action='store_true', default=False,
171 help='Debug logging.')
172 parser.add_argument('--target', required=True, action='append', default=[],
173 help='Name of the GN target to generate a license for')
174 parser.add_argument('output_dir',
175 help='Directory to output LICENSE.md to.')
176 parser.add_argument('buildfile_dirs', nargs="+",
177 help='Directories containing gn generated ninja files')
178 args = parser.parse_args()
179
180 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
181
182 builder = LicenseBuilder(args.buildfile_dirs, args.target)
183 builder.GenerateLicenseText(args.output_dir)
184
185
186if __name__ == '__main__':
187 sys.exit(main())