summaryrefslogtreecommitdiffstats
path: root/Source/cmMakefile.cxx
blob: 426f9569d4138d14bf8692df4728899ade0a1ea2 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#ifdef _MSC_VER
#pragma warning ( disable : 4786 )
#endif
#include "cmMakefile.h"
#include "cmClassFile.h"
#include "cmDirectory.h"
#include "cmSystemTools.h"
#include <fstream>
#include <iostream>


// default is not to be building executables
cmMakefile::cmMakefile()
{
  m_Executables = false;
}


// call print on all the classes in the makefile
void cmMakefile::Print()
{
  for(int i = 0; i < m_Classes.size(); i++)
    m_Classes[i].Print();
}

// Parse the given CMakeLists.txt file into a list of classes.
bool cmMakefile::ReadMakefile(const char* filename)
{
  m_BuildFlags.SetSourceHomeDirectory(this->GetHomeDirectory());
  m_BuildFlags.SetStartDirectory(this->GetCurrentDirectory());
  m_BuildFlags.ParseDirectories();
  m_BuildFlags.ExpandVaribles(this);
 
  std::ifstream fin(filename);
  if(!fin)
    {
    std::cerr << "error can not open file " << filename << std::endl;
    return false;
    }
  char inbuffer[2048];
  while ( fin.getline(inbuffer, 2047 ) )
    {
    std::string line = inbuffer;
    cmClassFile file;
    std::string::size_type pos = line.find("SOURCE_FILES");
    if((pos != std::string::npos) && (pos == 0 ) )
      {
      if(line.find("\\") != std::string::npos)
	{
	this->ReadClasses(fin, false);
	}
      }
#ifdef _WIN32
    else if(line.find("WIN32_SOURCE_FILES") != std::string::npos)
      {
      if(line.find("\\") != std::string::npos)
	{
	this->ReadClasses(fin, false);
	}
      }
#else
    else if(line.find("UNIX_SOURCE_FILES") != std::string::npos)
      {
      if(line.find("\\") != std::string::npos)
	{
	this->ReadClasses(fin, false);
	}
      }
#endif
    else if(line.find("ABSTRACT_CLASSES") != std::string::npos)
      {
      if(line.find("\\") != std::string::npos)
	{
	this->ReadClasses(fin, true);
	}
      }
    else if(line.find("AUX_SOURCE_DIRECTORY") != std::string::npos)
      {
      this->ReadTemplateInstanceDirectory(line);
      }
    else if(line.find("SUBDIRS") != std::string::npos)
      {
      if(line.find("\\") != std::string::npos)
	{
        cmSystemTools::ReadList(m_SubDirectories, fin);
	}
      }
    else if(line.find("EXECUTABLES") != std::string::npos || line.find("TESTS") != std::string::npos )
      {
      if(line.find("\\") != std::string::npos)
	{
	this->ReadClasses(fin, false);
	m_Executables = true;
	}
      }
    else if(line.find("BEGIN MAKE VERBATIM") != std::string::npos)
      {
      char inbuffer[2048];
      bool done = false;
      m_MakeVerbatim.push_back("# Begin CMakeLists Verbatim\n");
      while(!done)
        {
        fin.getline(inbuffer, 2047);
        m_MakeVerbatim.push_back(inbuffer);
        if((m_MakeVerbatim.end()-1)->find("END MAKE VERBATIM") 
           != std::string::npos )
          {
          done = true;
          *(m_MakeVerbatim.end()-1) = "# End CMakeLists VERBATIM\n\n";
          }
        }
      }
    else if(line.find("LIBRARY") != std::string::npos)
      {
      std::string libname = cmSystemTools::ExtractVariable("LIBRARY", 
                                                           line.c_str());
      this->SetLibraryName(libname.c_str());
      }
    else if(line.find("PROJECT") != std::string::npos)
      {
      std::string libname = cmSystemTools::ExtractVariable("PROJECT", 
                                                           line.c_str());
      this->SetProjectName(libname.c_str());
      }
    }
  return true;
}
  

// Read a list from the Makefile stream
void cmMakefile::ReadClasses(std::ifstream& fin,
			      bool abstract)
{
  char inbuffer[2048];
  bool done = false;
  while (!done)
    {  
    // read a line from the makefile
    fin.getline(inbuffer, 2047); 
    // convert to a string class
    std::string classname = inbuffer;
    // if the line does not end in \ then we are at the
    // end of the list
    if(classname.find('\\') == std::string::npos)
      {
      done = true;
      }
    // remove extra spaces and \ from the class name
    classname = cmSystemTools::CleanUpName(classname.c_str());
    
    // if this is not an abstract list then add new class
    // to the list of classes in this makefile
    if(!abstract)
      {
      cmClassFile file;
      file.SetName(classname.c_str(), this->GetCurrentDirectory());
      file.m_AbstractClass = false;
      m_Classes.push_back(file);
      }
    else
      {
      // if this is an abstract list, then look
      // for an existing class and set it to abstract
      for(int i = 0; i < m_Classes.size(); i++)
	{
	if(m_Classes[i].m_ClassName == classname)
	  {
	  m_Classes[i].m_AbstractClass = true;
	  break;
	  }
	}
      }
    }
}

// Find all of the files in dir as specified from this line:
// AUX_SOURCE_DIRECTORY = dir
// Add all the files to the m_Classes array.

void cmMakefile::ReadTemplateInstanceDirectory(std::string& line)
{
  std::string::size_type start = line.find("=");
  if(start != std::string::npos)
    {
    std::string templateDirectory = line.substr(start+1, line.size());
    templateDirectory = cmSystemTools::CleanUpName(templateDirectory.c_str());
    m_TemplateDirectories.push_back(templateDirectory);
    std::string tdir = this->GetCurrentDirectory();
    tdir += "/";
    tdir += templateDirectory;
    // Load all the files in the directory
    cmDirectory dir;
    if(dir.Load(tdir.c_str()))
      {
      int numfiles = dir.GetNumberOfFiles();
      for(int i =0; i < numfiles; ++i)
        {
        std::string file = dir.GetFile(i);
        // ignore files less than f.cxx in length
        if(file.size() > 4)
          {
          // Remove the extension
          std::string::size_type dotpos = file.rfind(".");
          file = file.substr(0, dotpos);
          std::string fullname = templateDirectory;
          fullname += "/";
          fullname += file;
          // add the file as a class file so 
          // depends can be done
          cmClassFile cmfile;
          cmfile.SetName(fullname.c_str(), this->GetCurrentDirectory());
          cmfile.m_AbstractClass = false;
          m_Classes.push_back(cmfile);
          }
        }
      }
    else
      {
      std::cerr << "Warning can not open template instance directory "
                << templateDirectory.c_str() << std::endl;
      }
    }
}