|
| 1 | +import os |
| 2 | +import sys |
| 3 | + |
| 4 | + |
| 5 | +def module_exists(module_name): |
| 6 | + try: |
| 7 | + __import__(module_name) |
| 8 | + except ImportError: |
| 9 | + return False |
| 10 | + else: |
| 11 | + return True |
| 12 | + |
| 13 | + |
| 14 | +def check_cmake_exists(cmake_command): |
| 15 | + """ |
| 16 | + Check whether CMake is installed. If not, print |
| 17 | + informative error message and quits. |
| 18 | + """ |
| 19 | + from subprocess import Popen, PIPE |
| 20 | + |
| 21 | + p = Popen('{0} --version'.format(cmake_command), |
| 22 | + shell=True, |
| 23 | + stdin=PIPE, |
| 24 | + stdout=PIPE) |
| 25 | + if not ('cmake version' in p.communicate()[0].decode('UTF-8')): |
| 26 | + sys.stderr.write(' This code is built using CMake\n\n') |
| 27 | + sys.stderr.write(' CMake is not found\n') |
| 28 | + sys.stderr.write(' get CMake at http://www.cmake.org/\n') |
| 29 | + sys.stderr.write(' on many clusters CMake is installed\n') |
| 30 | + sys.stderr.write(' but you have to load it first:\n') |
| 31 | + sys.stderr.write(' $ module load cmake\n') |
| 32 | + sys.exit(1) |
| 33 | + |
| 34 | + |
| 35 | +def setup_build_path(build_path): |
| 36 | + """ |
| 37 | + Create build directory. If this already exists, print informative |
| 38 | + error message and quit. |
| 39 | + """ |
| 40 | + if os.path.isdir(build_path): |
| 41 | + fname = os.path.join(build_path, 'CMakeCache.txt') |
| 42 | + if os.path.exists(fname): |
| 43 | + sys.stderr.write('aborting setup\n') |
| 44 | + sys.stderr.write('build directory {0} which contains CMakeCache.txt already exists\n'.format(build_path)) |
| 45 | + sys.stderr.write('remove the build directory and then rerun setup\n') |
| 46 | + sys.exit(1) |
| 47 | + else: |
| 48 | + os.makedirs(build_path, 0o755) |
| 49 | + |
| 50 | + |
| 51 | +def test_adapt_cmake_command_to_platform(): |
| 52 | + |
| 53 | + cmake_command = "FC=foo CC=bar CXX=RABOOF cmake -DTHIS -DTHAT='this and that cmake' .." |
| 54 | + res = adapt_cmake_command_to_platform(cmake_command, 'linux') |
| 55 | + assert res == cmake_command |
| 56 | + res = adapt_cmake_command_to_platform(cmake_command, 'win32') |
| 57 | + assert res == "set FC=foo && set CC=bar && set CXX=RABOOF && cmake -DTHIS -DTHAT='this and that cmake' .." |
| 58 | + |
| 59 | + cmake_command = "cmake -DTHIS -DTHAT='this and that cmake' .." |
| 60 | + res = adapt_cmake_command_to_platform(cmake_command, 'linux') |
| 61 | + assert res == cmake_command |
| 62 | + res = adapt_cmake_command_to_platform(cmake_command, 'win32') |
| 63 | + assert res == cmake_command |
| 64 | + |
| 65 | + |
| 66 | +def adapt_cmake_command_to_platform(cmake_command, platform): |
| 67 | + """ |
| 68 | + Adapt CMake command to MS Windows platform. |
| 69 | + """ |
| 70 | + if platform == 'win32': |
| 71 | + pos = cmake_command.find('cmake') |
| 72 | + s = ['set {0} &&'.format(e) for e in cmake_command[:pos].split()] |
| 73 | + s.append(cmake_command[pos:]) |
| 74 | + return ' '.join(s) |
| 75 | + else: |
| 76 | + return cmake_command |
| 77 | + |
| 78 | + |
| 79 | +def run_cmake(command, build_path, default_build_path): |
| 80 | + """ |
| 81 | + Execute CMake command. |
| 82 | + """ |
| 83 | + from subprocess import Popen, PIPE |
| 84 | + from shutil import rmtree |
| 85 | + |
| 86 | + topdir = os.getcwd() |
| 87 | + os.chdir(build_path) |
| 88 | + p = Popen(command, |
| 89 | + shell=True, |
| 90 | + stdin=PIPE, |
| 91 | + stdout=PIPE, |
| 92 | + stderr=PIPE) |
| 93 | + stdout_coded, stderr_coded = p.communicate() |
| 94 | + stdout = stdout_coded.decode('UTF-8') |
| 95 | + stderr = stderr_coded.decode('UTF-8') |
| 96 | + |
| 97 | + # print cmake output to screen |
| 98 | + print(stdout) |
| 99 | + |
| 100 | + if stderr: |
| 101 | + sys.stderr.write(stderr) |
| 102 | + sys.exit(1) |
| 103 | + |
| 104 | + # write cmake output to file |
| 105 | + with open('cmake_output', 'w') as f: |
| 106 | + f.write(stdout) |
| 107 | + |
| 108 | + # change directory and return |
| 109 | + os.chdir(topdir) |
| 110 | + |
| 111 | + if 'Configuring incomplete' in stdout: |
| 112 | + # configuration was not successful |
| 113 | + if (build_path == default_build_path): |
| 114 | + # remove build_path iff not set by the user |
| 115 | + # otherwise removal can be dangerous |
| 116 | + rmtree(default_build_path) |
| 117 | + else: |
| 118 | + # configuration was successful |
| 119 | + save_setup_command(sys.argv, build_path) |
| 120 | + print_build_help(build_path, default_build_path) |
| 121 | + |
| 122 | + |
| 123 | +def print_build_help(build_path, default_build_path): |
| 124 | + """ |
| 125 | + Print help text after configuration step is done. |
| 126 | + """ |
| 127 | + print(' configure step is done') |
| 128 | + print(' now you need to compile the sources:') |
| 129 | + if (build_path == default_build_path): |
| 130 | + print(' $ cd build') |
| 131 | + else: |
| 132 | + print(' $ cd ' + build_path) |
| 133 | + print(' $ make') |
| 134 | + |
| 135 | + |
| 136 | +def save_setup_command(argv, build_path): |
| 137 | + """ |
| 138 | + Save setup command to a file. |
| 139 | + """ |
| 140 | + file_name = os.path.join(build_path, 'setup_command') |
| 141 | + with open(file_name, 'w') as f: |
| 142 | + f.write(' '.join(argv[:]) + '\n') |
| 143 | + |
| 144 | + |
| 145 | +def configure(root_directory, build_path, cmake_command, only_show): |
| 146 | + """ |
| 147 | + Main configure function. |
| 148 | + """ |
| 149 | + default_build_path = os.path.join(root_directory, 'build') |
| 150 | + |
| 151 | + # check that CMake is available, if not stop |
| 152 | + check_cmake_exists('cmake') |
| 153 | + |
| 154 | + # deal with build path |
| 155 | + if build_path is None: |
| 156 | + build_path = default_build_path |
| 157 | + if not only_show: |
| 158 | + setup_build_path(build_path) |
| 159 | + |
| 160 | + cmake_command = adapt_cmake_command_to_platform(cmake_command, sys.platform) |
| 161 | + |
| 162 | + print('{0}\n'.format(cmake_command)) |
| 163 | + if only_show: |
| 164 | + sys.exit(0) |
| 165 | + |
| 166 | + run_cmake(cmake_command, build_path, default_build_path) |
0 commit comments