OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can b |
| 3 # found in the LICENSE file. |
| 4 |
| 5 """Enumerates the BoringSSL source in src/ and generates two gypi files: |
| 6 boringssl.gypi and boringssl_tests.gypi.""" |
| 7 |
| 8 import os |
| 9 import subprocess |
| 10 import sys |
| 11 |
| 12 |
| 13 # OS_ARCH_COMBOS maps from OS and platform to the OpenSSL assembly "style" for |
| 14 # that platform and the extension used by asm files. |
| 15 OS_ARCH_COMBOS = [ |
| 16 ('linux', 'arm', 'elf', 'S'), |
| 17 ('linux', 'x86', 'elf', 'S'), |
| 18 ('linux', 'x86_64', 'elf', 'S'), |
| 19 ('mac', 'x86', 'macosx', 'S'), |
| 20 ('mac', 'x86_64', 'macosx', 'S'), |
| 21 ('win', 'x86_64', 'masm', 'asm'), |
| 22 ] |
| 23 |
| 24 # NON_PERL_FILES enumerates assembly files that are not processed by the |
| 25 # perlasm system. |
| 26 NON_PERL_FILES = { |
| 27 ('linux', 'arm'): [ |
| 28 'src/crypto/poly1305/poly1305_arm_asm.S', |
| 29 'src/crypto/chacha/chacha_vec_arm.S', |
| 30 ], |
| 31 } |
| 32 |
| 33 FILE_HEADER = """# Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 34 # Use of this source code is governed by a BSD-style license that can be |
| 35 # found in the LICENSE file. |
| 36 |
| 37 # This file is created by update_gypi_and_asm.py. Do not edit manually. |
| 38 |
| 39 """ |
| 40 |
| 41 |
| 42 def FindCMakeFiles(directory): |
| 43 """Returns list of all CMakeLists.txt files recursively in directory.""" |
| 44 cmakefiles = [] |
| 45 |
| 46 for (path, _, filenames) in os.walk(directory): |
| 47 for filename in filenames: |
| 48 if filename == 'CMakeLists.txt': |
| 49 cmakefiles.append(os.path.join(path, filename)) |
| 50 |
| 51 return cmakefiles |
| 52 |
| 53 |
| 54 def NoTests(dent, is_dir): |
| 55 """Filter function that can be passed to FindCFiles in order to remove test |
| 56 sources.""" |
| 57 if is_dir: |
| 58 return dent != 'test' |
| 59 return 'test.' not in dent and not dent.startswith('example_') |
| 60 |
| 61 |
| 62 def OnlyTests(dent, is_dir): |
| 63 """Filter function that can be passed to FindCFiles in order to remove |
| 64 non-test sources.""" |
| 65 if is_dir: |
| 66 return True |
| 67 return '_test.' in dent or dent.startswith('example_') |
| 68 |
| 69 |
| 70 def FindCFiles(directory, filter_func): |
| 71 """Recurses through directory and returns a list of paths to all the C source |
| 72 files that pass filter_func.""" |
| 73 cfiles = [] |
| 74 |
| 75 for (path, dirnames, filenames) in os.walk(directory): |
| 76 for filename in filenames: |
| 77 if filename.endswith('.c') and filter_func(filename, False): |
| 78 cfiles.append(os.path.join(path, filename)) |
| 79 continue |
| 80 |
| 81 for (i, dirname) in enumerate(dirnames): |
| 82 if not filter_func(dirname, True): |
| 83 del dirnames[i] |
| 84 |
| 85 return cfiles |
| 86 |
| 87 |
| 88 def ExtractPerlAsmFromCMakeFile(cmakefile): |
| 89 """Parses the contents of the CMakeLists.txt file passed as an argument and |
| 90 returns a list of all the perlasm() directives found in the file.""" |
| 91 perlasms = [] |
| 92 with open(cmakefile) as f: |
| 93 for line in f: |
| 94 line = line.strip() |
| 95 if not line.startswith('perlasm('): |
| 96 continue |
| 97 if not line.endswith(')'): |
| 98 raise ValueError('Bad perlasm line in %s' % cmakefile) |
| 99 # Remove "perlasm(" from start and ")" from end |
| 100 params = line[8:-1].split() |
| 101 if len(params) < 2: |
| 102 raise ValueError('Bad perlasm line in %s' % cmakefile) |
| 103 perlasms.append({ |
| 104 'extra_args': params[2:], |
| 105 'input': os.path.join(os.path.dirname(cmakefile), params[1]), |
| 106 'output': os.path.join(os.path.dirname(cmakefile), params[0]), |
| 107 }) |
| 108 |
| 109 return perlasms |
| 110 |
| 111 |
| 112 def ReadPerlAsmOperations(): |
| 113 """Returns a list of all perlasm() directives found in CMake config files in |
| 114 src/.""" |
| 115 perlasms = [] |
| 116 cmakefiles = FindCMakeFiles('src') |
| 117 |
| 118 for cmakefile in cmakefiles: |
| 119 perlasms.extend(ExtractPerlAsmFromCMakeFile(cmakefile)) |
| 120 |
| 121 return perlasms |
| 122 |
| 123 |
| 124 def PerlAsm(output_filename, input_filename, perlasm_style, extra_args): |
| 125 """Runs the a perlasm script and puts the output into output_filename.""" |
| 126 base_dir = os.path.dirname(output_filename) |
| 127 if not os.path.isdir(base_dir): |
| 128 os.makedirs(base_dir) |
| 129 output = subprocess.check_output( |
| 130 ['perl', input_filename, perlasm_style] + extra_args) |
| 131 with open(output_filename, 'w+') as out_file: |
| 132 out_file.write(output) |
| 133 |
| 134 |
| 135 def WriteAsmFiles(perlasms): |
| 136 """Generates asm files from perlasm directives for each supported OS x |
| 137 platform combination.""" |
| 138 asmfiles = {} |
| 139 |
| 140 for osarch in OS_ARCH_COMBOS: |
| 141 (osname, arch, perlasm_style, asm_ext) = osarch |
| 142 key = (osname, arch) |
| 143 outDir = '%s-%s' % key |
| 144 |
| 145 for perlasm in perlasms: |
| 146 filename = os.path.basename(perlasm['input']) |
| 147 output = perlasm['output'] |
| 148 if not output.startswith('src'): |
| 149 raise ValueError('output missing src: %s' % output) |
| 150 output = os.path.join(outDir, output[4:]) |
| 151 output = output.replace('${ASM_EXT}', asm_ext) |
| 152 |
| 153 found = False |
| 154 if arch == 'x86_64' and ('x86_64' in filename or 'avx2' in filename): |
| 155 found = True |
| 156 elif arch == 'x86' and 'x86' in filename and 'x86_64' not in filename: |
| 157 found = True |
| 158 elif arch == 'arm' and 'arm' in filename: |
| 159 found = True |
| 160 |
| 161 if found: |
| 162 PerlAsm(output, perlasm['input'], perlasm_style, perlasm['extra_args']) |
| 163 asmfiles.setdefault(key, []).append(output) |
| 164 |
| 165 for (key, non_perl_asm_files) in NON_PERL_FILES.iteritems(): |
| 166 asmfiles.setdefault(key, []).extend(non_perl_asm_files) |
| 167 |
| 168 return asmfiles |
| 169 |
| 170 |
| 171 def PrintVariableSection(out, name, files): |
| 172 out.write(' \'%s\': [\n' % name) |
| 173 for f in sorted(files): |
| 174 out.write(' \'%s\',\n' % f) |
| 175 out.write(' ],\n') |
| 176 |
| 177 |
| 178 def main(): |
| 179 crypto_c_files = FindCFiles(os.path.join('src', 'crypto'), NoTests) |
| 180 ssl_c_files = FindCFiles(os.path.join('src', 'ssl'), NoTests) |
| 181 |
| 182 with open('boringssl.gypi', 'w+') as gypi: |
| 183 gypi.write(FILE_HEADER + '{\n \'variables\': {\n') |
| 184 |
| 185 PrintVariableSection( |
| 186 gypi, 'boringssl_lib_sources', crypto_c_files + ssl_c_files) |
| 187 |
| 188 perlasms = ReadPerlAsmOperations() |
| 189 |
| 190 for ((osname, arch), asm_files) in sorted( |
| 191 WriteAsmFiles(perlasms).iteritems()): |
| 192 PrintVariableSection(gypi, 'boringssl_%s_%s_sources' % |
| 193 (osname, arch), asm_files) |
| 194 |
| 195 gypi.write(' }\n}\n') |
| 196 |
| 197 test_c_files = FindCFiles(os.path.join('src', 'crypto'), OnlyTests) |
| 198 |
| 199 with open('boringssl_tests.gypi', 'w+') as test_gypi: |
| 200 test_gypi.write(FILE_HEADER + '{\n \'targets\': [\n') |
| 201 |
| 202 test_names = [] |
| 203 for test in test_c_files: |
| 204 test_name = 'boringssl_%s' % os.path.splitext(os.path.basename(test))[0] |
| 205 test_gypi.write(""" { |
| 206 'target_name': '%s', |
| 207 'type': 'executable', |
| 208 'dependencies': [ |
| 209 'boringssl', |
| 210 ], |
| 211 'sources': [ |
| 212 '%s', |
| 213 ], |
| 214 },\n""" % (test_name, test)) |
| 215 test_names.append(test_name) |
| 216 |
| 217 test_names.sort() |
| 218 |
| 219 test_gypi.write(""" ], |
| 220 'variables': { |
| 221 'boringssl_test_targets': [\n""") |
| 222 |
| 223 for test in test_names: |
| 224 test_gypi.write(""" '%s',\n""" % test) |
| 225 |
| 226 test_gypi.write(' ],\n }\n}\n') |
| 227 |
| 228 return 0 |
| 229 |
| 230 |
| 231 if __name__ == '__main__': |
| 232 sys.exit(main()) |
OLD | NEW |