Remake
Functions
Path helpers

Functions

static void init_working_dir ()
 
static void init_prefix_dir ()
 
static std::string normalize_abs (std::string const &s, std::string const &p)
 
static std::string normalize (std::string const &s, std::string const &w, std::string const &p)
 
static void normalize_list (string_list &l, std::string const &w, std::string const &p)
 

Detailed Description

Function Documentation

◆ init_prefix_dir()

static void init_prefix_dir ( )
static

Initialize prefix_dir and switch to it.

Definition at line 899 of file remake.cpp.

900 {
901  for (;;)
902  {
903  struct stat s;
904  if (stat((prefix_dir + "/Remakefile").c_str(), &s) == 0)
905  {
906  if (!changed_prefix_dir) return;
907  if (chdir(prefix_dir.c_str()))
908  {
909  perror("Failed to change working directory");
910  exit(EXIT_FAILURE);
911  }
912  if (show_targets)
913  {
914  std::cout << "remake: Entering directory `" << prefix_dir << '\'' << std::endl;
915  }
916  return;
917  }
918  size_t pos = prefix_dir.find_last_of('/');
919  if (pos == std::string::npos)
920  {
921  std::cerr << "Failed to locate Remakefile in the current directory or one of its parents" << std::endl;
922  exit(EXIT_FAILURE);
923  }
924  prefix_dir.erase(pos);
925  changed_prefix_dir = true;
926  }
927 }

Referenced by main().

◆ init_working_dir()

static void init_working_dir ( )
static

Initialize working_dir.

Definition at line 877 of file remake.cpp.

878 {
879  char buf[1024];
880  char *res = getcwd(buf, sizeof(buf));
881  if (!res)
882  {
883  perror("Failed to get working directory");
884  exit(EXIT_FAILURE);
885  }
886  working_dir = buf;
887 #ifdef WINDOWS
888  for (size_t i = 0, l = working_dir.size(); i != l; ++i)
889  {
890  if (working_dir[i] == '\\') working_dir[i] = '/';
891  }
892 #endif
894 }

Referenced by main().

◆ normalize()

static std::string normalize ( std::string const &  s,
std::string const &  w,
std::string const &  p 
)
static

Normalize path s (possibly relative to w) with respect to p.

  • If both p and w are empty, the function just removes ".", "..", "//".
  • If only p is empty, the function returns an absolute path.

Definition at line 955 of file remake.cpp.

956 {
957 #ifdef WINDOWS
958  char const *delim = "/\\";
959 #else
960  char delim = '/';
961 #endif
962  size_t pos = s.find_first_of(delim);
963  if (pos == std::string::npos && w == p) return s;
964  bool absolute = pos == 0;
965  if (!absolute && w != p && !w.empty())
966  return normalize(w + '/' + s, w, p);
967  size_t prev = 0, len = s.length();
968  string_list l;
969  for (;;)
970  {
971  if (pos != prev)
972  {
973  std::string n = s.substr(prev, pos - prev);
974  if (n == "..")
975  {
976  if (!l.empty()) l.pop_back();
977  else if (!absolute && !w.empty())
978  return normalize(w + '/' + s, w, p);
979  }
980  else if (n != ".")
981  l.push_back(n);
982  }
983  ++pos;
984  if (pos >= len) break;
985  prev = pos;
986  pos = s.find_first_of(delim, prev);
987  if (pos == std::string::npos) pos = len;
988  }
989  string_list::const_iterator i = l.begin(), i_end = l.end();
990  if (i == i_end) return absolute ? "/" : ".";
991  std::string n;
992  if (absolute) n.push_back('/');
993  n.append(*i);
994  for (++i; i != i_end; ++i)
995  {
996  n.push_back('/');
997  n.append(*i);
998  }
999  if (absolute && !p.empty()) return normalize_abs(n, p);
1000  return n;
1001 }

Referenced by main(), and normalize_list().

◆ normalize_abs()

static std::string normalize_abs ( std::string const &  s,
std::string const &  p 
)
static

Normalize an absolute path with respect to p. Paths outside the subtree are left unchanged.

Definition at line 933 of file remake.cpp.

934 {
935  size_t l = p.length();
936  if (s.compare(0, l, p)) return s;
937  size_t ll = s.length();
938  if (ll == l) return ".";
939  if (s[l] != '/')
940  {
941  size_t pos = s.rfind('/', l);
942  assert(pos != std::string::npos);
943  return s.substr(pos + 1);
944  }
945  if (ll == l + 1) return ".";
946  return s.substr(l + 1);
947 }

Referenced by normalize().

◆ normalize_list()

static void normalize_list ( string_list l,
std::string const &  w,
std::string const &  p 
)
static

Normalize the content of a list of targets.

Definition at line 1006 of file remake.cpp.

1007 {
1008  for (string_list::iterator i = l.begin(),
1009  i_end = l.end(); i != i_end; ++i)
1010  {
1011  *i = normalize(*i, w, p);
1012  }
1013 }

Referenced by load_rule(), and main().

string_list
std::list< std::string > string_list
Definition: remake.cpp:471
normalize
static std::string normalize(std::string const &s, std::string const &w, std::string const &p)
Definition: remake.cpp:955
normalize_abs
static std::string normalize_abs(std::string const &s, std::string const &p)
Definition: remake.cpp:933
changed_prefix_dir
static bool changed_prefix_dir
Definition: remake.cpp:746
prefix_dir
static std::string prefix_dir
Definition: remake.cpp:741
working_dir
static std::string working_dir
Definition: remake.cpp:736
show_targets
static bool show_targets
Definition: remake.cpp:721