Debian Codegraph @

lib_pypy/_cffi_ssl/_cffi_src/utils.py

raw symbols SCIP
1 # This file is dual licensed under the terms of the Apache License, Version
2 # 2.0, and the BSD License. See the LICENSE file in the root of this repository
3 # for complete details.
4
5 from __future__ import absolute_import, division, print_function
6
7 import os
8 import sys
9 from distutils.ccompiler import new_compiler
10 from distutils.dist import Distribution
11
12 from cffi import FFI
13
14
15 # Load the cryptography __about__ to get the current package version
16 base_src = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17 about = {}
18 with open(os.path.join(base_src, "cryptography", "__about__.py")) as f:
19 exec(f.read(), about)
20
21
22 def build_ffi_for_binding(module_name, module_prefix, modules, libraries=[],
23 extra_compile_args=[], extra_link_args=[]):
24 """
25 Modules listed in ``modules`` should have the following attributes:
26
27 * ``INCLUDES``: A string containing C includes.
28 * ``TYPES``: A string containing C declarations for types.
29 * ``FUNCTIONS``: A string containing C declarations for functions & macros.
30 * ``CUSTOMIZATIONS``: A string containing arbitrary top-level C code, this
31 can be used to do things like test for a define and provide an
32 alternate implementation based on that.
33 """
34 types = []
35 includes = []
36 functions = []
37 customizations = []
38 for name in modules:
39 __import__(module_prefix + name)
40 module = sys.modules[module_prefix + name]
41
42 types.append(module.TYPES)
43 functions.append(module.FUNCTIONS)
44 includes.append(module.INCLUDES)
45 customizations.append(module.CUSTOMIZATIONS)
46
47 verify_source = "\n".join(
48 includes +
49 customizations
50 )
51 ffi = build_ffi(
52 module_name,
53 cdef_source="\n".join(types + functions),
54 verify_source=verify_source,
55 libraries=libraries,
56 extra_compile_args=extra_compile_args,
57 extra_link_args=extra_link_args,
58 )
59
60 return ffi
61
62
63 def build_ffi(module_name, cdef_source, verify_source, libraries=[],
64 extra_compile_args=[], extra_link_args=[]):
65 ffi = FFI()
66 # Always add the CRYPTOGRAPHY_PACKAGE_VERSION to the shared object
67 cdef_source += "\nstatic const char *const CRYPTOGRAPHY_PACKAGE_VERSION;"
68 verify_source += '\n#define CRYPTOGRAPHY_PACKAGE_VERSION "{}"'.format(
69 about["__version__"]
70 )
71 # Add WSAGetLastError
72 if sys.platform == 'win32':
73 cdef_source += "\nint WSAGetLastError();"
74 ffi.cdef(cdef_source)
75 ffi.set_source(
76 module_name,
77 verify_source,
78 libraries=libraries,
79 extra_compile_args=extra_compile_args,
80 extra_link_args=extra_link_args,
81 )
82 return ffi
83
84
85 def extra_link_args(compiler_type):
86 if compiler_type == 'msvc':
87 # Enable NX and ASLR for Windows builds on MSVC. These are enabled by
88 # default on Python 3.3+ but not on 2.x.
89 return ['/NXCOMPAT', '/DYNAMICBASE']
90 else:
91 # The PyPy buildbots install OpenSSL to /usr/local/lib
92 if os.path.exists("/usr/local/lib"):
93 return["-L/usr/local/lib"]
94 return []
95
96
97 def compiler_type():
98 """
99 Gets the compiler type from distutils. On Windows with MSVC it will be
100 "msvc". On macOS and linux it is "unix".
101 """
102 dist = Distribution()
103 dist.parse_config_files()
104 cmd = dist.get_command_obj('build')
105 cmd.ensure_finalized()
106 compiler = new_compiler(compiler=cmd.compiler)
107 return compiler.compiler_type