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

« back to all changes in this revision

Viewing changes to scons/simula-scons/simula_scons/pkgconfiggenerators/hypre.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 getHypreDir(sconsEnv=None):
 
9
    default = os.path.join(os.path.sep,"usr","local")
 
10
    hypre_dir = getPackageDir("hypre", sconsEnv=sconsEnv, default=default)
 
11
    return hypre_dir
 
12
 
 
13
def pkgVersion(compiler=None, linker=None, cflags=None, sconsEnv=None):
 
14
  # A test for checking the hypre version
 
15
  cpp_version_str = r"""
 
16
#include <stdio.h>
 
17
#include <HYPRE.h>
 
18
#include <HYPRE_config.h>
 
19
 
 
20
int main() {
 
21
  #ifdef HYPRE_PACKAGE_VERSION
 
22
    printf("%s",HYPRE_PACKAGE_VERSION);
 
23
  #endif
 
24
  return 0;
 
25
}
 
26
"""
 
27
  write_cppfile(cpp_version_str, "hypre_config_test_version.c")
 
28
 
 
29
  if not compiler:
 
30
    # FIXME: get_compiler() returns a C++ compiler. Should be a C compiler.
 
31
    compiler = get_compiler(sconsEnv)
 
32
  if not linker:
 
33
    linker = get_linker(sconsEnv)
 
34
  if not cflags:
 
35
    cflags = pkgCflags()
 
36
  cmdstr = "%s -o a.out %s hypre_config_test_version.c" % (compiler, cflags)
 
37
  compileFailed, cmdoutput = getstatusoutput(cmdstr)
 
38
  if compileFailed:
 
39
    remove_cfile("hypre_config_test_version.c")
 
40
    raise UnableToCompileException("Hypre", cmd=cmdstr,
 
41
                                   program=cpp_version_str, errormsg=cmdoutput)
 
42
  cmdstr = os.path.join(os.getcwd(), "a.out")
 
43
  runFailed, cmdoutput = getstatusoutput(cmdstr)
 
44
  if runFailed:
 
45
    remove_cfile("hypre_config_test_version.c", execfile=True)
 
46
    raise UnableToRunException("Hypre", errormsg=cmdoutput)
 
47
  hypre_version = cmdoutput
 
48
 
 
49
  remove_cfile("hypre_config_test_version.c", execfile=True)
 
50
 
 
51
  return hypre_version
 
52
 
 
53
def pkgLibs(compiler=None, cflags=None, sconsEnv=None):
 
54
  # A test to see if we need to link with blas and lapack
 
55
  cpp_libs_str = r"""
 
56
#include <stdio.h>
 
57
#include <HYPRE.h>
 
58
#include <HYPRE_config.h>
 
59
 
 
60
int main() {
 
61
  #ifdef HAVE_BLAS
 
62
    printf("-lblas\n");
 
63
  #endif
 
64
  #ifdef HAVE_LAPACK
 
65
    printf("-llapack\n");
 
66
  #endif
 
67
  return 0;
 
68
}
 
69
"""
 
70
  write_cppfile(cpp_libs_str, "hypre_config_test_libs.c")
 
71
 
 
72
  if not compiler:
 
73
    # FIXME: get_compiler() returns a C++ compiler. Do we need a C compiler?
 
74
    compiler = get_compiler(sconsEnv)
 
75
  if not cflags:
 
76
    cflags = pkgCflags()
 
77
  cmdstr = "%s -o a.out %s hypre_config_test_libs.c" % (compiler,cflags)
 
78
  compileFailed, cmdoutput = getstatusoutput(cmdstr)
 
79
  if compileFailed:
 
80
    remove_cfile("hypre_config_test_libs.c")
 
81
    raise UnableToCompileException("Hypre", cmd=cmdstr,
 
82
                                   program=cpp_libs_str, errormsg=cmdoutput)
 
83
 
 
84
  cmdstr = os.path.join(os.getcwd(), "a.out")
 
85
  runFailed, cmdoutput = getstatusoutput(cmdstr)
 
86
  if runFailed:
 
87
    remove_cfile("hypre_config_test_libs.c", execfile=True)
 
88
    raise UnableToRunException("Hypre", errormsg=cmdoutput)
 
89
  blaslapack_str = string.join(string.split(cmdoutput, '\n'))
 
90
    
 
91
  remove_cfile("hypre_config_test_libs.c", execfile=True)
 
92
 
 
93
  if get_architecture() == "darwin":
 
94
    libs_str = "-framework vecLib"
 
95
  else:
 
96
    libs_str = "-L%s %s" % (getAtlasDir(sconsEnv=sconsEnv),blaslapack_str)
 
97
 
 
98
  libs_dir = os.path.join(getHypreDir(sconsEnv=sconsEnv),"lib")
 
99
  libs_str += " -L%s -lHYPRE" % libs_dir
 
100
  
 
101
  return libs_str
 
102
 
 
103
def pkgCflags(sconsEnv=None):
 
104
  include_dir = os.path.join(getHypreDir(sconsEnv=sconsEnv),"include")
 
105
  cflags = "-I%s -fPIC" % include_dir
 
106
  if get_architecture() == "darwin":
 
107
    # Additional cflags required on Mac
 
108
    cflags += " -fno_common"
 
109
  return cflags
 
110
 
 
111
def pkgTests(forceCompiler=None, sconsEnv=None,
 
112
             version=None, libs=None, cflags=None, **kwargs):
 
113
  """Run the tests for this package.
 
114
     
 
115
     If Ok, return various variables, if not we will end with an exception.
 
116
     forceCompiler, if set, should be a tuple containing (compiler, linker)
 
117
     or just a string, which in that case will be used as both
 
118
  """
 
119
  # Set architecture up front.
 
120
  arch = get_architecture()
 
121
 
 
122
  if not forceCompiler:
 
123
    compiler = get_compiler(sconsEnv)
 
124
    linker = get_linker(sconsEnv)
 
125
  else:
 
126
    compiler, linker = set_forced_compiler(forceCompiler)
 
127
 
 
128
  if not cflags:
 
129
    cflags = pkgCflags(sconsEnv=sconsEnv)
 
130
  if not version:
 
131
    version = pkgVersion(compiler=compiler, cflags=cflags, sconsEnv=sconsEnv)
 
132
  if not libs:
 
133
    libs = pkgLibs(compiler=compiler, cflags=cflags, sconsEnv=sconsEnv)
 
134
 
 
135
  # A test to see if we can actually use the libHYPRE
 
136
  cpp_testlib_str = r"""
 
137
#include <stdio.h>
 
138
#include <HYPRE.h>
 
139
#include <HYPRE_config.h>
 
140
#include <_hypre_IJ_mv.h>
 
141
 
 
142
int main(int argc, char *argv[])
 
143
{
 
144
#ifdef HYPRE_SEQUENTIAL
 
145
  MPI_Comm comm = MPI_COMM_NULL;
 
146
  HYPRE_IJMatrix A;
 
147
  HYPRE_IJMatrixCreate(comm, 0, 10, 0, 10, &A);
 
148
#else
 
149
  int rank, nprocs;
 
150
 
 
151
  MPI_Init(&argc,&argv);
 
152
  MPI_Comm_size(MPI_COMM_WORLD,&nprocs);
 
153
  MPI_Comm_rank(MPI_COMM_WORLD,&rank);
 
154
  HYPRE_IJMatrix A;
 
155
  HYPRE_IJMatrixCreate(MPI_COMM_WORLD, 0, 10, 0, 10, &A);
 
156
  MPI_Finalize();
 
157
#endif
 
158
  return 0;
 
159
 
160
"""
 
161
  write_cppfile(cpp_testlib_str, "hypre_config_test_libstest.c")
 
162
 
 
163
  cmdstr = "%s %s -c hypre_config_test_libstest.c" % (compiler,cflags)
 
164
  compileFailed, cmdoutput = getstatusoutput(cmdstr)
 
165
  if compileFailed:
 
166
    remove_cfile("hypre_config_test_libstest.c")
 
167
    raise UnableToCompileException("Hypre", cmd=cmdstr,
 
168
                                   program=cpp_testlib_str, errormsg=cmdoutput)
 
169
  
 
170
  cmdstr = "%s -o a.out %s hypre_config_test_libstest.o" % (linker,libs)
 
171
  linkFailed, cmdoutput = getstatusoutput(cmdstr)
 
172
  if linkFailed:
 
173
    remove_cfile("hypre_config_test_libstest.c", ofile=True)
 
174
    raise UnableToLinkException("Hypre", cmd=cmdstr,
 
175
                                program=cpp_testlib_str, errormsg=cmdoutput)
 
176
 
 
177
  cmdstr = os.path.join(os.getcwd(), "a.out")
 
178
  runFailed, cmdoutput = getstatusoutput(cmdstr)
 
179
  if runFailed:
 
180
    remove_cfile("hypre_config_test_libstest.c", execfile=True, ofile=True)
 
181
    raise UnableToRunException("Hypre", errormsg=cmdoutput)
 
182
  
 
183
  remove_cfile("hypre_config_test_libstest.c", execfile=True, ofile=True)
 
184
 
 
185
  return version, libs, cflags
 
186
 
 
187
def generatePkgConf(directory=suitablePkgConfDir(), sconsEnv=None, **kwargs):
 
188
  """Generate a pkg-config file for Hypre."""
 
189
  
 
190
  version, libs, cflags = pkgTests(sconsEnv=sconsEnv)
 
191
 
 
192
  pkg_file_str = r"""Name: Hypre
 
193
Version: %s
 
194
Description: The Hypre project - http://www.llnl.gov/casc/hypre/software.html
 
195
Libs: %s
 
196
Cflags: %s
 
197
""" % (version, libs, cflags)
 
198
  
 
199
  pkg_file = open(os.path.join(directory,"hypre.pc"), 'w')
 
200
  pkg_file.write(pkg_file_str)
 
201
  pkg_file.close()
 
202
  print "done\n Found Hypre and generated pkg-config file in \n '%s'" % directory
 
203
 
 
204
if __name__ == "__main__":
 
205
  generatePkgConf(directory=".")