cmDepends.cxx 8.86 KB
Newer Older
1 2 3
/*============================================================================
  CMake - Cross Platform Makefile Generator
  Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4

5 6
  Distributed under the OSI-approved BSD License (the "License");
  see accompanying file Copyright.txt for details.
7

8 9 10 11
  This software is distributed WITHOUT ANY WARRANTY; without even the
  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the License for more information.
============================================================================*/
12 13
#include "cmDepends.h"

14 15
#include "cmLocalGenerator.h"
#include "cmMakefile.h"
16 17
#include "cmGeneratedFileStream.h"
#include "cmSystemTools.h"
18
#include "cmFileTimeComparison.h"
Bill Hoffman's avatar
Bill Hoffman committed
19
#include <string.h>
20

21
//----------------------------------------------------------------------------
22
cmDepends::cmDepends(cmLocalGenerator* lg, const char* targetDir):
23
  CompileDirectory(),
24
  LocalGenerator(lg),
25 26
  Verbose(false),
  FileComparison(0),
27
  TargetDirectory(targetDir),
28
  MaxPath(16384),
Ken Martin's avatar
Ken Martin committed
29 30
  Dependee(new char[MaxPath]),
  Depender(new char[MaxPath])
31
{
32 33 34 35 36
}

//----------------------------------------------------------------------------
cmDepends::~cmDepends()
{
Ken Martin's avatar
Ken Martin committed
37 38
  delete [] this->Dependee;
  delete [] this->Depender;
39 40
}

41
//----------------------------------------------------------------------------
42 43
bool cmDepends::Write(std::ostream &makeDepends,
                      std::ostream &internalDepends)
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
  // Lookup the set of sources to scan.
  std::string srcLang = "CMAKE_DEPENDS_CHECK_";
  srcLang += this->Language;
  cmMakefile* mf = this->LocalGenerator->GetMakefile();
  const char* srcStr = mf->GetSafeDefinition(srcLang.c_str());
  std::vector<std::string> pairs;
  cmSystemTools::ExpandListArgument(srcStr, pairs);

  for(std::vector<std::string>::iterator si = pairs.begin();
      si != pairs.end();)
    {
    // Get the source and object file.
    std::string const& src = *si++;
    if(si == pairs.end()) { break; }
    std::string obj = *si++;

    // Make sure the object file is relative to the top of the build tree.
    obj = this->LocalGenerator->Convert(obj.c_str(),
                                        cmLocalGenerator::HOME_OUTPUT,
                                        cmLocalGenerator::MAKEFILE);

    // Write the dependencies for this pair.
    if(!this->WriteDependencies(src.c_str(), obj.c_str(),
                                makeDepends, internalDepends))
      {
      return false;
      }
    }

74 75 76 77 78 79 80
  return this->Finalize(makeDepends, internalDepends);
}

//----------------------------------------------------------------------------
bool cmDepends::Finalize(std::ostream&,
                         std::ostream&)
{
81
  return true;
82 83 84
}

//----------------------------------------------------------------------------
85 86
bool cmDepends::Check(const char *makeFile, const char *internalFile,
                      std::map<std::string, DependencyVector>& validDeps)
87
{
88 89
  // Dependency checks must be done in proper working directory.
  std::string oldcwd = ".";
Ken Martin's avatar
Ken Martin committed
90
  if(this->CompileDirectory != ".")
91
    {
92 93 94
    // Get the CWD but do not call CollapseFullPath because
    // we only need it to cd back, and the form does not matter
    oldcwd = cmSystemTools::GetCurrentWorkingDirectory(false);
Ken Martin's avatar
Ken Martin committed
95
    cmSystemTools::ChangeDirectory(this->CompileDirectory.c_str());
96 97
    }

98
  // Check whether dependencies must be regenerated.
99
  bool okay = true;
100
  std::ifstream fin(internalFile);
101
  if(!(fin && this->CheckDependencies(fin, validDeps)))
102 103
    {
    // Clear all dependencies so they will be regenerated.
104
    this->Clear(makeFile);
105 106
    cmSystemTools::RemoveFile(internalFile);
    okay = false;
107
    }
108 109 110 111 112 113

  // Restore working directory.
  if(oldcwd != ".")
    {
    cmSystemTools::ChangeDirectory(oldcwd.c_str());
    }
114 115

  return okay;
116 117 118
}

//----------------------------------------------------------------------------
119
void cmDepends::Clear(const char *file)
120
{
121
  // Print verbose output.
Ken Martin's avatar
Ken Martin committed
122
  if(this->Verbose)
123 124
    {
    cmOStringStream msg;
125
    msg << "Clearing dependencies in \"" << file << "\"." << std::endl;
126 127 128
    cmSystemTools::Stdout(msg.str().c_str());
    }

129
  // Write an empty dependency file.
130
  cmGeneratedFileStream depFileStream(file);
131
  depFileStream
132
    << "# Empty dependencies file\n"
133 134 135
    << "# This may be replaced when dependencies are built." << std::endl;
}

136 137 138 139 140 141 142 143
//----------------------------------------------------------------------------
bool cmDepends::WriteDependencies(const char*, const char*,
                                  std::ostream&, std::ostream&)
{
  // This should be implemented by the subclass.
  return false;
}

144
//----------------------------------------------------------------------------
145 146
bool cmDepends::CheckDependencies(std::istream& internalDepends,
                            std::map<std::string, DependencyVector>& validDeps)
147 148 149 150 151
{
  // Parse dependencies from the stream.  If any dependee is missing
  // or newer than the depender then dependencies should be
  // regenerated.
  bool okay = true;
152
  bool dependerExists = false;
153 154
  DependencyVector* currentDependencies = 0;

Ken Martin's avatar
Ken Martin committed
155
  while(internalDepends.getline(this->Dependee, this->MaxPath))
156
    {
Ken Martin's avatar
Ken Martin committed
157 158
    if ( this->Dependee[0] == 0 || this->Dependee[0] == '#' || 
         this->Dependee[0] == '\r' )
159 160 161 162
      {
      continue;
      }
    size_t len = internalDepends.gcount()-1;
Ken Martin's avatar
Ken Martin committed
163
    if ( this->Dependee[len-1] == '\r' )
164 165
      {
      len --;
Ken Martin's avatar
Ken Martin committed
166
      this->Dependee[len] = 0;
167
      }
Ken Martin's avatar
Ken Martin committed
168
    if ( this->Dependee[0] != ' ' )
169
      {
Ken Martin's avatar
Ken Martin committed
170
      memcpy(this->Depender, this->Dependee, len+1);
171 172 173 174 175
      // Calling FileExists() for the depender here saves in many cases 50%
      // of the calls to FileExists() further down in the loop. E.g. for
      // kdelibs/khtml this reduces the number of calls from 184k down to 92k,
      // or the time for cmake -E cmake_depends from 0.3 s down to 0.21 s.
      dependerExists = cmSystemTools::FileExists(this->Depender);
176 177 178
      DependencyVector tmp;
      validDeps[this->Depender] = tmp;
      currentDependencies = &validDeps[this->Depender];
179 180 181 182 183 184 185 186 187 188 189 190 191
      continue;
      }
    /*
    // Parse the dependency line.
    if(!this->ParseDependency(line.c_str()))
      {
      continue;
      }
      */

    // Dependencies must be regenerated if the dependee does not exist
    // or if the depender exists and is older than the dependee.
    bool regenerate = false;
Ken Martin's avatar
Ken Martin committed
192 193
    const char* dependee = this->Dependee+1;
    const char* depender = this->Depender;
194 195 196 197 198
    if (currentDependencies != 0)
      {
      currentDependencies->push_back(dependee);
      }

199 200 201 202 203 204
    if(!cmSystemTools::FileExists(dependee))
      {
      // The dependee does not exist.
      regenerate = true;

      // Print verbose output.
Ken Martin's avatar
Ken Martin committed
205
      if(this->Verbose)
206 207 208 209 210 211 212 213
        {
        cmOStringStream msg;
        msg << "Dependee \"" << dependee
            << "\" does not exist for depender \""
            << depender << "\"." << std::endl;
        cmSystemTools::Stdout(msg.str().c_str());
        }
      }
214
    else if(dependerExists)
215 216 217
      {
      // The dependee and depender both exist.  Compare file times.
      int result = 0;
Ken Martin's avatar
Ken Martin committed
218
      if((!this->FileComparison->FileTimeCompare(depender, dependee,
219 220 221 222 223 224
                                             &result) || result < 0))
        {
        // The depender is older than the dependee.
        regenerate = true;

        // Print verbose output.
Ken Martin's avatar
Ken Martin committed
225
        if(this->Verbose)
226 227 228 229 230 231 232 233 234 235 236 237 238 239
          {
          cmOStringStream msg;
          msg << "Dependee \"" << dependee
              << "\" is newer than depender \""
              << depender << "\"." << std::endl;
          cmSystemTools::Stdout(msg.str().c_str());
          }
        }
      }
    if(regenerate)
      {
      // Dependencies must be regenerated.
      okay = false;

240 241 242 243 244 245 246 247
      // Remove the information of this depender from the map, it needs
      // to be rescanned
      if (currentDependencies != 0)
        {
        validDeps.erase(this->Depender);
        currentDependencies = 0;
        }

248
      // Remove the depender to be sure it is rebuilt.
249 250 251 252 253
      if (dependerExists)
        {
        cmSystemTools::RemoveFile(depender);
        dependerExists = false;
        }
254 255 256 257 258 259
      }
    }

  return okay;
}

260 261 262
//----------------------------------------------------------------------------
void cmDepends::SetIncludePathFromLanguage(const char* lang)
{
263
  // Look for the new per "TARGET_" variant first:
264
  const char * includePath = 0;
265 266
  std::string includePathVar = "CMAKE_";
  includePathVar += lang;
267
  includePathVar += "_TARGET_INCLUDE_PATH";
268
  cmMakefile* mf = this->LocalGenerator->GetMakefile();
269 270
  includePath = mf->GetDefinition(includePathVar.c_str());
  if(includePath)
271 272 273
    {
    cmSystemTools::ExpandListArgument(includePath, this->IncludePath);
    }
274 275 276 277 278 279
  else
    {
    // Fallback to the old directory level variable if no per-target var:
    includePathVar = "CMAKE_";
    includePathVar += lang;
    includePathVar += "_INCLUDE_PATH";
280 281
    includePath = mf->GetDefinition(includePathVar.c_str());
    if(includePath)
282 283 284 285
      {
      cmSystemTools::ExpandListArgument(includePath, this->IncludePath);
      }
    }
286
}