cmUnixMakefileGenerator.cxx 8.57 KB
Newer Older
1
2
3
#include "cmUnixMakefileGenerator.h"
#include "cmMakefile.h"
#include "cmStandardIncludes.h"
4
#include "cmSystemTools.h"
5
6
7
8
9
10
11
12
13
14
15
#include "cmClassFile.h"
#include "cmMakeDepend.h"

void cmUnixMakefileGenerator::GenerateMakefile()
{
  cmMakeDepend md;
  md.SetMakefile(m_Makefile);
  md.DoDepends();
  this->OutputMakefile("CMakeTargets.make"); 
}

16
17

// Output the depend information for all the classes 
18
// in the makefile.  These would have been generated
19
20
// by the class cmMakeDepend GenerateMakefile
void cmUnixMakefileGenerator::OutputDepends(std::ostream& fout)
21
{
22
23
  std::vector<cmClassFile>& Classes = m_Makefile->GetClasses();
  for(unsigned int i = 0; i < Classes.size(); i++)
24
    {
25
    if(!Classes[i].m_AbstractClass && !Classes[i].m_HeaderFileOnly)
26
      {
27
      if( Classes[i].m_Depends.size())
28
	{
29
	fout << Classes[i].m_ClassName << ".o : \\\n";
30
	for(std::vector<std::string>::iterator j =  
31
32
	      Classes[i].m_Depends.begin();
	    j != Classes[i].m_Depends.end(); ++j)
33
	  {
34
	  if(j+1 == Classes[i].m_Depends.end())
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
	    {
	    fout << *j << " \n";
	    }
	  else
	    {
	    fout << *j << " \\\n";
	    }
	  }
	fout << "\n\n";
	}
      }
    }
}

// fix up names of directories so they can be used
// as targets in makefiles.
inline std::string FixDirectoryName(const char* dir)
{
  std::string s = dir;
  // replace ../ with 3 under bars
  size_t pos = s.find("../");
  if(pos != std::string::npos)
    {
    s.replace(pos, 3, "___");
    }
  // replace / directory separators with a single under bar 
  pos = s.find("/");
  while(pos != std::string::npos)
    {
    s.replace(pos, 1, "_");
    pos = s.find("/");
    }
  return s;
}

70
71
72
73
74
75
76
77
// This is where CMakeTargets.make is generated
// This function ouputs the following:
// 1. Include flags for the compiler
// 2. List of .o files that need to be compiled
// 3. Rules to build executables including -l and -L options
// 4. Rules to build in sub directories
// 5. The name of the library being built, if it is a library

78
void cmUnixMakefileGenerator::OutputMakefile(const char* file)
79
{
80
81
  std::vector<std::string>& auxSourceDirs = m_Makefile->GetAuxSourceDirectories();
  if( auxSourceDirs.size() )
82
83
84
85
    {
    // For the case when this is running as a remote build
    // on unix, make the directory
    
86
87
    for(std::vector<std::string>::iterator i = auxSourceDirs.begin();
        i != auxSourceDirs.end(); ++i)
88
89
90
91
92
      {
      cmSystemTools::MakeDirectory(i->c_str());
      }
    }
  
93
94
95
  std::ofstream fout(file);
  if(!fout)
    {
96
    cmSystemTools::Error("Error can not open for write: ", file);
97
98
    return;
    }
99
100
  // Output Include paths
  fout << "INCLUDE_FLAGS = ";
101
  std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
102
103
104
105
106
107
108
109
110
111
  std::vector<std::string>::iterator i;
  for(i = includes.begin(); i != includes.end(); ++i)
    {
    std::string include = *i;
    fout << "-I" << i->c_str() << " ";
    }
  fout << " ${LOCAL_INCLUDE_FLAGS} ";
  fout << "\n";
  // see if there are files to compile in this makefile
  // These are used for both libraries and executables
112
113
  std::vector<cmClassFile>& Classes = m_Makefile->GetClasses();
  if(Classes.size() )
114
    {
115
    // Ouput Library name if there are SRC_OBJS
116
    if(strlen(m_Makefile->GetLibraryName()) > 0)
117
      {
118
      fout << "LIBRARY = " <<  m_Makefile->GetLibraryName() << "\n\n";
119
      fout << "BUILD_LIB_FILE = lib${LIBRARY}${CMAKE_LIB_EXT}\n\n";
120
121
      }
    // Output SRC_OBJ list for all the classes to be compiled
122
    fout << "SRC_OBJ = \\\n";
123
    for(unsigned int i = 0; i < Classes.size(); i++)
124
      {
125
      if(!Classes[i].m_AbstractClass && !Classes[i].m_HeaderFileOnly)
126
	{
127
128
	fout << Classes[i].m_ClassName << ".o ";
	if(i ==  Classes.size() -1)
129
130
131
132
133
134
135
136
137
138
139
	  {
	  fout << "\n\n";
	  }
	else
	  {
	  fout << "\\\n";
	  }
	}
      }
    fout << "\n";
    }
140
  std::vector<std::string>& MakeVerbatim = m_Makefile->GetMakeVerbatim();
141
  // Ouput user make text embeded in the input file
142
  for(unsigned int i =0; i < MakeVerbatim.size(); i++)
143
    {
144
    fout << MakeVerbatim[i] << "\n";
145
146
147
    }
  fout << "\n\n";

148
  // Output rules for building executables  
149
  if( m_Makefile->HasExecutables() )
150
    {
151
152
153
    // collect all the flags needed for linking libraries
    std::string linkLibs;        
    std::vector<std::string>::iterator j;
154
    std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
155
    for(j = libdirs.begin(); j != libdirs.end(); ++j)
156
      { 
157
158
      std::string::size_type pos = (*j).find("-L");
      if((pos == std::string::npos || pos > 0)
159
160
161
162
         && (*j).find("${") == std::string::npos)
        {
        linkLibs += "-L";
        }
163
164
165
      linkLibs += *j;
      linkLibs += " ";
      }
166
    std::vector<std::string>& libs = m_Makefile->GetLinkLibraries();
167
168
    for(j = libs.begin(); j != libs.end(); ++j)
      {
169
170
      std::string::size_type pos = (*j).find("-l");
      if((pos == std::string::npos || pos > 0)
171
172
173
174
         && (*j).find("${") == std::string::npos)
        {
        linkLibs += "-l";
        }
175
176
177
      linkLibs += *j;
      linkLibs += " ";
      }
178
    std::vector<std::string>& libsUnix = m_Makefile->GetLinkLibrariesUnix();
179
180
181
182
183
184
    for(j = libsUnix.begin(); j != libsUnix.end(); ++j)
      {
      linkLibs += *j;
      linkLibs += " ";
      }
    linkLibs += " ${LOCAL_LINK_FLAGS} ";
185
186
187
188
189
    // create and output a varible in the makefile that
    // each executable will depend on.  This will have all the
    // libraries that the executable uses
    fout << "CMAKE_DEPEND_LIBS = ";
    this->OutputDependLibraries(fout);
190
    // Now create rules for all of the executables to be built
191
    for(unsigned int i = 0; i < Classes.size(); i++)
192
      {
193
      if(!Classes[i].m_AbstractClass && !Classes[i].m_HeaderFileOnly)
194
	{ 
195
        std::string DotO = Classes[i].m_ClassName;
196
        DotO += ".o";
197
        fout << Classes[i].m_ClassName << ": " << DotO << " ";
198
        fout << "${CMAKE_DEPEND_LIBS}\n";
199
	fout << "\t${CXX}  ${CXX_FLAGS}  " << m_Makefile->GetDefineFlags()
200
201
202
	     << DotO.c_str() << " "
             << linkLibs.c_str() 
	     << " -o $@ ""\n\n";
203
204
	}
      }
205
    // ouput the list of executables
206
    fout << "EXECUTABLES = \\\n";
207
    for(unsigned int i = 0; i < Classes.size(); i++)
208
      {
209
      if(!Classes[i].m_AbstractClass && !Classes[i].m_HeaderFileOnly)
210
	{ 
211
212
        fout << Classes[i].m_ClassName;
	if(i < Classes.size()-1)
Bill Hoffman's avatar
Bill Hoffman committed
213
214
215
216
	  {
	    fout << " \\";
	  }
	fout << "\n";
217
218
219
	}
      }
    fout << "\n";
220
    }
221
  // Output Sub directory build rules
222
223
224
225
  const std::vector<std::string>& SubDirectories
    = m_Makefile->GetSubDirectories();
    
  if( SubDirectories.size() )
226
227
    {
    fout << "SUBDIR_BUILD = \\\n";
228
229
    unsigned int i;
    for(i =0; i < SubDirectories.size(); i++)
230
      { 
231
      std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
232
      fout << "build_" << subdir.c_str();
233
      if(i == SubDirectories.size()-1)
234
235
236
237
238
239
240
241
242
243
	{
	fout << " \n\n";
	}
      else
	{
	fout << " \\\n";
	}
      }
    fout << std::endl;
    fout << "SUBDIR_CLEAN = \\\n";
244
    for(i =0; i < SubDirectories.size(); i++)
245
      { 
246
      std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
247
      fout << "clean_" << subdir.c_str();
248
      if(i == SubDirectories.size()-1)
249
250
251
252
253
254
255
256
257
258
259
	{
	fout << " \n\n";
	}
      else
	{
	fout << " \\\n";
	}
      }
    fout << std::endl;
    fout << "alldirs : ${SUBDIR_BUILD}\n\n";

260
    for(i =0; i < SubDirectories.size(); i++)
261
      {
262
      std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
263
      fout << "build_" << subdir.c_str() << ":\n";
264
      fout << "\tcd " << SubDirectories[i].c_str()
Bill Hoffman's avatar
Bill Hoffman committed
265
	   << "; ${MAKE} -${MAKEFLAGS} CMakeTargets.make\n";
266
      fout << "\tcd " << SubDirectories[i].c_str()
267
268
269
	   << "; ${MAKE} -${MAKEFLAGS} all\n\n";

      fout << "clean_" << subdir.c_str() << ": \n";
270
      fout << "\tcd " << SubDirectories[i].c_str() 
271
272
273
274
275
	   << "; ${MAKE} -${MAKEFLAGS} clean\n\n";
      }
    }
  this->OutputDepends(fout);
}
276

277
278
279
280

// output the list of libraries that the executables 
// in this makefile will depend on.
void cmUnixMakefileGenerator::OutputDependLibraries(std::ostream& fout)
281
{
282
283
  std::vector<std::string>& libs = m_Makefile->GetLinkLibraries();
  std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
  std::vector<std::string>::iterator dir, lib, endlibs, enddirs;
  for(lib = libs.begin(); lib != libs.end(); ++lib)
    {
    bool found = false;
    for(dir = libdirs.begin(); dir != libdirs.end() && !found; ++dir)
      {
      std::string expression = "LIBRARY.*=.*";
      expression += lib->c_str();
      if(cmSystemTools::Grep(dir->c_str(), "CMakeTargets.make", expression.c_str()))
        {
        std::string libpath = *dir;
        libpath += "/lib";
        libpath += *lib;
        libpath += "${CMAKE_LIB_EXT}";
        fout << libpath << " ";
        found = true;
        }
      }
    }
  fout << "\n";
}