~ubuntu-branches/ubuntu/maverick/dolfin/maverick

« back to all changes in this revision

Viewing changes to scons/simula-scons/simula_scons/pkgconfiggenerators/gmp.py

  • Committer: Bazaar Package Importer
  • Author(s): Johannes Ring
  • Date: 2008-09-16 08:41:20 UTC
  • Revision ID: james.westby@ubuntu.com-20080916084120-i8k3u6lhx3mw3py3
Tags: upstream-0.9.2
ImportĀ upstreamĀ versionĀ 0.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
import os,sys
 
3
import string
 
4
import os.path
 
5
 
 
6
from commonPkgConfigUtils import *
 
7
 
 
8
def getGmpDir(sconsEnv=None):
 
9
    gmp_dir = getPackageDir("gmp", sconsEnv=sconsEnv,
 
10
                            default=os.path.join(os.path.sep, "usr"))
 
11
    return gmp_dir
 
12
 
 
13
def pkgVersion(compiler=None, linker=None,
 
14
               cflags=None, libs=None, sconsEnv=None):
 
15
  cpp_test_version_str = r"""
 
16
#include <stdio.h>
 
17
#include <gmpxx.h>
 
18
 
 
19
int main() {
 
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);
 
24
      #else
 
25
        printf("%d.%d", __GNU_MP_VERSION, __GNU_MP_VERSION_MINOR);
 
26
      #endif
 
27
    #else
 
28
      printf("%d", __GNU_MP_VERSION);
 
29
    #endif
 
30
  #endif
 
31
  return 0;
 
32
}
 
33
"""
 
34
  cppfile = "gmp_config_test_version.cpp"
 
35
  write_cppfile(cpp_test_version_str, cppfile);
 
36
 
 
37
  if not compiler:
 
38
    compiler = get_compiler(sconsEnv=sconsEnv)
 
39
  if not linker:
 
40
    compiler = get_linker(sconsEnv=sconsEnv)
 
41
  if not cflags:
 
42
    cflags = pkgCflags(sconsEnv=sconsEnv)
 
43
  if not libs:
 
44
    libs = pkgLibs(sconsEnv=sconsEnv)
 
45
 
 
46
  cmdstr = "%s %s -c %s" % (compiler, cflags, cppfile)
 
47
  compileFailed, cmdoutput = getstatusoutput(cmdstr)
 
48
  if compileFailed:
 
49
    remove_cppfile(cppfile)
 
50
    raise UnableToCompileException("GMP", cmd=cmdstr,
 
51
                                   program=cpp_test_version_str,
 
52
                                   errormsg=cmdoutput)
 
53
 
 
54
  cmdstr = "%s -o a.out %s %s" % (linker, libs, cppfile.replace('.cpp', '.o'))
 
55
  linkFailed, cmdoutput = getstatusoutput(cmdstr)
 
56
  if linkFailed:
 
57
    remove_cppfile(cppfile, ofile=True)
 
58
    raise UnableToLinkException("GMP", cmd=cmdstr,
 
59
                                program=cpp_test_version_str,
 
60
                                errormsg=cmdoutput)
 
61
 
 
62
  cmdstr = os.path.join(os.getcwd(), "a.out")
 
63
  runFailed, cmdoutput = getstatusoutput(cmdstr)
 
64
  if runFailed:
 
65
    remove_cppfile(cppfile, ofile=True, execfile=True)
 
66
    raise UnableToRunException("GMP", errormsg=cmdoutput)
 
67
  version = cmdoutput
 
68
 
 
69
  remove_cppfile(cppfile, ofile=True, execfile=True)
 
70
  return version
 
71
 
 
72
def pkgCflags(sconsEnv=None):
 
73
    return "-I%s" % os.path.join(getGmpDir(sconsEnv=sconsEnv), "include")
 
74
 
 
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")
 
79
    else:
 
80
        libs = "-L%s -lgmp -lgmpxx" % \
 
81
               os.path.join(getGmpDir(sconsEnv), "lib")
 
82
    return libs
 
83
 
 
84
def pkgTests(forceCompiler=None, sconsEnv=None,
 
85
             cflags=None, libs=None, version=None, **kwargs):
 
86
    """Run the tests for this package
 
87
     
 
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
 
91
    """
 
92
 
 
93
    if not forceCompiler:
 
94
        compiler = get_compiler(sconsEnv)
 
95
        linker = get_linker(sconsEnv)
 
96
    else:
 
97
        compiler, linker = set_forced_compiler(forceCompiler)
 
98
 
 
99
    if not cflags:
 
100
        cflags = pkgCflags(sconsEnv=sconsEnv)
 
101
    if not libs:
 
102
        libs = pkgLibs(sconsEnv=sconsEnv)
 
103
    if not version:
 
104
        version = pkgVersion(sconsEnv=sconsEnv, compiler=compiler,
 
105
                             linker=linker, cflags=cflags, libs=libs)
 
106
 
 
107
    # A program that do a real GMP test (thanks to Benjamin Kehlet)
 
108
    cpp_test_lib_str = r"""
 
109
#include <cstdlib>
 
110
#include <iostream>
 
111
#include <gmpxx.h>
 
112
 
 
113
int main (void)
 
114
{
 
115
  mpz_class integer1("1000000023457323");
 
116
  mpz_class integer2("54367543212");
 
117
  mpz_class int_result = integer1*integer2;
 
118
 
 
119
  std::cout << integer1 << " * " << integer2 << " = "
 
120
            << int_result << std::endl;
 
121
 
 
122
  return EXIT_SUCCESS;
 
123
}
 
124
"""
 
125
    cpp_file = "gmp_config_test_lib.cpp"
 
126
    write_cppfile(cpp_test_lib_str, cpp_file);
 
127
 
 
128
    # try to compile the simple GMP test
 
129
    cmdstr = "%s %s -c %s" % (compiler, cflags, cpp_file)
 
130
    compileFailed, cmdoutput = getstatusoutput(cmdstr)
 
131
    if compileFailed:
 
132
        remove_cppfile(cpp_file)
 
133
        raise UnableToCompileException("GMP", cmd=cmdstr,
 
134
                                       program=cpp_test_lib_str,
 
135
                                       errormsg=cmdoutput)
 
136
 
 
137
    cmdstr = "%s -o a.out %s %s" % \
 
138
             (linker, libs, cpp_file.replace('.cpp', '.o'))
 
139
    linkFailed, cmdoutput = getstatusoutput(cmdstr)
 
140
    if linkFailed:
 
141
        remove_cppfile(cpp_file, ofile=True)
 
142
        raise UnableToLinkException("GMP", cmd=cmdstr,
 
143
                                    program=cpp_test_lib_str,
 
144
                                    errormsg=cmdoutput)
 
145
 
 
146
    cmdstr = os.path.join(os.getcwd(), "a.out")
 
147
    runFailed, cmdoutput = getstatusoutput(cmdstr)
 
148
    if runFailed:
 
149
        remove_cppfile(cpp_file, ofile=True, execfile=True)
 
150
        raise UnableToRunException("GMP", errormsg=cmdoutput)
 
151
    try:
 
152
        value = cmdoutput.split("=")[1].strip()
 
153
        assert value == '54367544487317021840341476'
 
154
    except:
 
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)
 
159
 
 
160
    remove_cppfile(cpp_file, ofile=True, execfile=True)
 
161
 
 
162
    return version, cflags, libs
 
163
 
 
164
def generatePkgConf(directory=suitablePkgConfDir(), sconsEnv=None, **kwargs):
 
165
    
 
166
    version, cflags, libs = pkgTests(sconsEnv=sconsEnv)
 
167
 
 
168
    pkg_file_str = r"""Name: GNU MP
 
169
Version: %s
 
170
Description: GNU Multiple Precision Arithmetic Library
 
171
Libs: %s
 
172
Cflags: %s
 
173
""" % (version, libs, cflags)
 
174
 
 
175
    pkg_file = open(os.path.join(directory, "gmp.pc"), 'w')
 
176
    pkg_file.write(pkg_file_str)
 
177
    pkg_file.close()
 
178
    print "done\n Found GMP and generated pkg-config file in\n '%s'" % directory
 
179
 
 
180
if __name__ == "__main__":
 
181
    generatePkgConf(directory=".")