summaryrefslogtreecommitdiffstats
path: root/Source/cmUtilitySourceCommand.cxx
blob: 0a8c9b75d6b55e1246d7904f4932c22d017bb184 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) 2000 National Library of Medicine
  All rights reserved.

  See COPYRIGHT.txt for copyright details.

=========================================================================*/
#include "cmUtilitySourceCommand.h"

// cmUtilitySourceCommand
bool cmUtilitySourceCommand::Invoke(std::vector<std::string>& args)
{
  if(args.size() < 3)
    {
    this->SetError("called with incorrect number of arguments");
    return false;
    }
  
  std::vector<std::string>::const_iterator arg = args.begin();
  
  // The first argument is the cache entry name.
  std::string cacheEntry = *arg++;
  const char* cacheValue =
    cmCacheManager::GetInstance()->GetCacheValue(cacheEntry.c_str());
  // If it exists already, we are done.
  if(cacheValue)
    {
    // Set the makefile's definition with the cache value.
    m_Makefile->AddDefinition(cacheEntry.c_str(), cacheValue);
    return true;
    }
  
  // The second argument is the utility's executable name, which will be
  // needed later.
  std::string utilityName = *arg++;
  
  // The third argument specifies the relative directory of the source
  // of the utility.
  std::string relativeSource = *arg++;
  std::string utilitySource = m_Makefile->GetCurrentDirectory();
  utilitySource = utilitySource+"/"+relativeSource;
  
  // If the directory doesn't exist, the source has not been included.
  if(!cmSystemTools::FileExists(utilitySource.c_str()))
    { return true; }
  
  // Make sure all the files exist in the source directory.
  while(arg != args.end())
    {
    std::string file = utilitySource+"/"+*arg++;
    if(!cmSystemTools::FileExists(file.c_str()))
      { return true; }
    }
  
  // The source exists.  Construct the cache entry for the executable's
  // location.
  std::string cmakeCFG = m_Makefile->GetDefinition("CMAKE_CFG");
  std::string utilityExecutable = m_Makefile->GetCurrentOutputDirectory();
  utilityExecutable =
    (utilityExecutable+"/"+relativeSource+"/"+cmakeCFG+"/"
     +utilityName+cmSystemTools::GetExecutableExtension());
  
  // Enter the value into the cache.
  cmCacheManager::GetInstance()->AddCacheEntry(cacheEntry.c_str(),
                                               utilityExecutable.c_str(),
                                               cmCacheManager::FILEPATH);
  
  // Set the definition in the makefile.
  m_Makefile->AddDefinition(cacheEntry.c_str(), utilityExecutable.c_str());
  
  return true;
}