1 | import os |
---|
2 | import sys |
---|
3 | import py_compile |
---|
4 | |
---|
5 | # Function to build a .pyc from a .py |
---|
6 | def build_pyc(target, source, env): |
---|
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]) |
---|
16 | return None |
---|
17 | |
---|
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 | |
---|
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 | |
---|
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') |
---|
44 | opts.Add('METIS_DIR', 'Location of the metis directory relative to the pymetis directory') |
---|
45 | opts.Add('OPTIMISATION_LEVEL', 'Optimisation level for the building of .pyo files. Must be 1 or 2') |
---|
46 | |
---|
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', |
---|
54 | 'Location to install compiled sources', |
---|
55 | os.path.join(sys.exec_prefix, 'lib', 'python' + sys.version[:3], 'site-packages'))) |
---|
56 | |
---|
57 | opts.Add(BoolOption('INSTALL_PYTHON_SOURCE', |
---|
58 | 'Install the .py files as well as the .pyc/.pyo files', |
---|
59 | 0)) |
---|
60 | |
---|
61 | env = Environment(options = opts) |
---|
62 | env.Append(BUILDERS={'Pyc' : pyc_builder, |
---|
63 | 'Pyo' : pyo_builder}) |
---|
64 | |
---|
65 | Help(opts.GenerateHelpText(env)) |
---|
66 | |
---|
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 | |
---|
70 | # Where to find the Python.h |
---|
71 | if sys.platform == 'win32': |
---|
72 | # Prefer MinGW over MSVC |
---|
73 | Tool('mingw')(env) |
---|
74 | |
---|
75 | python_include = os.path.join(sys.exec_prefix, 'include') |
---|
76 | # Windows installs need to be told the lib path and the python library name |
---|
77 | # else it won't work right. |
---|
78 | python_libdir = os.path.join(sys.exec_prefix, 'libs') |
---|
79 | env.Append(LIBPATH=[python_libdir]) |
---|
80 | python_libname = 'python%d%d' % (sys.version_info[0:2]) |
---|
81 | env.Append(LIBS=[python_libname]) |
---|
82 | else: |
---|
83 | python_include = os.path.join(sys.exec_prefix, 'include', 'python' + sys.version[:3]) |
---|
84 | |
---|
85 | # Check existence of Python.h |
---|
86 | headerfile = python_include + os.sep + 'Python.h' |
---|
87 | try: |
---|
88 | open(headerfile, 'r') |
---|
89 | except: |
---|
90 | raise """Did not find Python header file %s. |
---|
91 | Make sure files for Python C-extensions are installed. |
---|
92 | In debian linux, for example, you need to install a |
---|
93 | package called something like python2.3-dev""" %headerfile |
---|
94 | |
---|
95 | env.Append(CPPPATH=[python_include]) |
---|
96 | |
---|
97 | # Set appropriate CCFLAGS for the compiler. |
---|
98 | if env['CC'] == 'gcc': |
---|
99 | env.Append(CCFLAGS=['${GCCFLAGS}']) |
---|
100 | elif env['CC'] == 'cl': |
---|
101 | env.Append(CCFLAGS=['${MSVCFLAGS}']) |
---|
102 | |
---|
103 | Export('env') |
---|
104 | |
---|
105 | # Build .pyc and .pyo files of every .py in here and below. |
---|
106 | files = [] |
---|
107 | #dirs = filter(os.path.isdir, os.listdir('.')) |
---|
108 | #Only build the source/anuga directory for now |
---|
109 | dirs = [os.path.join('source', 'anuga')] |
---|
110 | while(dirs != []): |
---|
111 | dirs += filter(os.path.isdir, map(lambda x : os.path.join(dirs[0], x), os.listdir(dirs[0]))) |
---|
112 | files += filter(lambda x : x[-3:] == '.py', map(lambda x : os.path.join(dirs[0], x), os.listdir(dirs[0]))) |
---|
113 | dirs = dirs[1:] |
---|
114 | for x in files: |
---|
115 | env.Pyc(x + 'c', x) |
---|
116 | env.Pyo(x + 'o', x) |
---|
117 | # We have to cut the first character off the result of os.path.dirname(x).replace('anuga_core, '') |
---|
118 | # or else we will start taking paths relative to the root directory. |
---|
119 | instdir = os.path.join(env['PREFIX'], os.path.dirname(x).replace('source', '')[1:]) |
---|
120 | env.Install(instdir, x + 'c') |
---|
121 | env.Install(instdir, x + 'o') |
---|
122 | if env['INSTALL_PYTHON_SOURCE']: |
---|
123 | env.Install(instdir, x) |
---|
124 | |
---|
125 | env.Alias('install', '${PREFIX}') |
---|
126 | |
---|
127 | SConscript(['source/anuga/SConscript']) |
---|