cmFindFileCommand.cxx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 "cmFindFileCommand.h"
  12. #include "cmCacheManager.h"
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. // cmFindFileCommand
  16. bool cmFindFileCommand::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. if(strcmp(cacheValue, "NOTFOUND") != 0)
  34. {
  35. m_Makefile->AddDefinition(define, cacheValue);
  36. }
  37. return true;
  38. }
  39. // if it is not in the cache, then search the system path
  40. std::vector<std::string> path;
  41. // add any user specified paths
  42. for (unsigned int j = 2; j < args.size(); j++)
  43. {
  44. // expand variables
  45. std::string exp = args[j];
  46. m_Makefile->ExpandVariablesInString(exp);
  47. path.push_back(exp);
  48. }
  49. // add the standard path
  50. cmSystemTools::GetPath(path);
  51. for(unsigned int k=0; k < path.size(); k++)
  52. {
  53. std::string tryPath = path[k];
  54. tryPath += "/";
  55. tryPath += *i;
  56. if(cmSystemTools::FileExists(tryPath.c_str()))
  57. {
  58. // Save the value in the cache
  59. cmCacheManager::GetInstance()->AddCacheEntry(define,
  60. tryPath.c_str(),
  61. "Path to a file.",
  62. cmCacheManager::FILEPATH);
  63. m_Makefile->AddDefinition(define, tryPath.c_str());
  64. return true;
  65. }
  66. }
  67. return false;
  68. }