cmUnixMakefileGenerator.cxx 34 KB

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