1 """This module contains helper functions for configuration using pkg-config."""
2
3 import os
4 from .output import get_status_output
5 import re
6
7
8 _swig_binary_cache = None
9 _swig_version_cache = None
10 _pkg_config_installed = None
11 _header_and_library_cache = {}
12
14 """ Check if a particular swig binary is available"""
15 global _swig_binary_cache
16 if not isinstance(binary, str):
17 raise TypeError("expected a 'str' as first argument")
18 if not isinstance(path, str):
19 raise TypeError("expected a 'str' as second argument")
20 swig_binary = os.path.join(path, binary)
21 if swig_binary == _swig_binary_cache:
22 return True
23
24 result, output = get_status_output("%s -version"%swig_binary)
25 if result != 0:
26 return False
27
28
29 _swig_binary_cache = swig_binary
30
31
32 _swig_version_cache = None
33
34 return True
35
39
52
54 """ Check the swig version
55
56 Returns True if the version of the installed swig is equal or greater than the
57 version passed to the function.
58
59 If same is True, the function returns True if and only if the two versions
60 are the same.
61
62 Usage:
63 if instant.check_swig_version('1.3.36'):
64 print "Swig version is greater than or equal to 1.3.36"
65 else:
66 print "Swig version is lower than 1.3.36"
67 """
68 assert isinstance(version,str), "Provide the first version number as a 'str'"
69 assert len(version.split("."))==3, "Provide the version number as three numbers seperated by '.'"
70
71 installed_version = list(map(int, get_swig_version().split('.')))
72 handed_version = list(map(int, version.split('.')))
73
74
75 if same:
76 return all(i == h for i, h in zip(installed_version,handed_version))
77
78 swig_enough = True
79 for i, v in enumerate([v for v in installed_version]):
80 if handed_version[i] < v:
81 break
82 elif handed_version[i] == v:
83 continue
84 else:
85 swig_enough = False
86 break
87
88 return swig_enough
89
91 """This function returns list of include files, flags,
92 libraries and library directories obtain from a pkgconfig file.
93
94 The usage is:
95 (includes, flags, libraries, libdirs) = \
96 header_and_libs_from_pkgconfig(*list_of_packages)
97 or:
98 (includes, flags, libraries, libdirs, linkflags) = \
99 header_and_libs_from_pkgconfig(*list_of_packages, \
100 returnLinkFlags=True)
101 """
102 global _pkg_config_installed, _header_and_library_cache
103 returnLinkFlags = kwargs.get("returnLinkFlags", False)
104 if _pkg_config_installed is None:
105 result, output = get_status_output("pkg-config --version ")
106 _pkg_config_installed = (result == 0)
107 if not _pkg_config_installed:
108 raise OSError("The pkg-config package is not installed on the system.")
109
110 env = os.environ.copy()
111 try:
112 assert env["PKG_CONFIG_ALLOW_SYSTEM_CFLAGS"] == "0"
113 except:
114 env["PKG_CONFIG_ALLOW_SYSTEM_CFLAGS"] = "1"
115
116 includes = []
117 flags = []
118 libs = []
119 libdirs = []
120 linkflags = []
121 for pack in packages:
122 if not pack in _header_and_library_cache:
123 result, output = get_status_output(\
124 "pkg-config --exists %s " % pack, env=env)
125 if result == 0:
126 tmp = get_status_output(\
127 "pkg-config --cflags-only-I %s " % pack, env=env)[1].split()
128 _includes = [i[2:] for i in tmp]
129
130 _flags = get_status_output(\
131 "pkg-config --cflags-only-other %s " % pack, env=env)[1].split()
132
133 tmp = get_status_output(\
134 "pkg-config --libs-only-l %s " % pack, env=env)[1].split()
135 _libs = [i[2:] for i in tmp]
136
137 tmp = get_status_output(\
138 "pkg-config --libs-only-L %s " % pack, env=env)[1].split()
139 _libdirs = [i[2:] for i in tmp]
140
141 _linkflags = get_status_output(\
142 "pkg-config --libs-only-other %s " % pack, env=env)[1].split()
143
144 _header_and_library_cache[pack] = (_includes, _flags, _libs, \
145 _libdirs, _linkflags)
146 else:
147 _header_and_library_cache[pack] = None
148
149 result = _header_and_library_cache[pack]
150 if not result:
151 raise OSError("The pkg-config file %s does not exist" % pack)
152
153 _includes, _flags, _libs, _libdirs, _linkflags = result
154 includes.extend(_includes)
155 flags.extend(_flags)
156 libs.extend(_libs)
157 libdirs.extend(_libdirs)
158 linkflags.extend(_linkflags)
159
160 if returnLinkFlags:
161 return (includes, flags, libs, libdirs, linkflags)
162 return (includes, flags, libs, libdirs)
163