Chromium Code Reviews| Index: third_party/boringssl/update_gypi_and_asm.py |
| diff --git a/third_party/boringssl/update_gypi_and_asm.py b/third_party/boringssl/update_gypi_and_asm.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9ef1d06617e8ed3687f44a7514f05af216591e00 |
| --- /dev/null |
| +++ b/third_party/boringssl/update_gypi_and_asm.py |
| @@ -0,0 +1,259 @@ |
| +# This utility enumerates the BoringSSL source in src/ and generates two gypi |
|
M-A Ruel
2014/07/09 00:24:21
Add chromium copyright
agl
2014/07/09 16:42:01
Done.
|
| +# files: boringssl.gypi and boringssl_tests.gypi |
| + |
| +import os |
| +import os.path |
|
M-A Ruel
2014/07/09 00:24:21
not necessary
agl
2014/07/09 16:42:01
Done.
|
| +import stat |
| +import subprocess |
| + |
| + |
| +def FindCMakeFiles(directory): |
| + '''FindCMakeFiles recurses through directory and returns a list of the paths |
|
M-A Ruel
2014/07/09 00:24:21
""" and verb for docstring, e.g.
"""Returns list
agl
2014/07/09 16:42:01
Done.
|
| + to all CMakeLists.txt files.''' |
| + cmakefiles = [] |
| + dents = os.listdir(directory) |
| + |
| + for dent in dents: |
|
M-A Ruel
2014/07/09 00:24:21
What you want is:
for root, dirs, files in os.walk
agl
2014/07/09 16:42:01
Done.
|
| + path = os.path.join(directory, dent) |
| + if dent == 'CMakeLists.txt': |
| + cmakefiles.append(path) |
| + continue |
| + |
| + statinfo = os.lstat(path) |
| + if not stat.S_ISDIR(statinfo.st_mode): |
| + continue |
| + |
| + cmakefiles.extend(FindCMakeFiles(path)) |
| + |
| + return cmakefiles |
| + |
| + |
| +def NoTests(dent, is_dir): |
| + '''NoTests is a filter function that can be passed to FindCFiles in order to |
| + remove test sources.''' |
| + if is_dir: |
| + return dent != 'test' |
| + return 'test.' not in dent and not dent.startswith('example_') |
| + |
| + |
| +def OnlyTests(dent, is_dir): |
| + '''OnlyTests is a filter function that can be passed to FindCFiles in order to |
| + remove non-test sources.''' |
| + if is_dir: |
| + return True |
| + return '_test.' in dent or dent.startswith('example_') |
| + |
| + |
| +def FindCFiles(directory, filter_func): |
| + '''FindCFiles recurses through directory and returns a list of paths to all |
| + the C source files that pass filter_func.''' |
| + cfiles = [] |
| + dents = os.listdir(directory) |
| + |
| + for dent in dents: |
|
M-A Ruel
2014/07/09 00:24:21
os.walk
agl
2014/07/09 16:42:01
Done.
|
| + path = os.path.join(directory, dent) |
| + if dent.endswith('.c') and filter_func(dent, False): |
| + cfiles.append(path) |
| + continue |
| + |
| + statinfo = os.lstat(path) |
| + if not stat.S_ISDIR(statinfo.st_mode): |
| + continue |
| + |
| + if not filter_func(dent, True): |
| + continue |
| + |
| + cfiles.extend(FindCFiles(path, filter_func)) |
| + |
| + return cfiles |
| + |
| + |
| +def ExtractPerlAsmFromCMakeFile(cmakefile): |
| + '''ExtractPerlAsmFromCMakeFile parses the contents of the CMakeLists.txt file |
| + passed as an argument and returns a list of all the perlasm() directives |
| + found in the file.''' |
| + perlasms = [] |
| + f = file(cmakefile, 'r') |
| + |
| + for line in f.readlines(): |
|
M-A Ruel
2014/07/09 00:24:21
with open(cmakefile) as f:
for line in f:
..
agl
2014/07/09 16:42:01
Done.
|
| + line = line.strip() |
| + if not line.startswith('perlasm('): |
| + continue |
| + if not line.endswith(')'): |
| + raise ValueError('Bad perlasm line in %s' % cmakefile) |
| +# Remove "perlasm(" from start and ")" from end |
|
M-A Ruel
2014/07/09 00:24:21
Align comment with rest of script.
agl
2014/07/09 16:42:01
Done.
|
| + line = line[8:-1] |
| + params = line.split() |
|
M-A Ruel
2014/07/09 00:24:21
params = line[8:-1].split()
agl
2014/07/09 16:42:00
Done.
|
| + if len(params) < 2: |
| + raise ValueError('Bad perlasm line in %s' % cmakefile) |
| + perlasms.append({ |
| + 'output': os.path.join(os.path.dirname(cmakefile), params[0]), |
|
M-A Ruel
2014/07/09 00:24:21
sort keys
agl
2014/07/09 16:42:00
Done.
|
| + 'input': os.path.join(os.path.dirname(cmakefile), params[1]), |
| + 'extra_args': params[2:], |
| + }) |
| + |
| + f.close() |
| + return perlasms |
| + |
| + |
| +def ReadPerlAsmOperations(): |
| + '''ReadPerlAsmOperations returns a list of all perlasm() directives found in |
|
M-A Ruel
2014/07/09 00:24:21
"""Returns ...
agl
2014/07/09 16:42:01
Done.
|
| + CMake config files in src/.''' |
| + perlasms = [] |
| + cmakefiles = FindCMakeFiles('src') |
| + |
| + for cmakefile in cmakefiles: |
| + perlasms.extend(ExtractPerlAsmFromCMakeFile(cmakefile)) |
| + |
| + return perlasms |
| + |
| +# kOSArchCombos maps from OS and platform to the OpenSSL assembly "style" for |
| +# that platform and the extension used by asm files. |
| +kOSArchCombos = [ |
|
M-A Ruel
2014/07/09 00:24:21
ALL_CAPS const at top of file.
agl
2014/07/09 16:42:01
Done.
|
| + ('linux', 'arm', 'elf', 'S'), |
| + ('linux', 'x86', 'elf', 'S'), |
| + ('linux', 'x86_64', 'elf', 'S'), |
| + ('mac', 'x86', 'macosx', 'S'), |
| + ('mac', 'x86_64', 'macosx', 'S'), |
| + ('win', 'x86_64', 'masm', 'asm'), |
| +] |
| + |
| +# kNonPerlFiles enumerates assembly files that are not processed by the perlasm |
| +# system. |
| +kNonPerlFiles = { |
| + ('linux', 'arm'): [ |
| + 'src/crypto/poly1305/poly1305_arm_asm.S', |
| + 'src/crypto/chacha/chacha_vec_arm.S', |
| + ], |
| +} |
| + |
| + |
| +def PerlAsm(output_filename, input_filename, perlasm_style, extra_args): |
| + '''PerlAsm runs the a perlasm script and puts the output into |
| + output_filename.''' |
| + try: |
| + os.makedirs(os.path.dirname(output_filename)) |
| + except OSError: |
| + pass |
| + output = subprocess.check_output( |
| + ['perl', input_filename, perlasm_style] + extra_args) |
| + out_file = file(output_filename, 'w+') |
|
M-A Ruel
2014/07/09 00:24:21
use with statement
agl
2014/07/09 16:42:00
Done.
|
| + out_file.write(output) |
| + out_file.close() |
| + |
| + |
| +def WriteAsmFiles(perlasms): |
| + '''WriteAsmFiles generates asm files from perlasm directives for each |
| + supported OS x platform combination.''' |
| + asmfiles = {} |
| + |
| + for osarch in kOSArchCombos: |
| + (osname, arch, perlasm_style, asm_ext) = osarch |
| + key = (osname, arch) |
| + outDir = '%s-%s' % key |
| + |
| + for perlasm in perlasms: |
| + filename = os.path.basename(perlasm['input']) |
| + output = perlasm['output'] |
| + if not output.startswith('src'): |
| + raise ValueError('output missing src: %s' % output) |
| + output = os.path.join(outDir, output[4:]) |
| + output = output.replace('${ASM_EXT}', asm_ext) |
| + |
| + found = False |
| + if (arch == 'x86_64' and |
| + ('x86_64' in filename or 'avx2' in filename)): |
| + found = True |
| + elif (arch == 'x86' and |
| + 'x86' in filename and |
| + 'x86_64' not in filename): |
| + found = True |
| + elif (arch == 'arm' and |
|
M-A Ruel
2014/07/09 00:24:21
this seems to fit 80 cols
agl
2014/07/09 16:42:01
Done.
|
| + 'arm' in filename): |
| + found = True |
| + |
| + if found: |
| + PerlAsm(output, perlasm['input'], perlasm_style, perlasm['extra_args']) |
| + asmfiles[key] = asmfiles.get(key, []) + [output] |
| + |
| + for (key, non_perl_asm_files) in kNonPerlFiles.items(): |
|
M-A Ruel
2014/07/09 00:24:21
s/items/iteritems/
agl
2014/07/09 16:42:01
Done.
|
| + asmfiles[key] = asmfiles.get(key, []) + non_perl_asm_files |
| + |
| + return asmfiles |
| + |
| + |
| +def PrintVariableSection(out, name, files): |
| + out.write(""" '%s': [\n""" % name) |
| + files.sort() |
| + for f in files: |
| + out.write(""" '%s',\n""" % f) |
| + out.write(''' ],\n''') |
| + |
| +crypto_c_files = FindCFiles(os.path.join('src', 'crypto'), NoTests) |
|
Ryan Sleevi
2014/07/09 00:03:35
Chromium Python style is to do all this in a main(
M-A Ruel
2014/07/09 00:24:21
Yes,
def main():
foo ...
bar ...
return 0
agl
2014/07/09 16:42:00
Done.
agl
2014/07/09 16:42:01
Done.
|
| +ssl_c_files = FindCFiles(os.path.join('src', 'ssl'), NoTests) |
| + |
| +gypi = file('boringssl.gypi', 'w+') |
| + |
| +header = '''# Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +# This file is created by update_gypi_and_asm.py. Do not edit manually. |
| + |
| +''' |
| + |
| +gypi.write(header + '''{ |
| + 'variables': {\n''') |
| + |
| +PrintVariableSection( |
| + gypi, 'boringssl_lib_sources', crypto_c_files + ssl_c_files) |
| + |
| +perlasms = ReadPerlAsmOperations() |
| +asmfiles = WriteAsmFiles(perlasms) |
| + |
| +for ((osname, arch), asm_files) in asmfiles.items(): |
| + PrintVariableSection(gypi, 'boringssl_%s_%s_sources' % |
| + (osname, arch), asm_files) |
| + |
| +gypi.write(''' } |
| +}\n''') |
| + |
| +gypi.close() |
| + |
| + |
| +test_c_files = FindCFiles(os.path.join('src', 'crypto'), OnlyTests) |
| + |
| +test_gypi = file('boringssl_tests.gypi', 'w+') |
| + |
| +test_gypi.write(header + '''{ |
| + 'targets': [\n''') |
| + |
| +test_names = [] |
| +for test in test_c_files: |
| + test_name = 'boringssl_%s' % os.path.splitext(os.path.basename(test))[0] |
| + test_gypi.write(''' { |
| + 'target_name': '%s', |
| + 'type': 'executable', |
| + 'dependencies': [ |
| + 'boringssl', |
| + ], |
| + 'sources': [ |
| + '%s', |
| + ], |
| + },\n''' % (test_name, test)) |
| + test_names.append(test_name) |
| + |
| +test_names.sort() |
| + |
| +test_gypi.write(''' ], |
| + 'variables': { |
| + 'boringssl_test_targets': [\n''') |
| + |
| +for test in test_names: |
| + test_gypi.write(""" '%s',\n""" % test) |
| + |
| +test_gypi.write(''' ], |
| + } |
| +}\n''') |
| + |
| +test_gypi.close() |