Files Classes Functions Hierarchy
00001 #ifndef CREATEMAKEFILE_H 00002 #define CREATEMAKEFILE_H 00003 00004 #include <fstream> 00005 #include <set> 00006 #include <string> 00007 using namespace std; 00008 00009 #include <commandline.h> 00010 #include <makefilebtglobals.h> 00011 #include <makestate.h> 00012 #include <processqueuebuild.h> 00013 00018 template <typename CFC> 00019 class createmakefile 00020 { 00021 public: 00022 00024 makestate<CFC> ms; 00025 00027 set<string> paths; 00028 00030 string makefile; 00031 00033 string lib; 00034 00036 string corrupt; 00037 00039 void pathsbuild(); 00040 00042 createmakefile(int argc, char** argv); 00043 00045 stringc pathsstring() const; 00047 stringc objsstring(); 00048 00050 void eval(); 00051 00052 }; 00053 00054 //--------------------------------------------------------- 00055 // Implementation 00056 00057 template <typename CFC> 00058 stringc createmakefile<CFC>::objsstring() 00059 { 00060 string str; 00061 for (ms.objs.reset(); !ms.objs; ++ms.objs) 00062 { 00063 str += (ms.objs()->filename + " "); 00064 } 00065 00066 return str; 00067 } 00068 00069 template <typename CFC> 00070 stringc createmakefile<CFC>::pathsstring() const 00071 { 00072 string str; 00073 00074 set<string>::const_iterator i=paths.begin(); 00075 set<string>::const_iterator iend=paths.end(); 00076 for ( ; i!=iend; ++i) 00077 str += ("-I" + *i + " "); 00078 00079 return str; 00080 } 00081 00082 template <typename CFC> 00083 createmakefile<CFC>::createmakefile(int argc, char** argv) 00084 { 00085 commandline cmd(argc,argv); 00086 cmd.mapvar(ms.driver,"driver"); 00087 00088 makefile="Makefile"; 00089 cmd.mapvar(makefile,"makefile"); 00090 00091 lib="libraries"; 00092 cmd.mapvar(lib,"lib"); 00093 00094 corrupt=""; 00095 cmd.mapvar(corrupt,"corrupt"); 00096 00097 //processqueuebuild<cfcontainervec> pq(ms); 00098 processqueuebuild<CFC> pq(ms); 00099 for ( ; !pq; ++pq ); 00100 00101 ms.objsdependenciesPass1(); 00102 ms.objsdependenciesPass2(); 00103 00104 pathsbuild(); 00105 00106 00107 // NOTE: I have a bud that I could not track. 00108 // 00109 // Problem: main.h in main.o includes for cfcontainerhash, 00110 // but not for cfcontainervec 00111 // 00112 // Solution: remove header. Not ideal, but until the bug 00113 // can be found this is best. 00114 00115 // driver.o had driver.h in includes! 00116 { 00117 compilationfile* x1 = ms.proj.files[ms.proj.driverh()]; 00118 assert(x1); 00119 compilationfile* x2 = ms.objs[ms.proj.drivero()]; 00120 assert(x2); 00121 00122 string str; 00123 for (ms.objs.reset(); !ms.objs; ++ms.objs) 00124 { 00125 if (ms.objs() != x2) 00126 continue; 00127 00128 //ms.objs()->print(); 00129 00130 vector<compilationfile*>& includes(ms.objs()->includes); 00131 vector<compilationfile*>::iterator f = find(includes.begin(),includes.end(),x1); 00132 if (f!=includes.end()) 00133 { 00134 //cout << "found" << endl; 00135 includes.erase(f); 00136 } 00137 } 00138 } 00139 00140 } 00141 00142 00143 template <typename CFC> 00144 void createmakefile<CFC>::pathsbuild() 00145 { 00146 compilationfile* x; 00147 for ( ms.objs.reset(); !ms.objs; ++ms.objs ) 00148 { 00149 x = ms.objs(); 00150 assert(x); 00151 uint kmax=x->includes.size(); 00152 for (uint k=0; k<kmax; ++k) 00153 { 00154 assert(x->includes[k]); 00155 paths.insert( x->includes[k]->path ); 00156 } 00157 } 00158 } 00159 00160 template <typename CFC> 00161 void createmakefile<CFC>::eval() 00162 { 00163 ofstream file(makefile.c_str()); 00164 00165 file << "# This makefile was generated using the build tool found at" << endl; 00166 file << "# http://www.fluxionsdividebyzero.com/p1/misc/makefilebuildtool.html" << endl << endl; 00167 file << "CC=g++ -Wall" << endl; 00168 file << "INC=" << pathsstring() << endl; 00169 file << "OBJ=" << objsstring() << endl; 00170 00171 // Look for libraries file, echo its contents after LIB= 00172 ifstream filelibraries(lib.c_str()); 00173 //cout << SHOW(libraries) << endl; 00174 string lib2; 00175 if (filelibraries.eof()==false) 00176 getline(filelibraries,lib2); 00177 file << "LIB=" << lib2 << endl << endl << endl; 00178 00179 //cout << SHOW(corrupt) << endl; 00180 00181 if (corrupt.empty()) 00182 file << ms.driver << ": $(OBJ)" << endl; 00183 else 00184 { 00185 compilationfile* x; 00186 bool found=false; 00187 for (ms.objs.reset(); !ms.objs; ++ms.objs) 00188 { 00189 x=ms.objs(); 00190 assert(x); 00191 //cout << SHOW(x->filename) << endl; 00192 if (x->filename == corrupt) 00193 found=true; 00194 } 00195 if (found==false) 00196 { 00197 cout << "error: " << corrupt << " not a dependency" << endl; 00198 return; 00199 } 00200 00201 file << ms.driver << ": " << corrupt << endl; 00202 } 00203 00204 00205 file << " $(CC) $(INC) -o " << ms.driver << " $(OBJ) $(LIB)" << endl; 00206 00207 compilationfile* x; 00208 for (ms.objs.reset(); !ms.objs; ++ms.objs) 00209 { 00210 x=ms.objs(); 00211 assert(x); 00212 00213 file << x->filename << ":"; 00214 00215 for (uint i=0; i<x->includes.size(); ++i) 00216 { 00217 if (x->includes[i]->isobj()) 00218 file << " " << x->includes[i]->filename; 00219 else 00220 file << " " << x->includes[i]->filenamefull(); 00221 } 00222 file << endl; 00223 file << " $(CC) $(INC) -c " << x->linksource->filenamefull() << endl; 00224 } 00225 00226 file << endl; 00227 file << "clean:" << endl; 00228 file << " rm *.o *.order *.out gmon.* " << ms.driver << endl; 00229 file << endl; 00230 00231 file << endl; 00232 file << "# Re-compile specific parts of the program. e.g." << endl; 00233 file << "# $ make del targ=windowscale" << endl; 00234 file << "targ=__nopattern__" << endl; 00235 file << "del:" << endl; 00236 file << " rm *${targ}*.o; make" << endl; 00237 file << endl; 00238 00239 00240 // system("if [ -e tempheaders.txt ]; then rm tempheaders.txt; fi"); 00241 string arg("if [ -e "+makefilebtglobals::tempheaders+" ]; then rm " 00242 + makefilebtglobals::tempheaders+"; fi"); 00243 system(arg.c_str()); 00244 system("if [ -e tempcpp.txt ]; then rm tempcpp.txt; fi"); 00245 } 00246 00247 00248 #endif 00249 00250
1.5.8