tkchin | 0090b68 | 2016-04-28 18:02:27 -0700 | [diff] [blame] | 1 | #!/usr/bin/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 HTML for a prebuilt version of WebRTC for iOS.""" |
| 12 | |
| 13 | import sys |
| 14 | |
| 15 | import argparse |
| 16 | import cgi |
tkchin | 0090b68 | 2016-04-28 18:02:27 -0700 | [diff] [blame] | 17 | import os |
| 18 | import re |
| 19 | import textwrap |
kthelgason | 96feb2a | 2017-03-13 08:05:59 -0700 | [diff] [blame] | 20 | import subprocess |
tkchin | 0090b68 | 2016-04-28 18:02:27 -0700 | [diff] [blame] | 21 | |
| 22 | |
| 23 | LIB_TO_LICENSES_DICT = { |
tkchin | e76db89 | 2016-05-06 11:19:46 -0700 | [diff] [blame] | 24 | 'boringssl': ['third_party/boringssl/src/LICENSE'], |
tkchin | 0090b68 | 2016-04-28 18:02:27 -0700 | [diff] [blame] | 25 | 'expat': ['third_party/expat/files/COPYING'], |
| 26 | 'jsoncpp': ['third_party/jsoncpp/LICENSE'], |
| 27 | 'opus': ['third_party/opus/src/COPYING'], |
kthelgason | 96feb2a | 2017-03-13 08:05:59 -0700 | [diff] [blame] | 28 | 'protobuf': ['third_party/protobuf/LICENSE'], |
| 29 | 'libsrtp': ['third_party/libsrtp/LICENSE'], |
| 30 | 'usrsctp': ['third_party/usrsctp/LICENSE'], |
tkchin | 0090b68 | 2016-04-28 18:02:27 -0700 | [diff] [blame] | 31 | 'webrtc': ['webrtc/LICENSE', 'webrtc/LICENSE_THIRD_PARTY'], |
kthelgason | 96feb2a | 2017-03-13 08:05:59 -0700 | [diff] [blame] | 32 | 'libvpx': ['third_party/libvpx/source/libvpx/LICENSE'], |
| 33 | 'libyuv': ['third_party/libyuv/LICENSE'], |
tkchin | 0090b68 | 2016-04-28 18:02:27 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0])) |
kthelgason | 96feb2a | 2017-03-13 08:05:59 -0700 | [diff] [blame] | 37 | CHECKOUT_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir)) |
tkchin | 0090b68 | 2016-04-28 18:02:27 -0700 | [diff] [blame] | 38 | WEBRTC_ROOT = os.path.join(CHECKOUT_ROOT, 'webrtc') |
| 39 | |
kjellander | dd460e2 | 2017-04-12 12:06:13 -0700 | [diff] [blame] | 40 | |
kthelgason | 96feb2a | 2017-03-13 08:05:59 -0700 | [diff] [blame] | 41 | def GetThirdPartyLibraries(buildfile_dir, target_name): |
kjellander | dd460e2 | 2017-04-12 12:06:13 -0700 | [diff] [blame] | 42 | def ExtractLibName(string_list): |
kthelgason | 96feb2a | 2017-03-13 08:05:59 -0700 | [diff] [blame] | 43 | # Sample input: |
| 44 | # [" //third_party/usrsctp:usrsctp", " //webrtc:webrtc_common"] |
| 45 | # Sample output: |
| 46 | # ["usrsctp"] |
kjellander | dd460e2 | 2017-04-12 12:06:13 -0700 | [diff] [blame] | 47 | return re.sub(r'\(.*\)', '', string_list).strip().split( |
| 48 | os.path.sep)[-1].split(':')[0] |
kthelgason | 96feb2a | 2017-03-13 08:05:59 -0700 | [diff] [blame] | 49 | output = subprocess.check_output( |
| 50 | ["gn", "desc", buildfile_dir, target_name, '--all']) .split(os.linesep) |
kjellander | dd460e2 | 2017-04-12 12:06:13 -0700 | [diff] [blame] | 51 | return [ExtractLibName(x) for x in output if re.search(r'third_party', x)] |
tkchin | 0090b68 | 2016-04-28 18:02:27 -0700 | [diff] [blame] | 52 | |
| 53 | |
| 54 | class LicenseBuilder(object): |
| 55 | |
kthelgason | 96feb2a | 2017-03-13 08:05:59 -0700 | [diff] [blame] | 56 | def __init__(self, buildfile_dirs, target_name): |
| 57 | self.buildfile_dirs = buildfile_dirs |
| 58 | self.target_name = target_name |
tkchin | 0090b68 | 2016-04-28 18:02:27 -0700 | [diff] [blame] | 59 | |
kthelgason | 96feb2a | 2017-03-13 08:05:59 -0700 | [diff] [blame] | 60 | def GenerateLicenseText(self, output_dir): |
| 61 | # Get a list of third_party libs from gn. For fat libraries we must consider |
| 62 | # all architectures, hence the multiple buildfile directories. |
| 63 | # The `sum` function flattens the 2d list. |
| 64 | third_party_libs = sum([GetThirdPartyLibraries(buildfile, self.target_name) |
| 65 | for buildfile in self.buildfile_dirs], []) |
Kári Tristan Helgason | 1edbda0 | 2017-06-13 10:45:42 +0200 | [diff] [blame^] | 66 | assert len(third_party_libs) > 0 |
tkchin | 0090b68 | 2016-04-28 18:02:27 -0700 | [diff] [blame] | 67 | |
kthelgason | 96feb2a | 2017-03-13 08:05:59 -0700 | [diff] [blame] | 68 | # Generate amalgamated list of libraries. Will exit with error if a |
tkchin | 0090b68 | 2016-04-28 18:02:27 -0700 | [diff] [blame] | 69 | # lib is unrecognized. |
| 70 | license_libs = set() |
kthelgason | 96feb2a | 2017-03-13 08:05:59 -0700 | [diff] [blame] | 71 | for static_lib in third_party_libs: |
| 72 | license_path = LIB_TO_LICENSES_DICT.get(static_lib) |
| 73 | if static_lib == 'yasm': |
| 74 | # yasm is a build-time dep only, and doesn't need a license. |
| 75 | continue |
tkchin | 0090b68 | 2016-04-28 18:02:27 -0700 | [diff] [blame] | 76 | if license_path is None: |
kthelgason | 96feb2a | 2017-03-13 08:05:59 -0700 | [diff] [blame] | 77 | print 'Missing license path for lib: %s' % static_lib |
tkchin | 0090b68 | 2016-04-28 18:02:27 -0700 | [diff] [blame] | 78 | return 1 |
kthelgason | 96feb2a | 2017-03-13 08:05:59 -0700 | [diff] [blame] | 79 | license_libs.add(static_lib) |
tkchin | 0090b68 | 2016-04-28 18:02:27 -0700 | [diff] [blame] | 80 | |
| 81 | # Put webrtc at the front of the list. |
tkchin | 0090b68 | 2016-04-28 18:02:27 -0700 | [diff] [blame] | 82 | license_libs = sorted(license_libs) |
| 83 | license_libs.insert(0, 'webrtc') |
| 84 | |
| 85 | # Generate HTML. |
| 86 | output_license_file = open(os.path.join(output_dir, 'LICENSE.html'), 'w+') |
| 87 | output_license_file.write('<!DOCTYPE html>\n') |
| 88 | output_license_file.write('<html>\n<head>\n') |
| 89 | output_license_file.write('<meta charset="UTF-8">\n') |
| 90 | output_license_file.write('<title>Licenses</title>\n') |
| 91 | style_tag = textwrap.dedent('''\ |
| 92 | <style> |
| 93 | body { margin: 0; font-family: sans-serif; } |
| 94 | pre { background-color: #eeeeee; padding: 1em; white-space: pre-wrap; } |
| 95 | p { margin: 1em; white-space: nowrap; } |
| 96 | </style> |
| 97 | ''') |
| 98 | output_license_file.write(style_tag) |
| 99 | output_license_file.write('</head>\n') |
| 100 | |
| 101 | for license_lib in license_libs: |
| 102 | output_license_file.write('<p>%s<br/></p>\n' % license_lib) |
| 103 | output_license_file.write('<pre>\n') |
| 104 | for path in LIB_TO_LICENSES_DICT[license_lib]: |
| 105 | license_path = os.path.join(CHECKOUT_ROOT, path) |
| 106 | with open(license_path, 'r') as license_file: |
| 107 | license_text = cgi.escape(license_file.read(), quote=True) |
| 108 | output_license_file.write(license_text) |
| 109 | output_license_file.write('\n') |
| 110 | output_license_file.write('</pre>\n') |
| 111 | |
| 112 | output_license_file.write('</body>\n') |
| 113 | output_license_file.write('</html>') |
| 114 | output_license_file.close() |
| 115 | return 0 |
| 116 | |
| 117 | |
kjellander | dd460e2 | 2017-04-12 12:06:13 -0700 | [diff] [blame] | 118 | def main(): |
tkchin | 0090b68 | 2016-04-28 18:02:27 -0700 | [diff] [blame] | 119 | parser = argparse.ArgumentParser(description='Generate WebRTC LICENSE.html') |
kthelgason | 96feb2a | 2017-03-13 08:05:59 -0700 | [diff] [blame] | 120 | parser.add_argument('target_name', |
| 121 | help='Name of the GN target to generate a license for') |
tkchin | 0090b68 | 2016-04-28 18:02:27 -0700 | [diff] [blame] | 122 | parser.add_argument('output_dir', |
| 123 | help='Directory to output LICENSE.html to.') |
kthelgason | 96feb2a | 2017-03-13 08:05:59 -0700 | [diff] [blame] | 124 | parser.add_argument('buildfile_dirs', nargs="+", |
| 125 | help='Directories containing gn generated ninja files') |
tkchin | 0090b68 | 2016-04-28 18:02:27 -0700 | [diff] [blame] | 126 | args = parser.parse_args() |
kthelgason | 96feb2a | 2017-03-13 08:05:59 -0700 | [diff] [blame] | 127 | builder = LicenseBuilder(args.buildfile_dirs, args.target_name) |
| 128 | sys.exit(builder.GenerateLicenseText(args.output_dir)) |
kjellander | dd460e2 | 2017-04-12 12:06:13 -0700 | [diff] [blame] | 129 | |
| 130 | |
| 131 | if __name__ == '__main__': |
| 132 | main() |