cmGetFilenameComponentCommand.cxx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmGetFilenameComponentCommand.h"
  14. #include "cmSystemTools.h"
  15. // cmGetFilenameComponentCommand
  16. bool cmGetFilenameComponentCommand::InitialPass(std::vector<std::string> const& args)
  17. {
  18. if(args.size() < 3)
  19. {
  20. this->SetError("called with incorrect number of arguments");
  21. return false;
  22. }
  23. // Check and see if the value has been stored in the cache
  24. // already, if so use that value
  25. if(args.size() == 4 && args[3] == "CACHE")
  26. {
  27. const char* cacheValue = this->Makefile->GetDefinition(args[0].c_str());
  28. if(cacheValue && !cmSystemTools::IsNOTFOUND(cacheValue))
  29. {
  30. return true;
  31. }
  32. }
  33. std::string result;
  34. std::string filename = args[1];
  35. cmSystemTools::ExpandRegistryValues(filename);
  36. std::string storeArgs;
  37. std::string programArgs;
  38. if (args[2] == "PATH")
  39. {
  40. result = cmSystemTools::GetFilenamePath(filename);
  41. }
  42. else if (args[2] == "NAME")
  43. {
  44. result = cmSystemTools::GetFilenameName(filename);
  45. }
  46. else if (args[2] == "PROGRAM")
  47. {
  48. for(unsigned int i=2; i < args.size(); ++i)
  49. {
  50. if(args[i] == "PROGRAM_ARGS")
  51. {
  52. i++;
  53. if(i < args.size())
  54. {
  55. storeArgs = args[i];
  56. }
  57. }
  58. }
  59. cmSystemTools::SplitProgramFromArgs(filename.c_str(), result, programArgs);
  60. }
  61. else if (args[2] == "EXT")
  62. {
  63. result = cmSystemTools::GetFilenameExtension(filename);
  64. }
  65. else if (args[2] == "NAME_WE")
  66. {
  67. result = cmSystemTools::GetFilenameWithoutExtension(filename);
  68. }
  69. else if (args[2] == "ABSOLUTE")
  70. {
  71. // If the path given is relative evaluate it relative to the
  72. // current source directory.
  73. if(!cmSystemTools::FileIsFullPath(filename.c_str()))
  74. {
  75. std::string fname = this->Makefile->GetCurrentDirectory();
  76. if(!fname.empty())
  77. {
  78. fname += "/";
  79. fname += filename;
  80. filename = fname;
  81. }
  82. }
  83. // Collapse the path to its simplest form.
  84. result = cmSystemTools::CollapseFullPath(filename.c_str());
  85. }
  86. else
  87. {
  88. std::string err = "unknown component " + args[2];
  89. this->SetError(err.c_str());
  90. return false;
  91. }
  92. if(args.size() == 4 && args[3] == "CACHE")
  93. {
  94. if(programArgs.size() && storeArgs.size())
  95. {
  96. this->Makefile->AddCacheDefinition(storeArgs.c_str(),
  97. programArgs.c_str(),
  98. "",
  99. args[2] == "PATH" ? cmCacheManager::FILEPATH
  100. : cmCacheManager::STRING);
  101. }
  102. this->Makefile->AddCacheDefinition(args[0].c_str(),
  103. result.c_str(),
  104. "",
  105. args[2] == "PATH" ? cmCacheManager::FILEPATH
  106. : cmCacheManager::STRING);
  107. }
  108. else
  109. {
  110. if(programArgs.size() && storeArgs.size())
  111. {
  112. this->Makefile->AddDefinition(storeArgs.c_str(), programArgs.c_str());
  113. }
  114. this->Makefile->AddDefinition(args[0].c_str(), result.c_str());
  115. }
  116. return true;
  117. }