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 | |
| 15 | """Enumerates the BoringSSL source in src/ and either generates two gypi files |
| 16 | (boringssl.gypi and boringssl_tests.gypi) for Chromium, or generates |
| 17 | source-list files for Android.""" |
| 18 | |
| 19 | import os |
| 20 | import subprocess |
| 21 | import sys |
| 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'): [ |
| 41 | 'src/crypto/poly1305/poly1305_arm_asm.S', |
| 42 | 'src/crypto/chacha/chacha_vec_arm.S', |
| 43 | 'src/crypto/cpu-arm-asm.S', |
| 44 | ], |
| 45 | } |
| 46 | |
| 47 | |
| 48 | class Chromium(object): |
| 49 | |
| 50 | def __init__(self): |
| 51 | self.header = \ |
| 52 | """# Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 53 | # Use of this source code is governed by a BSD-style license that can be |
| 54 | # found in the LICENSE file. |
| 55 | |
| 56 | # This file is created by generate_build_files.py. Do not edit manually. |
| 57 | |
| 58 | """ |
| 59 | |
| 60 | def PrintVariableSection(self, out, name, files): |
| 61 | out.write(' \'%s\': [\n' % name) |
| 62 | for f in sorted(files): |
| 63 | out.write(' \'%s\',\n' % f) |
| 64 | out.write(' ],\n') |
| 65 | |
| 66 | def WriteFiles(self, files, asm_outputs): |
| 67 | with open('boringssl.gypi', 'w+') as gypi: |
| 68 | gypi.write(self.header + '{\n \'variables\': {\n') |
| 69 | |
| 70 | self.PrintVariableSection( |
| 71 | gypi, 'boringssl_lib_sources', files['crypto'] + files['ssl']) |
| 72 | |
| 73 | for ((osname, arch), asm_files) in asm_outputs: |
| 74 | self.PrintVariableSection(gypi, 'boringssl_%s_%s_sources' % |
| 75 | (osname, arch), asm_files) |
| 76 | |
| 77 | gypi.write(' }\n}\n') |
| 78 | |
| 79 | with open('boringssl_tests.gypi', 'w+') as test_gypi: |
| 80 | test_gypi.write(self.header + '{\n \'targets\': [\n') |
| 81 | |
| 82 | test_names = [] |
| 83 | for test in sorted(files['test']): |
| 84 | test_name = 'boringssl_%s' % os.path.splitext(os.path.basename(test))[0] |
| 85 | test_gypi.write(""" { |
| 86 | 'target_name': '%s', |
| 87 | 'type': 'executable', |
| 88 | 'dependencies': [ |
| 89 | 'boringssl.gyp:boringssl', |
| 90 | ], |
| 91 | 'sources': [ |
| 92 | '%s', |
| 93 | ], |
| 94 | # TODO(davidben): Fix size_t truncations in BoringSSL. |
| 95 | # https://crbug.com/429039 |
| 96 | 'msvs_disabled_warnings': [ 4267, ], |
| 97 | },\n""" % (test_name, test)) |
| 98 | test_names.append(test_name) |
| 99 | |
| 100 | test_names.sort() |
| 101 | |
| 102 | test_gypi.write(""" ], |
| 103 | 'variables': { |
| 104 | 'boringssl_test_targets': [\n""") |
| 105 | |
| 106 | for test in test_names: |
| 107 | test_gypi.write(""" '%s',\n""" % test) |
| 108 | |
| 109 | test_gypi.write(' ],\n }\n}\n') |
| 110 | |
| 111 | |
| 112 | class Android(object): |
| 113 | |
| 114 | def __init__(self): |
| 115 | self.header = \ |
| 116 | """# Copyright (C) 2015 The Android Open Source Project |
| 117 | # |
| 118 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 119 | # you may not use this file except in compliance with the License. |
| 120 | # You may obtain a copy of the License at |
| 121 | # |
| 122 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 123 | # |
| 124 | # Unless required by applicable law or agreed to in writing, software |
| 125 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 126 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 127 | # See the License for the specific language governing permissions and |
| 128 | # limitations under the License. |
| 129 | |
| 130 | """ |
| 131 | |
| 132 | def PrintVariableSection(self, out, name, files): |
| 133 | out.write('%s := \\\n' % name) |
| 134 | for f in sorted(files): |
| 135 | out.write(' %s\\\n' % f) |
| 136 | out.write('\n') |
| 137 | |
| 138 | def WriteFiles(self, files, asm_outputs): |
| 139 | with open('sources.mk', 'w+') as makefile: |
| 140 | makefile.write(self.header) |
| 141 | |
| 142 | files['crypto'].append('android_compat_hacks.c') |
| 143 | files['crypto'].append('android_compat_keywrap.c') |
| 144 | self.PrintVariableSection(makefile, 'crypto_sources', files['crypto']) |
| 145 | self.PrintVariableSection(makefile, 'ssl_sources', files['ssl']) |
| 146 | self.PrintVariableSection(makefile, 'tool_sources', files['tool']) |
| 147 | |
| 148 | for ((osname, arch), asm_files) in asm_outputs: |
| 149 | self.PrintVariableSection( |
| 150 | makefile, '%s_%s_sources' % (osname, arch), asm_files) |
| 151 | |
| 152 | |
| 153 | def FindCMakeFiles(directory): |
| 154 | """Returns list of all CMakeLists.txt files recursively in directory.""" |
| 155 | cmakefiles = [] |
| 156 | |
| 157 | for (path, _, filenames) in os.walk(directory): |
| 158 | for filename in filenames: |
| 159 | if filename == 'CMakeLists.txt': |
| 160 | cmakefiles.append(os.path.join(path, filename)) |
| 161 | |
| 162 | return cmakefiles |
| 163 | |
| 164 | |
| 165 | def NoTests(dent, is_dir): |
| 166 | """Filter function that can be passed to FindCFiles in order to remove test |
| 167 | sources.""" |
| 168 | if is_dir: |
| 169 | return dent != 'test' |
| 170 | return 'test.' not in dent and not dent.startswith('example_') |
| 171 | |
| 172 | |
| 173 | def OnlyTests(dent, is_dir): |
| 174 | """Filter function that can be passed to FindCFiles in order to remove |
| 175 | non-test sources.""" |
| 176 | if is_dir: |
| 177 | return True |
| 178 | return '_test.' in dent or dent.startswith('example_') |
| 179 | |
| 180 | |
| 181 | def FindCFiles(directory, filter_func): |
| 182 | """Recurses through directory and returns a list of paths to all the C source |
| 183 | files that pass filter_func.""" |
| 184 | cfiles = [] |
| 185 | |
| 186 | for (path, dirnames, filenames) in os.walk(directory): |
| 187 | for filename in filenames: |
| 188 | if not filename.endswith('.c') and not filename.endswith('.cc'): |
| 189 | continue |
| 190 | if not filter_func(filename, False): |
| 191 | continue |
| 192 | cfiles.append(os.path.join(path, filename)) |
| 193 | |
| 194 | for (i, dirname) in enumerate(dirnames): |
| 195 | if not filter_func(dirname, True): |
| 196 | del dirnames[i] |
| 197 | |
| 198 | return cfiles |
| 199 | |
| 200 | |
| 201 | def ExtractPerlAsmFromCMakeFile(cmakefile): |
| 202 | """Parses the contents of the CMakeLists.txt file passed as an argument and |
| 203 | returns a list of all the perlasm() directives found in the file.""" |
| 204 | perlasms = [] |
| 205 | with open(cmakefile) as f: |
| 206 | for line in f: |
| 207 | line = line.strip() |
| 208 | if not line.startswith('perlasm('): |
| 209 | continue |
| 210 | if not line.endswith(')'): |
| 211 | raise ValueError('Bad perlasm line in %s' % cmakefile) |
| 212 | # Remove "perlasm(" from start and ")" from end |
| 213 | params = line[8:-1].split() |
| 214 | if len(params) < 2: |
| 215 | raise ValueError('Bad perlasm line in %s' % cmakefile) |
| 216 | perlasms.append({ |
| 217 | 'extra_args': params[2:], |
| 218 | 'input': os.path.join(os.path.dirname(cmakefile), params[1]), |
| 219 | 'output': os.path.join(os.path.dirname(cmakefile), params[0]), |
| 220 | }) |
| 221 | |
| 222 | return perlasms |
| 223 | |
| 224 | |
| 225 | def ReadPerlAsmOperations(): |
| 226 | """Returns a list of all perlasm() directives found in CMake config files in |
| 227 | src/.""" |
| 228 | perlasms = [] |
| 229 | cmakefiles = FindCMakeFiles('src') |
| 230 | |
| 231 | for cmakefile in cmakefiles: |
| 232 | perlasms.extend(ExtractPerlAsmFromCMakeFile(cmakefile)) |
| 233 | |
| 234 | return perlasms |
| 235 | |
| 236 | |
| 237 | def PerlAsm(output_filename, input_filename, perlasm_style, extra_args): |
| 238 | """Runs the a perlasm script and puts the output into output_filename.""" |
| 239 | base_dir = os.path.dirname(output_filename) |
| 240 | if not os.path.isdir(base_dir): |
| 241 | os.makedirs(base_dir) |
| 242 | output = subprocess.check_output( |
| 243 | ['perl', input_filename, perlasm_style] + extra_args) |
| 244 | with open(output_filename, 'w+') as out_file: |
| 245 | out_file.write(output) |
| 246 | |
| 247 | |
| 248 | def ArchForAsmFilename(filename): |
| 249 | """Returns the architectures that a given asm file should be compiled for |
| 250 | based on substrings in the filename.""" |
| 251 | |
| 252 | if 'x86_64' in filename or 'avx2' in filename: |
| 253 | return ['x86_64'] |
| 254 | elif ('x86' in filename and 'x86_64' not in filename) or '586' in filename: |
| 255 | return ['x86'] |
| 256 | elif 'armx' in filename: |
| 257 | return ['arm', 'aarch64'] |
| 258 | elif 'armv8' in filename: |
| 259 | return ['aarch64'] |
| 260 | elif 'arm' in filename: |
| 261 | return ['arm'] |
| 262 | else: |
| 263 | raise ValueError('Unknown arch for asm filename: ' + filename) |
| 264 | |
| 265 | |
| 266 | def WriteAsmFiles(perlasms): |
| 267 | """Generates asm files from perlasm directives for each supported OS x |
| 268 | platform combination.""" |
| 269 | asmfiles = {} |
| 270 | |
| 271 | for osarch in OS_ARCH_COMBOS: |
| 272 | (osname, arch, perlasm_style, extra_args, asm_ext) = osarch |
| 273 | key = (osname, arch) |
| 274 | outDir = '%s-%s' % key |
| 275 | |
| 276 | for perlasm in perlasms: |
| 277 | filename = os.path.basename(perlasm['input']) |
| 278 | output = perlasm['output'] |
| 279 | if not output.startswith('src'): |
| 280 | raise ValueError('output missing src: %s' % output) |
| 281 | output = os.path.join(outDir, output[4:]) |
| 282 | output = output.replace('${ASM_EXT}', asm_ext) |
| 283 | |
| 284 | if arch in ArchForAsmFilename(filename): |
| 285 | PerlAsm(output, perlasm['input'], perlasm_style, |
| 286 | perlasm['extra_args'] + extra_args) |
| 287 | asmfiles.setdefault(key, []).append(output) |
| 288 | |
| 289 | for (key, non_perl_asm_files) in NON_PERL_FILES.iteritems(): |
| 290 | asmfiles.setdefault(key, []).extend(non_perl_asm_files) |
| 291 | |
| 292 | return asmfiles |
| 293 | |
| 294 | |
| 295 | def main(platform): |
| 296 | crypto_c_files = FindCFiles(os.path.join('src', 'crypto'), NoTests) |
| 297 | ssl_c_files = FindCFiles(os.path.join('src', 'ssl'), NoTests) |
| 298 | tool_cc_files = FindCFiles(os.path.join('src', 'tool'), NoTests) |
| 299 | |
| 300 | # Generate err_data.c |
| 301 | with open('err_data.c', 'w+') as err_data: |
| 302 | subprocess.check_call(['go', 'run', 'err_data_generate.go'], |
| 303 | cwd=os.path.join('src', 'crypto', 'err'), |
| 304 | stdout=err_data) |
| 305 | crypto_c_files.append('err_data.c') |
| 306 | |
| 307 | test_c_files = FindCFiles(os.path.join('src', 'crypto'), OnlyTests) |
| 308 | test_c_files += FindCFiles(os.path.join('src', 'ssl'), OnlyTests) |
| 309 | |
| 310 | files = { |
| 311 | 'crypto': crypto_c_files, |
| 312 | 'ssl': ssl_c_files, |
| 313 | 'tool': tool_cc_files, |
| 314 | 'test': test_c_files, |
| 315 | } |
| 316 | |
| 317 | asm_outputs = sorted(WriteAsmFiles(ReadPerlAsmOperations()).iteritems()) |
| 318 | |
| 319 | platform.WriteFiles(files, asm_outputs) |
| 320 | |
| 321 | return 0 |
| 322 | |
| 323 | |
| 324 | def Usage(): |
| 325 | print 'Usage: python %s [chromium|android]' % sys.argv[0] |
| 326 | sys.exit(1) |
| 327 | |
| 328 | |
| 329 | if __name__ == '__main__': |
| 330 | if len(sys.argv) != 2: |
| 331 | Usage() |
| 332 | |
| 333 | platform = None |
| 334 | if sys.argv[1] == 'chromium': |
| 335 | platform = Chromium() |
| 336 | elif sys.argv[1] == 'android': |
| 337 | platform = Android() |
| 338 | else: |
| 339 | Usage() |
| 340 | |
| 341 | sys.exit(main(platform)) |