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