Remake
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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

static void init_prefix_dir ( )
static

Initialize prefix_dir and switch to it.

Definition at line 875 of file remake.cpp.

Referenced by main().

876 {
877  for (;;)
878  {
879  struct stat s;
880  if (stat((prefix_dir + "/Remakefile").c_str(), &s) == 0)
881  {
882  if (!changed_prefix_dir) return;
883  if (chdir(prefix_dir.c_str()))
884  {
885  perror("Failed to change working directory");
886  exit(EXIT_FAILURE);
887  }
888  if (show_targets)
889  {
890  std::cout << "remake: Entering directory `" << prefix_dir << '\'' << std::endl;
891  }
892  return;
893  }
894  size_t pos = prefix_dir.find_last_of('/');
895  if (pos == std::string::npos)
896  {
897  std::cerr << "Failed to locate Remakefile in the current directory or one of its parents" << std::endl;
898  exit(EXIT_FAILURE);
899  }
900  prefix_dir.erase(pos);
901  changed_prefix_dir = true;
902  }
903 }
static std::string prefix_dir
Definition: remake.cpp:724
static bool show_targets
Definition: remake.cpp:704
static bool changed_prefix_dir
Definition: remake.cpp:729
static void init_working_dir ( )
static

Initialize working_dir.

Definition at line 853 of file remake.cpp.

Referenced by main().

854 {
855  char buf[1024];
856  char *res = getcwd(buf, sizeof(buf));
857  if (!res)
858  {
859  perror("Failed to get working directory");
860  exit(EXIT_FAILURE);
861  }
862  working_dir = buf;
863 #ifdef WINDOWS
864  for (size_t i = 0, l = working_dir.size(); i != l; ++i)
865  {
866  if (working_dir[i] == '\\') working_dir[i] = '/';
867  }
868 #endif
870 }
static std::string working_dir
Definition: remake.cpp:719
static std::string prefix_dir
Definition: remake.cpp:724
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 931 of file remake.cpp.

Referenced by main(), and normalize_list().

932 {
933 #ifdef WINDOWS
934  char const *delim = "/\\";
935 #else
936  char delim = '/';
937 #endif
938  size_t pos = s.find_first_of(delim);
939  if (pos == std::string::npos && w == p) return s;
940  bool absolute = pos == 0;
941  if (!absolute && w != p && !w.empty())
942  return normalize(w + '/' + s, w, p);
943  size_t prev = 0, len = s.length();
944  string_list l;
945  for (;;)
946  {
947  if (pos != prev)
948  {
949  std::string n = s.substr(prev, pos - prev);
950  if (n == "..")
951  {
952  if (!l.empty()) l.pop_back();
953  else if (!absolute && !w.empty())
954  return normalize(w + '/' + s, w, p);
955  }
956  else if (n != ".")
957  l.push_back(n);
958  }
959  ++pos;
960  if (pos >= len) break;
961  prev = pos;
962  pos = s.find_first_of(delim, prev);
963  if (pos == std::string::npos) pos = len;
964  }
965  string_list::const_iterator i = l.begin(), i_end = l.end();
966  if (i == i_end) return absolute ? "/" : ".";
967  std::string n;
968  if (absolute) n.push_back('/');
969  n.append(*i);
970  for (++i; i != i_end; ++i)
971  {
972  n.push_back('/');
973  n.append(*i);
974  }
975  if (absolute && !p.empty()) return normalize_abs(n, p);
976  return n;
977 }
static std::string normalize(std::string const &s, std::string const &w, std::string const &p)
Definition: remake.cpp:931
std::list< std::string > string_list
Definition: remake.cpp:455
static std::string normalize_abs(std::string const &s, std::string const &p)
Definition: remake.cpp:909
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 909 of file remake.cpp.

Referenced by normalize().

910 {
911  size_t l = p.length();
912  if (s.compare(0, l, p)) return s;
913  size_t ll = s.length();
914  if (ll == l) return ".";
915  if (s[l] != '/')
916  {
917  size_t pos = s.rfind('/', l);
918  assert(pos != std::string::npos);
919  return s.substr(pos + 1);
920  }
921  if (ll == l + 1) return ".";
922  return s.substr(l + 1);
923 }
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 982 of file remake.cpp.

Referenced by load_rule(), and main().

983 {
984  for (string_list::iterator i = l.begin(),
985  i_end = l.end(); i != i_end; ++i)
986  {
987  *i = normalize(*i, w, p);
988  }
989 }
static std::string normalize(std::string const &s, std::string const &w, std::string const &p)
Definition: remake.cpp:931