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