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