6
from commonPkgConfigUtils import *
8
def getGmpDir(sconsEnv=None):
9
gmp_dir = getPackageDir("gmp", sconsEnv=sconsEnv,
10
default=os.path.join(os.path.sep, "usr"))
13
def pkgVersion(compiler=None, linker=None,
14
cflags=None, libs=None, sconsEnv=None):
15
cpp_test_version_str = r"""
20
#ifdef __GNU_MP_VERSION
21
#ifdef __GNU_MP_VERSION_MINOR
22
#ifdef __GNU_MP_VERSION_PATCHLEVEL
23
printf("%d.%d.%d", __GNU_MP_VERSION, __GNU_MP_VERSION_MINOR, __GNU_MP_VERSION_PATCHLEVEL);
25
printf("%d.%d", __GNU_MP_VERSION, __GNU_MP_VERSION_MINOR);
28
printf("%d", __GNU_MP_VERSION);
34
cppfile = "gmp_config_test_version.cpp"
35
write_cppfile(cpp_test_version_str, cppfile);
38
compiler = get_compiler(sconsEnv=sconsEnv)
40
compiler = get_linker(sconsEnv=sconsEnv)
42
cflags = pkgCflags(sconsEnv=sconsEnv)
44
libs = pkgLibs(sconsEnv=sconsEnv)
46
cmdstr = "%s %s -c %s" % (compiler, cflags, cppfile)
47
compileFailed, cmdoutput = getstatusoutput(cmdstr)
49
remove_cppfile(cppfile)
50
raise UnableToCompileException("GMP", cmd=cmdstr,
51
program=cpp_test_version_str,
54
cmdstr = "%s -o a.out %s %s" % (linker, libs, cppfile.replace('.cpp', '.o'))
55
linkFailed, cmdoutput = getstatusoutput(cmdstr)
57
remove_cppfile(cppfile, ofile=True)
58
raise UnableToLinkException("GMP", cmd=cmdstr,
59
program=cpp_test_version_str,
62
cmdstr = os.path.join(os.getcwd(), "a.out")
63
runFailed, cmdoutput = getstatusoutput(cmdstr)
65
remove_cppfile(cppfile, ofile=True, execfile=True)
66
raise UnableToRunException("GMP", errormsg=cmdoutput)
69
remove_cppfile(cppfile, ofile=True, execfile=True)
72
def pkgCflags(sconsEnv=None):
73
return "-I%s" % os.path.join(getGmpDir(sconsEnv=sconsEnv), "include")
75
def pkgLibs(sconsEnv=None):
76
if get_architecture().startswith("win"):
77
libs = "-L%s -lgmp-3 -lgmpxx-4" % \
78
os.path.join(getGmpDir(sconsEnv), "bin")
80
libs = "-L%s -lgmp -lgmpxx" % \
81
os.path.join(getGmpDir(sconsEnv), "lib")
84
def pkgTests(forceCompiler=None, sconsEnv=None,
85
cflags=None, libs=None, version=None, **kwargs):
86
"""Run the tests for this package
88
If Ok, return various variables, if not we will end with an exception.
89
forceCompiler, if set, should be a tuple containing (compiler, linker)
90
or just a string, which in that case will be used as both
94
compiler = get_compiler(sconsEnv)
95
linker = get_linker(sconsEnv)
97
compiler, linker = set_forced_compiler(forceCompiler)
100
cflags = pkgCflags(sconsEnv=sconsEnv)
102
libs = pkgLibs(sconsEnv=sconsEnv)
104
version = pkgVersion(sconsEnv=sconsEnv, compiler=compiler,
105
linker=linker, cflags=cflags, libs=libs)
107
# A program that do a real GMP test (thanks to Benjamin Kehlet)
108
cpp_test_lib_str = r"""
115
mpz_class integer1("1000000023457323");
116
mpz_class integer2("54367543212");
117
mpz_class int_result = integer1*integer2;
119
std::cout << integer1 << " * " << integer2 << " = "
120
<< int_result << std::endl;
125
cpp_file = "gmp_config_test_lib.cpp"
126
write_cppfile(cpp_test_lib_str, cpp_file);
128
# try to compile the simple GMP test
129
cmdstr = "%s %s -c %s" % (compiler, cflags, cpp_file)
130
compileFailed, cmdoutput = getstatusoutput(cmdstr)
132
remove_cppfile(cpp_file)
133
raise UnableToCompileException("GMP", cmd=cmdstr,
134
program=cpp_test_lib_str,
137
cmdstr = "%s -o a.out %s %s" % \
138
(linker, libs, cpp_file.replace('.cpp', '.o'))
139
linkFailed, cmdoutput = getstatusoutput(cmdstr)
141
remove_cppfile(cpp_file, ofile=True)
142
raise UnableToLinkException("GMP", cmd=cmdstr,
143
program=cpp_test_lib_str,
146
cmdstr = os.path.join(os.getcwd(), "a.out")
147
runFailed, cmdoutput = getstatusoutput(cmdstr)
149
remove_cppfile(cpp_file, ofile=True, execfile=True)
150
raise UnableToRunException("GMP", errormsg=cmdoutput)
152
value = cmdoutput.split("=")[1].strip()
153
assert value == '54367544487317021840341476'
155
errormsg = "GMP test does not produce correct result, " \
156
"check your GMP installation."
157
errormsg += "\n%s" % cmdoutput
158
raise UnableToRunException("GMP", errormsg=errormsg)
160
remove_cppfile(cpp_file, ofile=True, execfile=True)
162
return version, cflags, libs
164
def generatePkgConf(directory=suitablePkgConfDir(), sconsEnv=None, **kwargs):
166
version, cflags, libs = pkgTests(sconsEnv=sconsEnv)
168
pkg_file_str = r"""Name: GNU MP
170
Description: GNU Multiple Precision Arithmetic Library
173
""" % (version, libs, cflags)
175
pkg_file = open(os.path.join(directory, "gmp.pc"), 'w')
176
pkg_file.write(pkg_file_str)
178
print "done\n Found GMP and generated pkg-config file in\n '%s'" % directory
180
if __name__ == "__main__":
181
generatePkgConf(directory=".")