Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 1 | # Copyright (c) 2015, Google Inc. |
| 2 | # |
| 3 | # Permission to use, copy, modify, and/or distribute this software for any |
| 4 | # purpose with or without fee is hereby granted, provided that the above |
| 5 | # copyright notice and this permission notice appear in all copies. |
| 6 | # |
| 7 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | # OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 | |
Matt Braithwaite | 1669589 | 2016-06-09 09:34:11 -0700 | [diff] [blame] | 15 | """Enumerates source files for consumption by various build systems.""" |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 16 | |
Matt Braithwaite | 1669589 | 2016-06-09 09:34:11 -0700 | [diff] [blame] | 17 | import optparse |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 18 | import os |
| 19 | import subprocess |
| 20 | import sys |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 21 | import json |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 22 | |
| 23 | |
| 24 | # OS_ARCH_COMBOS maps from OS and platform to the OpenSSL assembly "style" for |
| 25 | # that platform and the extension used by asm files. |
| 26 | OS_ARCH_COMBOS = [ |
| 27 | ('linux', 'arm', 'linux32', [], 'S'), |
| 28 | ('linux', 'aarch64', 'linux64', [], 'S'), |
| 29 | ('linux', 'x86', 'elf', ['-fPIC', '-DOPENSSL_IA32_SSE2'], 'S'), |
| 30 | ('linux', 'x86_64', 'elf', [], 'S'), |
| 31 | ('mac', 'x86', 'macosx', ['-fPIC', '-DOPENSSL_IA32_SSE2'], 'S'), |
| 32 | ('mac', 'x86_64', 'macosx', [], 'S'), |
| 33 | ('win', 'x86', 'win32n', ['-DOPENSSL_IA32_SSE2'], 'asm'), |
| 34 | ('win', 'x86_64', 'nasm', [], 'asm'), |
| 35 | ] |
| 36 | |
| 37 | # NON_PERL_FILES enumerates assembly files that are not processed by the |
| 38 | # perlasm system. |
| 39 | NON_PERL_FILES = { |
| 40 | ('linux', 'arm'): [ |
Adam Langley | 7b8b9c1 | 2016-01-04 07:13:00 -0800 | [diff] [blame] | 41 | 'src/crypto/curve25519/asm/x25519-asm-arm.S', |
David Benjamin | 3c4a5cb | 2016-03-29 17:43:31 -0400 | [diff] [blame] | 42 | 'src/crypto/poly1305/poly1305_arm_asm.S', |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 43 | ], |
Matt Braithwaite | e021a24 | 2016-01-14 13:41:46 -0800 | [diff] [blame] | 44 | ('linux', 'x86_64'): [ |
| 45 | 'src/crypto/curve25519/asm/x25519-asm-x86_64.S', |
| 46 | ], |
Piotr Sikora | 8ca0b41 | 2016-06-02 11:59:21 -0700 | [diff] [blame] | 47 | ('mac', 'x86_64'): [ |
| 48 | 'src/crypto/curve25519/asm/x25519-asm-x86_64.S', |
| 49 | ], |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Matt Braithwaite | 1669589 | 2016-06-09 09:34:11 -0700 | [diff] [blame] | 52 | PREFIX = None |
| 53 | |
| 54 | |
| 55 | def PathOf(x): |
| 56 | return x if not PREFIX else os.path.join(PREFIX, x) |
| 57 | |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 58 | |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 59 | class Android(object): |
| 60 | |
| 61 | def __init__(self): |
| 62 | self.header = \ |
| 63 | """# Copyright (C) 2015 The Android Open Source Project |
| 64 | # |
| 65 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 66 | # you may not use this file except in compliance with the License. |
| 67 | # You may obtain a copy of the License at |
| 68 | # |
| 69 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 70 | # |
| 71 | # Unless required by applicable law or agreed to in writing, software |
| 72 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 73 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 74 | # See the License for the specific language governing permissions and |
| 75 | # limitations under the License. |
| 76 | |
Dan Willemsen | b57e4fc | 2016-07-21 11:08:44 -0700 | [diff] [blame^] | 77 | # This file is created by generate_build_files.py. Do not edit manually. |
| 78 | |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 79 | """ |
| 80 | |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 81 | def ExtraFiles(self): |
| 82 | return ['android_compat_hacks.c', 'android_compat_keywrap.c'] |
| 83 | |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 84 | def PrintVariableSection(self, out, name, files): |
| 85 | out.write('%s := \\\n' % name) |
| 86 | for f in sorted(files): |
| 87 | out.write(' %s\\\n' % f) |
| 88 | out.write('\n') |
| 89 | |
| 90 | def WriteFiles(self, files, asm_outputs): |
Dan Willemsen | b57e4fc | 2016-07-21 11:08:44 -0700 | [diff] [blame^] | 91 | # New Android.bp format |
| 92 | with open('sources.bp', 'w+') as blueprint: |
| 93 | blueprint.write(self.header.replace('#', '//')) |
| 94 | |
| 95 | blueprint.write('cc_defaults {\n') |
| 96 | blueprint.write(' name: "libcrypto_sources",\n') |
| 97 | blueprint.write(' srcs: [\n') |
| 98 | for f in sorted(files['crypto'] + self.ExtraFiles()): |
| 99 | blueprint.write(' "%s",\n' % f) |
| 100 | blueprint.write(' ],\n') |
| 101 | blueprint.write(' target: {\n') |
| 102 | |
| 103 | for ((osname, arch), asm_files) in asm_outputs: |
| 104 | if osname != 'linux': |
| 105 | continue |
| 106 | if arch == 'aarch64': |
| 107 | arch = 'arm64' |
| 108 | |
| 109 | blueprint.write(' android_%s: {\n' % arch) |
| 110 | blueprint.write(' srcs: [\n') |
| 111 | for f in sorted(asm_files): |
| 112 | blueprint.write(' "%s",\n' % f) |
| 113 | blueprint.write(' ],\n') |
| 114 | blueprint.write(' },\n') |
| 115 | |
| 116 | if arch == 'x86' or arch == 'x86_64': |
| 117 | blueprint.write(' linux_%s: {\n' % arch) |
| 118 | blueprint.write(' srcs: [\n') |
| 119 | for f in sorted(asm_files): |
| 120 | blueprint.write(' "%s",\n' % f) |
| 121 | blueprint.write(' ],\n') |
| 122 | blueprint.write(' },\n') |
| 123 | |
| 124 | blueprint.write(' },\n') |
| 125 | blueprint.write('}\n\n') |
| 126 | |
| 127 | blueprint.write('cc_defaults {\n') |
| 128 | blueprint.write(' name: "libssl_sources",\n') |
| 129 | blueprint.write(' srcs: [\n') |
| 130 | for f in sorted(files['ssl']): |
| 131 | blueprint.write(' "%s",\n' % f) |
| 132 | blueprint.write(' ],\n') |
| 133 | blueprint.write('}\n\n') |
| 134 | |
| 135 | blueprint.write('cc_defaults {\n') |
| 136 | blueprint.write(' name: "bssl_sources",\n') |
| 137 | blueprint.write(' srcs: [\n') |
| 138 | for f in sorted(files['tool']): |
| 139 | blueprint.write(' "%s",\n' % f) |
| 140 | blueprint.write(' ],\n') |
| 141 | blueprint.write('}\n\n') |
| 142 | |
| 143 | blueprint.write('cc_defaults {\n') |
| 144 | blueprint.write(' name: "boringssl_test_support_sources",\n') |
| 145 | blueprint.write(' srcs: [\n') |
| 146 | for f in sorted(files['test_support']): |
| 147 | blueprint.write(' "%s",\n' % f) |
| 148 | blueprint.write(' ],\n') |
| 149 | blueprint.write('}\n\n') |
| 150 | |
| 151 | blueprint.write('cc_defaults {\n') |
| 152 | blueprint.write(' name: "boringssl_tests_sources",\n') |
| 153 | blueprint.write(' srcs: [\n') |
| 154 | for f in sorted(files['test']): |
| 155 | blueprint.write(' "%s",\n' % f) |
| 156 | blueprint.write(' ],\n') |
| 157 | blueprint.write('}\n') |
| 158 | |
| 159 | # Legacy Android.mk format, only used by Trusty in new branches |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 160 | with open('sources.mk', 'w+') as makefile: |
| 161 | makefile.write(self.header) |
| 162 | |
Piotr Sikora | 6ae67df | 2015-11-23 18:46:33 -0800 | [diff] [blame] | 163 | crypto_files = files['crypto'] + self.ExtraFiles() |
| 164 | self.PrintVariableSection(makefile, 'crypto_sources', crypto_files) |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 165 | |
| 166 | for ((osname, arch), asm_files) in asm_outputs: |
Dan Willemsen | b57e4fc | 2016-07-21 11:08:44 -0700 | [diff] [blame^] | 167 | if osname != 'linux': |
| 168 | continue |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 169 | self.PrintVariableSection( |
| 170 | makefile, '%s_%s_sources' % (osname, arch), asm_files) |
| 171 | |
| 172 | |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 173 | class AndroidStandalone(Android): |
| 174 | """AndroidStandalone is for Android builds outside of the Android-system, i.e. |
| 175 | |
| 176 | for applications that wish wish to ship BoringSSL. |
| 177 | """ |
| 178 | |
| 179 | def ExtraFiles(self): |
| 180 | return [] |
| 181 | |
| 182 | |
| 183 | class Bazel(object): |
| 184 | """Bazel outputs files suitable for including in Bazel files.""" |
| 185 | |
| 186 | def __init__(self): |
| 187 | self.firstSection = True |
| 188 | self.header = \ |
| 189 | """# This file is created by generate_build_files.py. Do not edit manually. |
| 190 | |
| 191 | """ |
| 192 | |
| 193 | def PrintVariableSection(self, out, name, files): |
| 194 | if not self.firstSection: |
| 195 | out.write('\n') |
| 196 | self.firstSection = False |
| 197 | |
| 198 | out.write('%s = [\n' % name) |
| 199 | for f in sorted(files): |
Matt Braithwaite | 1669589 | 2016-06-09 09:34:11 -0700 | [diff] [blame] | 200 | out.write(' "%s",\n' % PathOf(f)) |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 201 | out.write(']\n') |
| 202 | |
| 203 | def WriteFiles(self, files, asm_outputs): |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 204 | with open('BUILD.generated.bzl', 'w+') as out: |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 205 | out.write(self.header) |
| 206 | |
| 207 | self.PrintVariableSection(out, 'ssl_headers', files['ssl_headers']) |
| 208 | self.PrintVariableSection( |
| 209 | out, 'ssl_internal_headers', files['ssl_internal_headers']) |
| 210 | self.PrintVariableSection(out, 'ssl_sources', files['ssl']) |
| 211 | self.PrintVariableSection(out, 'crypto_headers', files['crypto_headers']) |
| 212 | self.PrintVariableSection( |
| 213 | out, 'crypto_internal_headers', files['crypto_internal_headers']) |
| 214 | self.PrintVariableSection(out, 'crypto_sources', files['crypto']) |
| 215 | self.PrintVariableSection(out, 'tool_sources', files['tool']) |
Adam Langley | f11f233 | 2016-06-30 11:56:19 -0700 | [diff] [blame] | 216 | self.PrintVariableSection(out, 'tool_headers', files['tool_headers']) |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 217 | |
| 218 | for ((osname, arch), asm_files) in asm_outputs: |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 219 | self.PrintVariableSection( |
Piotr Sikora | 3f5fe60 | 2015-10-28 12:24:35 -0700 | [diff] [blame] | 220 | out, 'crypto_sources_%s_%s' % (osname, arch), asm_files) |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 221 | |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 222 | with open('BUILD.generated_tests.bzl', 'w+') as out: |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 223 | out.write(self.header) |
| 224 | |
| 225 | out.write('test_support_sources = [\n') |
Matt Braithwaite | dfdd49c | 2016-06-13 17:06:48 -0700 | [diff] [blame] | 226 | for filename in (files['test_support'] + |
| 227 | files['crypto_internal_headers'] + |
| 228 | files['ssl_internal_headers']): |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 229 | if os.path.basename(filename) == 'malloc.cc': |
| 230 | continue |
Matt Braithwaite | 1669589 | 2016-06-09 09:34:11 -0700 | [diff] [blame] | 231 | out.write(' "%s",\n' % PathOf(filename)) |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 232 | |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 233 | out.write(']\n\n') |
| 234 | |
Matt Braithwaite | dfdd49c | 2016-06-13 17:06:48 -0700 | [diff] [blame] | 235 | out.write('def create_tests(copts, crypto, ssl):\n') |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 236 | name_counts = {} |
| 237 | for test in files['tests']: |
| 238 | name = os.path.basename(test[0]) |
| 239 | name_counts[name] = name_counts.get(name, 0) + 1 |
| 240 | |
| 241 | first = True |
| 242 | for test in files['tests']: |
| 243 | name = os.path.basename(test[0]) |
| 244 | if name_counts[name] > 1: |
| 245 | if '/' in test[1]: |
| 246 | name += '_' + os.path.splitext(os.path.basename(test[1]))[0] |
| 247 | else: |
| 248 | name += '_' + test[1].replace('-', '_') |
| 249 | |
| 250 | if not first: |
| 251 | out.write('\n') |
| 252 | first = False |
| 253 | |
| 254 | src_prefix = 'src/' + test[0] |
| 255 | for src in files['test']: |
| 256 | if src.startswith(src_prefix): |
| 257 | src = src |
| 258 | break |
| 259 | else: |
| 260 | raise ValueError("Can't find source for %s" % test[0]) |
| 261 | |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 262 | out.write(' native.cc_test(\n') |
| 263 | out.write(' name = "%s",\n' % name) |
| 264 | out.write(' size = "small",\n') |
Matt Braithwaite | dfdd49c | 2016-06-13 17:06:48 -0700 | [diff] [blame] | 265 | out.write(' srcs = ["%s"] + test_support_sources,\n' % |
Matt Braithwaite | 1669589 | 2016-06-09 09:34:11 -0700 | [diff] [blame] | 266 | PathOf(src)) |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 267 | |
| 268 | data_files = [] |
| 269 | if len(test) > 1: |
| 270 | |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 271 | out.write(' args = [\n') |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 272 | for arg in test[1:]: |
| 273 | if '/' in arg: |
Matt Braithwaite | 1669589 | 2016-06-09 09:34:11 -0700 | [diff] [blame] | 274 | out.write(' "$(location %s)",\n' % |
| 275 | PathOf(os.path.join('src', arg))) |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 276 | data_files.append('src/%s' % arg) |
| 277 | else: |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 278 | out.write(' "%s",\n' % arg) |
| 279 | out.write(' ],\n') |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 280 | |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 281 | out.write(' copts = copts,\n') |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 282 | |
| 283 | if len(data_files) > 0: |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 284 | out.write(' data = [\n') |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 285 | for filename in data_files: |
Matt Braithwaite | 1669589 | 2016-06-09 09:34:11 -0700 | [diff] [blame] | 286 | out.write(' "%s",\n' % PathOf(filename)) |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 287 | out.write(' ],\n') |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 288 | |
| 289 | if 'ssl/' in test[0]: |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 290 | out.write(' deps = [\n') |
Matt Braithwaite | dfdd49c | 2016-06-13 17:06:48 -0700 | [diff] [blame] | 291 | out.write(' crypto,\n') |
| 292 | out.write(' ssl,\n') |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 293 | out.write(' ],\n') |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 294 | else: |
Matt Braithwaite | dfdd49c | 2016-06-13 17:06:48 -0700 | [diff] [blame] | 295 | out.write(' deps = [crypto],\n') |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 296 | out.write(' )\n') |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 297 | |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 298 | |
David Benjamin | 38d01c6 | 2016-04-21 18:47:57 -0400 | [diff] [blame] | 299 | class GN(object): |
| 300 | |
| 301 | def __init__(self): |
| 302 | self.firstSection = True |
| 303 | self.header = \ |
| 304 | """# Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 305 | # Use of this source code is governed by a BSD-style license that can be |
| 306 | # found in the LICENSE file. |
| 307 | |
| 308 | # This file is created by generate_build_files.py. Do not edit manually. |
| 309 | |
| 310 | """ |
| 311 | |
| 312 | def PrintVariableSection(self, out, name, files): |
| 313 | if not self.firstSection: |
| 314 | out.write('\n') |
| 315 | self.firstSection = False |
| 316 | |
| 317 | out.write('%s = [\n' % name) |
| 318 | for f in sorted(files): |
| 319 | out.write(' "%s",\n' % f) |
| 320 | out.write(']\n') |
| 321 | |
| 322 | def WriteFiles(self, files, asm_outputs): |
| 323 | with open('BUILD.generated.gni', 'w+') as out: |
| 324 | out.write(self.header) |
| 325 | |
| 326 | self.PrintVariableSection(out, 'crypto_sources', files['crypto']) |
| 327 | self.PrintVariableSection(out, 'ssl_sources', files['ssl']) |
| 328 | |
| 329 | for ((osname, arch), asm_files) in asm_outputs: |
| 330 | self.PrintVariableSection( |
| 331 | out, 'crypto_sources_%s_%s' % (osname, arch), asm_files) |
| 332 | |
| 333 | fuzzers = [os.path.splitext(os.path.basename(fuzzer))[0] |
| 334 | for fuzzer in files['fuzz']] |
| 335 | self.PrintVariableSection(out, 'fuzzers', fuzzers) |
| 336 | |
| 337 | with open('BUILD.generated_tests.gni', 'w+') as out: |
| 338 | self.firstSection = True |
| 339 | out.write(self.header) |
| 340 | |
| 341 | self.PrintVariableSection(out, '_test_support_sources', |
| 342 | files['test_support']) |
| 343 | out.write('\n') |
| 344 | |
| 345 | out.write('template("create_tests") {\n') |
| 346 | |
| 347 | all_tests = [] |
| 348 | for test in sorted(files['test']): |
| 349 | test_name = 'boringssl_%s' % os.path.splitext(os.path.basename(test))[0] |
| 350 | all_tests.append(test_name) |
| 351 | |
| 352 | out.write(' executable("%s") {\n' % test_name) |
| 353 | out.write(' sources = [\n') |
| 354 | out.write(' "%s",\n' % test) |
| 355 | out.write(' ]\n') |
| 356 | out.write(' sources += _test_support_sources\n') |
David Benjamin | b3be1cf | 2016-04-27 19:15:06 -0400 | [diff] [blame] | 357 | out.write(' if (defined(invoker.configs_exclude)) {\n') |
| 358 | out.write(' configs -= invoker.configs_exclude\n') |
| 359 | out.write(' }\n') |
David Benjamin | 38d01c6 | 2016-04-21 18:47:57 -0400 | [diff] [blame] | 360 | out.write(' configs += invoker.configs\n') |
| 361 | out.write(' deps = invoker.deps\n') |
| 362 | out.write(' }\n') |
| 363 | out.write('\n') |
| 364 | |
| 365 | out.write(' group(target_name) {\n') |
| 366 | out.write(' deps = [\n') |
| 367 | for test_name in sorted(all_tests): |
| 368 | out.write(' ":%s",\n' % test_name) |
| 369 | out.write(' ]\n') |
| 370 | out.write(' }\n') |
| 371 | out.write('}\n') |
| 372 | |
| 373 | |
| 374 | class GYP(object): |
| 375 | |
| 376 | def __init__(self): |
| 377 | self.header = \ |
| 378 | """# Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 379 | # Use of this source code is governed by a BSD-style license that can be |
| 380 | # found in the LICENSE file. |
| 381 | |
| 382 | # This file is created by generate_build_files.py. Do not edit manually. |
| 383 | |
| 384 | """ |
| 385 | |
| 386 | def PrintVariableSection(self, out, name, files): |
| 387 | out.write(' \'%s\': [\n' % name) |
| 388 | for f in sorted(files): |
| 389 | out.write(' \'%s\',\n' % f) |
| 390 | out.write(' ],\n') |
| 391 | |
| 392 | def WriteFiles(self, files, asm_outputs): |
| 393 | with open('boringssl.gypi', 'w+') as gypi: |
| 394 | gypi.write(self.header + '{\n \'variables\': {\n') |
| 395 | |
| 396 | self.PrintVariableSection( |
| 397 | gypi, 'boringssl_ssl_sources', files['ssl']) |
| 398 | self.PrintVariableSection( |
| 399 | gypi, 'boringssl_crypto_sources', files['crypto']) |
| 400 | |
| 401 | for ((osname, arch), asm_files) in asm_outputs: |
| 402 | self.PrintVariableSection(gypi, 'boringssl_%s_%s_sources' % |
| 403 | (osname, arch), asm_files) |
| 404 | |
| 405 | gypi.write(' }\n}\n') |
| 406 | |
| 407 | with open('boringssl_tests.gypi', 'w+') as test_gypi: |
| 408 | test_gypi.write(self.header + '{\n \'targets\': [\n') |
| 409 | |
| 410 | test_names = [] |
| 411 | for test in sorted(files['test']): |
| 412 | test_name = 'boringssl_%s' % os.path.splitext(os.path.basename(test))[0] |
| 413 | test_gypi.write(""" { |
| 414 | 'target_name': '%s', |
| 415 | 'type': 'executable', |
| 416 | 'dependencies': [ |
| 417 | 'boringssl.gyp:boringssl', |
| 418 | ], |
| 419 | 'sources': [ |
| 420 | '%s', |
| 421 | '<@(boringssl_test_support_sources)', |
| 422 | ], |
| 423 | # TODO(davidben): Fix size_t truncations in BoringSSL. |
| 424 | # https://crbug.com/429039 |
| 425 | 'msvs_disabled_warnings': [ 4267, ], |
| 426 | },\n""" % (test_name, test)) |
| 427 | test_names.append(test_name) |
| 428 | |
| 429 | test_names.sort() |
| 430 | |
| 431 | test_gypi.write(' ],\n \'variables\': {\n') |
| 432 | |
| 433 | self.PrintVariableSection( |
| 434 | test_gypi, 'boringssl_test_support_sources', files['test_support']) |
| 435 | |
| 436 | test_gypi.write(' \'boringssl_test_targets\': [\n') |
| 437 | |
| 438 | for test in sorted(test_names): |
| 439 | test_gypi.write(""" '%s',\n""" % test) |
| 440 | |
| 441 | test_gypi.write(' ],\n }\n}\n') |
| 442 | |
| 443 | |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 444 | def FindCMakeFiles(directory): |
| 445 | """Returns list of all CMakeLists.txt files recursively in directory.""" |
| 446 | cmakefiles = [] |
| 447 | |
| 448 | for (path, _, filenames) in os.walk(directory): |
| 449 | for filename in filenames: |
| 450 | if filename == 'CMakeLists.txt': |
| 451 | cmakefiles.append(os.path.join(path, filename)) |
| 452 | |
| 453 | return cmakefiles |
| 454 | |
| 455 | |
| 456 | def NoTests(dent, is_dir): |
| 457 | """Filter function that can be passed to FindCFiles in order to remove test |
| 458 | sources.""" |
| 459 | if is_dir: |
| 460 | return dent != 'test' |
| 461 | return 'test.' not in dent and not dent.startswith('example_') |
| 462 | |
| 463 | |
| 464 | def OnlyTests(dent, is_dir): |
| 465 | """Filter function that can be passed to FindCFiles in order to remove |
| 466 | non-test sources.""" |
| 467 | if is_dir: |
David Benjamin | 2607383 | 2015-05-11 20:52:48 -0400 | [diff] [blame] | 468 | return dent != 'test' |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 469 | return '_test.' in dent or dent.startswith('example_') |
| 470 | |
| 471 | |
David Benjamin | 2607383 | 2015-05-11 20:52:48 -0400 | [diff] [blame] | 472 | def AllFiles(dent, is_dir): |
| 473 | """Filter function that can be passed to FindCFiles in order to include all |
| 474 | sources.""" |
| 475 | return True |
| 476 | |
| 477 | |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 478 | def SSLHeaderFiles(dent, is_dir): |
| 479 | return dent in ['ssl.h', 'tls1.h', 'ssl23.h', 'ssl3.h', 'dtls1.h'] |
| 480 | |
| 481 | |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 482 | def FindCFiles(directory, filter_func): |
| 483 | """Recurses through directory and returns a list of paths to all the C source |
| 484 | files that pass filter_func.""" |
| 485 | cfiles = [] |
| 486 | |
| 487 | for (path, dirnames, filenames) in os.walk(directory): |
| 488 | for filename in filenames: |
| 489 | if not filename.endswith('.c') and not filename.endswith('.cc'): |
| 490 | continue |
| 491 | if not filter_func(filename, False): |
| 492 | continue |
| 493 | cfiles.append(os.path.join(path, filename)) |
| 494 | |
| 495 | for (i, dirname) in enumerate(dirnames): |
| 496 | if not filter_func(dirname, True): |
| 497 | del dirnames[i] |
| 498 | |
| 499 | return cfiles |
| 500 | |
| 501 | |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 502 | def FindHeaderFiles(directory, filter_func): |
| 503 | """Recurses through directory and returns a list of paths to all the header files that pass filter_func.""" |
| 504 | hfiles = [] |
| 505 | |
| 506 | for (path, dirnames, filenames) in os.walk(directory): |
| 507 | for filename in filenames: |
| 508 | if not filename.endswith('.h'): |
| 509 | continue |
| 510 | if not filter_func(filename, False): |
| 511 | continue |
| 512 | hfiles.append(os.path.join(path, filename)) |
| 513 | |
Matt Braithwaite | dfdd49c | 2016-06-13 17:06:48 -0700 | [diff] [blame] | 514 | for (i, dirname) in enumerate(dirnames): |
| 515 | if not filter_func(dirname, True): |
| 516 | del dirnames[i] |
| 517 | |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 518 | return hfiles |
| 519 | |
| 520 | |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 521 | def ExtractPerlAsmFromCMakeFile(cmakefile): |
| 522 | """Parses the contents of the CMakeLists.txt file passed as an argument and |
| 523 | returns a list of all the perlasm() directives found in the file.""" |
| 524 | perlasms = [] |
| 525 | with open(cmakefile) as f: |
| 526 | for line in f: |
| 527 | line = line.strip() |
| 528 | if not line.startswith('perlasm('): |
| 529 | continue |
| 530 | if not line.endswith(')'): |
| 531 | raise ValueError('Bad perlasm line in %s' % cmakefile) |
| 532 | # Remove "perlasm(" from start and ")" from end |
| 533 | params = line[8:-1].split() |
| 534 | if len(params) < 2: |
| 535 | raise ValueError('Bad perlasm line in %s' % cmakefile) |
| 536 | perlasms.append({ |
| 537 | 'extra_args': params[2:], |
| 538 | 'input': os.path.join(os.path.dirname(cmakefile), params[1]), |
| 539 | 'output': os.path.join(os.path.dirname(cmakefile), params[0]), |
| 540 | }) |
| 541 | |
| 542 | return perlasms |
| 543 | |
| 544 | |
| 545 | def ReadPerlAsmOperations(): |
| 546 | """Returns a list of all perlasm() directives found in CMake config files in |
| 547 | src/.""" |
| 548 | perlasms = [] |
| 549 | cmakefiles = FindCMakeFiles('src') |
| 550 | |
| 551 | for cmakefile in cmakefiles: |
| 552 | perlasms.extend(ExtractPerlAsmFromCMakeFile(cmakefile)) |
| 553 | |
| 554 | return perlasms |
| 555 | |
| 556 | |
| 557 | def PerlAsm(output_filename, input_filename, perlasm_style, extra_args): |
| 558 | """Runs the a perlasm script and puts the output into output_filename.""" |
| 559 | base_dir = os.path.dirname(output_filename) |
| 560 | if not os.path.isdir(base_dir): |
| 561 | os.makedirs(base_dir) |
David Benjamin | fdd8e9c | 2016-06-26 13:18:50 -0400 | [diff] [blame] | 562 | subprocess.check_call( |
| 563 | ['perl', input_filename, perlasm_style] + extra_args + [output_filename]) |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 564 | |
| 565 | |
| 566 | def ArchForAsmFilename(filename): |
| 567 | """Returns the architectures that a given asm file should be compiled for |
| 568 | based on substrings in the filename.""" |
| 569 | |
| 570 | if 'x86_64' in filename or 'avx2' in filename: |
| 571 | return ['x86_64'] |
| 572 | elif ('x86' in filename and 'x86_64' not in filename) or '586' in filename: |
| 573 | return ['x86'] |
| 574 | elif 'armx' in filename: |
| 575 | return ['arm', 'aarch64'] |
| 576 | elif 'armv8' in filename: |
| 577 | return ['aarch64'] |
| 578 | elif 'arm' in filename: |
| 579 | return ['arm'] |
| 580 | else: |
| 581 | raise ValueError('Unknown arch for asm filename: ' + filename) |
| 582 | |
| 583 | |
| 584 | def WriteAsmFiles(perlasms): |
| 585 | """Generates asm files from perlasm directives for each supported OS x |
| 586 | platform combination.""" |
| 587 | asmfiles = {} |
| 588 | |
| 589 | for osarch in OS_ARCH_COMBOS: |
| 590 | (osname, arch, perlasm_style, extra_args, asm_ext) = osarch |
| 591 | key = (osname, arch) |
| 592 | outDir = '%s-%s' % key |
| 593 | |
| 594 | for perlasm in perlasms: |
| 595 | filename = os.path.basename(perlasm['input']) |
| 596 | output = perlasm['output'] |
| 597 | if not output.startswith('src'): |
| 598 | raise ValueError('output missing src: %s' % output) |
| 599 | output = os.path.join(outDir, output[4:]) |
William Hesse | c618c40 | 2015-06-22 16:34:02 +0200 | [diff] [blame] | 600 | if output.endswith('-armx.${ASM_EXT}'): |
| 601 | output = output.replace('-armx', |
| 602 | '-armx64' if arch == 'aarch64' else '-armx32') |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 603 | output = output.replace('${ASM_EXT}', asm_ext) |
| 604 | |
| 605 | if arch in ArchForAsmFilename(filename): |
| 606 | PerlAsm(output, perlasm['input'], perlasm_style, |
| 607 | perlasm['extra_args'] + extra_args) |
| 608 | asmfiles.setdefault(key, []).append(output) |
| 609 | |
| 610 | for (key, non_perl_asm_files) in NON_PERL_FILES.iteritems(): |
| 611 | asmfiles.setdefault(key, []).extend(non_perl_asm_files) |
| 612 | |
| 613 | return asmfiles |
| 614 | |
| 615 | |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 616 | def main(platforms): |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 617 | crypto_c_files = FindCFiles(os.path.join('src', 'crypto'), NoTests) |
| 618 | ssl_c_files = FindCFiles(os.path.join('src', 'ssl'), NoTests) |
David Benjamin | 38d01c6 | 2016-04-21 18:47:57 -0400 | [diff] [blame] | 619 | tool_c_files = FindCFiles(os.path.join('src', 'tool'), NoTests) |
Adam Langley | f11f233 | 2016-06-30 11:56:19 -0700 | [diff] [blame] | 620 | tool_h_files = FindHeaderFiles(os.path.join('src', 'tool'), AllFiles) |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 621 | |
| 622 | # Generate err_data.c |
| 623 | with open('err_data.c', 'w+') as err_data: |
| 624 | subprocess.check_call(['go', 'run', 'err_data_generate.go'], |
| 625 | cwd=os.path.join('src', 'crypto', 'err'), |
| 626 | stdout=err_data) |
| 627 | crypto_c_files.append('err_data.c') |
| 628 | |
David Benjamin | 38d01c6 | 2016-04-21 18:47:57 -0400 | [diff] [blame] | 629 | test_support_c_files = FindCFiles(os.path.join('src', 'crypto', 'test'), |
| 630 | AllFiles) |
Matt Braithwaite | dfdd49c | 2016-06-13 17:06:48 -0700 | [diff] [blame] | 631 | test_support_h_files = ( |
| 632 | FindHeaderFiles(os.path.join('src', 'crypto', 'test'), AllFiles) + |
| 633 | FindHeaderFiles(os.path.join('src', 'ssl', 'test'), AllFiles)) |
David Benjamin | 2607383 | 2015-05-11 20:52:48 -0400 | [diff] [blame] | 634 | |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 635 | test_c_files = FindCFiles(os.path.join('src', 'crypto'), OnlyTests) |
| 636 | test_c_files += FindCFiles(os.path.join('src', 'ssl'), OnlyTests) |
| 637 | |
David Benjamin | 38d01c6 | 2016-04-21 18:47:57 -0400 | [diff] [blame] | 638 | fuzz_c_files = FindCFiles(os.path.join('src', 'fuzz'), NoTests) |
| 639 | |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 640 | ssl_h_files = ( |
| 641 | FindHeaderFiles( |
| 642 | os.path.join('src', 'include', 'openssl'), |
| 643 | SSLHeaderFiles)) |
| 644 | |
| 645 | def NotSSLHeaderFiles(filename, is_dir): |
| 646 | return not SSLHeaderFiles(filename, is_dir) |
| 647 | crypto_h_files = ( |
| 648 | FindHeaderFiles( |
| 649 | os.path.join('src', 'include', 'openssl'), |
| 650 | NotSSLHeaderFiles)) |
| 651 | |
| 652 | ssl_internal_h_files = FindHeaderFiles(os.path.join('src', 'ssl'), NoTests) |
| 653 | crypto_internal_h_files = FindHeaderFiles( |
| 654 | os.path.join('src', 'crypto'), NoTests) |
| 655 | |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 656 | with open('src/util/all_tests.json', 'r') as f: |
| 657 | tests = json.load(f) |
David Benjamin | f277add | 2016-03-09 14:38:24 -0500 | [diff] [blame] | 658 | # Skip tests for libdecrepit. Consumers import that manually. |
| 659 | tests = [test for test in tests if not test[0].startswith("decrepit/")] |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 660 | test_binaries = set([test[0] for test in tests]) |
| 661 | test_sources = set([ |
| 662 | test.replace('.cc', '').replace('.c', '').replace( |
| 663 | 'src/', |
| 664 | '') |
| 665 | for test in test_c_files]) |
| 666 | if test_binaries != test_sources: |
| 667 | print 'Test sources and configured tests do not match' |
| 668 | a = test_binaries.difference(test_sources) |
| 669 | if len(a) > 0: |
| 670 | print 'These tests are configured without sources: ' + str(a) |
| 671 | b = test_sources.difference(test_binaries) |
| 672 | if len(b) > 0: |
| 673 | print 'These test sources are not configured: ' + str(b) |
| 674 | |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 675 | files = { |
| 676 | 'crypto': crypto_c_files, |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 677 | 'crypto_headers': crypto_h_files, |
| 678 | 'crypto_internal_headers': crypto_internal_h_files, |
David Benjamin | 38d01c6 | 2016-04-21 18:47:57 -0400 | [diff] [blame] | 679 | 'fuzz': fuzz_c_files, |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 680 | 'ssl': ssl_c_files, |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 681 | 'ssl_headers': ssl_h_files, |
| 682 | 'ssl_internal_headers': ssl_internal_h_files, |
David Benjamin | 38d01c6 | 2016-04-21 18:47:57 -0400 | [diff] [blame] | 683 | 'tool': tool_c_files, |
Adam Langley | f11f233 | 2016-06-30 11:56:19 -0700 | [diff] [blame] | 684 | 'tool_headers': tool_h_files, |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 685 | 'test': test_c_files, |
Matt Braithwaite | dfdd49c | 2016-06-13 17:06:48 -0700 | [diff] [blame] | 686 | 'test_support': test_support_h_files + test_support_c_files, |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 687 | 'tests': tests, |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | asm_outputs = sorted(WriteAsmFiles(ReadPerlAsmOperations()).iteritems()) |
| 691 | |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 692 | for platform in platforms: |
| 693 | platform.WriteFiles(files, asm_outputs) |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 694 | |
| 695 | return 0 |
| 696 | |
| 697 | |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 698 | if __name__ == '__main__': |
Matt Braithwaite | 1669589 | 2016-06-09 09:34:11 -0700 | [diff] [blame] | 699 | parser = optparse.OptionParser(usage='Usage: %prog [--prefix=<path>]' |
| 700 | ' [android|android-standalone|bazel|gn|gyp]') |
| 701 | parser.add_option('--prefix', dest='prefix', |
| 702 | help='For Bazel, prepend argument to all source files') |
| 703 | options, args = parser.parse_args(sys.argv[1:]) |
| 704 | PREFIX = options.prefix |
| 705 | |
| 706 | if not args: |
| 707 | parser.print_help() |
| 708 | sys.exit(1) |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 709 | |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 710 | platforms = [] |
Matt Braithwaite | 1669589 | 2016-06-09 09:34:11 -0700 | [diff] [blame] | 711 | for s in args: |
David Benjamin | 38d01c6 | 2016-04-21 18:47:57 -0400 | [diff] [blame] | 712 | if s == 'android': |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 713 | platforms.append(Android()) |
| 714 | elif s == 'android-standalone': |
| 715 | platforms.append(AndroidStandalone()) |
| 716 | elif s == 'bazel': |
| 717 | platforms.append(Bazel()) |
David Benjamin | 38d01c6 | 2016-04-21 18:47:57 -0400 | [diff] [blame] | 718 | elif s == 'gn': |
| 719 | platforms.append(GN()) |
| 720 | elif s == 'gyp': |
| 721 | platforms.append(GYP()) |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 722 | else: |
Matt Braithwaite | 1669589 | 2016-06-09 09:34:11 -0700 | [diff] [blame] | 723 | parser.print_help() |
| 724 | sys.exit(1) |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 725 | |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 726 | sys.exit(main(platforms)) |