25 namespace basis {
namespace util {
35 cout <<
"Contact:\n " << contact << endl;
39 void print_version(
const char* name,
const char* version,
const char* project,
40 const char* copyright,
const char* license)
45 if (project && *project) cout <<
" (" << project <<
")";
46 cout <<
" " << version << endl;
47 if (copyright && *copyright) cout <<
"Copyright (c) " << copyright <<
". All rights reserved." << endl;
48 if (license && *license) cout << license << endl;
54 return targets != NULL ? targets->
targetuid(name) :
"";
60 return targets != NULL && targets->
istarget(name);
69 string exec_name = targets != NULL ? targets->
basename(name) :
"";
71 if (exec_name.empty()) {
81 vector<string>
which(2);
82 which[0] =
"basis.which";
86 if (
execute(which,
true, &oss,
true, 0,
false, targets) == 0) {
87 exec_name = oss.str();
88 const string::size_type end = exec_name.find_last_not_of(
" \t\n\r");
89 if (end == string::npos) {
92 exec_name.erase(end + 1);
104 string exec_path =
exepath(name, targets);
105 if (exec_path.empty())
return "";
109 if (ext ==
".exe" || ext ==
".com") exec_path = fname;
117 string exec_path =
exepath(name, targets);
132 vector<string>
qsplit(
const string& args)
138 int execute(
const string& cmd,
bool quiet, ostream* out,
139 bool allow_fail,
int verbose,
bool simulate,
143 return execute(args, quiet, out, allow_fail, verbose, simulate, targets);
147 int execute(vector<string> args,
bool quiet, ostream* out,
148 bool allow_fail,
int verbose,
bool simulate,
151 if (args.empty() || args[0].empty()) {
155 string exec_path =
exepath(args[0], targets);
157 if (!exec_path.empty()) args[0] = exec_path;
159 if (verbose > 0 || simulate) {
161 if (simulate) cout <<
" (simulated)";
169 if (!p.
popen(args, Subprocess::RM_NONE, Subprocess::RM_PIPE, Subprocess::RM_PIPE)) {
173 if (!quiet || out != NULL) {
174 while ((n = p.
read(buf, 1023)) > 0) {
180 if (out) *out << buf;
188 while ((n = p.
read(buf, 1023,
true)) > 0) {
195 if (status != 0 && !allow_fail) {
virtual bool istarget(const std::string &target) const =0
Determine whether a given build target is known.
function targetuid(out uid, in name)
Get UID of build target.
#define assert(condition)
Assertion without custom message.
virtual std::string basename(const std::string &target) const =0
Get name of executable file without directory path.
Main module of project-independent BASIS utilities.
virtual std::string targetuid(const std::string &target) const =0
Get UID of build target.
function istarget(in target)
Determine whether a given build target is known.
def which(command, path=None, verbose=0, exts=None)
function tostring(out var, in elements)
Build quoted string from array.
std::string join(const std::string &base, const std::string &path)
Join two paths, e.g., base path and relative path.
bool popen(const CommandLine &args, const RedirectMode rm_in=RM_NONE, const RedirectMode rm_out=RM_NONE, const RedirectMode rm_err=RM_NONE, const Environment *env=NULL)
Open new subprocess.
Module used to execute subprocesses.
bool isabs(const std::string &path)
Test whether a given path is absolute.
Exception type thrown by execute().
#define BASIS_THROW(type, msg)
Throw exception with given message.
std::string basename(const std::string &path)
Get file name.
Provides information about executable build targets.
int read(void *buf, size_t nbuf, bool err=false)
Read data from stdout or stderr of subprocess.
void split(const std::string &path, std::string &head, std::string &tail)
Split path into two parts.
bool wait()
Wait for subprocess to terminate.
void print_version(const char *name, const char *version=NULL, const char *project=NULL, const char *copyright=NULL, const char *license=NULL)
Print version information including copyright and license notices.
Platform-independent interface to create and control a subprocess.
function exepath(out path, in target)
Get absolute path of executable file.
void splitext(const std::string &path, std::string &head, std::string &ext, const std::set< std::string > *exts=NULL, bool icase=false)
Get file name extension.
function qsplit(out var, in str)
Split (quoted) string.
std::string dirname(const std::string &path)
Get file directory.
function exedir(out dir, in name)
Get directory of executable file.
MultiSwitchArg verbose("v", "verbose", "Increase verbosity of output messages.", false)
virtual std::string dirname(const std::string &target) const =0
Get absolute path to directory containing executable.
void print_contact(const char *contact=NULL)
Print contact information.
function exename(out file, in name)
Get name of executable file.
function execute(in options, in cmd, in args)
Execute command as subprocess.