cmGetFilenameComponentCommand.cxx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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[args.size() - 1] == "CACHE") {
  19. const char* cacheValue = this->Makefile->GetDefinition(args[0]);
  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] == "ABSOLUTE" || args[2] == "REALPATH") {
  85. // If the path given is relative, evaluate it relative to the
  86. // current source directory unless the user passes a different
  87. // base directory.
  88. std::string baseDir = this->Makefile->GetCurrentSourceDirectory();
  89. for (unsigned int i = 3; i < args.size(); ++i) {
  90. if (args[i] == "BASE_DIR") {
  91. ++i;
  92. if (i < args.size()) {
  93. baseDir = args[i];
  94. }
  95. }
  96. }
  97. // Collapse the path to its simplest form.
  98. result = cmSystemTools::CollapseFullPath(filename, baseDir);
  99. if (args[2] == "REALPATH") {
  100. // Resolve symlinks if possible
  101. result = cmSystemTools::GetRealPath(result);
  102. }
  103. } else {
  104. std::string err = "unknown component " + args[2];
  105. this->SetError(err);
  106. return false;
  107. }
  108. if (args.size() >= 4 && args[args.size() - 1] == "CACHE") {
  109. if (!programArgs.empty() && !storeArgs.empty()) {
  110. this->Makefile->AddCacheDefinition(
  111. storeArgs, programArgs.c_str(), "",
  112. args[2] == "PATH" ? cmStateEnums::FILEPATH : cmStateEnums::STRING);
  113. }
  114. this->Makefile->AddCacheDefinition(
  115. args[0], result.c_str(), "",
  116. args[2] == "PATH" ? cmStateEnums::FILEPATH : cmStateEnums::STRING);
  117. } else {
  118. if (!programArgs.empty() && !storeArgs.empty()) {
  119. this->Makefile->AddDefinition(storeArgs, programArgs.c_str());
  120. }
  121. this->Makefile->AddDefinition(args[0], result.c_str());
  122. }
  123. return true;
  124. }