cmGetFilenameComponentCommand.cxx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmGetFilenameComponentCommand.h"
  4. #include "cmMakefile.h"
  5. #include "cmStateTypes.h"
  6. #include "cmSystemTools.h"
  7. class cmExecutionStatus;
  8. // cmGetFilenameComponentCommand
  9. bool cmGetFilenameComponentCommand::InitialPass(
  10. std::vector<std::string> const& args, cmExecutionStatus&)
  11. {
  12. if (args.size() < 3) {
  13. this->SetError("called with incorrect number of arguments");
  14. return false;
  15. }
  16. // Check and see if the value has been stored in the cache
  17. // already, if so use that value
  18. if (args.size() >= 4 && args.back() == "CACHE") {
  19. const char* cacheValue = this->Makefile->GetDefinition(args.front());
  20. if (cacheValue && !cmSystemTools::IsNOTFOUND(cacheValue)) {
  21. return true;
  22. }
  23. }
  24. std::string result;
  25. std::string filename = args[1];
  26. if (filename.find("[HKEY") != std::string::npos) {
  27. // Check the registry as the target application would view it.
  28. cmSystemTools::KeyWOW64 view = cmSystemTools::KeyWOW64_32;
  29. cmSystemTools::KeyWOW64 other_view = cmSystemTools::KeyWOW64_64;
  30. if (this->Makefile->PlatformIs64Bit()) {
  31. view = cmSystemTools::KeyWOW64_64;
  32. other_view = cmSystemTools::KeyWOW64_32;
  33. }
  34. cmSystemTools::ExpandRegistryValues(filename, view);
  35. if (filename.find("/registry") != std::string::npos) {
  36. std::string other = args[1];
  37. cmSystemTools::ExpandRegistryValues(other, other_view);
  38. if (other.find("/registry") == std::string::npos) {
  39. filename = other;
  40. }
  41. }
  42. }
  43. std::string storeArgs;
  44. std::string programArgs;
  45. if (args[2] == "DIRECTORY" || args[2] == "PATH") {
  46. result = cmSystemTools::GetFilenamePath(filename);
  47. } else if (args[2] == "NAME") {
  48. result = cmSystemTools::GetFilenameName(filename);
  49. } else if (args[2] == "PROGRAM") {
  50. for (unsigned int i = 2; i < args.size(); ++i) {
  51. if (args[i] == "PROGRAM_ARGS") {
  52. i++;
  53. if (i < args.size()) {
  54. storeArgs = args[i];
  55. }
  56. }
  57. }
  58. // First assume the path to the program was specified with no
  59. // arguments and with no quoting or escaping for spaces.
  60. // Only bother doing this if there is non-whitespace.
  61. if (!cmSystemTools::TrimWhitespace(filename).empty()) {
  62. result = cmSystemTools::FindProgram(filename);
  63. }
  64. // If that failed then assume a command-line string was given
  65. // and split the program part from the rest of the arguments.
  66. if (result.empty()) {
  67. std::string program;
  68. if (cmSystemTools::SplitProgramFromArgs(filename, program,
  69. programArgs)) {
  70. if (cmSystemTools::FileExists(program)) {
  71. result = program;
  72. } else {
  73. result = cmSystemTools::FindProgram(program);
  74. }
  75. }
  76. if (result.empty()) {
  77. programArgs.clear();
  78. }
  79. }
  80. } else if (args[2] == "EXT") {
  81. result = cmSystemTools::GetFilenameExtension(filename);
  82. } else if (args[2] == "NAME_WE") {
  83. result = cmSystemTools::GetFilenameWithoutExtension(filename);
  84. } else if (args[2] == "LAST_EXT") {
  85. result = cmSystemTools::GetFilenameLastExtension(filename);
  86. } else if (args[2] == "NAME_WLE") {
  87. result = cmSystemTools::GetFilenameWithoutLastExtension(filename);
  88. } else if (args[2] == "ABSOLUTE" || args[2] == "REALPATH") {
  89. // If the path given is relative, evaluate it relative to the
  90. // current source directory unless the user passes a different
  91. // base directory.
  92. std::string baseDir = this->Makefile->GetCurrentSourceDirectory();
  93. for (unsigned int i = 3; i < args.size(); ++i) {
  94. if (args[i] == "BASE_DIR") {
  95. ++i;
  96. if (i < args.size()) {
  97. baseDir = args[i];
  98. }
  99. }
  100. }
  101. // Collapse the path to its simplest form.
  102. result = cmSystemTools::CollapseFullPath(filename, baseDir);
  103. if (args[2] == "REALPATH") {
  104. // Resolve symlinks if possible
  105. result = cmSystemTools::GetRealPath(result);
  106. }
  107. } else {
  108. std::string err = "unknown component " + args[2];
  109. this->SetError(err);
  110. return false;
  111. }
  112. if (args.size() >= 4 && args.back() == "CACHE") {
  113. if (!programArgs.empty() && !storeArgs.empty()) {
  114. this->Makefile->AddCacheDefinition(
  115. storeArgs, programArgs.c_str(), "",
  116. args[2] == "PATH" ? cmStateEnums::FILEPATH : cmStateEnums::STRING);
  117. }
  118. this->Makefile->AddCacheDefinition(
  119. args.front(), result.c_str(), "",
  120. args[2] == "PATH" ? cmStateEnums::FILEPATH : cmStateEnums::STRING);
  121. } else {
  122. if (!programArgs.empty() && !storeArgs.empty()) {
  123. this->Makefile->AddDefinition(storeArgs, programArgs);
  124. }
  125. this->Makefile->AddDefinition(args.front(), result);
  126. }
  127. return true;
  128. }