cmGetFilenameComponentCommand.cxx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. cmSystemTools::SplitProgramFromArgs(filename, result, programArgs);
  59. } else if (args[2] == "EXT") {
  60. result = cmSystemTools::GetFilenameExtension(filename);
  61. } else if (args[2] == "NAME_WE") {
  62. result = cmSystemTools::GetFilenameWithoutExtension(filename);
  63. } else if (args[2] == "ABSOLUTE" || args[2] == "REALPATH") {
  64. // If the path given is relative, evaluate it relative to the
  65. // current source directory unless the user passes a different
  66. // base directory.
  67. std::string baseDir = this->Makefile->GetCurrentSourceDirectory();
  68. for (unsigned int i = 3; i < args.size(); ++i) {
  69. if (args[i] == "BASE_DIR") {
  70. ++i;
  71. if (i < args.size()) {
  72. baseDir = args[i];
  73. }
  74. }
  75. }
  76. // Collapse the path to its simplest form.
  77. result = cmSystemTools::CollapseFullPath(filename, baseDir);
  78. if (args[2] == "REALPATH") {
  79. // Resolve symlinks if possible
  80. result = cmSystemTools::GetRealPath(result);
  81. }
  82. } else {
  83. std::string err = "unknown component " + args[2];
  84. this->SetError(err);
  85. return false;
  86. }
  87. if (args.size() >= 4 && args[args.size() - 1] == "CACHE") {
  88. if (!programArgs.empty() && !storeArgs.empty()) {
  89. this->Makefile->AddCacheDefinition(
  90. storeArgs, programArgs.c_str(), "",
  91. args[2] == "PATH" ? cmStateEnums::FILEPATH : cmStateEnums::STRING);
  92. }
  93. this->Makefile->AddCacheDefinition(
  94. args[0], result.c_str(), "",
  95. args[2] == "PATH" ? cmStateEnums::FILEPATH : cmStateEnums::STRING);
  96. } else {
  97. if (!programArgs.empty() && !storeArgs.empty()) {
  98. this->Makefile->AddDefinition(storeArgs, programArgs.c_str());
  99. }
  100. this->Makefile->AddDefinition(args[0], result.c_str());
  101. }
  102. return true;
  103. }