cmFindProgramCommand.cxx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #include "cmFindProgramCommand.h"
  33. #include "cmCacheManager.h"
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. // cmFindProgramCommand
  37. bool cmFindProgramCommand::Invoke(std::vector<std::string>& args)
  38. {
  39. if(args.size() < 2 )
  40. {
  41. this->SetError("called with incorrect number of arguments");
  42. return false;
  43. }
  44. std::vector<std::string>::iterator i = args.begin();
  45. // Use the first argument as the name of something to be defined
  46. const char* define = (*i).c_str();
  47. i++; // move iterator to next arg
  48. // Now check and see if the value has been stored in the cache
  49. // already, if so use that value and don't look for the program
  50. const char* cacheValue
  51. = cmCacheManager::GetInstance()->GetCacheValue(define);
  52. if(cacheValue)
  53. {
  54. m_Makefile->AddDefinition(define, cacheValue);
  55. return true;
  56. }
  57. // if it is not in the cache, then search the system path
  58. // add any user specified paths
  59. std::vector<std::string> path;
  60. for (unsigned int j = 2; j < args.size(); j++)
  61. {
  62. // expand variables
  63. std::string exp = args[j];
  64. m_Makefile->ExpandVariablesInString(exp);
  65. path.push_back(exp);
  66. }
  67. cmSystemTools::GetPath(path);
  68. for(unsigned int k=0; k < path.size(); k++)
  69. {
  70. std::string tryPath = path[k];
  71. tryPath += "/";
  72. tryPath += *i;
  73. #ifdef _WIN32
  74. tryPath += ".exe";
  75. #endif
  76. if(cmSystemTools::FileExists(tryPath.c_str()))
  77. {
  78. // Save the value in the cache
  79. cmCacheManager::GetInstance()->AddCacheEntry(define,
  80. tryPath.c_str(),
  81. "Path to a program.",
  82. cmCacheManager::FILEPATH);
  83. m_Makefile->AddDefinition(define, tryPath.c_str());
  84. return true;
  85. }
  86. }
  87. return false;
  88. }