cmFindProgramCommand.cxx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2000 National Library of Medicine
  8. All rights reserved.
  9. See COPYRIGHT.txt for copyright details.
  10. =========================================================================*/
  11. #include "cmFindProgramCommand.h"
  12. #include "cmCacheManager.h"
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. // cmFindProgramCommand
  16. bool cmFindProgramCommand::Invoke(std::vector<std::string>& args)
  17. {
  18. if(args.size() < 2 )
  19. {
  20. this->SetError("called with incorrect number of arguments");
  21. return false;
  22. }
  23. std::vector<std::string>::iterator i = args.begin();
  24. // Use the first argument as the name of something to be defined
  25. const char* define = (*i).c_str();
  26. i++; // move iterator to next arg
  27. // Now check and see if the value has been stored in the cache
  28. // already, if so use that value and don't look for the program
  29. const char* cacheValue
  30. = cmCacheManager::GetInstance()->GetCacheValue(define);
  31. if(cacheValue)
  32. {
  33. m_Makefile->AddDefinition(define, cacheValue);
  34. return true;
  35. }
  36. // if it is not in the cache, then search the system path
  37. // add any user specified paths
  38. std::vector<std::string> path;
  39. for (unsigned int j = 2; j < args.size(); j++)
  40. {
  41. // expand variables
  42. std::string exp = args[j];
  43. m_Makefile->ExpandVariablesInString(exp);
  44. path.push_back(exp);
  45. }
  46. cmSystemTools::GetPath(path);
  47. for(unsigned int k=0; k < path.size(); k++)
  48. {
  49. std::string tryPath = path[k];
  50. tryPath += "/";
  51. tryPath += *i;
  52. #ifdef _WIN32
  53. tryPath += ".exe";
  54. #endif
  55. if(cmSystemTools::FileExists(tryPath.c_str()))
  56. {
  57. // Save the value in the cache
  58. cmCacheManager::GetInstance()->AddCacheEntry(define,
  59. tryPath.c_str(),
  60. "Path to a program.",
  61. cmCacheManager::FILEPATH);
  62. m_Makefile->AddDefinition(define, tryPath.c_str());
  63. return true;
  64. }
  65. }
  66. return false;
  67. }