cmGetFilenameComponentCommand.cxx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 = m_Makefile->GetDefinition(args[0].c_str());
  28. if(cacheValue && strcmp(cacheValue, "NOTFOUND"))
  29. {
  30. return true;
  31. }
  32. }
  33. std::string result;
  34. std::string filename = args[1];
  35. std::string storeArgs;
  36. std::string programArgs;
  37. if (args[2] == "PATH")
  38. {
  39. result = cmSystemTools::GetFilenamePath(filename);
  40. }
  41. else if (args[2] == "NAME")
  42. {
  43. result = cmSystemTools::GetFilenameName(filename);
  44. }
  45. else if (args[2] == "PROGRAM")
  46. {
  47. for(unsigned int i=2; i < args.size(); ++i)
  48. {
  49. if(args[i] == "PROGRAM_ARGS")
  50. {
  51. i++;
  52. if(i < args.size())
  53. {
  54. storeArgs = args[i];
  55. }
  56. }
  57. }
  58. cmSystemTools::SplitProgramFromArgs(filename.c_str(), result, programArgs);
  59. }
  60. else if (args[2] == "EXT")
  61. {
  62. result = cmSystemTools::GetFilenameExtension(filename);
  63. }
  64. else if (args[2] == "NAME_WE")
  65. {
  66. result = cmSystemTools::GetFilenameWithoutExtension(filename);
  67. }
  68. else
  69. {
  70. std::string err = "unknow component " + args[2];
  71. this->SetError(err.c_str());
  72. return false;
  73. }
  74. if(args.size() == 4 && args[3] == "CACHE")
  75. {
  76. if(programArgs.size() && storeArgs.size())
  77. {
  78. m_Makefile->AddCacheDefinition(storeArgs.c_str(),
  79. programArgs.c_str(),
  80. "",
  81. args[2] == "PATH" ? cmCacheManager::FILEPATH
  82. : cmCacheManager::STRING);
  83. }
  84. m_Makefile->AddCacheDefinition(args[0].c_str(),
  85. result.c_str(),
  86. "",
  87. args[2] == "PATH" ? cmCacheManager::FILEPATH
  88. : cmCacheManager::STRING);
  89. }
  90. else
  91. {
  92. if(programArgs.size() && storeArgs.size())
  93. {
  94. m_Makefile->AddDefinition(storeArgs.c_str(), programArgs.c_str());
  95. }
  96. m_Makefile->AddDefinition(args[0].c_str(), result.c_str());
  97. }
  98. return true;
  99. }