summaryrefslogtreecommitdiffstats
path: root/Source/cmUtilitySourceCommand.cxx
blob: 0cb83ddb3c34181ef8ac0f0c8a94043a0e8475bb (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
82
83
84
85
86
/*=========================================================================

  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.
  std::string cmakeCFGout = m_Makefile->GetDefinition("CMAKE_CFG_OUTDIR");
  std::string utilityDirectory = m_Makefile->GetCurrentOutputDirectory();
  utilityDirectory += "/"+relativeSource;
  
  // Tell the makefile where to look for this utility.
  m_Makefile->AddUtilityDirectory(utilityDirectory.c_str());
  
  // Construct the cache entry for the executable's location.
  std::string utilityExecutable =
    utilityDirectory+"/"+cmakeCFGout+"/"
    +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;
}