cmUnixMakefileGenerator.cxx 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #include "cmUnixMakefileGenerator.h"
  33. #include "cmMakefile.h"
  34. #include "cmStandardIncludes.h"
  35. #include "cmSystemTools.h"
  36. #include "cmSourceFile.h"
  37. #include "cmMakeDepend.h"
  38. #include "cmCacheManager.h"
  39. #include "cmGeneratedFileStream.h"
  40. cmUnixMakefileGenerator::cmUnixMakefileGenerator()
  41. {
  42. m_CacheOnly = false;
  43. m_Recurse = false;
  44. }
  45. void cmUnixMakefileGenerator::GenerateMakefile()
  46. {
  47. if(m_CacheOnly)
  48. {
  49. // Generate the cache only stuff
  50. this->GenerateCacheOnly();
  51. // if recurse then generate for all sub- makefiles
  52. if(m_Recurse)
  53. {
  54. this->RecursiveGenerateCacheOnly();
  55. }
  56. }
  57. // normal makefile output
  58. else
  59. {
  60. // Generate depends
  61. cmMakeDepend md;
  62. md.SetMakefile(m_Makefile);
  63. md.GenerateDependInformation();
  64. this->ProcessDepends(md);
  65. // output the makefile fragment
  66. this->OutputMakefile("Makefile");
  67. }
  68. }
  69. void cmUnixMakefileGenerator::ProcessDepends(const cmMakeDepend &md)
  70. {
  71. // Now create cmDependInformation objects for files in the directory
  72. cmTargets &tgts = m_Makefile->GetTargets();
  73. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  74. {
  75. std::vector<cmSourceFile> &classes = l->second.GetSourceFiles();
  76. for(std::vector<cmSourceFile>::iterator i = classes.begin();
  77. i != classes.end(); ++i)
  78. {
  79. if(!i->GetIsAHeaderFileOnly())
  80. {
  81. // get the depends
  82. const cmDependInformation *info =
  83. md.GetDependInformationForSourceFile(*i);
  84. // Delete any hints from the source file's dependencies.
  85. i->GetDepends().erase(i->GetDepends().begin(), i->GetDepends().end());
  86. // Now add the real dependencies for the file.
  87. if (info)
  88. {
  89. for( cmDependInformation::IndexSet::const_iterator indx =
  90. info->m_IndexSet.begin();
  91. indx != info->m_IndexSet.end(); ++indx)
  92. {
  93. i->GetDepends().push_back(md.GetDependInformation()[*indx]->m_FullPath);
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. // This is where CMakeTargets.make is generated
  101. void cmUnixMakefileGenerator::OutputMakefile(const char* file)
  102. {
  103. // Create sub directories fro aux source directories
  104. std::vector<std::string>& auxSourceDirs =
  105. m_Makefile->GetAuxSourceDirectories();
  106. if( auxSourceDirs.size() )
  107. {
  108. // For the case when this is running as a remote build
  109. // on unix, make the directory
  110. for(std::vector<std::string>::iterator i = auxSourceDirs.begin();
  111. i != auxSourceDirs.end(); ++i)
  112. {
  113. cmSystemTools::MakeDirectory(i->c_str());
  114. }
  115. }
  116. // Create a stream that writes to a temporary file
  117. // then does a copy at the end. This is to allow users
  118. // to hit control-c during the make of the makefile
  119. cmGeneratedFileStream tempFile(file);
  120. tempFile.SetAlwaysCopy(true);
  121. std::ostream& fout = tempFile.GetStream();
  122. if(!fout)
  123. {
  124. cmSystemTools::Error("Error can not open for write: ", file);
  125. return;
  126. }
  127. fout << "# CMAKE generated Makefile, DO NOT EDIT!\n"
  128. << "# Generated from the following files:\n# "
  129. << m_Makefile->GetHomeOutputDirectory() << "/CMakeCache.txt\n";
  130. std::vector<std::string> lfiles = m_Makefile->GetListFiles();
  131. // sort the array
  132. std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  133. // remove duplicates
  134. std::vector<std::string>::iterator new_end =
  135. std::unique(lfiles.begin(), lfiles.end());
  136. lfiles.erase(new_end, lfiles.end());
  137. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  138. i != lfiles.end(); ++i)
  139. {
  140. fout << "# " << i->c_str() << "\n";
  141. }
  142. fout << "\n\n";
  143. // create a make variable with all of the sources for this Makefile
  144. // for depend purposes.
  145. fout << "CMAKE_MAKEFILE_SOURCES = ";
  146. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  147. i != lfiles.end(); ++i)
  148. {
  149. fout << " " << i->c_str();
  150. }
  151. // Add the cache to the list
  152. fout << " " << m_Makefile->GetHomeOutputDirectory() << "/CMakeCache.txt\n";
  153. fout << "\n\n";
  154. this->OutputMakeVariables(fout);
  155. this->OutputMakeFlags(fout);
  156. this->OutputTargetRules(fout);
  157. this->OutputDependencies(fout);
  158. this->OutputTargets(fout);
  159. this->OutputSubDirectoryRules(fout);
  160. std::string dependName = m_Makefile->GetStartOutputDirectory();
  161. dependName += "/cmake.depends";
  162. if(!this->m_CacheOnly)
  163. {
  164. std::ofstream dependout(dependName.c_str());
  165. if(!dependout)
  166. {
  167. cmSystemTools::Error("Error can not open for write: ", dependName.c_str());
  168. return;
  169. }
  170. this->OutputObjectDepends(dependout);
  171. }
  172. this->OutputCustomRules(fout);
  173. this->OutputMakeRules(fout);
  174. this->OutputInstallRules(fout);
  175. // only add the depend include if the depend file exists
  176. if(cmSystemTools::FileExists(dependName.c_str()))
  177. {
  178. fout << "include cmake.depends\n";
  179. }
  180. }
  181. // Output the rules for any targets
  182. void cmUnixMakefileGenerator::OutputTargetRules(std::ostream& fout)
  183. {
  184. // for each target add to the list of targets
  185. fout << "TARGETS = ";
  186. const cmTargets &tgts = m_Makefile->GetTargets();
  187. // list libraries first
  188. bool dll = cmCacheManager::GetInstance()->IsOn("BUILD_SHARED_LIBS");
  189. for(cmTargets::const_iterator l = tgts.begin();
  190. l != tgts.end(); l++)
  191. {
  192. if (l->second.GetType() == cmTarget::LIBRARY &&
  193. l->second.IsInAll())
  194. {
  195. fout << " \\\nlib" << l->first.c_str();
  196. if(dll)
  197. {
  198. fout << m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  199. }
  200. else
  201. {
  202. fout << ".a";
  203. }
  204. }
  205. }
  206. // executables
  207. for(cmTargets::const_iterator l = tgts.begin();
  208. l != tgts.end(); l++)
  209. {
  210. if ((l->second.GetType() == cmTarget::EXECUTABLE ||
  211. l->second.GetType() == cmTarget::WIN32_EXECUTABLE) &&
  212. l->second.IsInAll())
  213. {
  214. fout << " \\\n" << l->first.c_str();
  215. }
  216. }
  217. // list utilities last
  218. for(cmTargets::const_iterator l = tgts.begin();
  219. l != tgts.end(); l++)
  220. {
  221. if (l->second.GetType() == cmTarget::UTILITY &&
  222. l->second.IsInAll())
  223. {
  224. fout << " \\\n" << l->first.c_str();
  225. }
  226. }
  227. fout << "\n\n";
  228. // get the classes from the source lists then add them to the groups
  229. for(cmTargets::const_iterator l = tgts.begin();
  230. l != tgts.end(); l++)
  231. {
  232. std::vector<cmSourceFile> classes = l->second.GetSourceFiles();
  233. if (classes.begin() != classes.end())
  234. {
  235. fout << l->first << "_SRC_OBJS = ";
  236. for(std::vector<cmSourceFile>::iterator i = classes.begin();
  237. i != classes.end(); i++)
  238. {
  239. if(!i->IsAHeaderFileOnly())
  240. {
  241. fout << "\\\n" << i->GetSourceName() << ".o ";
  242. }
  243. }
  244. fout << "\n\n";
  245. }
  246. }
  247. fout << "CLEAN_OBJECT_FILES = ";
  248. for(cmTargets::const_iterator l = tgts.begin();
  249. l != tgts.end(); l++)
  250. {
  251. std::vector<cmSourceFile> classes = l->second.GetSourceFiles();
  252. if (classes.begin() != classes.end())
  253. {
  254. fout << "${" << l->first << "_SRC_OBJS} ";
  255. }
  256. }
  257. fout << "\n";
  258. }
  259. /**
  260. * Output the linking rules on a command line. For executables,
  261. * targetLibrary should be a NULL pointer. For libraries, it should point
  262. * to the name of the library. This will not link a library against itself.
  263. */
  264. void cmUnixMakefileGenerator::OutputLinkLibraries(std::ostream& fout,
  265. const char* targetLibrary,
  266. const cmTarget &tgt)
  267. {
  268. // Try to emit each search path once
  269. std::set<std::string> emitted;
  270. // Embed runtime search paths if possible and if required.
  271. bool outputRuntime = cmCacheManager::GetInstance()->IsOn("BUILD_SHARED_LIBS");
  272. std::string runtimeFlag;
  273. std::string runtimeSep;
  274. std::vector<std::string> runtimeDirs;
  275. if(m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_FLAG"))
  276. {
  277. runtimeFlag = m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_FLAG");
  278. }
  279. if(m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_SEP"))
  280. {
  281. runtimeSep = m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_SEP");
  282. }
  283. // concatenate all paths or no?
  284. bool runtimeConcatenate = ( runtimeSep!="" );
  285. if(runtimeFlag == "")
  286. {
  287. outputRuntime = false;
  288. }
  289. // collect all the flags needed for linking libraries
  290. std::string linkLibs;
  291. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  292. for(std::vector<std::string>::iterator libDir = libdirs.begin();
  293. libDir != libdirs.end(); ++libDir)
  294. {
  295. std::string libpath = cmSystemTools::EscapeSpaces(libDir->c_str());
  296. if(libpath != "/usr/lib")
  297. {
  298. if(emitted.insert(libpath).second)
  299. {
  300. std::string::size_type pos = libDir->find("-L");
  301. if((pos == std::string::npos || pos > 0)
  302. && libDir->find("${") == std::string::npos)
  303. {
  304. linkLibs += "-L";
  305. if(outputRuntime)
  306. {
  307. runtimeDirs.push_back( libpath );
  308. }
  309. }
  310. linkLibs += libpath;
  311. linkLibs += " ";
  312. }
  313. }
  314. }
  315. std::string librariesLinked;
  316. const cmTarget::LinkLibraries& libs = tgt.GetLinkLibraries();
  317. cmRegularExpression reg("lib(.*)(\\.so$|\\.a|\\.sl$)");
  318. for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
  319. lib != libs.end(); ++lib)
  320. {
  321. // Don't link the library against itself!
  322. if(targetLibrary && (lib->first == targetLibrary)) continue;
  323. // don't look at debug libraries
  324. if (lib->second == cmTarget::DEBUG) continue;
  325. // skip zero size library entries, this may happen
  326. // if a variable expands to nothing.
  327. if (lib->first.size() == 0) continue;
  328. // if it is a full path break it into -L and -l
  329. cmRegularExpression reg("(^[ \t]*\\-l)|(\\${)");
  330. if(lib->first.find('/') != std::string::npos
  331. && !reg.find(lib->first))
  332. {
  333. std::string dir, file;
  334. cmSystemTools::SplitProgramPath(lib->first.c_str(),
  335. dir, file);
  336. std::string libpath = cmSystemTools::EscapeSpaces(dir.c_str());
  337. if(libpath != "/usr/lib")
  338. {
  339. if(emitted.insert(libpath).second)
  340. {
  341. linkLibs += "-L";
  342. linkLibs += libpath;
  343. linkLibs += " ";
  344. if(outputRuntime)
  345. {
  346. runtimeDirs.push_back( libpath );
  347. }
  348. }
  349. }
  350. cmRegularExpression libname("lib(.*)\\.(.*)");
  351. if(libname.find(file))
  352. {
  353. librariesLinked += "-l";
  354. file = libname.match(1);
  355. librariesLinked += file;
  356. librariesLinked += " ";
  357. }
  358. }
  359. // not a full path, so add -l name
  360. else
  361. {
  362. if(!reg.find(lib->first))
  363. {
  364. librariesLinked += "-l";
  365. }
  366. librariesLinked += lib->first;
  367. librariesLinked += " ";
  368. }
  369. }
  370. linkLibs += librariesLinked;
  371. if(!targetLibrary)
  372. {
  373. // For executables, add these a second time so order does not matter
  374. linkLibs += librariesLinked;
  375. }
  376. fout << linkLibs;
  377. if(outputRuntime && runtimeDirs.size()>0)
  378. {
  379. fout << runtimeFlag;
  380. std::vector<std::string>::iterator itr = runtimeDirs.begin();
  381. fout << *itr;
  382. ++itr;
  383. for( ; itr != runtimeDirs.end(); ++itr )
  384. {
  385. if(runtimeConcatenate)
  386. {
  387. fout << runtimeSep << *itr;
  388. }
  389. else
  390. {
  391. fout << " " << runtimeFlag << *itr;
  392. }
  393. }
  394. fout << " ";
  395. }
  396. }
  397. void cmUnixMakefileGenerator::OutputTargets(std::ostream& fout)
  398. {
  399. // for each target
  400. const cmTargets &tgts = m_Makefile->GetTargets();
  401. for(cmTargets::const_iterator l = tgts.begin();
  402. l != tgts.end(); l++)
  403. {
  404. if (l->second.GetType() == cmTarget::LIBRARY)
  405. {
  406. fout << "#---------------------------------------------------------\n";
  407. fout << "# rules for a library\n";
  408. fout << "#\n";
  409. fout << "lib" << l->first << ".a: ${" <<
  410. l->first << "_SRC_OBJS} \n";
  411. fout << "\t${CMAKE_AR} cr lib" << l->first << ".a ${" <<
  412. l->first << "_SRC_OBJS} \n";
  413. fout << "\t${CMAKE_RANLIB} lib" << l->first << ".a\n";
  414. fout << std::endl;
  415. fout << "lib" << l->first << "$(SHLIB_SUFFIX): ${" <<
  416. l->first << "_SRC_OBJS} ${CMAKE_DEPEND_LIBS}\n";
  417. fout << "\trm -f lib" << l->first << "$(SHLIB_SUFFIX)\n";
  418. fout << "\t$(CMAKE_CXX_COMPILER) ${CMAKE_SHLIB_LINK_FLAGS} ${CMAKE_SHLIB_BUILD_FLAGS} ${CMAKE_CXX_FLAGS} -o \\\n";
  419. fout << "\t lib" << l->first << "$(SHLIB_SUFFIX) \\\n";
  420. fout << "\t ${" << l->first <<
  421. "_SRC_OBJS} ";
  422. this->OutputLinkLibraries(fout, l->first.c_str(), l->second);
  423. fout << "\n\n";
  424. }
  425. else if ((l->second.GetType() == cmTarget::EXECUTABLE)
  426. || (l->second.GetType() == cmTarget::WIN32_EXECUTABLE))
  427. {
  428. fout << l->first << ": ${" <<
  429. l->first << "_SRC_OBJS} ${CMAKE_DEPEND_LIBS}\n";
  430. fout << "\t${CMAKE_CXX_COMPILER} ${CMAKE_SHLIB_LINK_FLAGS} ${CMAKE_CXXFLAGS} ${" <<
  431. l->first << "_SRC_OBJS} ";
  432. this->OutputLinkLibraries(fout, NULL,l->second);
  433. fout << " -o " << l->first << "\n\n";
  434. }
  435. }
  436. }
  437. // output the list of libraries that the executables
  438. // in this makefile will depend on.
  439. void cmUnixMakefileGenerator::OutputDependencies(std::ostream& fout)
  440. {
  441. // Each dependency should only be emitted once.
  442. std::set<std::string> emitted;
  443. fout << "CMAKE_DEPEND_LIBS = ";
  444. cmTarget::LinkLibraries& libs = m_Makefile->GetLinkLibraries();
  445. cmTarget::LinkLibraries::const_iterator lib2;
  446. // Search the list of libraries that will be linked into
  447. // the executable
  448. emitted.clear();
  449. for(lib2 = libs.begin(); lib2 != libs.end(); ++lib2)
  450. {
  451. // loop over the list of directories that the libraries might
  452. // be in, looking for an ADD_LIBRARY(lib...) line. This would
  453. // be stored in the cache
  454. if( ! emitted.insert(lib2->first).second ) continue;
  455. const char* cacheValue
  456. = cmCacheManager::GetInstance()->GetCacheValue(lib2->first.c_str());
  457. if(cacheValue)
  458. {
  459. std::string libpath = cacheValue;
  460. libpath += "/lib";
  461. libpath += lib2->first;
  462. bool dll = cmCacheManager::GetInstance()->IsOn("BUILD_SHARED_LIBS");
  463. if(dll)
  464. {
  465. libpath += m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  466. }
  467. else
  468. {
  469. libpath += ".a";
  470. }
  471. fout << libpath << " ";
  472. }
  473. }
  474. fout << "\n\n";
  475. emitted.clear();
  476. for(lib2 = libs.begin(); lib2 != libs.end(); ++lib2)
  477. {
  478. // loop over the list of directories that the libraries might
  479. // be in, looking for an ADD_LIBRARY(lib...) line. This would
  480. // be stored in the cache
  481. if( ! emitted.insert(lib2->first).second ) continue;
  482. const char* cacheValue
  483. = cmCacheManager::GetInstance()->GetCacheValue(lib2->first.c_str());
  484. if(cacheValue)
  485. {
  486. std::string library = "lib";
  487. library += lib2->first;
  488. bool dll = cmCacheManager::GetInstance()->IsOn("BUILD_SHARED_LIBS");
  489. if(dll)
  490. {
  491. library += m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  492. }
  493. else
  494. {
  495. library += ".a";
  496. }
  497. std::string libpath = cacheValue;
  498. libpath += "/";
  499. libpath += library;
  500. // put out a rule to build the library if it does not exist
  501. fout << libpath.c_str()
  502. << ":\n\tcd " << cacheValue
  503. << "; make " << library.c_str() << "\n\n";
  504. }
  505. }
  506. }
  507. // output make include flags
  508. void cmUnixMakefileGenerator::OutputMakeFlags(std::ostream& fout)
  509. {
  510. // Output Include paths
  511. fout << "INCLUDE_FLAGS = ";
  512. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  513. std::vector<std::string>::iterator i;
  514. fout << "-I" << m_Makefile->GetStartDirectory() << " ";
  515. for(i = includes.begin(); i != includes.end(); ++i)
  516. {
  517. std::string include = *i;
  518. fout << "-I" << cmSystemTools::EscapeSpaces(i->c_str()).c_str() << " ";
  519. }
  520. fout << m_Makefile->GetDefineFlags();
  521. fout << "\n\n";
  522. fout << "default_target: all\n\n";
  523. // see if there are files to compile in this makefile
  524. // These are used for both libraries and executables
  525. }
  526. // fix up names of directories so they can be used
  527. // as targets in makefiles.
  528. inline std::string FixDirectoryName(const char* dir)
  529. {
  530. std::string s = dir;
  531. // replace ../ with 3 under bars
  532. size_t pos = s.find("../");
  533. if(pos != std::string::npos)
  534. {
  535. s.replace(pos, 3, "___");
  536. }
  537. // replace / directory separators with a single under bar
  538. pos = s.find("/");
  539. while(pos != std::string::npos)
  540. {
  541. s.replace(pos, 1, "_");
  542. pos = s.find("/");
  543. }
  544. return s;
  545. }
  546. void
  547. cmUnixMakefileGenerator::
  548. OutputSubDirectoryVars(std::ostream& fout,
  549. const char* var,
  550. const char* target,
  551. const char* target1,
  552. const char* target2,
  553. const std::vector<std::string>& SubDirectories)
  554. {
  555. if( SubDirectories.size() == 0)
  556. {
  557. return;
  558. }
  559. fout << "# Variable for making " << target << " in subdirectories.\n";
  560. fout << var << " = \\\n";
  561. unsigned int i;
  562. for(i =0; i < SubDirectories.size(); i++)
  563. {
  564. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  565. fout << target << "_" << subdir.c_str();
  566. if(i == SubDirectories.size()-1)
  567. {
  568. fout << " \n\n";
  569. }
  570. else
  571. {
  572. fout << " \\\n";
  573. }
  574. }
  575. fout << "# Targets for making " << target << " in subdirectories.\n";
  576. for(unsigned int i =0; i < SubDirectories.size(); i++)
  577. {
  578. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  579. fout << target << "_" << subdir.c_str() << ":\n";
  580. if(target1)
  581. {
  582. fout << "\t@if test ! -d " << SubDirectories[i].c_str() << "; then ${MAKE} rebuild_cache; fi\n"
  583. "\tcd " << SubDirectories[i].c_str()
  584. << "; ${MAKE} -${MAKEFLAGS} " << target1 << "\n";
  585. }
  586. if(target2)
  587. {
  588. fout << "\t@cd " << SubDirectories[i].c_str()
  589. << "; ${MAKE} -${MAKEFLAGS} " << target2 << "\n";
  590. }
  591. }
  592. fout << "\n\n";
  593. }
  594. // output rules for decending into sub directories
  595. void cmUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  596. {
  597. // Output Sub directory build rules
  598. const std::vector<std::string>& SubDirectories
  599. = m_Makefile->GetSubDirectories();
  600. if( SubDirectories.size() == 0)
  601. {
  602. return;
  603. }
  604. this->OutputSubDirectoryVars(fout, "SUBDIR_BUILD", "build",
  605. "cmake.depends",
  606. "all",
  607. SubDirectories);
  608. this->OutputSubDirectoryVars(fout, "SUBDIR_CLEAN", "clean",
  609. "clean",
  610. 0,
  611. SubDirectories);
  612. this->OutputSubDirectoryVars(fout, "SUBDIR_DEPEND", "depend",
  613. "depend",
  614. 0,
  615. SubDirectories);
  616. this->OutputSubDirectoryVars(fout, "SUBDIR_INSTALL", "install",
  617. "install",
  618. 0,
  619. SubDirectories);
  620. }
  621. // Output the depend information for all the classes
  622. // in the makefile. These would have been generated
  623. // by the class cmMakeDepend GenerateMakefile
  624. void cmUnixMakefileGenerator::OutputObjectDepends(std::ostream& fout)
  625. {
  626. // Iterate over every target.
  627. std::map<std::string, cmTarget>& targets = m_Makefile->GetTargets();
  628. for(std::map<std::string, cmTarget>::const_iterator target = targets.begin();
  629. target != targets.end(); ++target)
  630. {
  631. // Iterate over every source for this target.
  632. const std::vector<cmSourceFile>& sources = target->second.GetSourceFiles();
  633. for(std::vector<cmSourceFile>::const_iterator source = sources.begin();
  634. source != sources.end(); ++source)
  635. {
  636. if(!source->IsAHeaderFileOnly())
  637. {
  638. if(!source->GetDepends().empty())
  639. {
  640. fout << source->GetSourceName() << ".o :";
  641. // Iterate through all the dependencies for this source.
  642. for(std::vector<std::string>::const_iterator dep =
  643. source->GetDepends().begin();
  644. dep != source->GetDepends().end(); ++dep)
  645. {
  646. fout << " \\\n" << dep->c_str();
  647. }
  648. fout << "\n\n";
  649. }
  650. }
  651. }
  652. }
  653. }
  654. // Output each custom rule in the following format:
  655. // output: source depends...
  656. // (tab) command...
  657. void cmUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
  658. {
  659. // We may be modifying the source groups temporarily, so make a copy.
  660. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  661. const cmTargets &tgts = m_Makefile->GetTargets();
  662. for(cmTargets::const_iterator tgt = tgts.begin();
  663. tgt != tgts.end(); ++tgt)
  664. {
  665. // add any custom rules to the source groups
  666. for (std::vector<cmCustomCommand>::const_iterator cr =
  667. tgt->second.GetCustomCommands().begin();
  668. cr != tgt->second.GetCustomCommands().end(); ++cr)
  669. {
  670. cmSourceGroup& sourceGroup =
  671. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  672. sourceGroups);
  673. cmCustomCommand cc(*cr);
  674. cc.ExpandVariables(*m_Makefile);
  675. sourceGroup.AddCustomCommand(cc);
  676. }
  677. }
  678. // Loop through every source group.
  679. for(std::vector<cmSourceGroup>::const_iterator sg =
  680. sourceGroups.begin(); sg != sourceGroups.end(); ++sg)
  681. {
  682. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  683. if(buildRules.empty())
  684. { continue; }
  685. std::string name = sg->GetName();
  686. if(name != "")
  687. {
  688. fout << "# Start of source group \"" << name.c_str() << "\"\n";
  689. }
  690. // Loop through each source in the source group.
  691. for(cmSourceGroup::BuildRules::const_iterator cc =
  692. buildRules.begin(); cc != buildRules.end(); ++ cc)
  693. {
  694. std::string source = cc->first;
  695. const cmSourceGroup::Commands& commands = cc->second;
  696. // Loop through every command generating code from the current source.
  697. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  698. c != commands.end(); ++c)
  699. {
  700. std::string command = c->first;
  701. const cmSourceGroup::CommandFiles& commandFiles = c->second;
  702. // if the command has no outputs, then it is a utility command
  703. // with no outputs
  704. if(commandFiles.m_Outputs.size() == 0)
  705. {
  706. fout << source.c_str() << ": ";
  707. // Write out all the dependencies for this rule.
  708. for(std::set<std::string>::const_iterator d =
  709. commandFiles.m_Depends.begin();
  710. d != commandFiles.m_Depends.end(); ++d)
  711. {
  712. std::string dep = cmSystemTools::EscapeSpaces(d->c_str());
  713. fout << " " << dep.c_str();
  714. }
  715. fout << "\n\t" << command.c_str() << "\n\n";
  716. }
  717. // Write a rule for every output generated by this command.
  718. for(std::set<std::string>::const_iterator output =
  719. commandFiles.m_Outputs.begin();
  720. output != commandFiles.m_Outputs.end(); ++output)
  721. {
  722. std::string src = cmSystemTools::EscapeSpaces(source.c_str());
  723. fout << output->c_str() << ": " << src.c_str();
  724. // Write out all the dependencies for this rule.
  725. for(std::set<std::string>::const_iterator d =
  726. commandFiles.m_Depends.begin();
  727. d != commandFiles.m_Depends.end(); ++d)
  728. {
  729. std::string dep = cmSystemTools::EscapeSpaces(d->c_str());
  730. fout << " " << dep.c_str();
  731. }
  732. fout << "\n\t" << command.c_str() << "\n\n";
  733. }
  734. }
  735. }
  736. if(name != "")
  737. {
  738. fout << "# End of source group \"" << name.c_str() << "\"\n\n";
  739. }
  740. }
  741. }
  742. void cmUnixMakefileGenerator::GenerateCacheOnly()
  743. {
  744. cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory());
  745. std::string dest = m_Makefile->GetStartOutputDirectory();
  746. dest += "/Makefile";
  747. std::cout << "cmake: creating : " << dest.c_str() << std::endl;
  748. this->OutputMakefile(dest.c_str());
  749. return;
  750. }
  751. void cmUnixMakefileGenerator::RecursiveGenerateCacheOnly()
  752. {
  753. std::vector<cmMakefile*> makefiles;
  754. m_Makefile->FindSubDirectoryCMakeListsFiles(makefiles);
  755. for(std::vector<cmMakefile*>::iterator i = makefiles.begin();
  756. i != makefiles.end(); ++i)
  757. {
  758. cmMakefile* mf = *i;
  759. if(m_Makefile->GetDefinition("RUN_CONFIGURE"))
  760. {
  761. mf->AddDefinition("RUN_CONFIGURE", true);
  762. }
  763. cmUnixMakefileGenerator* gen = new cmUnixMakefileGenerator;
  764. gen->SetCacheOnlyOn();
  765. gen->SetRecurseOff();
  766. mf->SetMakefileGenerator(gen);
  767. mf->GenerateMakefile();
  768. }
  769. // CLEAN up the makefiles created
  770. for(unsigned int i =0; i < makefiles.size(); ++i)
  771. {
  772. delete makefiles[i];
  773. }
  774. }
  775. void cmUnixMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  776. {
  777. if(strcmp(m_Makefile->GetHomeDirectory(),
  778. m_Makefile->GetHomeOutputDirectory()) == 0)
  779. {
  780. fout << "srcdir = .\n\n";
  781. }
  782. else
  783. {
  784. fout << "srcdir = " << m_Makefile->GetStartDirectory() << "\n";
  785. fout << "VPATH = " << m_Makefile->GetStartDirectory() << "\n";
  786. }
  787. const char* variables =
  788. "# the standard shell for make\n"
  789. "SHELL = /bin/sh\n"
  790. "\n"
  791. "CMAKE_LIB_EXT = @CMAKE_LIB_EXT@\n"
  792. "CMAKE_RANLIB = @CMAKE_RANLIB@\n"
  793. "CMAKE_AR = @CMAKE_AR@\n"
  794. "CMAKE_C_COMPILER = @CMAKE_C_COMPILER@\n"
  795. "CMAKE_CFLAGS = @CMAKE_C_FLAGS@ @CMAKE_SHLIB_CFLAGS@ \n"
  796. "\n"
  797. "CMAKE_CXX_COMPILER = @CMAKE_CXX_COMPILER@\n"
  798. "CMAKE_CXXFLAGS = @CMAKE_CXX_FLAGS@ @CMAKE_SHLIB_CFLAGS@ @CMAKE_TEMPLATE_FLAGS@ \n"
  799. "\n"
  800. "CMAKE_SHLIB_BUILD_FLAGS = @CMAKE_SHLIB_BUILD_FLAGS@\n"
  801. "CMAKE_SHLIB_LINK_FLAGS = @CMAKE_SHLIB_LINK_FLAGS@\n"
  802. "DL_LIBS = @CMAKE_DL_LIBS@\n"
  803. "SHLIB_LD_LIBS = @CMAKE_SHLIB_LD_LIBS@\n"
  804. "SHLIB_SUFFIX = @CMAKE_SHLIB_SUFFIX@\n"
  805. "THREAD_LIBS = @CMAKE_THREAD_LIBS@\n"
  806. "\n"
  807. "# set up the path to the rulesgen program\n"
  808. "CMAKE_COMMAND = ${CMAKE_COMMAND}\n"
  809. "\n"
  810. "\n"
  811. "\n";
  812. std::string replaceVars = variables;
  813. bool dll = cmCacheManager::GetInstance()->IsOn("BUILD_SHARED_LIBS");
  814. if(!dll)
  815. {
  816. // if not a dll then remove the shlib -fpic flag
  817. m_Makefile->AddDefinition("CMAKE_SHLIB_CFLAGS", "");
  818. }
  819. m_Makefile->ExpandVariablesInString(replaceVars);
  820. fout << replaceVars.c_str();
  821. fout << "CMAKE_CURRENT_SOURCE = " << m_Makefile->GetStartDirectory() << "\n";
  822. fout << "CMAKE_CURRENT_BINARY = " << m_Makefile->GetStartOutputDirectory() << "\n";
  823. }
  824. void cmUnixMakefileGenerator::OutputInstallRules(std::ostream& fout)
  825. {
  826. bool dll = cmCacheManager::GetInstance()->IsOn("BUILD_SHARED_LIBS");
  827. const char* root
  828. = cmCacheManager::GetInstance()->GetCacheValue("CMAKE_ROOT");
  829. fout << "INSTALL = " << root << "/Templates/install-sh -c\n";
  830. fout << "INSTALL_PROGRAM = ${INSTALL}\n";
  831. fout << "INSTALL_DATA = ${INSTALL} -m 644\n";
  832. const cmTargets &tgts = m_Makefile->GetTargets();
  833. fout << "install: ${SUBDIR_INSTALL}\n";
  834. fout << "\t@echo \"Installing ...\"\n";
  835. const char* prefix
  836. = cmCacheManager::GetInstance()->GetCacheValue("CMAKE_INSTALL_PREFIX");
  837. if (!prefix)
  838. {
  839. prefix = "/usr/local";
  840. }
  841. for(cmTargets::const_iterator l = tgts.begin();
  842. l != tgts.end(); l++)
  843. {
  844. if (l->second.GetInstallPath() != "")
  845. {
  846. // first make the directories for each target
  847. fout << "\t@if [ ! -d " << prefix << l->second.GetInstallPath() <<
  848. " ] ; then \\\n";
  849. fout << "\t echo \"Making directory " << prefix
  850. << l->second.GetInstallPath() << " \"; \\\n";
  851. fout << "\t mkdir -p " << prefix << l->second.GetInstallPath()
  852. << "; \\\n";
  853. fout << "\t chmod 755 " << prefix << l->second.GetInstallPath()
  854. << "; \\\n";
  855. fout << "\t else true; \\\n";
  856. fout << "\t fi\n";
  857. // now install the target
  858. switch (l->second.GetType())
  859. {
  860. case cmTarget::LIBRARY:
  861. fout << "\t$(INSTALL_DATA) lib" << l->first;
  862. if(dll)
  863. {
  864. fout << m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  865. }
  866. else
  867. {
  868. fout << ".a";
  869. }
  870. fout << " " << prefix << l->second.GetInstallPath() << "\n";
  871. break;
  872. case cmTarget::WIN32_EXECUTABLE:
  873. case cmTarget::EXECUTABLE:
  874. fout << "\t$(INSTALL_PROGRAM) " << l->first
  875. << " " << prefix << l->second.GetInstallPath() << "\n";
  876. break;
  877. case cmTarget::INSTALL:
  878. {
  879. const std::vector<std::string> &sf = l->second.GetSourceLists();
  880. std::vector<std::string>::const_iterator i;
  881. for (i = sf.begin(); i != sf.end(); ++i)
  882. {
  883. fout << "\t@ echo \"Installing " << *i << " \"\n";
  884. fout << "\t@if [ -e " << *i << " ] ; then \\\n";
  885. fout << "\t $(INSTALL_DATA) " << *i
  886. << " " << prefix << l->second.GetInstallPath() << "; \\\n";
  887. fout << "\t elif [ -e ${srcdir}/" << *i << " ] ; then \\\n";
  888. fout << "\t $(INSTALL_DATA) ${srcdir}/" << *i
  889. << " " << prefix << l->second.GetInstallPath() << "; \\\n";
  890. fout << "\telse \\\n";
  891. fout << "\t echo \" ERROR!!! Unable to find: " << *i
  892. << " \"; \\\n";
  893. fout << "\t fi\n";
  894. }
  895. }
  896. break;
  897. case cmTarget::UTILITY:
  898. default:
  899. break;
  900. }
  901. }
  902. }
  903. }
  904. void cmUnixMakefileGenerator::OutputMakeRules(std::ostream& fout)
  905. {
  906. this->OutputMakeRule(fout,
  907. "# tell make about .cxx and .java",
  908. ".SUFFIXES", ".cxx .java .class", 0);
  909. this->OutputMakeRule(fout,
  910. "# build c file",
  911. ".c.o",
  912. 0,
  913. "${CMAKE_C_COMPILER} ${CMAKE_CFLAGS} ${INCLUDE_FLAGS} -c $< -o $@");
  914. this->OutputMakeRule(fout,
  915. "# build cplusplus file",
  916. ".cxx.o",
  917. 0,
  918. "${CMAKE_CXX_COMPILER} ${CMAKE_CXXFLAGS} ${INCLUDE_FLAGS} -c $< -o $@");
  919. this->OutputMakeRule(fout,
  920. "Default build rule",
  921. "all",
  922. "Makefile cmake.depends ${TARGETS} ${SUBDIR_BUILD} ${CMAKE_COMMAND}",
  923. 0);
  924. this->OutputMakeRule(fout,
  925. "remove generated files",
  926. "clean",
  927. "${SUBDIR_CLEAN}",
  928. "rm -f ${CLEAN_OBJECT_FILES} ${EXECUTABLES} ${TARGETS}");
  929. this->OutputMakeRule(fout,
  930. "Rule to build the Makefile",
  931. "Makefile",
  932. "${CMAKE_COMMAND} ${CMAKE_MAKEFILE_SOURCES} ",
  933. "${CMAKE_COMMAND} "
  934. "-S${CMAKE_CURRENT_SOURCE} -O${CMAKE_CURRENT_BINARY} "
  935. "-H${CMAKE_SOURCE_DIR} -B${CMAKE_BINARY_DIR}");
  936. this->OutputMakeRule(fout,
  937. "Rule to build the cmake.depends",
  938. "cmake.depends",
  939. "${CMAKE_COMMAND} ${CMAKE_MAKEFILE_SOURCES} ",
  940. "${CMAKE_COMMAND} "
  941. "-S${CMAKE_CURRENT_SOURCE} -O${CMAKE_CURRENT_BINARY} "
  942. "-H${CMAKE_SOURCE_DIR} -B${CMAKE_BINARY_DIR}");
  943. this->OutputMakeRule(fout,
  944. "Rule to force the build of cmake.depends",
  945. "depend",
  946. "${SUBDIR_DEPEND}",
  947. "${CMAKE_COMMAND} "
  948. "-S${CMAKE_CURRENT_SOURCE} -O${CMAKE_CURRENT_BINARY} "
  949. "-H${CMAKE_SOURCE_DIR} -B${CMAKE_BINARY_DIR}");
  950. this->OutputMakeRule(fout,
  951. "Rebuild the cache",
  952. "rebuild_cache",
  953. "${CMAKE_BINARY_DIR}/CMakeCache.txt",
  954. "${CMAKE_COMMAND} "
  955. "-H${CMAKE_SOURCE_DIR} -B${CMAKE_BINARY_DIR}");
  956. this->OutputMakeRule(fout,
  957. "Rebuild the cache",
  958. "${CMAKE_BINARY_DIR}/CMakeCache.txt",
  959. 0,
  960. "${CMAKE_COMMAND} "
  961. "-H${CMAKE_SOURCE_DIR} -B${CMAKE_BINARY_DIR}");
  962. this->OutputMakeRule(fout,
  963. "Rebuild cmake dummy rule",
  964. "${CMAKE_COMMAND}",
  965. 0,
  966. "echo \"cmake might be out of date\"");
  967. this->OutputMakeRule(fout,
  968. "Rule to keep make from removing Makefiles "
  969. "if control-C is hit during a run of cmake.",
  970. ".PRECIOUS",
  971. "Makefile cmake.depends",
  972. 0);
  973. }
  974. void cmUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  975. const char* comment,
  976. const char* target,
  977. const char* depends,
  978. const char* command)
  979. {
  980. if(!target)
  981. {
  982. cmSystemTools::Error("no target for OutputMakeRule");
  983. return;
  984. }
  985. std::string replace;
  986. if(comment)
  987. {
  988. replace = comment;
  989. m_Makefile->ExpandVariablesInString(replace);
  990. fout << "# " << comment;
  991. }
  992. fout << "\n";
  993. replace = target;
  994. m_Makefile->ExpandVariablesInString(replace);
  995. fout << replace.c_str() << ": ";
  996. if(depends)
  997. {
  998. replace = depends;
  999. m_Makefile->ExpandVariablesInString(replace);
  1000. fout << replace.c_str();
  1001. }
  1002. fout << "\n";
  1003. if(command)
  1004. {
  1005. replace = command;
  1006. m_Makefile->ExpandVariablesInString(replace);
  1007. fout << "\t" << replace.c_str() << "\n\n";
  1008. }
  1009. fout << "\n\n\n";
  1010. }
  1011. void cmUnixMakefileGenerator::SetLocal (bool local)
  1012. {
  1013. if (local)
  1014. {
  1015. m_CacheOnly = false;
  1016. m_Recurse = false;
  1017. }
  1018. else
  1019. {
  1020. m_CacheOnly = true;
  1021. m_Recurse = true;
  1022. }
  1023. }
  1024. void cmUnixMakefileGenerator::ComputeSystemInfo()
  1025. {
  1026. if (m_CacheOnly)
  1027. {
  1028. // currently we run configure shell script here to determine the info
  1029. std::string output;
  1030. std::string cmd;
  1031. const char* root
  1032. = cmCacheManager::GetInstance()->GetCacheValue("CMAKE_ROOT");
  1033. cmd = root;
  1034. cmd += "/Templates/configure";
  1035. cmSystemTools::RunCommand(cmd.c_str(), output);
  1036. m_Makefile->AddDefinition("RUN_CONFIGURE", true);
  1037. }
  1038. // now load the settings
  1039. std::string fpath = m_Makefile->GetHomeOutputDirectory();
  1040. fpath += "/CMakeSystemConfig.cmake";
  1041. m_Makefile->ReadListFile(NULL,fpath.c_str());
  1042. }