cmGetFilenameComponentCommand.cxx 3.5 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
  17. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  18. {
  19. if(args.size() < 3)
  20. {
  21. this->SetError("called with incorrect number of arguments");
  22. return false;
  23. }
  24. // Check and see if the value has been stored in the cache
  25. // already, if so use that value
  26. if(args.size() == 4 && args[3] == "CACHE")
  27. {
  28. const char* cacheValue = this->Makefile->GetDefinition(args[0].c_str());
  29. if(cacheValue && !cmSystemTools::IsNOTFOUND(cacheValue))
  30. {
  31. return true;
  32. }
  33. }
  34. std::string result;
  35. std::string filename = args[1];
  36. cmSystemTools::ExpandRegistryValues(filename);
  37. std::string storeArgs;
  38. std::string programArgs;
  39. if (args[2] == "PATH")
  40. {
  41. result = cmSystemTools::GetFilenamePath(filename);
  42. }
  43. else if (args[2] == "NAME")
  44. {
  45. result = cmSystemTools::GetFilenameName(filename);
  46. }
  47. else if (args[2] == "PROGRAM")
  48. {
  49. for(unsigned int i=2; i < args.size(); ++i)
  50. {
  51. if(args[i] == "PROGRAM_ARGS")
  52. {
  53. i++;
  54. if(i < args.size())
  55. {
  56. storeArgs = args[i];
  57. }
  58. }
  59. }
  60. cmSystemTools::SplitProgramFromArgs(filename.c_str(),
  61. result, programArgs);
  62. }
  63. else if (args[2] == "EXT")
  64. {
  65. result = cmSystemTools::GetFilenameExtension(filename);
  66. }
  67. else if (args[2] == "NAME_WE")
  68. {
  69. result = cmSystemTools::GetFilenameWithoutExtension(filename);
  70. }
  71. else if (args[2] == "ABSOLUTE")
  72. {
  73. // If the path given is relative evaluate it relative to the
  74. // current source directory.
  75. if(!cmSystemTools::FileIsFullPath(filename.c_str()))
  76. {
  77. std::string fname = this->Makefile->GetCurrentDirectory();
  78. if(!fname.empty())
  79. {
  80. fname += "/";
  81. fname += filename;
  82. filename = fname;
  83. }
  84. }
  85. // Collapse the path to its simplest form.
  86. result = cmSystemTools::CollapseFullPath(filename.c_str());
  87. }
  88. else
  89. {
  90. std::string err = "unknown component " + args[2];
  91. this->SetError(err.c_str());
  92. return false;
  93. }
  94. if(args.size() == 4 && args[3] == "CACHE")
  95. {
  96. if(programArgs.size() && storeArgs.size())
  97. {
  98. this->Makefile->AddCacheDefinition
  99. (storeArgs.c_str(), programArgs.c_str(),
  100. "", args[2] == "PATH" ? cmCacheManager::FILEPATH
  101. : cmCacheManager::STRING);
  102. }
  103. this->Makefile->AddCacheDefinition
  104. (args[0].c_str(), result.c_str(), "",
  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. }