def build_regex_win32(using_cmake=False): # pragma: windows r"""Build the regex_win32 library.""" _regex_win32_dir = os.path.dirname(_regex_win32_lib) _regex_win32_cpp = os.path.join(_regex_win32_dir, 'regex_win32.cpp') _regex_win32_obj = os.path.join(_regex_win32_dir, 'regex_win32.obj') # Compile object cmd = [ 'cl', '/c', '/Zi', '/EHsc', '/I', '%s' % _regex_win32_dir, _regex_win32_cpp ] # '/out:%s' % _regex_win32_obj, comp_process = tools.popen_nobuffer(cmd, cwd=_regex_win32_dir) output, err = comp_process.communicate() exit_code = comp_process.returncode if exit_code != 0: # pragma: debug print(' '.join(cmd)) tools.print_encoded(output, end="") raise RuntimeError("Could not create regex_win32.obj") assert (os.path.isfile(_regex_win32_obj)) # Create library cmd = ['lib', '/out:%s' % _regex_win32_lib, _regex_win32_obj] comp_process = tools.popen_nobuffer(cmd, cwd=_regex_win32_dir) output, err = comp_process.communicate() exit_code = comp_process.returncode if exit_code != 0: # pragma: debug print(' '.join(cmd)) tools.print_encoded(output, end="") raise RuntimeError("Could not build regex_win32.lib") assert (os.path.isfile(_regex_win32_lib))
def run_cmake(self, target=None): r"""Run the cmake command on the source. Args: target (str, optional): Target to build. Raises: RuntimeError: If there is an error in running cmake. """ curdir = os.getcwd() os.chdir(self.sourcedir) if not os.path.isfile('CMakeLists.txt'): os.chdir(curdir) self.cleanup() raise IOError('No CMakeLists.txt file found in %s.' % self.sourcedir) # Configuration if target != 'clean': config_cmd = ['cmake'] + self.cmakeargs config_cmd += ['-H.', self.sourcedir, '-B%s' % self.builddir] self.debug(' '.join(config_cmd)) comp_process = tools.popen_nobuffer(config_cmd) output, err = comp_process.communicate() exit_code = comp_process.returncode if exit_code != 0: os.chdir(curdir) self.cleanup() self.error(backwards.bytes2unicode(output)) raise RuntimeError("CMake config failed with code %d." % exit_code) self.debug('Config output: \n%s' % output) # Build build_cmd = ['cmake', '--build', self.builddir, '--clean-first'] if self.target is not None: build_cmd += ['--target', self.target] self.info(' '.join(build_cmd)) comp_process = tools.popen_nobuffer(build_cmd) output, err = comp_process.communicate() exit_code = comp_process.returncode if exit_code != 0: os.chdir(curdir) self.error(backwards.bytes2unicode(output)) self.cleanup() raise RuntimeError("CMake build failed with code %d." % exit_code) self.debug('Build output: \n%s' % output) self.debug('Make complete') os.chdir(curdir)
def make_target(self, target): r"""Run the make command to make the target. Args: target (str): Target that should be made. Raises: RuntimeError: If there is an error in running the make. """ curdir = os.getcwd() os.chdir(self.makedir) if self.make_command == 'nmake': # pragma: windows make_opts = ['/NOLOGO', '/F'] else: make_opts = ['-f'] make_args = [self.make_command] + make_opts + [self.makefile, target] self.debug(' '.join(make_args)) if not os.path.isfile(self.makefile): os.chdir(curdir) raise IOError("Makefile %s not found" % self.makefile) comp_process = tools.popen_nobuffer(make_args) output, err = comp_process.communicate() exit_code = comp_process.returncode os.chdir(curdir) if exit_code != 0: self.error(output) raise RuntimeError("Make failed with code %d." % exit_code) self.debug('Make complete')
def build_regex_win32(): # pragma: windows r"""Build the regex_win32 library using cmake.""" # Configure project cmd = ['cmake', '-H.', '-Bbuild'] comp_process = tools.popen_nobuffer(cmd, cwd=_regex_win32_dir) output, err = comp_process.communicate() exit_code = comp_process.returncode if exit_code != 0: # pragma: debug print(' '.join(cmd)) tools.print_encoded(output, end="") raise RuntimeError("Could not config regex_win32") # Build project cmd = ['cmake', '--build', 'build', '--clean-first'] comp_process = tools.popen_nobuffer(cmd, cwd=_regex_win32_dir) output, err = comp_process.communicate() exit_code = comp_process.returncode if exit_code != 0: # pragma: debug print(' '.join(cmd)) tools.print_encoded(output, end="") raise RuntimeError("Could not build regex_win32") assert(os.path.isfile(_regex_win32_lib))
def test_popen_nobuffer(): r"""Test open of process without buffer.""" ans = os.getcwd() # + '\n' # ans = backwards.unicode2bytes(ans) # Test w/o shell if platform._is_win: # pragma: windows args = ['cmd', '/c', 'cd'] else: args = ['pwd'] p = tools.popen_nobuffer(args) out, err = p.communicate() res = out.decode('utf-8').splitlines()[0] nt.assert_equal(res, ans) # Test w/ shell if platform._is_win: # pragma: windows args = 'cd' else: args = 'pwd' p = tools.popen_nobuffer(args, shell=True) out, err = p.communicate() res = out.decode('utf-8').splitlines()[0] nt.assert_equal(res, ans)
def do_compile(src, out=None, cc=None, ccflags=None, ldflags=None, working_dir=None): r"""Compile a C/C++ program with necessary interface libraries. Args: src (list): List of source files. out (str, optional): Path where compile executable should be saved. Defaults to name of source file without extension on linux/osx and with .exe extension on windows. cc (str, optional): Compiler command. Defaults to gcc/g++ on linux/osx and cl on windows. ccflags (list, optional): Compiler flags. Defaults to []. ldflags (list, optional): Linker flags. Defaults to []. working_dir (str, optional): Working directory that input file paths are relative to. Defaults to current working directory. Returns: str: Full path to the compiled executable. """ if working_dir is None: # pragma: no cover working_dir = os.getcwd() if ccflags is None: # pragma: no cover ccflags = [] if ldflags is None: # pragma: no cover ldflags = [] _compile_flags, _linker_flags = get_flags() ldflags0 = _linker_flags if platform._is_win: # pragma: windows ccflags0 = ['/W4', '/Zi', "/EHsc"] else: ccflags0 = ['-g', '-Wall'] ccflags0 += _compile_flags # Change format for path (windows compat of examples) if platform._is_win: # pragma: windows for i in range(len(src)): src[i] = os.path.join(*(src[i].split('/'))) # Get primary file cfile = src[0] src_base, src_ext = os.path.splitext(cfile) # Select compiler if cc is None: if platform._is_win: # pragma: windows cc = 'cl' else: if src_ext in ['.c']: cc = 'gcc' else: cc = 'g++' # Create/fix executable if out is None: if platform._is_win: # pragma: windows osuffix = '_%s.exe' % src_ext[1:] else: osuffix = '_%s.out' % src_ext[1:] out = src_base + osuffix if not os.path.isabs(out): out = os.path.normpath(os.path.join(working_dir, out)) # Get flag specifying standard library if '++' in cc and (not platform._is_win): std_flag = None for a in ccflags: if a.startswith('-std='): std_flag = a break if std_flag is None: ccflags.append('-std=c++11') # Construct compile arguments compile_args = [cc] if not platform._is_win: compile_args += ["-o", out] compile_args += src + ccflags0 + ccflags if platform._is_win: # pragma: windows compile_args += ['/link', '/out:%s' % out] compile_args += ldflags0 + ldflags if os.path.isfile(out): os.remove(out) # Compile comp_process = tools.popen_nobuffer(compile_args) output, err = comp_process.communicate() exit_code = comp_process.returncode if exit_code != 0: # pragma: debug print(' '.join(compile_args)) tools.print_encoded(output, end="") raise RuntimeError("Compilation failed with code %d." % exit_code) assert (os.path.isfile(out)) return out