blob: 884628bcb867c1b5f04c80d2a2467dc26413d8f0 [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 = {
25 'android_tools': ['third_party/android_tools/LICENSE'],
Edward Lemur5c24c672017-11-06 20:29:00 +010026 'auto': ['third_party/auto/src/LICENSE.txt'],
Sami Kalliomäkie7fac682018-03-20 16:32:49 +010027 'bazel': ['third_party/bazel/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070028 'boringssl': ['third_party/boringssl/src/LICENSE'],
Mirko Bonadeif91ec562017-10-13 13:58:37 +020029 'errorprone': ['third_party/errorprone/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070030 'expat': ['third_party/expat/files/COPYING'],
Oleh Prypind42acbb2018-01-15 20:13:16 +010031 'fiat': ['third_party/boringssl/src/third_party/fiat/LICENSE'],
Edward Lemur5c24c672017-11-06 20:29:00 +010032 'guava': ['third_party/guava/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070033 'ijar': ['third_party/ijar/LICENSE'],
34 'jsoncpp': ['third_party/jsoncpp/LICENSE'],
Sami Kalliomäkie7fac682018-03-20 16:32:49 +010035 'jsr-305': ['third_party/jsr-305/src/ri/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070036 'libc++': ['buildtools/third_party/libc++/trunk/LICENSE.TXT'],
37 'libc++abi': ['buildtools/third_party/libc++abi/trunk/LICENSE.TXT'],
38 'libevent': ['base/third_party/libevent/LICENSE'],
39 'libjpeg_turbo': ['third_party/libjpeg_turbo/LICENSE.md'],
40 'libsrtp': ['third_party/libsrtp/LICENSE'],
41 'libvpx': ['third_party/libvpx/source/libvpx/LICENSE'],
42 'libyuv': ['third_party/libyuv/LICENSE'],
43 'openmax_dl': ['third_party/openmax_dl/LICENSE'],
44 'opus': ['third_party/opus/src/COPYING'],
45 'protobuf': ['third_party/protobuf/LICENSE'],
46 'usrsctp': ['third_party/usrsctp/LICENSE'],
Henrik Kjellander0e8f0532017-09-15 08:57:50 +020047 'webrtc': ['LICENSE', 'LICENSE_THIRD_PARTY'],
Mirko Bonadeife48ee92018-03-20 16:43:23 +010048 'zlib': ['third_party/zlib/LICENSE'],
sakal67e414c2017-09-05 00:16:15 -070049
50 # Compile time dependencies, no license needed:
51 'yasm': [],
52}
53
54SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
55CHECKOUT_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
Henrik Kjellanderec57e052017-10-17 21:36:01 +020056sys.path.append(os.path.join(CHECKOUT_ROOT, 'build'))
57import find_depot_tools
58
Sami Kalliomäkie7fac682018-03-20 16:32:49 +010059THIRD_PARTY_LIB_REGEX = r'^.*/third_party/([\w\-+]+).*$'
sakal67e414c2017-09-05 00:16:15 -070060
61class LicenseBuilder(object):
62
63 def __init__(self, buildfile_dirs, targets):
64 self.buildfile_dirs = buildfile_dirs
65 self.targets = targets
66
67 @staticmethod
68 def _ParseLibrary(dep):
69 """
70 Returns a regex match containing library name after third_party
71
72 Input one of:
73 //a/b/third_party/libname:c
74 //a/b/third_party/libname:c(//d/e/f:g)
75 //a/b/third_party/libname/c:d(//e/f/g:h)
76
77 Outputs match with libname in group 1 or None if this is not a third_party
78 dependency.
79 """
80 return re.match(THIRD_PARTY_LIB_REGEX, dep)
81
82 @staticmethod
83 def _RunGN(buildfile_dir, target):
Henrik Kjellanderec57e052017-10-17 21:36:01 +020084 cmd = [
85 sys.executable,
86 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py'),
87 'desc',
88 '--all',
89 '--format=json',
90 os.path.abspath(buildfile_dir),
91 target,
92 ]
sakal67e414c2017-09-05 00:16:15 -070093 logging.debug("Running: %r", cmd)
94 output_json = subprocess.check_output(cmd, cwd=CHECKOUT_ROOT)
95 logging.debug("Output: %s", output_json)
96 return output_json
97
98 @staticmethod
99 def _GetThirdPartyLibraries(buildfile_dir, target):
100 output = json.loads(LicenseBuilder._RunGN(buildfile_dir, target))
101 libraries = set()
102 for target in output.values():
103 third_party_matches = (
104 LicenseBuilder._ParseLibrary(dep) for dep in target['deps'])
105 libraries |= set(match.group(1) for match in third_party_matches if match)
106 return libraries
107
108 def GenerateLicenseText(self, output_dir):
109 # Get a list of third_party libs from gn. For fat libraries we must consider
110 # all architectures, hence the multiple buildfile directories.
111 third_party_libs = set()
112 for buildfile in self.buildfile_dirs:
113 for target in self.targets:
114 third_party_libs |= LicenseBuilder._GetThirdPartyLibraries(
115 buildfile, target)
116 assert len(third_party_libs) > 0
117
118 missing_licenses = third_party_libs - set(LIB_TO_LICENSES_DICT.keys())
119 if missing_licenses:
120 error_msg = 'Missing licenses: %s' % ', '.join(missing_licenses)
121 logging.error(error_msg)
122 raise Exception(error_msg)
123
124 # Put webrtc at the front of the list.
125 license_libs = sorted(third_party_libs)
126 license_libs.insert(0, 'webrtc')
127
128 logging.info("List of licenses: %s", ', '.join(license_libs))
129
130 # Generate markdown.
131 output_license_file = open(os.path.join(output_dir, 'LICENSE.md'), 'w+')
132 for license_lib in license_libs:
133 if len(LIB_TO_LICENSES_DICT[license_lib]) == 0:
134 logging.info("Skipping compile time dependency: %s", license_lib)
135 continue # Compile time dependency
136
137 output_license_file.write('# %s\n' % license_lib)
138 output_license_file.write('```\n')
139 for path in LIB_TO_LICENSES_DICT[license_lib]:
140 license_path = os.path.join(CHECKOUT_ROOT, path)
141 with open(license_path, 'r') as license_file:
142 license_text = cgi.escape(license_file.read(), quote=True)
143 output_license_file.write(license_text)
144 output_license_file.write('\n')
145 output_license_file.write('```\n\n')
146
147 output_license_file.close()
148
149
150def main():
151 parser = argparse.ArgumentParser(description='Generate WebRTC LICENSE.md')
152 parser.add_argument('--verbose', action='store_true', default=False,
153 help='Debug logging.')
154 parser.add_argument('--target', required=True, action='append', default=[],
155 help='Name of the GN target to generate a license for')
156 parser.add_argument('output_dir',
157 help='Directory to output LICENSE.md to.')
158 parser.add_argument('buildfile_dirs', nargs="+",
159 help='Directories containing gn generated ninja files')
160 args = parser.parse_args()
161
162 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
163
164 builder = LicenseBuilder(args.buildfile_dirs, args.target)
165 builder.GenerateLicenseText(args.output_dir)
166
167
168if __name__ == '__main__':
169 sys.exit(main())