Custom build commands to add std flags if required to compilation
()
| 185 | |
| 186 | |
| 187 | def get_build_overrides(): |
| 188 | """ |
| 189 | Custom build commands to add std flags if required to compilation |
| 190 | """ |
| 191 | from numpy.distutils.command.build_clib import build_clib |
| 192 | from numpy.distutils.command.build_ext import build_ext |
| 193 | from numpy._utils import _pep440 |
| 194 | |
| 195 | def try_compile(compiler, file, flags = [], verbose=False): |
| 196 | bk_ver = getattr(compiler, 'verbose', False) |
| 197 | compiler.verbose = verbose |
| 198 | try: |
| 199 | compiler.compile([file], extra_postargs=flags) |
| 200 | return True, '' |
| 201 | except CompileError as e: |
| 202 | return False, str(e) |
| 203 | finally: |
| 204 | compiler.verbose = bk_ver |
| 205 | |
| 206 | def flags_is_required(compiler, is_cpp, flags, code): |
| 207 | if is_cpp: |
| 208 | compiler = compiler.cxx_compiler() |
| 209 | suf = '.cpp' |
| 210 | else: |
| 211 | suf = '.c' |
| 212 | with tempfile.TemporaryDirectory() as temp_dir: |
| 213 | tmp_file = os.path.join(temp_dir, "test" + suf) |
| 214 | with open(tmp_file, "w+") as f: |
| 215 | f.write(code) |
| 216 | # without specify any flags in case of the required |
| 217 | # standard already supported by default, then there's |
| 218 | # no need for passing the flags |
| 219 | comp = try_compile(compiler, tmp_file) |
| 220 | if not comp[0]: |
| 221 | comp = try_compile(compiler, tmp_file, flags) |
| 222 | if not comp[0]: |
| 223 | # rerun to verbose the error |
| 224 | try_compile(compiler, tmp_file, flags, True) |
| 225 | if is_cpp: |
| 226 | raise RuntimeError( |
| 227 | "Broken toolchain during testing C++ compiler. \n" |
| 228 | "A compiler with support for C++17 language " |
| 229 | "features is required.\n" |
| 230 | f"Triggered the following error: {comp[1]}." |
| 231 | ) |
| 232 | else: |
| 233 | raise RuntimeError( |
| 234 | "Broken toolchain during testing C compiler. \n" |
| 235 | "A compiler with support for C99 language " |
| 236 | "features is required.\n" |
| 237 | f"Triggered the following error: {comp[1]}." |
| 238 | ) |
| 239 | return True |
| 240 | return False |
| 241 | |
| 242 | def std_cxx_flags(cmd): |
| 243 | compiler = cmd.compiler |
| 244 | flags = getattr(compiler, '__np_cache_cpp_flags', None) |