Skip to content

Commit

Permalink
Fix formatting checks
Browse files Browse the repository at this point in the history
  • Loading branch information
FranzBusch committed Oct 16, 2024
1 parent da453e5 commit e4039ca
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 92 deletions.
157 changes: 77 additions & 80 deletions scripts/build-asm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.md for the list of SwiftCrypto project authors
## See CONTRIBUTORS.txt for the list of SwiftCrypto project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

import contextlib
import subprocess
import os
import subprocess


# OS_ARCH_COMBOS maps from OS and platform to the OpenSSL assembly "style" for
Expand Down Expand Up @@ -49,85 +48,85 @@


def FindCMakeFiles(directory):
"""Returns list of all CMakeLists.txt files recursively in directory."""
cmakefiles = []
"""Returns list of all CMakeLists.txt files recursively in directory."""
cmakefiles = []

for (path, _, filenames) in os.walk(directory):
for filename in filenames:
if filename == 'CMakeLists.txt':
cmakefiles.append(os.path.join(path, filename))
for (path, _, filenames) in os.walk(directory):
for filename in filenames:
if filename == 'CMakeLists.txt':
cmakefiles.append(os.path.join(path, filename))

return cmakefiles
return cmakefiles


def ExtractPerlAsmFromCMakeFile(cmakefile):
"""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 = []
with open(cmakefile) as f:
for line in f:
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
params = line[8:-1].split()
if len(params) < 4:
raise ValueError('Bad perlasm line in %s: %s' % (cmakefile, line))
perlasms.append({
'arch': params[1],
'output': os.path.join(os.path.dirname(cmakefile), params[2]),
'input': os.path.join(os.path.dirname(cmakefile), params[3]),
'extra_args': params[4:],
})

return perlasms
"""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 = []
with open(cmakefile) as f:
for line in f:
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
params = line[8:-1].split()
if len(params) < 4:
raise ValueError('Bad perlasm line in %s: %s' % (cmakefile, line))
perlasms.append({
'arch': params[1],
'output': os.path.join(os.path.dirname(cmakefile), params[2]),
'input': os.path.join(os.path.dirname(cmakefile), params[3]),
'extra_args': params[4:],
})

return perlasms


def ReadPerlAsmOperations():
"""Returns a list of all perlasm() directives found in CMake config files in
src/."""
perlasms = []
cmakefiles = FindCMakeFiles('boringssl')
"""Returns a list of all perlasm() directives found in CMake config files in
src/."""
perlasms = []
cmakefiles = FindCMakeFiles('boringssl')

for cmakefile in cmakefiles:
perlasms.extend(ExtractPerlAsmFromCMakeFile(cmakefile))
for cmakefile in cmakefiles:
perlasms.extend(ExtractPerlAsmFromCMakeFile(cmakefile))

return perlasms
return perlasms


def PerlAsm(output_filename, input_filename, perlasm_style, extra_args):
"""Runs the a perlasm script and puts the output into output_filename."""
base_dir = os.path.dirname(output_filename)
if not os.path.isdir(base_dir):
os.makedirs(base_dir)
subprocess.check_call(
['perl', input_filename, perlasm_style] + extra_args + [output_filename])
"""Runs the a perlasm script and puts the output into output_filename."""
base_dir = os.path.dirname(output_filename)
if not os.path.isdir(base_dir):
os.makedirs(base_dir)
subprocess.check_call(
['perl', input_filename, perlasm_style] + extra_args + [output_filename])


def WriteAsmFiles(perlasms):
"""Generates asm files from perlasm directives for each supported OS x
platform combination."""
asmfiles = {}

for perlasm in perlasms:
for (osname, arch, perlasm_style, extra_args, asm_ext) in OS_ARCH_COMBOS:
if arch != perlasm['arch']:
continue
key = (osname, arch)
outDir = '%s-%s' % key

filename = os.path.basename(perlasm['input'])
output = perlasm['output']
if not output.startswith('boringssl/crypto'):
raise ValueError('output missing crypto: %s' % output)
output = os.path.join(outDir, output[17:])
output = '%s-%s.%s' % (output, osname, asm_ext)
per_command_extra_args = extra_args + perlasm['extra_args']
PerlAsm(output, perlasm['input'], perlasm_style, per_command_extra_args)
asmfiles.setdefault(key, []).append(output)

return asmfiles
"""Generates asm files from perlasm directives for each supported OS x
platform combination."""
asmfiles = {}

for perlasm in perlasms:
for (osname, arch, perlasm_style, extra_args, asm_ext) in OS_ARCH_COMBOS:
if arch != perlasm['arch']:
continue
key = (osname, arch)
outDir = '%s-%s' % key

output = perlasm['output']
if not output.startswith('boringssl/crypto'):
raise ValueError('output missing crypto: %s' % output)
output = os.path.join(outDir, output[17:])
output = '%s-%s.%s' % (output, osname, asm_ext)
per_command_extra_args = extra_args + perlasm['extra_args']
PerlAsm(output, perlasm['input'], perlasm_style, per_command_extra_args)
asmfiles.setdefault(key, []).append(output)

return asmfiles


def preprocessor_arch_for_arch(arch):
Expand All @@ -152,19 +151,19 @@ def preprocessor_platform_for_os(osname):

def asm_target(osname, arch, asm):
components = asm.split('/')
new_components = ["boringssl/crypto"] + components[1:-1] + [components[-1].replace('.S', '.' + osname + '.' + arch + '.S')]
new_components = ["boringssl/crypto"] + components[1:-1] + [components[-1].replace('.S', '.' + osname + '.' + arch + '.S')] # noqa: E501
return '/'.join(new_components)


def munge_file(pp_arch, pp_platform, source_lines, sink):
"""
Wraps a single assembly file in appropriate defines.
"""
sink.write(b"#if defined(%b) && defined(%b)\n" % (pp_arch.encode(), pp_platform.encode()))
sink.write(b"#if defined(%b) && defined(%b)\n" % (pp_arch.encode(), pp_platform.encode())) # noqa: E501
for line in source_lines:
sink.write(line)

sink.write(b"#endif // defined(%b) && defined(%b)\n" % (pp_arch.encode(), pp_platform.encode()))
sink.write(b"#endif // defined(%b) && defined(%b)\n" % (pp_arch.encode(), pp_platform.encode())) # noqa: E501


def munge_all_files(osname, arch, asms):
Expand All @@ -175,11 +174,10 @@ def munge_all_files(osname, arch, asms):
pp_arch = preprocessor_arch_for_arch(arch)
pp_platform = preprocessor_platform_for_os(osname)
target = asm_target(osname, arch, asm)

with open(asm, 'rb') as source:
with open(target, 'wb') as sink:
munge_file(pp_arch, pp_platform, source, sink)



def main():
Expand All @@ -194,15 +192,14 @@ def main():

for ((osname, arch), asm_files) in NON_PERL_FILES.items():
for asm_file in asm_files:
with open(asm_file, 'rb') as f:
lines = f.readlines()
with open(asm_file, 'rb') as f:
lines = f.readlines()
pp_arch = preprocessor_arch_for_arch(arch)
pp_platform = preprocessor_platform_for_os(osname)

with open(asm_file, 'wb') as sink:
munge_file(pp_arch, pp_platform, lines, sink)

pp_arch = preprocessor_arch_for_arch(arch)
pp_platform = preprocessor_platform_for_os(osname)

with open(asm_file, 'wb') as sink:
munge_file(pp_arch, pp_platform, lines, sink)

if __name__ == '__main__':
main()

2 changes: 1 addition & 1 deletion scripts/environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.md for the list of SwiftCrypto project authors
## See CONTRIBUTORS.txt for the list of SwiftCrypto project authors
##
## SPDX-License-Identifier: Apache-2.0
##
Expand Down
1 change: 1 addition & 0 deletions scripts/gyb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python2.7

import gyb
gyb.main()
13 changes: 6 additions & 7 deletions scripts/gyb.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def split_lines(s):
If the lines are later concatenated, the result is s, possibly
with a single appended newline.
"""
return [l + '\n' for l in s.split('\n')]
return [line + '\n' for line in s.split('\n')]


# text on a line up to the first '$$', '${', or '%%'
Expand All @@ -74,8 +74,7 @@ def split_lines(s):
^
(?:
(?P<gybLines>
(?P<_indent> [\ \t]* % (?! [{%] ) [\ \t]* ) (?! [\ \t] | ''' +
linesClose + r''' ) .*
(?P<_indent> [\ \t]* % (?! [{%] ) [\ \t]* ) (?! [\ \t] | ''' + linesClose + r''' ) .* # noqa: E501
( \n (?P=_indent) (?! ''' + linesClose + r''' ) .* ) *
)
| (?P<gybLinesClose> [\ \t]* % [ \t]* ''' + linesClose + r''' )
Expand Down Expand Up @@ -612,8 +611,8 @@ def format_children(self, indent):
return ' []'

return '\n'.join(
['', indent + '['] +
[x.__str__(indent + 4 * ' ') for x in self.children] +
['', indent + '['] + # noqa: W504
[x.__str__(indent + 4 * ' ') for x in self.children] + # noqa: W504
[indent + ']'])


Expand Down Expand Up @@ -657,7 +656,7 @@ def execute(self, context):

def __str__(self, indent=''):
return '\n'.join(
[indent + x for x in ['Literal:'] +
[indent + x for x in ['Literal:'] + # noqa: W504
strip_trailing_nl(self.text).split('\n')])


Expand Down Expand Up @@ -748,7 +747,7 @@ def __str__(self, indent=''):
s = indent + 'Code: {' + source_lines[0] + '}'
else:
s = indent + 'Code:\n' + indent + '{\n' + '\n'.join(
indent + 4 * ' ' + l for l in source_lines
indent + 4 * ' ' + line for line in source_lines
) + '\n' + indent + '}'
return s + self.format_children(indent)

Expand Down
13 changes: 9 additions & 4 deletions scripts/vendor-boringssl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ function mangle_symbols {
)

# Now cross compile for our targets.
docker run -t -i --rm --privileged -v$(pwd):/src -w/src --platform linux/arm64 swift:5.9-jammy \
docker run -t -i --rm --privileged -v"$(pwd)":/src -w/src --platform linux/arm64 swift:5.9-jammy \
swift build --product CCryptoBoringSSL
docker run -t -i --rm --privileged -v$(pwd):/src -w/src --platform linux/amd64 swift:5.9-jammy \
docker run -t -i --rm --privileged -v"$(pwd)":/src -w/src --platform linux/amd64 swift:5.9-jammy \
swift build --product CCryptoBoringSSL

# Now we need to generate symbol mangles for Linux. We can do this in
Expand Down Expand Up @@ -133,6 +133,7 @@ function mangle_symbols {
echo "ADDING symbol mangling"
perl -pi -e '$_ .= qq(\n#define BORINGSSL_PREFIX CCryptoBoringSSL\n) if /#define OPENSSL_HEADER_BASE_H/' "$DSTROOT/include/openssl/base.h"

# shellcheck disable=SC2044
for assembly_file in $(find "$DSTROOT" -name "*.S")
do
$sed -i '1 i #define BORINGSSL_PREFIX CCryptoBoringSSL' "$assembly_file"
Expand All @@ -145,6 +146,7 @@ case "$(uname -s)" in
sed=gsed
;;
*)
# shellcheck disable=SC2209
sed=sed
;;
esac
Expand Down Expand Up @@ -218,7 +220,7 @@ echo "COPYING boringssl"
for pattern in "${PATTERNS[@]}"
do
for i in $SRCROOT/$pattern; do
path=${i#$SRCROOT}
path=${i#"$SRCROOT"}
dest="$DSTROOT$path"
dest_dir=$(dirname "$dest")
mkdir -p "$dest_dir"
Expand Down Expand Up @@ -255,6 +257,7 @@ echo "RENAMING header files"
rmdir "include/openssl"

# Now change the imports from "<openssl/X> to "<CCryptoBoringSSL_X>", apply the same prefix to the 'boringssl_prefix_symbols' headers.
# shellcheck disable=SC2038
find . -name "*.[ch]" -or -name "*.cc" -or -name "*.S" -or -name "*.c.inc" | xargs $sed -i -e 's+include <openssl/\([[:alpha:]/]*/\)\{0,1\}+include <\1CCryptoBoringSSL_+' -e 's+include <boringssl_prefix_symbols+include <CCryptoBoringSSL_boringssl_prefix_symbols+' -e 's+include "openssl/\([[:alpha:]/]*/\)\{0,1\}+include "\1CCryptoBoringSSL_+'

# Okay now we need to rename the headers adding the prefix "CCryptoBoringSSL_".
Expand All @@ -266,6 +269,7 @@ echo "RENAMING header files"
done 3< <(find . -name "*.h" -print0 | sort -rz)

# Finally, make sure we refer to them by their prefixed names, and change any includes from angle brackets to quotation marks.
# shellcheck disable=SC2038
find . -name "*.h" | xargs $sed -i -e 's+include "\([[:alpha:]/]*/\)\{0,1\}+include "\1CCryptoBoringSSL_+' -e 's+include <\([[:alpha:]/]*/\)\{0,1\}CCryptoBoringSSL_\(.*\)>+include "\1CCryptoBoringSSL_\2"+'
popd
)
Expand All @@ -274,6 +278,7 @@ echo "RENAMING header files"
echo "PROTECTING against executable stacks"
(
cd "$DSTROOT"
# shellcheck disable=SC2038
find . -name "*.S" | xargs $sed -i '$ a #if defined(__linux__) && defined(__ELF__)\n.section .note.GNU-stack,"",%progbits\n#endif\n'
)

Expand All @@ -292,7 +297,7 @@ cat << EOF > "$DSTROOT/include/CCryptoBoringSSL.h"
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.md for the list of SwiftCrypto project authors
// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down

0 comments on commit e4039ca

Please sign in to comment.