cmGetFilenameComponentCommand.cxx 4.9 KB

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