cmGetFilenameComponentCommand.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmGetFilenameComponentCommand.h"
  11. #include "cmSystemTools.h"
  12. // cmGetFilenameComponentCommand
  13. bool cmGetFilenameComponentCommand
  14. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  15. {
  16. if(args.size() < 3)
  17. {
  18. this->SetError("called with incorrect number of arguments");
  19. return false;
  20. }
  21. // Check and see if the value has been stored in the cache
  22. // already, if so use that value
  23. if(args.size() == 4 && args[3] == "CACHE")
  24. {
  25. const char* cacheValue = this->Makefile->GetDefinition(args[0].c_str());
  26. if(cacheValue && !cmSystemTools::IsNOTFOUND(cacheValue))
  27. {
  28. return true;
  29. }
  30. }
  31. std::string result;
  32. std::string filename = args[1];
  33. if(filename.find("[HKEY") != filename.npos)
  34. {
  35. // Check the registry as the target application would view it.
  36. cmSystemTools::KeyWOW64 view = cmSystemTools::KeyWOW64_32;
  37. cmSystemTools::KeyWOW64 other_view = cmSystemTools::KeyWOW64_64;
  38. if(this->Makefile->PlatformIs64Bit())
  39. {
  40. view = cmSystemTools::KeyWOW64_64;
  41. other_view = cmSystemTools::KeyWOW64_32;
  42. }
  43. cmSystemTools::ExpandRegistryValues(filename, view);
  44. if(filename.find("/registry") != filename.npos)
  45. {
  46. std::string other = args[1];
  47. cmSystemTools::ExpandRegistryValues(other, other_view);
  48. if(other.find("/registry") == other.npos)
  49. {
  50. filename = other;
  51. }
  52. }
  53. }
  54. std::string storeArgs;
  55. std::string programArgs;
  56. if (args[2] == "PATH")
  57. {
  58. result = cmSystemTools::GetFilenamePath(filename);
  59. }
  60. else if (args[2] == "NAME")
  61. {
  62. result = cmSystemTools::GetFilenameName(filename);
  63. }
  64. else if (args[2] == "PROGRAM")
  65. {
  66. for(unsigned int i=2; i < args.size(); ++i)
  67. {
  68. if(args[i] == "PROGRAM_ARGS")
  69. {
  70. i++;
  71. if(i < args.size())
  72. {
  73. storeArgs = args[i];
  74. }
  75. }
  76. }
  77. cmSystemTools::SplitProgramFromArgs(filename.c_str(),
  78. result, programArgs);
  79. }
  80. else if (args[2] == "EXT")
  81. {
  82. result = cmSystemTools::GetFilenameExtension(filename);
  83. }
  84. else if (args[2] == "NAME_WE")
  85. {
  86. result = cmSystemTools::GetFilenameWithoutExtension(filename);
  87. }
  88. else if (args[2] == "ABSOLUTE" ||
  89. args[2] == "REALPATH")
  90. {
  91. // Collapse the path to its simplest form.
  92. // If the path given is relative evaluate it relative to the
  93. // current source directory.
  94. result = cmSystemTools::CollapseFullPath(
  95. filename.c_str(), this->Makefile->GetCurrentDirectory());
  96. if(args[2] == "REALPATH")
  97. {
  98. // Resolve symlinks if possible
  99. result = cmSystemTools::GetRealPath(filename.c_str());
  100. }
  101. }
  102. else
  103. {
  104. std::string err = "unknown component " + args[2];
  105. this->SetError(err.c_str());
  106. return false;
  107. }
  108. if(args.size() == 4 && args[3] == "CACHE")
  109. {
  110. if(programArgs.size() && storeArgs.size())
  111. {
  112. this->Makefile->AddCacheDefinition
  113. (storeArgs.c_str(), programArgs.c_str(),
  114. "", args[2] == "PATH" ? cmCacheManager::FILEPATH
  115. : cmCacheManager::STRING);
  116. }
  117. this->Makefile->AddCacheDefinition
  118. (args[0].c_str(), result.c_str(), "",
  119. args[2] == "PATH" ? cmCacheManager::FILEPATH
  120. : cmCacheManager::STRING);
  121. }
  122. else
  123. {
  124. if(programArgs.size() && storeArgs.size())
  125. {
  126. this->Makefile->AddDefinition(storeArgs.c_str(), programArgs.c_str());
  127. }
  128. this->Makefile->AddDefinition(args[0].c_str(), result.c_str());
  129. }
  130. return true;
  131. }