cmGetFilenameComponentCommand.cxx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. cmSystemTools::ExpandRegistryValues(filename);
  34. std::string storeArgs;
  35. std::string programArgs;
  36. if (args[2] == "PATH")
  37. {
  38. result = cmSystemTools::GetFilenamePath(filename);
  39. }
  40. else if (args[2] == "NAME")
  41. {
  42. result = cmSystemTools::GetFilenameName(filename);
  43. }
  44. else if (args[2] == "PROGRAM")
  45. {
  46. for(unsigned int i=2; i < args.size(); ++i)
  47. {
  48. if(args[i] == "PROGRAM_ARGS")
  49. {
  50. i++;
  51. if(i < args.size())
  52. {
  53. storeArgs = args[i];
  54. }
  55. }
  56. }
  57. cmSystemTools::SplitProgramFromArgs(filename.c_str(),
  58. 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 if (args[2] == "ABSOLUTE" ||
  69. args[2] == "REALPATH")
  70. {
  71. // Collapse the path to its simplest form.
  72. // If the path given is relative evaluate it relative to the
  73. // current source directory.
  74. result = cmSystemTools::CollapseFullPath(
  75. filename.c_str(), this->Makefile->GetCurrentDirectory());
  76. if(args[2] == "REALPATH")
  77. {
  78. // Resolve symlinks if possible
  79. result = cmSystemTools::GetRealPath(filename.c_str());
  80. }
  81. }
  82. else
  83. {
  84. std::string err = "unknown component " + args[2];
  85. this->SetError(err.c_str());
  86. return false;
  87. }
  88. if(args.size() == 4 && args[3] == "CACHE")
  89. {
  90. if(programArgs.size() && storeArgs.size())
  91. {
  92. this->Makefile->AddCacheDefinition
  93. (storeArgs.c_str(), programArgs.c_str(),
  94. "", args[2] == "PATH" ? cmCacheManager::FILEPATH
  95. : cmCacheManager::STRING);
  96. }
  97. this->Makefile->AddCacheDefinition
  98. (args[0].c_str(), result.c_str(), "",
  99. args[2] == "PATH" ? cmCacheManager::FILEPATH
  100. : cmCacheManager::STRING);
  101. }
  102. else
  103. {
  104. if(programArgs.size() && storeArgs.size())
  105. {
  106. this->Makefile->AddDefinition(storeArgs.c_str(), programArgs.c_str());
  107. }
  108. this->Makefile->AddDefinition(args[0].c_str(), result.c_str());
  109. }
  110. return true;
  111. }