• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/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"""Generates license markdown for a prebuilt version of WebRTC.
11
12Licenses are taken from dependent libraries which are determined by
13GN desc command `gn desc` on all targets specified via `--target` argument.
14
15One can see all dependencies by invoking this command:
16$ gn.py desc --all --format=json <out_directory> <target> | python -m json.tool
17(see "deps" subarray)
18
19Libraries are mapped to licenses via LIB_TO_LICENSES_DICT dictionary.
20
21"""
22
23import sys
24
25import argparse
26import cgi
27import json
28import logging
29import os
30import re
31import subprocess
32
33# Third_party library to licences mapping. Keys are names of the libraries
34# (right after the `third_party/` prefix)
35LIB_TO_LICENSES_DICT = {
36    'abseil-cpp': ['third_party/abseil-cpp/LICENSE'],
37    'android_ndk': ['third_party/android_ndk/NOTICE'],
38    'android_sdk': ['third_party/android_sdk/LICENSE'],
39    'auto': ['third_party/android_deps/libs/'
40             'com_google_auto_service_auto_service/LICENSE'],
41    'bazel': ['third_party/bazel/LICENSE'],
42    'boringssl': ['third_party/boringssl/src/LICENSE'],
43    'errorprone': ['third_party/android_deps/libs/'
44                   'com_google_errorprone_error_prone_core/LICENSE'],
45    'fiat': ['third_party/boringssl/src/third_party/fiat/LICENSE'],
46    'guava': ['third_party/guava/LICENSE'],
47    'ijar': ['third_party/ijar/LICENSE'],
48    'jsoncpp': ['third_party/jsoncpp/LICENSE'],
49    'libaom': ['third_party/libaom/source/libaom/LICENSE'],
50    'libc++': ['buildtools/third_party/libc++/trunk/LICENSE.TXT'],
51    'libc++abi': ['buildtools/third_party/libc++abi/trunk/LICENSE.TXT'],
52    'libevent': ['base/third_party/libevent/LICENSE'],
53    'libjpeg_turbo': ['third_party/libjpeg_turbo/LICENSE.md'],
54    'libsrtp': ['third_party/libsrtp/LICENSE'],
55    'libvpx': ['third_party/libvpx/source/libvpx/LICENSE'],
56    'libyuv': ['third_party/libyuv/LICENSE'],
57    'nasm': ['third_party/nasm/LICENSE'],
58    'opus': ['third_party/opus/src/COPYING'],
59    'pffft': ['third_party/pffft/LICENSE'],
60    'protobuf': ['third_party/protobuf/LICENSE'],
61    'rnnoise': ['third_party/rnnoise/COPYING'],
62    'usrsctp': ['third_party/usrsctp/LICENSE'],
63    'webrtc': ['LICENSE'],
64    'zlib': ['third_party/zlib/LICENSE'],
65    'base64': ['rtc_base/third_party/base64/LICENSE'],
66    'sigslot': ['rtc_base/third_party/sigslot/LICENSE'],
67    'portaudio': ['modules/third_party/portaudio/LICENSE'],
68    'fft': ['modules/third_party/fft/LICENSE'],
69    'g711': ['modules/third_party/g711/LICENSE'],
70    'g722': ['modules/third_party/g722/LICENSE'],
71    'ooura': ['common_audio/third_party/ooura/LICENSE'],
72    'spl_sqrt_floor': ['common_audio/third_party/spl_sqrt_floor/LICENSE'],
73
74    # TODO(bugs.webrtc.org/1110): Remove this hack. This is not a lib.
75    # For some reason it is listed as so in _GetThirdPartyLibraries.
76    'android_deps': [],
77
78    # Compile time dependencies, no license needed:
79    'yasm': [],
80    'ow2_asm': [],
81    'jdk': [],
82}
83
84# Third_party library _regex_ to licences mapping. Keys are regular expression
85# with names of the libraries (right after the `third_party/` prefix)
86LIB_REGEX_TO_LICENSES_DICT = {
87    'android_deps:android_support_annotations.*': [
88        'third_party/android_deps/libs/' +
89        'com_android_support_support_annotations/LICENSE'
90    ],
91
92    # Internal dependencies, licenses are already included by other dependencies
93    'android_deps:com_android_support_support_annotations.*': [],
94}
95
96
97def FindSrcDirPath():
98  """Returns the abs path to the src/ dir of the project."""
99  src_dir = os.path.dirname(os.path.abspath(__file__))
100  while os.path.basename(src_dir) != 'src':
101    src_dir = os.path.normpath(os.path.join(src_dir, os.pardir))
102  return src_dir
103
104
105SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
106WEBRTC_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
107SRC_DIR = FindSrcDirPath()
108sys.path.append(os.path.join(SRC_DIR, 'build'))
109import find_depot_tools
110
111THIRD_PARTY_LIB_SIMPLE_NAME_REGEX = r'^.*/third_party/([\w\-+]+).*$'
112THIRD_PARTY_LIB_REGEX_TEMPLATE = r'^.*/third_party/%s$'
113
114
115class LicenseBuilder(object):
116
117  def __init__(self,
118               buildfile_dirs,
119               targets,
120               lib_to_licenses_dict=None,
121               lib_regex_to_licenses_dict=None):
122    if lib_to_licenses_dict is None:
123      lib_to_licenses_dict = LIB_TO_LICENSES_DICT
124
125    if lib_regex_to_licenses_dict is None:
126      lib_regex_to_licenses_dict = LIB_REGEX_TO_LICENSES_DICT
127
128    self.buildfile_dirs = buildfile_dirs
129    self.targets = targets
130    self.lib_to_licenses_dict = lib_to_licenses_dict
131    self.lib_regex_to_licenses_dict = lib_regex_to_licenses_dict
132
133    self.common_licenses_dict = self.lib_to_licenses_dict.copy()
134    self.common_licenses_dict.update(self.lib_regex_to_licenses_dict)
135
136  @staticmethod
137  def _ParseLibraryName(dep):
138    """Returns library name after third_party
139
140    Input one of:
141    //a/b/third_party/libname:c
142    //a/b/third_party/libname:c(//d/e/f:g)
143    //a/b/third_party/libname/c:d(//e/f/g:h)
144
145    Outputs libname or None if this is not a third_party dependency.
146    """
147    groups = re.match(THIRD_PARTY_LIB_SIMPLE_NAME_REGEX, dep)
148    return groups.group(1) if groups else None
149
150  def _ParseLibrary(self, dep):
151    """Returns library simple or regex name that matches `dep` after third_party
152
153    This method matches `dep` dependency against simple names in
154    LIB_TO_LICENSES_DICT and regular expression names in
155    LIB_REGEX_TO_LICENSES_DICT keys
156
157    Outputs matched dict key or None if this is not a third_party dependency.
158    """
159    libname = LicenseBuilder._ParseLibraryName(dep)
160
161    for lib_regex in self.lib_regex_to_licenses_dict:
162      if re.match(THIRD_PARTY_LIB_REGEX_TEMPLATE % lib_regex, dep):
163        return lib_regex
164
165    return libname
166
167  @staticmethod
168  def _RunGN(buildfile_dir, target):
169    cmd = [
170        sys.executable,
171        os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py'),
172        'desc',
173        '--all',
174        '--format=json',
175        os.path.abspath(buildfile_dir),
176        target,
177    ]
178    logging.debug('Running: %r', cmd)
179    output_json = subprocess.check_output(cmd, cwd=WEBRTC_ROOT)
180    logging.debug('Output: %s', output_json)
181    return output_json
182
183  def _GetThirdPartyLibraries(self, buildfile_dir, target):
184    output = json.loads(LicenseBuilder._RunGN(buildfile_dir, target))
185    libraries = set()
186    for described_target in output.values():
187      third_party_libs = (
188          self._ParseLibrary(dep) for dep in described_target['deps'])
189      libraries |= set(lib for lib in third_party_libs if lib)
190    return libraries
191
192  def GenerateLicenseText(self, output_dir):
193    # Get a list of third_party libs from gn. For fat libraries we must consider
194    # all architectures, hence the multiple buildfile directories.
195    third_party_libs = set()
196    for buildfile in self.buildfile_dirs:
197      for target in self.targets:
198        third_party_libs |= self._GetThirdPartyLibraries(buildfile, target)
199    assert len(third_party_libs) > 0
200
201    missing_licenses = third_party_libs - set(self.common_licenses_dict.keys())
202    if missing_licenses:
203      error_msg = 'Missing licenses for following third_party targets: %s' % \
204                  ', '.join(missing_licenses)
205      logging.error(error_msg)
206      raise Exception(error_msg)
207
208    # Put webrtc at the front of the list.
209    license_libs = sorted(third_party_libs)
210    license_libs.insert(0, 'webrtc')
211
212    logging.info('List of licenses: %s', ', '.join(license_libs))
213
214    # Generate markdown.
215    output_license_file = open(os.path.join(output_dir, 'LICENSE.md'), 'w+')
216    for license_lib in license_libs:
217      if len(self.common_licenses_dict[license_lib]) == 0:
218        logging.info('Skipping compile time or internal dependency: %s',
219                     license_lib)
220        continue  # Compile time dependency
221
222      output_license_file.write('# %s\n' % license_lib)
223      output_license_file.write('```\n')
224      for path in self.common_licenses_dict[license_lib]:
225        license_path = os.path.join(WEBRTC_ROOT, path)
226        with open(license_path, 'r') as license_file:
227          license_text = cgi.escape(license_file.read(), quote=True)
228          output_license_file.write(license_text)
229          output_license_file.write('\n')
230      output_license_file.write('```\n\n')
231
232    output_license_file.close()
233
234
235def main():
236  parser = argparse.ArgumentParser(description='Generate WebRTC LICENSE.md')
237  parser.add_argument(
238      '--verbose', action='store_true', default=False, help='Debug logging.')
239  parser.add_argument(
240      '--target',
241      required=True,
242      action='append',
243      default=[],
244      help='Name of the GN target to generate a license for')
245  parser.add_argument('output_dir', help='Directory to output LICENSE.md to.')
246  parser.add_argument(
247      'buildfile_dirs',
248      nargs='+',
249      help='Directories containing gn generated ninja files')
250  args = parser.parse_args()
251
252  logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
253
254  builder = LicenseBuilder(args.buildfile_dirs, args.target)
255  builder.GenerateLicenseText(args.output_dir)
256
257
258if __name__ == '__main__':
259  sys.exit(main())
260