Remake
Functions
User interface

Functions

static void usage (int exit_status)
 
int main (int argc, char *argv[])
 

Detailed Description

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

This program behaves in two different ways.

  • If the environment contains the REMAKE_SOCKET variable, the client connects to this socket and sends to the server its build targets. It exits once it receives the server reply.
  • Otherwise, it creates a server that waits for build requests. It also creates a pseudo-client that requests the targets passed on the command line.

Definition at line 2984 of file remake.cpp.

2985 {
2986  std::string remakefile;
2987  string_list targets;
2988  bool literal_targets = false;
2989  bool indirect_targets = false;
2990 
2991  // Parse command-line arguments.
2992  for (int i = 1; i < argc; ++i)
2993  {
2994  std::string arg = argv[i];
2995  if (arg.empty()) usage(EXIT_FAILURE);
2996  if (literal_targets) goto new_target;
2997  if (arg == "-h" || arg == "--help") usage(EXIT_SUCCESS);
2998  if (arg == "-d")
2999  if (echo_scripts) debug.active = true;
3000  else echo_scripts = true;
3001  else if (arg == "-k" || arg =="--keep-going")
3002  keep_going = true;
3003  else if (arg == "-s" || arg == "--silent" || arg == "--quiet")
3004  show_targets = false;
3005  else if (arg == "-r")
3006  indirect_targets = true;
3007  else if (arg == "-B" || arg == "--always-make")
3008  obsolete_targets = true;
3009  else if (arg == "-f")
3010  {
3011  if (++i == argc) usage(EXIT_FAILURE);
3012  remakefile = argv[i];
3013  }
3014  else if (arg == "--")
3015  literal_targets = true;
3016  else if (arg.compare(0, 2, "-j") == 0)
3017  max_active_jobs = atoi(arg.c_str() + 2);
3018  else if (arg.compare(0, 7, "--jobs=") == 0)
3019  max_active_jobs = atoi(arg.c_str() + 7);
3020  else
3021  {
3022  if (arg[0] == '-') usage(EXIT_FAILURE);
3023  if (arg.find('=') != std::string::npos)
3024  {
3025  std::istringstream in(arg);
3026  std::string name = read_word(in);
3027  if (name.empty() || !expect_token(in, Equal)) usage(EXIT_FAILURE);
3028  read_words(in, variables[name]);
3029  continue;
3030  }
3031  new_target:
3032  targets.push_back(arg);
3033  DEBUG << "New target: " << arg << '\n';
3034  }
3035  }
3036 
3037  init_working_dir();
3039 
3040  if (indirect_targets)
3041  {
3042  load_dependencies(std::cin);
3043  string_list l;
3044  targets.swap(l);
3045  if (l.empty() && !dependencies.empty())
3046  {
3047  l.push_back(dependencies.begin()->second->targets.front());
3048  }
3049  for (string_list::const_iterator i = l.begin(),
3050  i_end = l.end(); i != i_end; ++i)
3051  {
3052  dependency_map::const_iterator j = dependencies.find(*i);
3053  if (j == dependencies.end()) continue;
3054  dependency_t const &dep = *j->second;
3055  for (string_set::const_iterator k = dep.deps.begin(),
3056  k_end = dep.deps.end(); k != k_end; ++k)
3057  {
3058  targets.push_back(normalize(*k, working_dir, working_dir));
3059  }
3060  }
3061  dependencies.clear();
3062  }
3063 
3064 #ifdef WINDOWS
3065  WSADATA wsaData;
3066  if (WSAStartup(MAKEWORD(2,2), &wsaData))
3067  {
3068  std::cerr << "Unexpected failure while initializing Windows Socket" << std::endl;
3069  return 1;
3070  }
3071 #endif
3072 
3073  // Run as client if REMAKE_SOCKET is present in the environment.
3074  if (char *sn = getenv("REMAKE_SOCKET")) client_mode(sn, targets);
3075 
3076  // Otherwise run as server.
3077  if (remakefile.empty())
3078  {
3079  remakefile = "Remakefile";
3080  init_prefix_dir();
3081  }
3083  server_mode(remakefile, targets);
3084 }
static std::string normalize(std::string const &s, std::string const &w, std::string const &p)
Definition: remake.cpp:938
static void server_mode(std::string const &remakefile, string_list const &targets)
Definition: remake.cpp:2817
static void init_prefix_dir()
Definition: remake.cpp:882
static variable_map variables
Definition: remake.cpp:604
std::list< std::string > string_list
Definition: remake.cpp:456
static int max_active_jobs
Definition: remake.cpp:646
static std::string read_word(std::istream &in, bool detect_equal=true)
Definition: remake.cpp:1105
static bool read_words(input_generator &in, string_list &res)
Definition: remake.cpp:1271
string_set deps
Definition: remake.cpp:499
static void init_working_dir()
Definition: remake.cpp:860
static bool keep_going
Definition: remake.cpp:652
static bool echo_scripts
Definition: remake.cpp:711
static std::string working_dir
Definition: remake.cpp:721
static std::string prefix_dir
Definition: remake.cpp:726
static bool obsolete_targets
Definition: remake.cpp:741
static bool show_targets
Definition: remake.cpp:706
static log debug
Definition: remake.cpp:786
#define DEBUG
Definition: remake.cpp:800
static void normalize_list(string_list &l, std::string const &w, std::string const &p)
Definition: remake.cpp:989
static int expect_token(std::istream &in, int mask)
Definition: remake.cpp:1059
bool active
Definition: remake.cpp:761
static void usage(int exit_status)
Definition: remake.cpp:2957
static void client_mode(char *socket_name, string_list const &targets)
Definition: remake.cpp:2865
static void load_dependencies(std::istream &in)
Definition: remake.cpp:1422
static dependency_map dependencies
Definition: remake.cpp:609

◆ usage()

static void usage ( int  exit_status)
static

Display usage and exit with exit_status.

Definition at line 2957 of file remake.cpp.

2958 {
2959  std::cerr << "Usage: remake [options] [target] ...\n"
2960  "Options\n"
2961  " -B, --always-make Unconditionally make all targets.\n"
2962  " -d Echo script commands.\n"
2963  " -d -d Print lots of debugging information.\n"
2964  " -f FILE Read FILE as Remakefile.\n"
2965  " -h, --help Print this message and exit.\n"
2966  " -j[N], --jobs=[N] Allow N jobs at once; infinite jobs with no arg.\n"
2967  " -k, --keep-going Keep going when some targets cannot be made.\n"
2968  " -r Look up targets from the dependencies on stdin.\n"
2969  " -s, --silent, --quiet Do not echo targets.\n";
2970  exit(exit_status);
2971 }

Referenced by main().