def get_compiler(): """ Determines the compiler that will be used to build extension modules. Returns ------- compiler : str The compiler option specified for the build, build_ext, or build_clib command; or the default compiler for the platform if none was specified. """ return new_compiler().compiler_type
def _detect_msvc_ver(self): stderr = "" try: msvc = new_compiler(compiler='msvc') msvc.initialize() PIPE = subprocess.PIPE kw = {'stdout': PIPE, 'stderr': PIPE, 'universal_newlines': True} with subprocess.Popen([msvc.cc], **kw) as process: _, stderr = process.communicate() except Exception: pass m = re.search(r"\s+(\d+(?:\.\d+)+)\s+", stderr) return tuple(map(int, m.group(1).split('.'))) if m else (16, 0)
def test_embeddable(self): import ctypes with open("embed_pil.c", "w") as fh: fh.write(""" #include "Python.h" int main(int argc, char* argv[]) { char *home = "%s"; wchar_t *whome = Py_DecodeLocale(home, NULL); Py_SetPythonHome(whome); Py_InitializeEx(0); Py_DECREF(PyImport_ImportModule("PIL.Image")); Py_Finalize(); Py_InitializeEx(0); Py_DECREF(PyImport_ImportModule("PIL.Image")); Py_Finalize(); PyMem_RawFree(whome); return 0; } """ % sys.prefix.replace("\\", "\\\\")) compiler = new_compiler() compiler.add_include_dir(sysconfig.get_config_var("INCLUDEPY")) libdir = sysconfig.get_config_var( "LIBDIR") or sysconfig.get_config_var("INCLUDEPY").replace( "include", "libs") compiler.add_library_dir(libdir) objects = compiler.compile(["embed_pil.c"]) compiler.link_executable(objects, "embed_pil") env = os.environ.copy() env["PATH"] = sys.prefix + ";" + env["PATH"] # do not display the Windows Error Reporting dialog ctypes.windll.kernel32.SetErrorMode(0x0002) process = subprocess.Popen(["embed_pil.exe"], env=env) process.communicate() assert process.returncode == 0
def check_openmp_support(openmp_flags=None): """ Check whether OpenMP test code can be compiled and run. Parameters ---------- openmp_flags : dict, optional This should be a dictionary with keys ``compiler_flags`` and ``linker_flags`` giving the compiliation and linking flags respectively. These are passed as `extra_postargs` to `compile()` and `link_executable()` respectively. If this is not set, the flags will be automatically determined using environment variables. Returns ------- result : bool `True` if the test passed, `False` otherwise. """ ccompiler = new_compiler() customize_compiler(ccompiler) if not openmp_flags: # customize_compiler() extracts info from os.environ. If certain keys # exist it uses these plus those from sysconfig.get_config_vars(). # If the key is missing in os.environ it is not extracted from # sysconfig.get_config_var(). E.g. 'LDFLAGS' get left out, preventing # clang from finding libomp.dylib because -L<path> is not passed to # linker. Call get_openmp_flags() to get flags missed by # customize_compiler(). openmp_flags = get_openmp_flags() compile_flags = openmp_flags.get('compiler_flags') link_flags = openmp_flags.get('linker_flags') with tempfile.TemporaryDirectory() as tmp_dir: start_dir = os.path.abspath('.') try: os.chdir(tmp_dir) # Write test program with open('test_openmp.c', 'w') as f: f.write(CCODE) os.mkdir('objects') # Compile, test program ccompiler.compile(['test_openmp.c'], output_dir='objects', extra_postargs=compile_flags) # Link test program objects = glob.glob( os.path.join('objects', '*' + ccompiler.obj_extension)) ccompiler.link_executable(objects, 'test_openmp', extra_postargs=link_flags) # Run test program output = subprocess.check_output('./test_openmp') output = output.decode(sys.stdout.encoding or 'utf-8').splitlines() if 'nthreads=' in output[0]: nthreads = int(output[0].strip().split('=')[1]) if len(output) == nthreads: is_openmp_supported = True else: log.warn( "Unexpected number of lines from output of test OpenMP " "program (output was {0})".format(output)) is_openmp_supported = False else: log.warn("Unexpected output from test OpenMP " "program (output was {0})".format(output)) is_openmp_supported = False except Exception: is_openmp_supported = False finally: os.chdir(start_dir) return is_openmp_supported