[3353] | 1 | import os |
---|
| 2 | import sys |
---|
[3359] | 3 | import py_compile |
---|
[3353] | 4 | |
---|
[3359] | 5 | # Function to build a .pyc from a .py |
---|
| 6 | def build_pyc(target, source, env): |
---|
[3544] | 7 | python = sys.executable |
---|
| 8 | command = "import py_compile; py_compile.compile('%s', doraise=True)" |
---|
| 9 | if sys.platform == 'win32': |
---|
| 10 | command %= str(source[0]).replace('\\', '\\\\') |
---|
| 11 | else: |
---|
| 12 | command %= str(source[0]) |
---|
| 13 | rv = os.system('%s -c "%s"' % (python, command)) |
---|
| 14 | if not rv == 0: |
---|
| 15 | raise SyntaxError, "Could not compile %s" % str(source[0]) |
---|
[3359] | 16 | return None |
---|
| 17 | |
---|
[3544] | 18 | # Function to build a .pyo from a .py |
---|
| 19 | def build_pyo(target, source, env): |
---|
| 20 | python = sys.executable |
---|
| 21 | command = "import py_compile; py_compile.compile('%s', doraise=True)" |
---|
| 22 | if sys.platform == 'win32': |
---|
| 23 | command %= str(source[0]).replace('\\', '\\\\') |
---|
| 24 | else: |
---|
| 25 | command %= str(source[0]) |
---|
| 26 | options = '-' + 'O' * env['OPTIMISATION_LEVEL'] |
---|
| 27 | rv = os.system('%s %s -c "%s"' % (python, options, command)) |
---|
| 28 | if not rv == 0: |
---|
| 29 | raise SyntaxError, "Could not compile %s" % str(source[0]) |
---|
| 30 | return None |
---|
| 31 | |
---|
[3545] | 32 | # Create the builders |
---|
| 33 | pyc_builder = Builder(action = build_pyc, |
---|
| 34 | suffix = '.pyc', |
---|
| 35 | src_suffix = '.py') |
---|
| 36 | pyo_builder = Builder(action = build_pyo, |
---|
| 37 | suffix = '.pyo', |
---|
| 38 | src_suffix = '.py') |
---|
| 39 | |
---|
[3353] | 40 | # Read in build options |
---|
| 41 | opts = Options('build_options.py') |
---|
| 42 | opts.Add('GCCFLAGS', 'Flags passed to GCC') |
---|
| 43 | opts.Add('MSVCFLAGS', 'Flags passed to the MSVC compiler') |
---|
[3414] | 44 | opts.Add('METIS_DIR', 'Location of the metis directory relative to the pymetis directory') |
---|
[3544] | 45 | opts.Add('OPTIMISATION_LEVEL', 'Optimisation level for the building of .pyo files. Must be 1 or 2') |
---|
[3353] | 46 | |
---|
[3545] | 47 | # Windows' site packages are in a different location to those on Linux |
---|
| 48 | if sys.platform == 'win32': |
---|
| 49 | opts.Add(PathOption('PREFIX', |
---|
| 50 | 'Location to install compiled sources', |
---|
| 51 | os.path.join(sys.exec_prefix, 'lib', 'site-packages'))) |
---|
| 52 | else: |
---|
| 53 | opts.Add(PathOption('PREFIX', |
---|
[3546] | 54 | 'Location to install compiled sources', |
---|
[3545] | 55 | os.path.join(sys.exec_prefix, 'lib', 'python' + sys.version[:3], 'site-packages'))) |
---|
[3359] | 56 | |
---|
[3545] | 57 | opts.Add(BoolOption('INSTALL_PYTHON_SOURCE', |
---|
| 58 | 'Install the .py files as well as the .pyc/.pyo files', |
---|
| 59 | 0)) |
---|
| 60 | |
---|
[3353] | 61 | env = Environment(options = opts) |
---|
[3544] | 62 | env.Append(BUILDERS={'Pyc' : pyc_builder, |
---|
| 63 | 'Pyo' : pyo_builder}) |
---|
[3353] | 64 | |
---|
| 65 | Help(opts.GenerateHelpText(env)) |
---|
| 66 | |
---|
[3544] | 67 | if not (env['OPTIMISATION_LEVEL'] == 1 or env['OPTIMISATION_LEVEL'] == 2): |
---|
| 68 | raise ValueError, "OPTIMISATION_LEVEL must be between 1 and 2 inclusive" |
---|
| 69 | |
---|
[3353] | 70 | # Where to find the Python.h |
---|
| 71 | if sys.platform == 'win32': |
---|
[3810] | 72 | # If we're on Windows, we need to know to work around the .exp file problem with MinGW+MSVC |
---|
| 73 | if env['CC'] == 'cl': |
---|
| 74 | env['no_import_lib'] = 1 |
---|
[3353] | 75 | # Prefer MinGW over MSVC |
---|
| 76 | Tool('mingw')(env) |
---|
[3682] | 77 | |
---|
[3353] | 78 | python_include = os.path.join(sys.exec_prefix, 'include') |
---|
| 79 | # Windows installs need to be told the lib path and the python library name |
---|
| 80 | # else it won't work right. |
---|
| 81 | python_libdir = os.path.join(sys.exec_prefix, 'libs') |
---|
| 82 | env.Append(LIBPATH=[python_libdir]) |
---|
| 83 | python_libname = 'python%d%d' % (sys.version_info[0:2]) |
---|
| 84 | env.Append(LIBS=[python_libname]) |
---|
| 85 | else: |
---|
[3545] | 86 | python_include = os.path.join(sys.exec_prefix, 'include', 'python' + sys.version[:3]) |
---|
[3353] | 87 | |
---|
| 88 | # Check existence of Python.h |
---|
| 89 | headerfile = python_include + os.sep + 'Python.h' |
---|
| 90 | try: |
---|
| 91 | open(headerfile, 'r') |
---|
| 92 | except: |
---|
| 93 | raise """Did not find Python header file %s. |
---|
| 94 | Make sure files for Python C-extensions are installed. |
---|
| 95 | In debian linux, for example, you need to install a |
---|
| 96 | package called something like python2.3-dev""" %headerfile |
---|
| 97 | |
---|
| 98 | env.Append(CPPPATH=[python_include]) |
---|
| 99 | |
---|
| 100 | # Set appropriate CCFLAGS for the compiler. |
---|
| 101 | if env['CC'] == 'gcc': |
---|
| 102 | env.Append(CCFLAGS=['${GCCFLAGS}']) |
---|
| 103 | elif env['CC'] == 'cl': |
---|
| 104 | env.Append(CCFLAGS=['${MSVCFLAGS}']) |
---|
| 105 | |
---|
[3604] | 106 | # Suppress the "lib" prefix on built files |
---|
| 107 | env['SHLIBPREFIX'] = "" |
---|
| 108 | |
---|
[3875] | 109 | anuga_root = '.' |
---|
[3599] | 110 | install_root = os.path.join(env['PREFIX'], 'anuga') |
---|
[3353] | 111 | |
---|
[3523] | 112 | # Build .pyc and .pyo files of every .py in here and below. |
---|
[3359] | 113 | files = [] |
---|
[3875] | 114 | dirs = filter(os.path.isdir, os.listdir('.')) |
---|
[3359] | 115 | while(dirs != []): |
---|
[3545] | 116 | dirs += filter(os.path.isdir, map(lambda x : os.path.join(dirs[0], x), os.listdir(dirs[0]))) |
---|
| 117 | files += filter(lambda x : x[-3:] == '.py', map(lambda x : os.path.join(dirs[0], x), os.listdir(dirs[0]))) |
---|
[3359] | 118 | dirs = dirs[1:] |
---|
| 119 | for x in files: |
---|
| 120 | env.Pyc(x + 'c', x) |
---|
[3544] | 121 | env.Pyo(x + 'o', x) |
---|
[3875] | 122 | env.Install(os.path.join(install_root, os.path.dirname(x)), x + 'c') |
---|
| 123 | env.Install(os.path.join(install_root, os.path.dirname(x)), x + 'o') |
---|
[3545] | 124 | if env['INSTALL_PYTHON_SOURCE']: |
---|
[3875] | 125 | env.Install(os.path.join(install_root, os.path.dirname(x)), x) |
---|
[3359] | 126 | |
---|
[3599] | 127 | # Define the install target |
---|
[3545] | 128 | env.Alias('install', '${PREFIX}') |
---|
[3599] | 129 | |
---|
| 130 | # Mesh_engine |
---|
| 131 | mesh_env = env.Copy() |
---|
| 132 | mesh_env.Append(CPPDEFINES=[('TRILIBRARY', 1), ('NO_TIMER', 1)]) |
---|
| 133 | |
---|
| 134 | mesh_dir = os.path.join(anuga_root, 'mesh_engine') |
---|
| 135 | mesh_install_dir = os.path.join(install_root, 'mesh_engine') |
---|
| 136 | |
---|
[3605] | 137 | triang = mesh_env.SharedLibrary(os.path.join(mesh_dir, 'triang'), map(lambda s: os.path.join(mesh_dir, s), ['triangle.c', 'triang.c'])) |
---|
| 138 | triangle = mesh_env.SharedLibrary(os.path.join(mesh_dir, 'triangle'), map(lambda s: os.path.join(mesh_dir, s), ['triangle.c'])) |
---|
| 139 | env.Install(mesh_install_dir, triang) |
---|
| 140 | env.Install(mesh_install_dir, triangle) |
---|
| 141 | |
---|
[3599] | 142 | # Utilities |
---|
| 143 | util_dir = os.path.join(anuga_root, 'utilities') |
---|
| 144 | util_install_dir = os.path.join(install_root, 'utilities') |
---|
[3605] | 145 | |
---|
[3599] | 146 | env.Install(util_install_dir, env.SharedLibrary(os.path.join(util_dir, 'polygon_ext'), |
---|
| 147 | map(lambda s: os.path.join(util_dir, s), ['polygon_ext.c']))) |
---|
| 148 | env.Install(util_install_dir, env.SharedLibrary(os.path.join(util_dir, 'util_ext'), |
---|
| 149 | map(lambda s: os.path.join(util_dir, s), ['util_ext.c']))) |
---|
| 150 | env.Install(util_install_dir, env.SharedLibrary(os.path.join(util_dir, 'sparse_ext'), |
---|
| 151 | map(lambda s: os.path.join(util_dir, s), ['sparse_ext.c']))) |
---|
| 152 | |
---|
| 153 | # Abstract_2d_finite_volumes |
---|
| 154 | a2fv_env = env.Copy() |
---|
| 155 | a2fv_env.Append(CPPPATH=[os.path.join('#', anuga_root, 'utilities')]) |
---|
| 156 | |
---|
| 157 | a2fv_dir = os.path.join(anuga_root, 'abstract_2d_finite_volumes') |
---|
[3752] | 158 | |
---|
[3599] | 159 | a2fv_install_dir = os.path.join(install_root, 'abstract_2d_finite_volumes') |
---|
[3605] | 160 | |
---|
[3599] | 161 | env.Install(a2fv_install_dir, a2fv_env.SharedLibrary(os.path.join(a2fv_dir, 'quantity_ext'), |
---|
| 162 | map(lambda s: os.path.join(a2fv_dir, s), ['quantity_ext.c']))) |
---|
| 163 | |
---|
| 164 | # Shallow_water |
---|
| 165 | sw_env = env.Copy() |
---|
| 166 | sw_env.Append(CPPPATH=[os.path.join('#', anuga_root, 'utilities')]) |
---|
| 167 | |
---|
| 168 | sw_dir = os.path.join(anuga_root, 'shallow_water') |
---|
| 169 | sw_install_dir = os.path.join(install_root, 'shallow_water') |
---|
[3605] | 170 | |
---|
[3599] | 171 | env.Install(sw_install_dir, sw_env.SharedLibrary(os.path.join(sw_dir, 'shallow_water_ext'), |
---|
| 172 | map(lambda s: os.path.join(sw_dir, s), ['shallow_water_ext.c']))) |
---|
[3752] | 173 | env.Install(a2fv_install_dir, sw_env.SharedLibrary(os.path.join(sw_dir, 'shallow_water_kinetic'), |
---|
| 174 | map(lambda s: os.path.join(sw_dir, s), ['shallow_water_kinetic_ext.c']))) |
---|