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 | |
| 77 | """ |
| 78 | |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 79 | def ExtraFiles(self): |
| 80 | return ['android_compat_hacks.c', 'android_compat_keywrap.c'] |
| 81 | |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 82 | def PrintVariableSection(self, out, name, files): |
| 83 | out.write('%s := \\\n' % name) |
| 84 | for f in sorted(files): |
| 85 | out.write(' %s\\\n' % f) |
| 86 | out.write('\n') |
| 87 | |
| 88 | def WriteFiles(self, files, asm_outputs): |
| 89 | with open('sources.mk', 'w+') as makefile: |
| 90 | makefile.write(self.header) |
| 91 | |
Piotr Sikora | 6ae67df | 2015-11-23 18:46:33 -0800 | [diff] [blame] | 92 | crypto_files = files['crypto'] + self.ExtraFiles() |
| 93 | self.PrintVariableSection(makefile, 'crypto_sources', crypto_files) |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 94 | self.PrintVariableSection(makefile, 'ssl_sources', files['ssl']) |
| 95 | self.PrintVariableSection(makefile, 'tool_sources', files['tool']) |
| 96 | |
| 97 | for ((osname, arch), asm_files) in asm_outputs: |
| 98 | self.PrintVariableSection( |
| 99 | makefile, '%s_%s_sources' % (osname, arch), asm_files) |
| 100 | |
| 101 | |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 102 | class AndroidStandalone(Android): |
| 103 | """AndroidStandalone is for Android builds outside of the Android-system, i.e. |
| 104 | |
| 105 | for applications that wish wish to ship BoringSSL. |
| 106 | """ |
| 107 | |
| 108 | def ExtraFiles(self): |
| 109 | return [] |
| 110 | |
| 111 | |
| 112 | class Bazel(object): |
| 113 | """Bazel outputs files suitable for including in Bazel files.""" |
| 114 | |
| 115 | def __init__(self): |
| 116 | self.firstSection = True |
| 117 | self.header = \ |
| 118 | """# This file is created by generate_build_files.py. Do not edit manually. |
| 119 | |
| 120 | """ |
| 121 | |
| 122 | def PrintVariableSection(self, out, name, files): |
| 123 | if not self.firstSection: |
| 124 | out.write('\n') |
| 125 | self.firstSection = False |
| 126 | |
| 127 | out.write('%s = [\n' % name) |
| 128 | for f in sorted(files): |
Matt Braithwaite | 1669589 | 2016-06-09 09:34:11 -0700 | [diff] [blame] | 129 | out.write(' "%s",\n' % PathOf(f)) |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 130 | out.write(']\n') |
| 131 | |
| 132 | def WriteFiles(self, files, asm_outputs): |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 133 | with open('BUILD.generated.bzl', 'w+') as out: |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 134 | out.write(self.header) |
| 135 | |
| 136 | self.PrintVariableSection(out, 'ssl_headers', files['ssl_headers']) |
| 137 | self.PrintVariableSection( |
| 138 | out, 'ssl_internal_headers', files['ssl_internal_headers']) |
| 139 | self.PrintVariableSection(out, 'ssl_sources', files['ssl']) |
| 140 | self.PrintVariableSection(out, 'crypto_headers', files['crypto_headers']) |
| 141 | self.PrintVariableSection( |
| 142 | out, 'crypto_internal_headers', files['crypto_internal_headers']) |
| 143 | self.PrintVariableSection(out, 'crypto_sources', files['crypto']) |
| 144 | self.PrintVariableSection(out, 'tool_sources', files['tool']) |
Adam Langley | f11f233 | 2016-06-30 11:56:19 -0700 | [diff] [blame^] | 145 | self.PrintVariableSection(out, 'tool_headers', files['tool_headers']) |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 146 | |
| 147 | for ((osname, arch), asm_files) in asm_outputs: |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 148 | self.PrintVariableSection( |
Piotr Sikora | 3f5fe60 | 2015-10-28 12:24:35 -0700 | [diff] [blame] | 149 | out, 'crypto_sources_%s_%s' % (osname, arch), asm_files) |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 150 | |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 151 | with open('BUILD.generated_tests.bzl', 'w+') as out: |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 152 | out.write(self.header) |
| 153 | |
| 154 | out.write('test_support_sources = [\n') |
Matt Braithwaite | dfdd49c | 2016-06-13 17:06:48 -0700 | [diff] [blame] | 155 | for filename in (files['test_support'] + |
| 156 | files['crypto_internal_headers'] + |
| 157 | files['ssl_internal_headers']): |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 158 | if os.path.basename(filename) == 'malloc.cc': |
| 159 | continue |
Matt Braithwaite | 1669589 | 2016-06-09 09:34:11 -0700 | [diff] [blame] | 160 | out.write(' "%s",\n' % PathOf(filename)) |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 161 | |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 162 | out.write(']\n\n') |
| 163 | |
Matt Braithwaite | dfdd49c | 2016-06-13 17:06:48 -0700 | [diff] [blame] | 164 | out.write('def create_tests(copts, crypto, ssl):\n') |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 165 | name_counts = {} |
| 166 | for test in files['tests']: |
| 167 | name = os.path.basename(test[0]) |
| 168 | name_counts[name] = name_counts.get(name, 0) + 1 |
| 169 | |
| 170 | first = True |
| 171 | for test in files['tests']: |
| 172 | name = os.path.basename(test[0]) |
| 173 | if name_counts[name] > 1: |
| 174 | if '/' in test[1]: |
| 175 | name += '_' + os.path.splitext(os.path.basename(test[1]))[0] |
| 176 | else: |
| 177 | name += '_' + test[1].replace('-', '_') |
| 178 | |
| 179 | if not first: |
| 180 | out.write('\n') |
| 181 | first = False |
| 182 | |
| 183 | src_prefix = 'src/' + test[0] |
| 184 | for src in files['test']: |
| 185 | if src.startswith(src_prefix): |
| 186 | src = src |
| 187 | break |
| 188 | else: |
| 189 | raise ValueError("Can't find source for %s" % test[0]) |
| 190 | |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 191 | out.write(' native.cc_test(\n') |
| 192 | out.write(' name = "%s",\n' % name) |
| 193 | out.write(' size = "small",\n') |
Matt Braithwaite | dfdd49c | 2016-06-13 17:06:48 -0700 | [diff] [blame] | 194 | out.write(' srcs = ["%s"] + test_support_sources,\n' % |
Matt Braithwaite | 1669589 | 2016-06-09 09:34:11 -0700 | [diff] [blame] | 195 | PathOf(src)) |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 196 | |
| 197 | data_files = [] |
| 198 | if len(test) > 1: |
| 199 | |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 200 | out.write(' args = [\n') |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 201 | for arg in test[1:]: |
| 202 | if '/' in arg: |
Matt Braithwaite | 1669589 | 2016-06-09 09:34:11 -0700 | [diff] [blame] | 203 | out.write(' "$(location %s)",\n' % |
| 204 | PathOf(os.path.join('src', arg))) |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 205 | data_files.append('src/%s' % arg) |
| 206 | else: |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 207 | out.write(' "%s",\n' % arg) |
| 208 | out.write(' ],\n') |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 209 | |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 210 | out.write(' copts = copts,\n') |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 211 | |
| 212 | if len(data_files) > 0: |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 213 | out.write(' data = [\n') |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 214 | for filename in data_files: |
Matt Braithwaite | 1669589 | 2016-06-09 09:34:11 -0700 | [diff] [blame] | 215 | out.write(' "%s",\n' % PathOf(filename)) |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 216 | out.write(' ],\n') |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 217 | |
| 218 | if 'ssl/' in test[0]: |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 219 | out.write(' deps = [\n') |
Matt Braithwaite | dfdd49c | 2016-06-13 17:06:48 -0700 | [diff] [blame] | 220 | out.write(' crypto,\n') |
| 221 | out.write(' ssl,\n') |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 222 | out.write(' ],\n') |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 223 | else: |
Matt Braithwaite | dfdd49c | 2016-06-13 17:06:48 -0700 | [diff] [blame] | 224 | out.write(' deps = [crypto],\n') |
Chuck Hays | c608d6b | 2015-10-06 17:54:16 -0700 | [diff] [blame] | 225 | out.write(' )\n') |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 226 | |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 227 | |
David Benjamin | 38d01c6 | 2016-04-21 18:47:57 -0400 | [diff] [blame] | 228 | class GN(object): |
| 229 | |
| 230 | def __init__(self): |
| 231 | self.firstSection = True |
| 232 | self.header = \ |
| 233 | """# Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 234 | # Use of this source code is governed by a BSD-style license that can be |
| 235 | # found in the LICENSE file. |
| 236 | |
| 237 | # This file is created by generate_build_files.py. Do not edit manually. |
| 238 | |
| 239 | """ |
| 240 | |
| 241 | def PrintVariableSection(self, out, name, files): |
| 242 | if not self.firstSection: |
| 243 | out.write('\n') |
| 244 | self.firstSection = False |
| 245 | |
| 246 | out.write('%s = [\n' % name) |
| 247 | for f in sorted(files): |
| 248 | out.write(' "%s",\n' % f) |
| 249 | out.write(']\n') |
| 250 | |
| 251 | def WriteFiles(self, files, asm_outputs): |
| 252 | with open('BUILD.generated.gni', 'w+') as out: |
| 253 | out.write(self.header) |
| 254 | |
| 255 | self.PrintVariableSection(out, 'crypto_sources', files['crypto']) |
| 256 | self.PrintVariableSection(out, 'ssl_sources', files['ssl']) |
| 257 | |
| 258 | for ((osname, arch), asm_files) in asm_outputs: |
| 259 | self.PrintVariableSection( |
| 260 | out, 'crypto_sources_%s_%s' % (osname, arch), asm_files) |
| 261 | |
| 262 | fuzzers = [os.path.splitext(os.path.basename(fuzzer))[0] |
| 263 | for fuzzer in files['fuzz']] |
| 264 | self.PrintVariableSection(out, 'fuzzers', fuzzers) |
| 265 | |
| 266 | with open('BUILD.generated_tests.gni', 'w+') as out: |
| 267 | self.firstSection = True |
| 268 | out.write(self.header) |
| 269 | |
| 270 | self.PrintVariableSection(out, '_test_support_sources', |
| 271 | files['test_support']) |
| 272 | out.write('\n') |
| 273 | |
| 274 | out.write('template("create_tests") {\n') |
| 275 | |
| 276 | all_tests = [] |
| 277 | for test in sorted(files['test']): |
| 278 | test_name = 'boringssl_%s' % os.path.splitext(os.path.basename(test))[0] |
| 279 | all_tests.append(test_name) |
| 280 | |
| 281 | out.write(' executable("%s") {\n' % test_name) |
| 282 | out.write(' sources = [\n') |
| 283 | out.write(' "%s",\n' % test) |
| 284 | out.write(' ]\n') |
| 285 | out.write(' sources += _test_support_sources\n') |
David Benjamin | b3be1cf | 2016-04-27 19:15:06 -0400 | [diff] [blame] | 286 | out.write(' if (defined(invoker.configs_exclude)) {\n') |
| 287 | out.write(' configs -= invoker.configs_exclude\n') |
| 288 | out.write(' }\n') |
David Benjamin | 38d01c6 | 2016-04-21 18:47:57 -0400 | [diff] [blame] | 289 | out.write(' configs += invoker.configs\n') |
| 290 | out.write(' deps = invoker.deps\n') |
| 291 | out.write(' }\n') |
| 292 | out.write('\n') |
| 293 | |
| 294 | out.write(' group(target_name) {\n') |
| 295 | out.write(' deps = [\n') |
| 296 | for test_name in sorted(all_tests): |
| 297 | out.write(' ":%s",\n' % test_name) |
| 298 | out.write(' ]\n') |
| 299 | out.write(' }\n') |
| 300 | out.write('}\n') |
| 301 | |
| 302 | |
| 303 | class GYP(object): |
| 304 | |
| 305 | def __init__(self): |
| 306 | self.header = \ |
| 307 | """# Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 308 | # Use of this source code is governed by a BSD-style license that can be |
| 309 | # found in the LICENSE file. |
| 310 | |
| 311 | # This file is created by generate_build_files.py. Do not edit manually. |
| 312 | |
| 313 | """ |
| 314 | |
| 315 | def PrintVariableSection(self, out, name, files): |
| 316 | out.write(' \'%s\': [\n' % name) |
| 317 | for f in sorted(files): |
| 318 | out.write(' \'%s\',\n' % f) |
| 319 | out.write(' ],\n') |
| 320 | |
| 321 | def WriteFiles(self, files, asm_outputs): |
| 322 | with open('boringssl.gypi', 'w+') as gypi: |
| 323 | gypi.write(self.header + '{\n \'variables\': {\n') |
| 324 | |
| 325 | self.PrintVariableSection( |
| 326 | gypi, 'boringssl_ssl_sources', files['ssl']) |
| 327 | self.PrintVariableSection( |
| 328 | gypi, 'boringssl_crypto_sources', files['crypto']) |
| 329 | |
| 330 | for ((osname, arch), asm_files) in asm_outputs: |
| 331 | self.PrintVariableSection(gypi, 'boringssl_%s_%s_sources' % |
| 332 | (osname, arch), asm_files) |
| 333 | |
| 334 | gypi.write(' }\n}\n') |
| 335 | |
| 336 | with open('boringssl_tests.gypi', 'w+') as test_gypi: |
| 337 | test_gypi.write(self.header + '{\n \'targets\': [\n') |
| 338 | |
| 339 | test_names = [] |
| 340 | for test in sorted(files['test']): |
| 341 | test_name = 'boringssl_%s' % os.path.splitext(os.path.basename(test))[0] |
| 342 | test_gypi.write(""" { |
| 343 | 'target_name': '%s', |
| 344 | 'type': 'executable', |
| 345 | 'dependencies': [ |
| 346 | 'boringssl.gyp:boringssl', |
| 347 | ], |
| 348 | 'sources': [ |
| 349 | '%s', |
| 350 | '<@(boringssl_test_support_sources)', |
| 351 | ], |
| 352 | # TODO(davidben): Fix size_t truncations in BoringSSL. |
| 353 | # https://crbug.com/429039 |
| 354 | 'msvs_disabled_warnings': [ 4267, ], |
| 355 | },\n""" % (test_name, test)) |
| 356 | test_names.append(test_name) |
| 357 | |
| 358 | test_names.sort() |
| 359 | |
| 360 | test_gypi.write(' ],\n \'variables\': {\n') |
| 361 | |
| 362 | self.PrintVariableSection( |
| 363 | test_gypi, 'boringssl_test_support_sources', files['test_support']) |
| 364 | |
| 365 | test_gypi.write(' \'boringssl_test_targets\': [\n') |
| 366 | |
| 367 | for test in sorted(test_names): |
| 368 | test_gypi.write(""" '%s',\n""" % test) |
| 369 | |
| 370 | test_gypi.write(' ],\n }\n}\n') |
| 371 | |
| 372 | |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 373 | def FindCMakeFiles(directory): |
| 374 | """Returns list of all CMakeLists.txt files recursively in directory.""" |
| 375 | cmakefiles = [] |
| 376 | |
| 377 | for (path, _, filenames) in os.walk(directory): |
| 378 | for filename in filenames: |
| 379 | if filename == 'CMakeLists.txt': |
| 380 | cmakefiles.append(os.path.join(path, filename)) |
| 381 | |
| 382 | return cmakefiles |
| 383 | |
| 384 | |
| 385 | def NoTests(dent, is_dir): |
| 386 | """Filter function that can be passed to FindCFiles in order to remove test |
| 387 | sources.""" |
| 388 | if is_dir: |
| 389 | return dent != 'test' |
| 390 | return 'test.' not in dent and not dent.startswith('example_') |
| 391 | |
| 392 | |
| 393 | def OnlyTests(dent, is_dir): |
| 394 | """Filter function that can be passed to FindCFiles in order to remove |
| 395 | non-test sources.""" |
| 396 | if is_dir: |
David Benjamin | 2607383 | 2015-05-11 20:52:48 -0400 | [diff] [blame] | 397 | return dent != 'test' |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 398 | return '_test.' in dent or dent.startswith('example_') |
| 399 | |
| 400 | |
David Benjamin | 2607383 | 2015-05-11 20:52:48 -0400 | [diff] [blame] | 401 | def AllFiles(dent, is_dir): |
| 402 | """Filter function that can be passed to FindCFiles in order to include all |
| 403 | sources.""" |
| 404 | return True |
| 405 | |
| 406 | |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 407 | def SSLHeaderFiles(dent, is_dir): |
| 408 | return dent in ['ssl.h', 'tls1.h', 'ssl23.h', 'ssl3.h', 'dtls1.h'] |
| 409 | |
| 410 | |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 411 | def FindCFiles(directory, filter_func): |
| 412 | """Recurses through directory and returns a list of paths to all the C source |
| 413 | files that pass filter_func.""" |
| 414 | cfiles = [] |
| 415 | |
| 416 | for (path, dirnames, filenames) in os.walk(directory): |
| 417 | for filename in filenames: |
| 418 | if not filename.endswith('.c') and not filename.endswith('.cc'): |
| 419 | continue |
| 420 | if not filter_func(filename, False): |
| 421 | continue |
| 422 | cfiles.append(os.path.join(path, filename)) |
| 423 | |
| 424 | for (i, dirname) in enumerate(dirnames): |
| 425 | if not filter_func(dirname, True): |
| 426 | del dirnames[i] |
| 427 | |
| 428 | return cfiles |
| 429 | |
| 430 | |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 431 | def FindHeaderFiles(directory, filter_func): |
| 432 | """Recurses through directory and returns a list of paths to all the header files that pass filter_func.""" |
| 433 | hfiles = [] |
| 434 | |
| 435 | for (path, dirnames, filenames) in os.walk(directory): |
| 436 | for filename in filenames: |
| 437 | if not filename.endswith('.h'): |
| 438 | continue |
| 439 | if not filter_func(filename, False): |
| 440 | continue |
| 441 | hfiles.append(os.path.join(path, filename)) |
| 442 | |
Matt Braithwaite | dfdd49c | 2016-06-13 17:06:48 -0700 | [diff] [blame] | 443 | for (i, dirname) in enumerate(dirnames): |
| 444 | if not filter_func(dirname, True): |
| 445 | del dirnames[i] |
| 446 | |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 447 | return hfiles |
| 448 | |
| 449 | |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 450 | def ExtractPerlAsmFromCMakeFile(cmakefile): |
| 451 | """Parses the contents of the CMakeLists.txt file passed as an argument and |
| 452 | returns a list of all the perlasm() directives found in the file.""" |
| 453 | perlasms = [] |
| 454 | with open(cmakefile) as f: |
| 455 | for line in f: |
| 456 | line = line.strip() |
| 457 | if not line.startswith('perlasm('): |
| 458 | continue |
| 459 | if not line.endswith(')'): |
| 460 | raise ValueError('Bad perlasm line in %s' % cmakefile) |
| 461 | # Remove "perlasm(" from start and ")" from end |
| 462 | params = line[8:-1].split() |
| 463 | if len(params) < 2: |
| 464 | raise ValueError('Bad perlasm line in %s' % cmakefile) |
| 465 | perlasms.append({ |
| 466 | 'extra_args': params[2:], |
| 467 | 'input': os.path.join(os.path.dirname(cmakefile), params[1]), |
| 468 | 'output': os.path.join(os.path.dirname(cmakefile), params[0]), |
| 469 | }) |
| 470 | |
| 471 | return perlasms |
| 472 | |
| 473 | |
| 474 | def ReadPerlAsmOperations(): |
| 475 | """Returns a list of all perlasm() directives found in CMake config files in |
| 476 | src/.""" |
| 477 | perlasms = [] |
| 478 | cmakefiles = FindCMakeFiles('src') |
| 479 | |
| 480 | for cmakefile in cmakefiles: |
| 481 | perlasms.extend(ExtractPerlAsmFromCMakeFile(cmakefile)) |
| 482 | |
| 483 | return perlasms |
| 484 | |
| 485 | |
| 486 | def PerlAsm(output_filename, input_filename, perlasm_style, extra_args): |
| 487 | """Runs the a perlasm script and puts the output into output_filename.""" |
| 488 | base_dir = os.path.dirname(output_filename) |
| 489 | if not os.path.isdir(base_dir): |
| 490 | os.makedirs(base_dir) |
David Benjamin | fdd8e9c | 2016-06-26 13:18:50 -0400 | [diff] [blame] | 491 | subprocess.check_call( |
| 492 | ['perl', input_filename, perlasm_style] + extra_args + [output_filename]) |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 493 | |
| 494 | |
| 495 | def ArchForAsmFilename(filename): |
| 496 | """Returns the architectures that a given asm file should be compiled for |
| 497 | based on substrings in the filename.""" |
| 498 | |
| 499 | if 'x86_64' in filename or 'avx2' in filename: |
| 500 | return ['x86_64'] |
| 501 | elif ('x86' in filename and 'x86_64' not in filename) or '586' in filename: |
| 502 | return ['x86'] |
| 503 | elif 'armx' in filename: |
| 504 | return ['arm', 'aarch64'] |
| 505 | elif 'armv8' in filename: |
| 506 | return ['aarch64'] |
| 507 | elif 'arm' in filename: |
| 508 | return ['arm'] |
| 509 | else: |
| 510 | raise ValueError('Unknown arch for asm filename: ' + filename) |
| 511 | |
| 512 | |
| 513 | def WriteAsmFiles(perlasms): |
| 514 | """Generates asm files from perlasm directives for each supported OS x |
| 515 | platform combination.""" |
| 516 | asmfiles = {} |
| 517 | |
| 518 | for osarch in OS_ARCH_COMBOS: |
| 519 | (osname, arch, perlasm_style, extra_args, asm_ext) = osarch |
| 520 | key = (osname, arch) |
| 521 | outDir = '%s-%s' % key |
| 522 | |
| 523 | for perlasm in perlasms: |
| 524 | filename = os.path.basename(perlasm['input']) |
| 525 | output = perlasm['output'] |
| 526 | if not output.startswith('src'): |
| 527 | raise ValueError('output missing src: %s' % output) |
| 528 | output = os.path.join(outDir, output[4:]) |
William Hesse | c618c40 | 2015-06-22 16:34:02 +0200 | [diff] [blame] | 529 | if output.endswith('-armx.${ASM_EXT}'): |
| 530 | output = output.replace('-armx', |
| 531 | '-armx64' if arch == 'aarch64' else '-armx32') |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 532 | output = output.replace('${ASM_EXT}', asm_ext) |
| 533 | |
| 534 | if arch in ArchForAsmFilename(filename): |
| 535 | PerlAsm(output, perlasm['input'], perlasm_style, |
| 536 | perlasm['extra_args'] + extra_args) |
| 537 | asmfiles.setdefault(key, []).append(output) |
| 538 | |
| 539 | for (key, non_perl_asm_files) in NON_PERL_FILES.iteritems(): |
| 540 | asmfiles.setdefault(key, []).extend(non_perl_asm_files) |
| 541 | |
| 542 | return asmfiles |
| 543 | |
| 544 | |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 545 | def main(platforms): |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 546 | crypto_c_files = FindCFiles(os.path.join('src', 'crypto'), NoTests) |
| 547 | ssl_c_files = FindCFiles(os.path.join('src', 'ssl'), NoTests) |
David Benjamin | 38d01c6 | 2016-04-21 18:47:57 -0400 | [diff] [blame] | 548 | tool_c_files = FindCFiles(os.path.join('src', 'tool'), NoTests) |
Adam Langley | f11f233 | 2016-06-30 11:56:19 -0700 | [diff] [blame^] | 549 | tool_h_files = FindHeaderFiles(os.path.join('src', 'tool'), AllFiles) |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 550 | |
| 551 | # Generate err_data.c |
| 552 | with open('err_data.c', 'w+') as err_data: |
| 553 | subprocess.check_call(['go', 'run', 'err_data_generate.go'], |
| 554 | cwd=os.path.join('src', 'crypto', 'err'), |
| 555 | stdout=err_data) |
| 556 | crypto_c_files.append('err_data.c') |
| 557 | |
David Benjamin | 38d01c6 | 2016-04-21 18:47:57 -0400 | [diff] [blame] | 558 | test_support_c_files = FindCFiles(os.path.join('src', 'crypto', 'test'), |
| 559 | AllFiles) |
Matt Braithwaite | dfdd49c | 2016-06-13 17:06:48 -0700 | [diff] [blame] | 560 | test_support_h_files = ( |
| 561 | FindHeaderFiles(os.path.join('src', 'crypto', 'test'), AllFiles) + |
| 562 | FindHeaderFiles(os.path.join('src', 'ssl', 'test'), AllFiles)) |
David Benjamin | 2607383 | 2015-05-11 20:52:48 -0400 | [diff] [blame] | 563 | |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 564 | test_c_files = FindCFiles(os.path.join('src', 'crypto'), OnlyTests) |
| 565 | test_c_files += FindCFiles(os.path.join('src', 'ssl'), OnlyTests) |
| 566 | |
David Benjamin | 38d01c6 | 2016-04-21 18:47:57 -0400 | [diff] [blame] | 567 | fuzz_c_files = FindCFiles(os.path.join('src', 'fuzz'), NoTests) |
| 568 | |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 569 | ssl_h_files = ( |
| 570 | FindHeaderFiles( |
| 571 | os.path.join('src', 'include', 'openssl'), |
| 572 | SSLHeaderFiles)) |
| 573 | |
| 574 | def NotSSLHeaderFiles(filename, is_dir): |
| 575 | return not SSLHeaderFiles(filename, is_dir) |
| 576 | crypto_h_files = ( |
| 577 | FindHeaderFiles( |
| 578 | os.path.join('src', 'include', 'openssl'), |
| 579 | NotSSLHeaderFiles)) |
| 580 | |
| 581 | ssl_internal_h_files = FindHeaderFiles(os.path.join('src', 'ssl'), NoTests) |
| 582 | crypto_internal_h_files = FindHeaderFiles( |
| 583 | os.path.join('src', 'crypto'), NoTests) |
| 584 | |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 585 | with open('src/util/all_tests.json', 'r') as f: |
| 586 | tests = json.load(f) |
David Benjamin | f277add | 2016-03-09 14:38:24 -0500 | [diff] [blame] | 587 | # Skip tests for libdecrepit. Consumers import that manually. |
| 588 | tests = [test for test in tests if not test[0].startswith("decrepit/")] |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 589 | test_binaries = set([test[0] for test in tests]) |
| 590 | test_sources = set([ |
| 591 | test.replace('.cc', '').replace('.c', '').replace( |
| 592 | 'src/', |
| 593 | '') |
| 594 | for test in test_c_files]) |
| 595 | if test_binaries != test_sources: |
| 596 | print 'Test sources and configured tests do not match' |
| 597 | a = test_binaries.difference(test_sources) |
| 598 | if len(a) > 0: |
| 599 | print 'These tests are configured without sources: ' + str(a) |
| 600 | b = test_sources.difference(test_binaries) |
| 601 | if len(b) > 0: |
| 602 | print 'These test sources are not configured: ' + str(b) |
| 603 | |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 604 | files = { |
| 605 | 'crypto': crypto_c_files, |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 606 | 'crypto_headers': crypto_h_files, |
| 607 | 'crypto_internal_headers': crypto_internal_h_files, |
David Benjamin | 38d01c6 | 2016-04-21 18:47:57 -0400 | [diff] [blame] | 608 | 'fuzz': fuzz_c_files, |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 609 | 'ssl': ssl_c_files, |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 610 | 'ssl_headers': ssl_h_files, |
| 611 | 'ssl_internal_headers': ssl_internal_h_files, |
David Benjamin | 38d01c6 | 2016-04-21 18:47:57 -0400 | [diff] [blame] | 612 | 'tool': tool_c_files, |
Adam Langley | f11f233 | 2016-06-30 11:56:19 -0700 | [diff] [blame^] | 613 | 'tool_headers': tool_h_files, |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 614 | 'test': test_c_files, |
Matt Braithwaite | dfdd49c | 2016-06-13 17:06:48 -0700 | [diff] [blame] | 615 | 'test_support': test_support_h_files + test_support_c_files, |
Adam Langley | 9c164b2 | 2015-06-10 18:54:47 -0700 | [diff] [blame] | 616 | 'tests': tests, |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | asm_outputs = sorted(WriteAsmFiles(ReadPerlAsmOperations()).iteritems()) |
| 620 | |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 621 | for platform in platforms: |
| 622 | platform.WriteFiles(files, asm_outputs) |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 623 | |
| 624 | return 0 |
| 625 | |
| 626 | |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 627 | if __name__ == '__main__': |
Matt Braithwaite | 1669589 | 2016-06-09 09:34:11 -0700 | [diff] [blame] | 628 | parser = optparse.OptionParser(usage='Usage: %prog [--prefix=<path>]' |
| 629 | ' [android|android-standalone|bazel|gn|gyp]') |
| 630 | parser.add_option('--prefix', dest='prefix', |
| 631 | help='For Bazel, prepend argument to all source files') |
| 632 | options, args = parser.parse_args(sys.argv[1:]) |
| 633 | PREFIX = options.prefix |
| 634 | |
| 635 | if not args: |
| 636 | parser.print_help() |
| 637 | sys.exit(1) |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 638 | |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 639 | platforms = [] |
Matt Braithwaite | 1669589 | 2016-06-09 09:34:11 -0700 | [diff] [blame] | 640 | for s in args: |
David Benjamin | 38d01c6 | 2016-04-21 18:47:57 -0400 | [diff] [blame] | 641 | if s == 'android': |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 642 | platforms.append(Android()) |
| 643 | elif s == 'android-standalone': |
| 644 | platforms.append(AndroidStandalone()) |
| 645 | elif s == 'bazel': |
| 646 | platforms.append(Bazel()) |
David Benjamin | 38d01c6 | 2016-04-21 18:47:57 -0400 | [diff] [blame] | 647 | elif s == 'gn': |
| 648 | platforms.append(GN()) |
| 649 | elif s == 'gyp': |
| 650 | platforms.append(GYP()) |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 651 | else: |
Matt Braithwaite | 1669589 | 2016-06-09 09:34:11 -0700 | [diff] [blame] | 652 | parser.print_help() |
| 653 | sys.exit(1) |
Adam Langley | 9e1a660 | 2015-05-05 17:47:53 -0700 | [diff] [blame] | 654 | |
Adam Langley | 049ef41 | 2015-06-09 18:20:57 -0700 | [diff] [blame] | 655 | sys.exit(main(platforms)) |