cmITKWrapTclCommand.cxx 6.63 KB
Newer Older
1 2
/*=========================================================================

3
  Program:   CMake - Cross-Platform Makefile Generator
4 5 6 7 8
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

9 10
  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#include "cmITKWrapTclCommand.h"
#include "cmMakeDepend.h"

cmITKWrapTclCommand::cmITKWrapTclCommand():
  m_MakeDepend(new cmMakeDepend)
{
}

cmITKWrapTclCommand::~cmITKWrapTclCommand()
{
  delete m_MakeDepend;
}

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


void 
cmITKWrapTclCommand::AddDependencies(cmDependInformation const *info,
                                     std::vector<std::string>& depends,
                                     std::set<cmDependInformation const*>& visited)
{
  if(!info)
    {
    return;
    }
  // add info to the visited set
  visited.insert(info);
    
  // add this dependency and the recurse
  // now recurse with info's dependencies
  for(cmDependInformation::DependencySet::const_iterator d = 
        info->m_DependencySet.begin();
      d != info->m_DependencySet.end(); ++d)
    {
    if (visited.find(*d) == visited.end())
      { 
      if((*d)->m_FullPath != "")
        {
        depends.push_back((*d)->m_FullPath);
        }
      this->AddDependencies(*d,depends,visited);
      }
    }
}

61
// cmITKWrapTclCommand
62
bool cmITKWrapTclCommand::InitialPass(std::vector<std::string> const& args)
63
{
64
  if(args.size() < 2 )
65 66 67 68 69 70
    {
    this->SetError("called with incorrect number of arguments");
    return false;
    }
  // keep the target name
  m_TargetName = args[0];
71
  m_Target = &m_Makefile->GetTargets()[m_TargetName.c_str()];
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  
  // Prepare the dependency generator.
  m_MakeDepend->SetMakefile(m_Makefile);
  
  for(std::vector<std::string>::const_iterator i = (args.begin() + 1);
      i != args.end(); ++i)
    {
    if(!this->CreateCableRule((*i).c_str())) { return false; }
    }
  
  return true;
}

bool cmITKWrapTclCommand::CreateCableRule(const char* configFile)
{
  std::string tclFile =
88
    cmSystemTools::GetFilenameWithoutExtension(configFile);
89 90 91 92 93 94 95 96 97 98 99
  tclFile += "_tcl";
  
  std::string inFile = m_Makefile->GetCurrentDirectory();
  inFile += "/";
  inFile += configFile;
  
  std::string outDir = m_Makefile->GetCurrentOutputDirectory();
  
  // Generate the rule to run cable to generate wrappers.
  std::string command = this->GetCableFromCache();
  std::vector<std::string> depends;
100 101 102 103 104 105 106 107 108
  
  // Special case for CMake's wrapping test.  Don't add dependency if
  // it is a dummy executable.
  if(command != "echo")
    {
    depends.push_back(command);
    }
  
  std::vector<std::string> commandArgs;
109 110 111 112 113
  commandArgs.push_back(inFile);
  commandArgs.push_back("-tcl");  
  std::string tmp = tclFile+".cxx";
  commandArgs.push_back(tmp);
#if !defined(_WIN32) || defined(__CYGWIN__)
114
  tmp = "${CMAKE_CXX_COMPILER}";
115
  m_Makefile->ExpandVariablesInString(tmp);
116 117 118 119
  if(tmp.length() > 0)
    {
    commandArgs.push_back("--gccxml-compiler");
    commandArgs.push_back(tmp);
120 121 122 123
    tmp = "${CMAKE_CXX_FLAGS}";
    m_Makefile->ExpandVariablesInString(tmp);
    commandArgs.push_back("--gccxml-cxxflags");
    commandArgs.push_back(tmp);
124
    }
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
#else
  const char* genName = m_Makefile->GetDefinition("CMAKE_GENERATOR");
  if (genName)
    {
    std::string gen = genName;
    if(gen == "Visual Studio 6")
      {
      commandArgs.push_back("--gccxml-compiler");
      commandArgs.push_back("msvc6");
      tmp = "${CMAKE_CXX_FLAGS}";
      m_Makefile->ExpandVariablesInString(tmp);
      commandArgs.push_back("--gccxml-cxxflags");
      commandArgs.push_back(tmp);
      }
    else if(gen == "Visual Studio 7")
      {
      commandArgs.push_back("--gccxml-compiler");
      commandArgs.push_back("msvc7");
      tmp = "${CMAKE_CXX_FLAGS}";
      m_Makefile->ExpandVariablesInString(tmp);
      commandArgs.push_back("--gccxml-cxxflags");
      commandArgs.push_back(tmp);
      }
    else if(gen == "NMake Makefiles")
      {
      tmp = "${CMAKE_CXX_COMPILER}";
      m_Makefile->ExpandVariablesInString(tmp);
      commandArgs.push_back("--gccxml-compiler");
      commandArgs.push_back(tmp);
      tmp = "${CMAKE_CXX_FLAGS}";
      m_Makefile->ExpandVariablesInString(tmp);
      commandArgs.push_back("--gccxml-cxxflags");
      commandArgs.push_back(tmp);      
      }
    }
160
#endif
161 162 163 164 165 166
  const char* gccxml = m_Makefile->GetDefinition("ITK_GCCXML_EXECUTABLE");
  if(gccxml)
    {
    commandArgs.push_back("--gccxml");
    commandArgs.push_back(gccxml);
    }
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
  tmp = "-I";
  tmp += m_Makefile->GetStartDirectory();
  commandArgs.push_back(tmp);
  const std::vector<std::string>& includes = 
    m_Makefile->GetIncludeDirectories();
  for(std::vector<std::string>::const_iterator i = includes.begin();
      i != includes.end(); ++i)
    {
    tmp = "-I";
    tmp += i->c_str();
    m_Makefile->ExpandVariablesInString(tmp);
    commandArgs.push_back(tmp);
    }
  
  // Get the dependencies.
  const cmDependInformation* info =
    m_MakeDepend->FindDependencies(inFile.c_str());
  if (info)
    {
186 187
    std::set<cmDependInformation const*> visited;
    this->AddDependencies(info, depends, visited);
188 189 190 191 192 193 194 195 196 197
    }
  
  std::vector<std::string> outputs;
  outputs.push_back(outDir+"/"+tclFile+".cxx");
  
  m_Makefile->AddCustomCommand(inFile.c_str(),
                               command.c_str(),
                               commandArgs, depends,
                               outputs, m_TargetName.c_str());
  
198
  // Add the source to the makefile.
199 200 201 202 203
  cmSourceFile file;
  file.SetName(tclFile.c_str(), outDir.c_str(), "cxx", false);
  // Set dependency hints.
  file.GetDepends().push_back(inFile.c_str());
  file.GetDepends().push_back("CableTclFacility/ctCalls.h");
204
  m_Makefile->AddSource(file);
205 206
  
  // Add the generated source to the package's source list.
207
  std::string srcname = file.GetSourceName() + ".cxx";
208 209
  m_Target->GetSourceLists().push_back(srcname);
  
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
  return true;
}

/**
 * Get the "CABLE" cache entry value.  If there is no cache entry for CABLE,
 * one will be created and initialized to NOTFOUND.
 */
std::string cmITKWrapTclCommand::GetCableFromCache() const
{
  const char* cable =
    m_Makefile->GetDefinition("CABLE");
  if(cable)
    { return cable; }

  m_Makefile->AddCacheDefinition("CABLE",
225
                                 "CABLE-NOTFOUND",
226 227
                                 "Path to CABLE executable.",
                                 cmCacheManager::FILEPATH);
228
  return "CABLE-NOTFOUND";
229
}