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