1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | from distutils.core import setup, Extension |
---|
4 | import distutils.sysconfig |
---|
5 | import os, sys |
---|
6 | import popen2 |
---|
7 | import string |
---|
8 | import tempfile |
---|
9 | |
---|
10 | def setup_compiler(): |
---|
11 | distutils.sysconfig.get_config_vars() |
---|
12 | config_vars = distutils.sysconfig._config_vars |
---|
13 | if sys.platform == 'sunos5': |
---|
14 | config_vars['LDSHARED'] = "gcc -G" |
---|
15 | config_vars['CCSHARED'] = "" |
---|
16 | |
---|
17 | |
---|
18 | def uniq_arr(arr): |
---|
19 | """Remove repeated values from an array and return new array.""" |
---|
20 | ret = [] |
---|
21 | for i in arr: |
---|
22 | if i not in ret: |
---|
23 | ret.append(i) |
---|
24 | return ret |
---|
25 | |
---|
26 | |
---|
27 | def _run_command(cmd): |
---|
28 | out_file, in_file, err_file = popen2.popen3(cmd) |
---|
29 | output = out_file.read() + err_file.read() |
---|
30 | out_file.close() |
---|
31 | in_file.close() |
---|
32 | err_file.close() |
---|
33 | # need this hack to get the exit status |
---|
34 | out_file = os.popen(cmd) |
---|
35 | if out_file.close(): |
---|
36 | # close returns exit status of command. |
---|
37 | return "" |
---|
38 | else: |
---|
39 | # no errors, out_file.close() returns None. |
---|
40 | return output |
---|
41 | |
---|
42 | |
---|
43 | def _get_mpi_cmd(): |
---|
44 | """Returns the output of the command used to compile using |
---|
45 | mpicc.""" |
---|
46 | # LAM |
---|
47 | output = _run_command("mpicc -showme") |
---|
48 | if output: |
---|
49 | return output |
---|
50 | |
---|
51 | # MPICH |
---|
52 | # works with MPICH version 1.2.1 (on Debian) |
---|
53 | output = _run_command("mpicc -compile_info -link_info") |
---|
54 | if output: |
---|
55 | return output |
---|
56 | |
---|
57 | # old version of MPICH needs this hack. |
---|
58 | tmp_base = tempfile.mktemp() |
---|
59 | tmp_c = tmp_base + ".c" |
---|
60 | tmp_o = tmp_base + ".o" |
---|
61 | tmp_file = open(tmp_c, "w") |
---|
62 | tmp_file.write('#include "mpi.h"\nint main(){return 0;}\n') |
---|
63 | tmp_file.close() |
---|
64 | output = _run_command("mpicc -show;"\ |
---|
65 | "mpicc -echo -c %s -o %s"%(tmp_c, tmp_o)) |
---|
66 | os.remove(tmp_c) |
---|
67 | if os.path.exists(tmp_o): |
---|
68 | os.remove(tmp_o) |
---|
69 | if output: |
---|
70 | return output |
---|
71 | else: |
---|
72 | return "" |
---|
73 | |
---|
74 | |
---|
75 | def get_mpi_flags(): |
---|
76 | output = _get_mpi_cmd() |
---|
77 | if not output: |
---|
78 | if sys.platform=='win32': |
---|
79 | output = "gcc -L$MPICH_DIR\SDK.gcc\lib -lmpich -I$MPICH_DIR\SDK.gcc\include" |
---|
80 | else: |
---|
81 | output = "cc -lmpi" |
---|
82 | |
---|
83 | # now get the include, library dirs and the libs to link with. |
---|
84 | flags = string.split(output) |
---|
85 | flags = uniq_arr(flags) # remove repeated values. |
---|
86 | inc_dirs = [] |
---|
87 | lib_dirs = [] |
---|
88 | libs = [] |
---|
89 | def_macros = [] |
---|
90 | undef_macros = [] |
---|
91 | for f in flags: |
---|
92 | if f[:2] == '-I': |
---|
93 | inc_dirs.append(f[2:]) |
---|
94 | elif f[:2] == '-L': |
---|
95 | lib_dirs.append(f[2:]) |
---|
96 | elif f[:2] == '-l': |
---|
97 | libs.append(f[2:]) |
---|
98 | elif f[:2] == '-U': |
---|
99 | undef_macros.append(f[2:]) |
---|
100 | elif f[:2] == '-D': |
---|
101 | tmp = string.split(f[2:], '=') |
---|
102 | if len(tmp) == 1: |
---|
103 | def_macros.append((tmp[0], None)) |
---|
104 | else: |
---|
105 | def_macros.append(tuple(tmp)) |
---|
106 | return {'inc_dirs': inc_dirs, 'lib_dirs': lib_dirs, 'libs':libs, |
---|
107 | 'def_macros': def_macros, 'undef_macros': undef_macros} |
---|
108 | |
---|
109 | |
---|
110 | if __name__ == "__main__": |
---|
111 | setup_compiler() |
---|
112 | |
---|
113 | mpi_flags = get_mpi_flags() |
---|
114 | |
---|
115 | setup(name="Pypar", |
---|
116 | version="1.5", |
---|
117 | description="Pypar - Parallel Python", |
---|
118 | long_description="Pypar - Parallel Python, no-frills MPI interface", |
---|
119 | author="Ole Nielsen", |
---|
120 | author_email="Ole.Nielsen@anu.edu.au", |
---|
121 | url="http://datamining.anu.edu.au/pypar", |
---|
122 | package_dir = {'': 'lib'}, |
---|
123 | packages = ['pypar'], |
---|
124 | ext_modules = [Extension('pypar.mpiext', |
---|
125 | ['lib/pypar/mpiext.c'], |
---|
126 | include_dirs=mpi_flags['inc_dirs'], |
---|
127 | library_dirs=mpi_flags['lib_dirs'], |
---|
128 | libraries=mpi_flags['libs'], |
---|
129 | define_macros=mpi_flags['def_macros'], |
---|
130 | undef_macros=mpi_flags['undef_macros'])] |
---|
131 | ) |
---|