Debian Codegraph @

nixio/test/xcompat/compile.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Copyright © 2018, German Neuroinformatics Node (G-Node)
3 #
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted under the terms of the BSD License. See
8 # LICENSE file in the root of the Project.
9 import sys
10 import os
11 import platform
12 from glob import glob
13
14 import sysconfig
15 from setuptools._distutils.ccompiler import new_compiler
16 from setuptools._distutils.errors import CompileError, LinkError
17
18
19 WINDOWS = False
20 if platform.platform().startswith("Windows"):
21 WINDOWS = True
22
23
24 def ccomp(filenames, destination,
25 library_dirs=None, include_dirs=None,
26 libraries=None, compile_args=None,
27 runtime_lib_dirs=None):
28 compiler = new_compiler()
29
30 sysconfig.customize_compiler(compiler)
31 if library_dirs:
32 compiler.set_library_dirs(library_dirs)
33 if include_dirs:
34 compiler.set_include_dirs(include_dirs)
35 if libraries:
36 compiler.set_libraries(libraries)
37 if runtime_lib_dirs:
38 compiler.set_runtime_library_dirs(runtime_lib_dirs)
39
40 try:
41 objnames = compiler.compile(filenames, output_dir=destination,
42 extra_postargs=compile_args)
43 for obj in objnames:
44 execname, _ = os.path.splitext(obj)
45 compiler.link_executable(
46 [obj], execname, output_dir=destination,
47 target_lang="c++",
48 )
49 except (CompileError, LinkError):
50 return False
51 return True
52
53
54 def maketests(destination):
55 scriptloc, _ = os.path.split(os.path.abspath(__file__))
56 os.chdir(scriptloc)
57 filenames = glob("*.cpp")
58
59 # look for libs and headers in both /usr/ and /usr/local/
60 library_dirs = ["/usr/lib", "/usr/local/lib"]
61 libenv = os.getenv("NIX_LIBDIR", None)
62 if libenv:
63 library_dirs.append(libenv)
64
65 include_dirs = ["/usr/include/", "/usr/local/include",
66 "/usr/include/nixio-1.0", "/usr/local/include/nixio-1.0",
67 "src"]
68 incenv = os.getenv("NIX_INCDIR", None)
69 if incenv and libenv:
70 include_dirs.append(libenv)
71
72 boost_libenv = os.getenv("BOOST_LIBDIR", None)
73 if boost_libenv:
74 library_dirs.append(boost_libenv)
75 boost_incenv = os.getenv("BOOST_INCDIR", None)
76 if boost_incenv:
77 include_dirs.append(boost_incenv)
78
79 runtime_dirs = ["/usr/lib", "/usr/local/lib"]
80 llp = os.getenv("LD_LIBRARY_PATH", None)
81 if llp is not None:
82 runtime_dirs.append(llp)
83 libraries = ["nixio"]
84 compile_args = ["--std=c++11"]
85 if WINDOWS:
86 compile_args = []
87
88 print("Compiling {}".format(" ".join(filenames)))
89 success = ccomp(filenames, destination,
90 library_dirs=library_dirs,
91 include_dirs=include_dirs,
92 libraries=libraries,
93 compile_args=compile_args,
94 runtime_lib_dirs=runtime_dirs)
95 if success:
96 print("Done")
97 else:
98 print("Failed")
99 return success
100
101
102 if __name__ == '__main__':
103 if len(sys.argv) < 2:
104 print("Need a target dir")
105 sys.exit(1)
106
107 dest = sys.argv[1]
108 sys.exit(not maketests(dest))