def so_dirs_path(self, flags): """ Assemble path for shared libraries in nonstandard directories. """ retval = separate_args(self.pkg.config.rpath) i = len(retval) - 1 if i < 0: return count = 0 pkg_lib = os.path.join(self.pkg.get_dir('libdir'), self.pkg.name) if os.path.isdir(pkg_lib): retval[i] += ":" + pkg_lib count += 1 if type(flags) == str: args = separate_args(flags) else: args = flags for arg in args: if (arg[0:2] == '-L' and arg[0:10] != '-L/usr/lib' and arg[0:6] != '-L/lib'): retval[i] += ":" + arg[2:] count += 1 if count == 0: retval = "" return retval
def __call__(self, parser, namespace, values, option_string=None): if getattr(namespace, self.dest) is None: setattr(namespace, self.dest, list()) for value in values: v_args = cs_exec_environment.separate_args(value) for sv in v_args: getattr(namespace, self.dest).append(sv)
def run_studymanager_command(_c, _log, pythondir=None): """ Run command with arguments. Redirection of the stdout or stderr of the command. """ assert type(_c) == str or type(_c) == unicode log.debug("run_studymanager_command: %s" % _c) try: _log.seek(0, os.SEEK_END) except: _log.seek(0, 2) def __text(_t): return "\n\nExecution failed --> %s: %s" \ "\n - command: %s" \ "\n - directory: %s\n\n" % \ (_t, str(retcode), _c, os.getcwd()) _l = "" cmd = separate_args(_c) env = os.environ.copy() if pythondir: pythondir = enquote_arg(pythondir) pythonpath = pythondir + ':' + env.get("PYTHONPATH", '') env.update([("PYTHONPATH", pythonpath)]) try: t1 = time.time() retcode = run_command(cmd, stdout=_log, stderr=_log, env=env) t2 = time.time() if retcode < 0: _l = __text("command was terminated by signal") elif retcode > 0: _l = __text("command return") except OSError: import traceback import sys exc_info = sys.exc_info() bt = traceback.format_exception(*exc_info) for l in bt: _log.write(l) retcode = 0 del exc_info _l = __text("unknown command") t1 = 0. t2 = 0. _log.flush() if _l: _log.write(_l) return retcode, "%.2f" % (t2 - t1)
def get_nprocs(self): """ Get the nprocs option in the run command """ args = separate_args(self.lines[self.run_cmd_line_id]) return get_command_single_value(args, ('--nprocs', '--nprocs=', '-n'))
def get_parameters(self): """ Get the parameters option in the run command """ args = separate_args(self.lines[self.run_cmd_line_id]) return get_command_single_value(args, ('--param', '--param=', '-p'))
def get_nthreads(self): """ Get the nthreads option in the run command """ args = separate_args(self.lines[self.run_cmd_line_id]) return get_command_single_value( args, ('--threads-per-task', '--threads-per-task=', '-nt'))
def get_compute_build(self): """ Get the compute-build option in the run command """ args = separate_args(self.lines[self.run_cmd_line_id]) return get_command_single_value( args, ('--compute-build', '--compute-build='))
def get_run_stage(self, stage): """ Return True if a given stage is specified in the run command, False otherwise """ args = separate_args(self.lines[self.run_cmd_line_id]) s_arg = '--' + stage if s_arg in args: return True return False
def get_run_command(self): """ Determine the name of the main command of the runcase, and the associated line in the file; this allows mixing code_saturne and neptune_cfd cases in the same study. """ # Read the runcase script from the Repository. self.cmd_name = None self.run_cmd_line_id = -1 for i in range(len(self.lines) - 1, -1, -1): line = self.lines[i] # Skip comment and empty lines if len(line) == 0: continue if line[0] == '#' or line[0:4] == 'rem ': continue j = line.find('#') if j > -1: line = line[0:j] args = separate_args(line.strip()) if args.count('run') == 1: if args.index('run') == 1: # "<package_name> run" for name in ('code_saturne', 'neptune_cfd'): if sys.platform.startswith('win'): test_name = name if args[0].find(test_name) == 0: self.cmd_name = name self.run_cmd_line_id = i return test_name = '\\' + name if args[0].find(test_name) == 0: self.cmd_name = name self.run_cmd_line_id = i return elif args[0] == name: self.cmd_name = name self.run_cmd_line_id = i return # We should have exited before reaching this. err_str = "Error: unable to determine the name of the script for " + self.path + os.linesep sys.stderr.write(err_str)
def get_run_id(self): """ Get the run id, id_prefix, and id_suffix options in the run command """ args = separate_args(self.lines[self.run_cmd_line_id]) run_id = get_command_single_value(args, ('--id', '--id=')) run_id_prefix = get_command_single_value( args, ('--id-prefix', '--id-prefix=')) run_id_suffix = get_command_single_value( args, ('--id-suffix', '--id-suffix=')) return run_id, run_id_prefix, run_id_suffix
def get_flags(self, flag, base_name=None): """ Determine compilation flags for a given flag type. """ cmd_line = self.get_pkg_path_flags(flag) # Build the command line, and split possible multiple arguments in lists. for lib in self.pkg.config.deplibs: if (self.pkg.config.libs[lib].have == True and (not self.pkg.config.libs[lib].dynamic_load)): cmd_line += separate_args( self.pkg.config.libs[lib].flags[flag]) if flag == 'cppflags' and base_name: add_dirs = [] if base_name in self.pkg.config.exec_include: add_dirs.append(self.pkg.config.exec_include[base_name]) for a_d in add_dirs: d = os.path.join(self.pkg.get_dir("includedir"), a_d) if os.path.isdir(d): add_flag = "-I" + d if add_flag not in cmd_line: cmd_line.insert(0, add_flag) # Specific handling of low-level libraries, which should come last, # such as -lm and -lpthread: # If -lm appears multiple times, only add it at the end of the # libraries, so that fast versions of the library may appear first if flag == 'libs': for lib in ['-lpthread', '-lm']: n = cmd_line.count(lib) if n > 0: for i in range(n): cmd_line.remove(lib) cmd_line.append(lib) # Adapt for relocation on build stage on some systems self.flags_relocation(flag, cmd_line) return cmd_line
def link_obj(self, exec_name, obj_files=None, opt_libs=None, stdout=sys.stdout, stderr=sys.stderr): """ Link function. """ retval = 0 # Short names pkg = self.pkg # Directories call_dir = None temp_dir = None o_files = obj_files p_libs = self.get_flags('libs') # Special handling for some linkers (such as Mac OS X), for which # no multiple definitions are allowable in static mode; # in this case, extract archive, then overwrite with user files. if pkg.config.special_user_link == 'ar_x': call_dir = os.getcwd() temp_dir = tempfile.mkdtemp(suffix=".cs_link") os.chdir(temp_dir) lib0 = os.path.join(self.get_ar_lib_dir(), 'lib' + p_libs[0][2:] + '.a') p_libs = p_libs[1:] cmd = ['ar', 'x', lib0] if run_command( cmd, pkg=pkg, echo=True, stdout=stdout, stderr=stderr) != 0: retval = 1 if obj_files: import shutil for f in obj_files: if os.path.isabs(f): f_src = f else: f_src = os.path.join(call_dir, f) shutil.copy2(f_src, temp_dir) dir_files = os.listdir(os.getcwd()) o_files = fnmatch.filter(dir_files, '*.o') # Prepare link command cmd = [self.get_compiler('ld')] cmd += ["-o", exec_name] if o_files: cmd += o_files if os.path.basename(exec_name) in self.pkg.config.exec_libs: cmd += [self.pkg.config.exec_libs[os.path.basename(exec_name)]] if opt_libs != None: if len(opt_libs) > 0: cmd += separate_args(opt_libs) cmd += self.get_flags('ldflags') cmd += p_libs if pkg.config.rpath != "": cmd += self.so_dirs_path(cmd) # Clean paths in link flags cmd_new = [] for c in cmd: if c[:2] == '-L': c = '-L' + os.path.normpath(c[2:]) if cmd_new.count(c) < 1: cmd_new.append(c) else: cmd_new.append(c) cmd = cmd_new # Call linker if retval == 0: if run_command( cmd, pkg=pkg, echo=True, stdout=stdout, stderr=stderr) != 0: retval = 1 # Cleanup for special cases if temp_dir: if not os.path.isabs(exec_name): import shutil shutil.copy2(exec_name, os.path.join(call_dir, exec_name)) for f in os.listdir(temp_dir): os.remove(os.path.join(temp_dir, f)) os.chdir(call_dir) os.rmdir(temp_dir) return retval
def compile_src(self, base_name=None, src_list=None, opt_cflags=None, opt_cxxflags=None, opt_fcflags=None, opt_nvccflags=None, keep_going=False, stdout=sys.stdout, stderr=sys.stderr): """ Compilation function. """ retval = 0 # Short names pkg = self.pkg # Find files to compile in source path c_files = fnmatch.filter(src_list, '*.c') h_files = fnmatch.filter(src_list, '*.h') cxx_files = fnmatch.filter(src_list, '*.cxx') + fnmatch.filter( src_list, '*.cpp') cu_files = fnmatch.filter(src_list, '*.cu') hxx_files = fnmatch.filter(src_list, '*.hxx') + fnmatch.filter( src_list, '*.hpp') f_files = fnmatch.filter(src_list, '*.[fF]90') o_files = fnmatch.filter(src_list, '*.o') # Determine additional include directories c_include_dirs = [] for f in h_files: c_include_dirs.append(os.path.dirname(f)) c_include_dirs = sorted(set(c_include_dirs)) cxx_include_dirs = [] for f in hxx_files: cxx_include_dirs.append(os.path.dirname(f)) cxx_include_dirs = sorted(set(cxx_include_dirs)) f_include_dirs = [] for f in f_files: f_include_dirs.append(os.path.dirname(f)) f_include_dirs = sorted(set(cxx_include_dirs)) # Compile files for f in c_files: if (retval != 0 and not keep_going): break cmd = [self.get_compiler('cc')] if opt_cflags != None: cmd += separate_args(opt_cflags) for d in c_include_dirs: cmd += ["-I", d] cmd.append('-DHAVE_CONFIG_H') if os.path.basename(f) == 'cs_base.c': cmd += ['-DLOCALEDIR=\\"' + pkg.get_dir('localedir') + '\\"', \ '-DPKGDATADIR=\\"' + pkg.get_dir('pkgdatadir') + '\\"'] cmd += self.get_flags('cppflags', base_name=base_name) cmd += separate_args(pkg.config.flags['cflags']) cmd += ["-c", f] if run_command( cmd, pkg=pkg, echo=True, stdout=stdout, stderr=stderr) != 0: retval = 1 o_files.append(self.obj_name(f)) for f in cxx_files: if (retval != 0 and not keep_going): break cmd = [self.get_compiler('cxx')] if opt_cxxflags != None: cmd += separate_args(opt_cxxflags) for d in cxx_include_dirs: cmd += ["-I", d] for d in c_include_dirs: cmd += ["-I", d] cmd.append('-DHAVE_CONFIG_H') cmd += self.get_flags('cppflags', base_name=base_name) cmd += separate_args(pkg.config.flags['cxxflags']) cmd += ["-c", f] if run_command( cmd, pkg=pkg, echo=True, stdout=stdout, stderr=stderr) != 0: retval = 1 o_files.append(self.obj_name(f)) for f in cu_files: if (retval != 0 and not keep_going): break cmd = [self.get_compiler('nvcc')] if opt_nvccflags != None: cmd += separate_args(opt_nvccflags) for d in cxx_include_dirs: cmd += ["-I", d] for d in c_include_dirs: cmd += ["-I", d] cmd.append('-DHAVE_CONFIG_H') cmd += self.get_flags('cppflags', base_name=base_name) cmd += separate_args(pkg.config.flags['nvccflags']) cmd += ["-c", f] if run_command( cmd, pkg=pkg, echo=True, stdout=stdout, stderr=stderr) != 0: retval = 1 o_files.append(self.obj_name(f)) for f in f_files: if (retval != 0 and not keep_going): break cmd = [self.get_compiler('fc')] f_base = os.path.basename(f) o_name = self.obj_name(f) if f_base == 'cs_user_boundary_conditions.f90': o_name = "cs_f_user_boundary_conditions.o" cmd += ["-o", o_name] if f_base == 'cs_user_parameters.f90': o_name = "cs_f_user_parameters.o" cmd += ["-o", o_name] if f_base == 'cs_user_extra_operations.f90': o_name = "cs_f_user_extra_operations.o" cmd += ["-o", o_name] if f_base == 'cs_user_initialization.f90': o_name = "cs_f_user_initialization.o" cmd += ["-o", o_name] if f_base == 'cs_user_physical_properties.f90': o_name = "cs_f_user_physical_properties.o" cmd += ["-o", o_name] if f_base == 'cs_user_porosity.f90': o_name = "cs_f_user_porosity.o" cmd += ["-o", o_name] if f_base == 'cs_user_source_terms.f90': o_name = "cs_f_user_source_terms.o" cmd += ["-o", o_name] if opt_fcflags != None: cmd += separate_args(opt_fcflags) for d in f_include_dirs: cmd += ["-I", d] if pkg.config.fcmodinclude != "-I": cmd += [pkg.config.fcmodinclude, srcdir] cmd += ["-I", pkg.get_dir('pkgincludedir')] if pkg.config.fcmodinclude != "-I": cmd += [pkg.config.fcmodinclude, pkg.get_dir('pkgincludedir')] cmd += separate_args(pkg.config.flags['fcflags']) cmd += ["-c", f] if run_command( cmd, pkg=pkg, echo=True, stdout=stdout, stderr=stderr) != 0: retval = 1 o_files.append(o_name) return retval, o_files
def get_run_args(self): """ Get the run command and arguments, as a list """ return separate_args(self.lines[self.run_cmd_line_id])