Debian Codegraph @

src/bindings/build.py

raw
1 # Copyright 2013 Donald Stufft and individual contributors
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import glob
16 import os.path
17 import sys
18
19 from cffi import FFI
20
21
22 __all__ = ["ffi"]
23
24
25 HEADERS = sorted(
26 glob.glob(os.path.join(os.path.abspath(os.path.dirname(__file__)), "*.h"))
27 )
28
29 MINIMAL_HEADERS = sorted(
30 glob.glob(
31 os.path.join(
32 os.path.abspath(os.path.dirname(__file__)), "minimal", "*.h"
33 )
34 )
35 )
36
37
38 # Build our FFI instance
39 ffi = FFI()
40
41 for header in HEADERS:
42 with open(header) as hfile:
43 ffi.cdef(hfile.read())
44
45 source = []
46
47 # SODIUM_STATIC controls the visibility of symbols in the headers. (see
48 # export.h in the libsodium source tree). If you do not set SODIUM_STATIC
49 # when linking against the static library in Windows then the compile will
50 # fail with no symbols found.
51 if os.getenv("PYNACL_SODIUM_STATIC") is not None:
52 source.append("#define SODIUM_STATIC")
53
54 source.append("#include <sodium.h>")
55
56 for header in MINIMAL_HEADERS:
57 with open(header) as hfile:
58 source.append(hfile.read())
59
60 if sys.platform == "win32":
61 libraries = ["libsodium"]
62 else:
63 libraries = ["sodium"]
64
65 # Set our source so that we can actually build our bindings to sodium.
66 ffi.set_source("_sodium", "\n".join(source), libraries=libraries)