cmUnixMakefileGenerator.cxx 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  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. // suppoirt override in output directories
  48. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  49. {
  50. m_LibraryOutputPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  51. if(m_LibraryOutputPath.size() &&
  52. m_LibraryOutputPath[m_LibraryOutputPath.size() -1] != '/')
  53. {
  54. m_LibraryOutputPath += "/";
  55. }
  56. cmSystemTools::MakeDirectory(m_LibraryOutputPath.c_str());
  57. m_Makefile->AddLinkDirectory(m_LibraryOutputPath.c_str());
  58. }
  59. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  60. {
  61. m_ExecutableOutputPath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  62. if(m_ExecutableOutputPath.size() &&
  63. m_ExecutableOutputPath[m_ExecutableOutputPath.size() -1] != '/')
  64. {
  65. m_ExecutableOutputPath += "/";
  66. }
  67. cmSystemTools::MakeDirectory(m_ExecutableOutputPath.c_str());
  68. m_Makefile->AddLinkDirectory(m_ExecutableOutputPath.c_str());
  69. }
  70. if(m_CacheOnly)
  71. {
  72. // Generate the cache only stuff
  73. this->GenerateCacheOnly();
  74. // if recurse then generate for all sub- makefiles
  75. if(m_Recurse)
  76. {
  77. this->RecursiveGenerateCacheOnly();
  78. }
  79. }
  80. // normal makefile output
  81. else
  82. {
  83. // Generate depends
  84. cmMakeDepend md;
  85. md.SetMakefile(m_Makefile);
  86. md.GenerateMakefileDependencies();
  87. this->ProcessDepends(md);
  88. // output the makefile fragment
  89. this->OutputMakefile("Makefile");
  90. }
  91. }
  92. void cmUnixMakefileGenerator::ProcessDepends(const cmMakeDepend &md)
  93. {
  94. // Now create cmDependInformation objects for files in the directory
  95. cmTargets &tgts = m_Makefile->GetTargets();
  96. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  97. {
  98. std::vector<cmSourceFile> &classes = l->second.GetSourceFiles();
  99. for(std::vector<cmSourceFile>::iterator i = classes.begin();
  100. i != classes.end(); ++i)
  101. {
  102. if(!i->GetIsAHeaderFileOnly())
  103. {
  104. // get the depends
  105. const cmDependInformation *info =
  106. md.GetDependInformationForSourceFile(*i);
  107. // Delete any hints from the source file's dependencies.
  108. i->GetDepends().erase(i->GetDepends().begin(), i->GetDepends().end());
  109. // Now add the real dependencies for the file.
  110. if (info)
  111. {
  112. for(cmDependInformation::DependencySet::const_iterator d =
  113. info->m_DependencySet.begin();
  114. d != info->m_DependencySet.end(); ++d)
  115. {
  116. // Make sure the full path is given. If not, the dependency was
  117. // not found.
  118. if((*d)->m_FullPath != "")
  119. {
  120. i->GetDepends().push_back((*d)->m_FullPath);
  121. }
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
  128. // This is where CMakeTargets.make is generated
  129. void cmUnixMakefileGenerator::OutputMakefile(const char* file)
  130. {
  131. // Create sub directories fro aux source directories
  132. std::vector<std::string>& auxSourceDirs =
  133. m_Makefile->GetAuxSourceDirectories();
  134. if( auxSourceDirs.size() )
  135. {
  136. // For the case when this is running as a remote build
  137. // on unix, make the directory
  138. for(std::vector<std::string>::iterator i = auxSourceDirs.begin();
  139. i != auxSourceDirs.end(); ++i)
  140. {
  141. if(i->c_str()[0] != '/')
  142. {
  143. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  144. if(dir.size() && dir[dir.size()-1] != '/')
  145. {
  146. dir += "/";
  147. }
  148. dir += *i;
  149. cmSystemTools::MakeDirectory(dir.c_str());
  150. }
  151. else
  152. {
  153. cmSystemTools::MakeDirectory(i->c_str());
  154. }
  155. }
  156. }
  157. // Create a stream that writes to a temporary file
  158. // then does a copy at the end. This is to allow users
  159. // to hit control-c during the make of the makefile
  160. cmGeneratedFileStream tempFile(file);
  161. tempFile.SetAlwaysCopy(true);
  162. std::ostream& fout = tempFile.GetStream();
  163. if(!fout)
  164. {
  165. cmSystemTools::Error("Error can not open for write: ", file);
  166. return;
  167. }
  168. fout << "# CMAKE generated Makefile, DO NOT EDIT!\n"
  169. << "# Generated from the following files:\n# "
  170. << m_Makefile->GetHomeOutputDirectory() << "/CMakeCache.txt\n";
  171. std::vector<std::string> lfiles = m_Makefile->GetListFiles();
  172. // sort the array
  173. std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  174. // remove duplicates
  175. std::vector<std::string>::iterator new_end =
  176. std::unique(lfiles.begin(), lfiles.end());
  177. lfiles.erase(new_end, lfiles.end());
  178. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  179. i != lfiles.end(); ++i)
  180. {
  181. fout << "# " << i->c_str() << "\n";
  182. }
  183. fout << "\n\n";
  184. // create a make variable with all of the sources for this Makefile
  185. // for depend purposes.
  186. fout << "CMAKE_MAKEFILE_SOURCES = ";
  187. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  188. i != lfiles.end(); ++i)
  189. {
  190. fout << " " << i->c_str();
  191. }
  192. // Add the cache to the list
  193. fout << " " << m_Makefile->GetHomeOutputDirectory() << "/CMakeCache.txt\n";
  194. fout << "\n\n";
  195. this->OutputMakeVariables(fout);
  196. this->OutputMakeFlags(fout);
  197. this->OutputTargetRules(fout);
  198. this->OutputDependencies(fout);
  199. this->OutputTargets(fout);
  200. this->OutputSubDirectoryRules(fout);
  201. std::string dependName = m_Makefile->GetStartOutputDirectory();
  202. dependName += "/cmake.depends";
  203. if(!this->m_CacheOnly)
  204. {
  205. std::ofstream dependout(dependName.c_str());
  206. if(!dependout)
  207. {
  208. cmSystemTools::Error("Error can not open for write: ", dependName.c_str());
  209. return;
  210. }
  211. this->OutputObjectDepends(dependout);
  212. }
  213. this->OutputCustomRules(fout);
  214. this->OutputMakeRules(fout);
  215. this->OutputInstallRules(fout);
  216. // only add the depend include if the depend file exists
  217. if(cmSystemTools::FileExists(dependName.c_str()))
  218. {
  219. fout << "include cmake.depends\n";
  220. }
  221. }
  222. // Output the rules for any targets
  223. void cmUnixMakefileGenerator::OutputTargetRules(std::ostream& fout)
  224. {
  225. // for each target add to the list of targets
  226. fout << "TARGETS = ";
  227. const cmTargets &tgts = m_Makefile->GetTargets();
  228. // list libraries first
  229. for(cmTargets::const_iterator l = tgts.begin();
  230. l != tgts.end(); l++)
  231. {
  232. if (l->second.IsInAll())
  233. {
  234. if(l->second.GetType() == cmTarget::STATIC_LIBRARY)
  235. {
  236. fout << " \\\n" << m_LibraryOutputPath << "lib" << l->first.c_str()
  237. << ".a";
  238. }
  239. else if(l->second.GetType() == cmTarget::SHARED_LIBRARY)
  240. {
  241. fout << " \\\n" << m_LibraryOutputPath << "lib" << l->first.c_str()
  242. << m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  243. }
  244. }
  245. }
  246. // executables
  247. for(cmTargets::const_iterator l = tgts.begin();
  248. l != tgts.end(); l++)
  249. {
  250. if ((l->second.GetType() == cmTarget::EXECUTABLE ||
  251. l->second.GetType() == cmTarget::WIN32_EXECUTABLE) &&
  252. l->second.IsInAll())
  253. {
  254. fout << " \\\n" << m_ExecutableOutputPath << l->first.c_str();
  255. }
  256. }
  257. // list utilities last
  258. for(cmTargets::const_iterator l = tgts.begin();
  259. l != tgts.end(); l++)
  260. {
  261. if (l->second.GetType() == cmTarget::UTILITY &&
  262. l->second.IsInAll())
  263. {
  264. fout << " \\\n" << l->first.c_str();
  265. }
  266. }
  267. fout << "\n\n";
  268. // get the classes from the source lists then add them to the groups
  269. for(cmTargets::const_iterator l = tgts.begin();
  270. l != tgts.end(); l++)
  271. {
  272. std::vector<cmSourceFile> classes = l->second.GetSourceFiles();
  273. if (classes.begin() != classes.end())
  274. {
  275. fout << l->first << "_SRC_OBJS = ";
  276. for(std::vector<cmSourceFile>::iterator i = classes.begin();
  277. i != classes.end(); i++)
  278. {
  279. if(!i->IsAHeaderFileOnly())
  280. {
  281. fout << "\\\n" << i->GetSourceName() << ".o ";
  282. }
  283. }
  284. fout << "\n\n";
  285. }
  286. }
  287. fout << "CLEAN_OBJECT_FILES = ";
  288. for(cmTargets::const_iterator l = tgts.begin();
  289. l != tgts.end(); l++)
  290. {
  291. std::vector<cmSourceFile> classes = l->second.GetSourceFiles();
  292. if (classes.begin() != classes.end())
  293. {
  294. fout << "${" << l->first << "_SRC_OBJS} ";
  295. }
  296. }
  297. fout << "\n";
  298. }
  299. /**
  300. * Output the linking rules on a command line. For executables,
  301. * targetLibrary should be a NULL pointer. For libraries, it should point
  302. * to the name of the library. This will not link a library against itself.
  303. */
  304. void cmUnixMakefileGenerator::OutputLinkLibraries(std::ostream& fout,
  305. const char* targetLibrary,
  306. const cmTarget &tgt)
  307. {
  308. // Try to emit each search path once
  309. std::set<std::string> emitted;
  310. // Embed runtime search paths if possible and if required.
  311. bool outputRuntime = true;
  312. std::string runtimeFlag;
  313. std::string runtimeSep;
  314. std::vector<std::string> runtimeDirs;
  315. if(m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_FLAG"))
  316. {
  317. runtimeFlag = m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_FLAG");
  318. }
  319. if(m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_SEP"))
  320. {
  321. runtimeSep = m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_SEP");
  322. }
  323. // concatenate all paths or no?
  324. bool runtimeConcatenate = ( runtimeSep!="" );
  325. if(runtimeFlag == "")
  326. {
  327. outputRuntime = false;
  328. }
  329. // Some search paths should never be emitted
  330. emitted.insert("");
  331. emitted.insert("/usr/lib");
  332. // collect all the flags needed for linking libraries
  333. std::string linkLibs;
  334. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  335. for(std::vector<std::string>::iterator libDir = libdirs.begin();
  336. libDir != libdirs.end(); ++libDir)
  337. {
  338. std::string libpath = cmSystemTools::EscapeSpaces(libDir->c_str());
  339. if (m_LibraryOutputPath.size())
  340. {
  341. if(m_LibraryOutputPath != libpath
  342. && (libpath.find(m_Makefile->GetHomeOutputDirectory())
  343. != std::string::npos))
  344. {
  345. emitted.insert(libpath);
  346. }
  347. }
  348. if(emitted.insert(libpath).second)
  349. {
  350. std::string::size_type pos = libDir->find("-L");
  351. if((pos == std::string::npos || pos > 0)
  352. && libDir->find("${") == std::string::npos)
  353. {
  354. linkLibs += "-L";
  355. if(outputRuntime)
  356. {
  357. runtimeDirs.push_back( libpath );
  358. }
  359. }
  360. linkLibs += libpath;
  361. linkLibs += " ";
  362. }
  363. }
  364. std::string librariesLinked;
  365. const cmTarget::LinkLibraries& libs = tgt.GetLinkLibraries();
  366. for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
  367. lib != libs.end(); ++lib)
  368. {
  369. // Don't link the library against itself!
  370. if(targetLibrary && (lib->first == targetLibrary)) continue;
  371. // don't look at debug libraries
  372. if (lib->second == cmTarget::DEBUG) continue;
  373. // skip zero size library entries, this may happen
  374. // if a variable expands to nothing.
  375. if (lib->first.size() == 0) continue;
  376. // if it is a full path break it into -L and -l
  377. cmRegularExpression reg("(^[ \t]*\\-l)|(\\${)");
  378. if(lib->first.find('/') != std::string::npos
  379. && !reg.find(lib->first))
  380. {
  381. std::string dir, file;
  382. cmSystemTools::SplitProgramPath(lib->first.c_str(),
  383. dir, file);
  384. std::string libpath = cmSystemTools::EscapeSpaces(dir.c_str());
  385. if(emitted.insert(libpath).second)
  386. {
  387. linkLibs += "-L";
  388. linkLibs += libpath;
  389. linkLibs += " ";
  390. if(outputRuntime)
  391. {
  392. runtimeDirs.push_back( libpath );
  393. }
  394. }
  395. cmRegularExpression libname("lib(.*)\\.(.*)");
  396. cmRegularExpression libname_noprefix("(.*)\\.(.*)");
  397. if(libname.find(file))
  398. {
  399. librariesLinked += "-l";
  400. file = libname.match(1);
  401. librariesLinked += file;
  402. librariesLinked += " ";
  403. }
  404. else if(libname_noprefix.find(file))
  405. {
  406. librariesLinked += "-l";
  407. file = libname_noprefix.match(1);
  408. librariesLinked += file;
  409. librariesLinked += " ";
  410. }
  411. }
  412. // not a full path, so add -l name
  413. else
  414. {
  415. if(!reg.find(lib->first))
  416. {
  417. librariesLinked += "-l";
  418. }
  419. librariesLinked += lib->first;
  420. librariesLinked += " ";
  421. }
  422. }
  423. linkLibs += librariesLinked;
  424. if(!targetLibrary)
  425. {
  426. // For executables, add these a second time so order does not matter
  427. linkLibs += librariesLinked;
  428. }
  429. fout << linkLibs;
  430. if(outputRuntime && runtimeDirs.size()>0)
  431. {
  432. // For the runtime search directories, do a "-Wl,-rpath,a:b:c" or
  433. // a "-R a -R b -R c" type link line
  434. fout << runtimeFlag;
  435. std::vector<std::string>::iterator itr = runtimeDirs.begin();
  436. fout << *itr;
  437. ++itr;
  438. for( ; itr != runtimeDirs.end(); ++itr )
  439. {
  440. if(runtimeConcatenate)
  441. {
  442. fout << runtimeSep << *itr;
  443. }
  444. else
  445. {
  446. fout << " " << runtimeFlag << *itr;
  447. }
  448. }
  449. fout << " ";
  450. }
  451. }
  452. void cmUnixMakefileGenerator::OutputTargets(std::ostream& fout)
  453. {
  454. // for each target
  455. const cmTargets &tgts = m_Makefile->GetTargets();
  456. for(cmTargets::const_iterator l = tgts.begin();
  457. l != tgts.end(); l++)
  458. {
  459. if (l->second.GetType() == cmTarget::STATIC_LIBRARY)
  460. {
  461. fout << "#---------------------------------------------------------\n";
  462. fout << "# rules for a static library\n";
  463. fout << "#\n";
  464. fout << m_LibraryOutputPath << "lib" << l->first << ".a: ${" <<
  465. l->first << "_SRC_OBJS} \n";
  466. fout << "\t${CMAKE_AR} ${CMAKE_AR_ARGS} "
  467. << m_LibraryOutputPath << "lib" << l->first << ".a ${" <<
  468. l->first << "_SRC_OBJS} \n";
  469. fout << "\t${CMAKE_RANLIB} "
  470. << m_LibraryOutputPath << "lib" << l->first << ".a\n";
  471. fout << "\n\n";
  472. }
  473. else if (l->second.GetType() == cmTarget::SHARED_LIBRARY)
  474. {
  475. fout << "#---------------------------------------------------------\n";
  476. fout << "# rules for a shared library\n";
  477. fout << "#\n";
  478. fout << m_LibraryOutputPath << "lib" << l->first << "$(SHLIB_SUFFIX): ${" <<
  479. l->first << "_SRC_OBJS} \n";
  480. fout << "\trm -f lib" << l->first << "$(SHLIB_SUFFIX)\n";
  481. fout << "\t$(CMAKE_CXX_COMPILER) ${CMAKE_SHLIB_LINK_FLAGS} "
  482. "${CMAKE_SHLIB_BUILD_FLAGS} ${CMAKE_CXXFLAGS} -o \\\n";
  483. fout << "\t " << m_LibraryOutputPath << "lib" << l->first << "$(SHLIB_SUFFIX) \\\n";
  484. fout << "\t ${" << l->first <<
  485. "_SRC_OBJS} ";
  486. this->OutputLinkLibraries(fout, l->first.c_str(), l->second);
  487. fout << "\n\n";
  488. }
  489. else if ((l->second.GetType() == cmTarget::EXECUTABLE)
  490. || (l->second.GetType() == cmTarget::WIN32_EXECUTABLE))
  491. {
  492. fout << m_ExecutableOutputPath << l->first << ": ${" <<
  493. l->first << "_SRC_OBJS} ${CMAKE_DEPEND_LIBS}\n";
  494. fout << "\t${CMAKE_CXX_COMPILER} ${CMAKE_SHLIB_LINK_FLAGS} ${CMAKE_CXXFLAGS} "
  495. << "${" << l->first << "_SRC_OBJS} ";
  496. this->OutputLinkLibraries(fout, NULL,l->second);
  497. fout << " -o " << m_ExecutableOutputPath << l->first << "\n\n";
  498. }
  499. }
  500. }
  501. // output the list of libraries that the executables
  502. // in this makefile will depend on.
  503. void cmUnixMakefileGenerator::OutputDependencies(std::ostream& fout)
  504. {
  505. // Each dependency should only be emitted once.
  506. std::set<std::string> emitted;
  507. fout << "CMAKE_DEPEND_LIBS = ";
  508. cmTarget::LinkLibraries& libs = m_Makefile->GetLinkLibraries();
  509. cmTarget::LinkLibraries::const_iterator lib2;
  510. // Search the list of libraries that will be linked into
  511. // the executable
  512. emitted.clear();
  513. for(lib2 = libs.begin(); lib2 != libs.end(); ++lib2)
  514. {
  515. if( ! emitted.insert(lib2->first).second ) continue;
  516. const char* cacheValue
  517. = m_Makefile->GetDefinition(lib2->first.c_str());
  518. if(cacheValue )
  519. {
  520. // if there is a cache value then this is a library that cmake
  521. // knows how to build, so we can depend on it
  522. std::string libpath;
  523. if (strcmp(m_Makefile->GetCurrentOutputDirectory(), cacheValue) != 0)
  524. {
  525. // if the library is not in the current directory, then get the full
  526. // path to it
  527. libpath = cacheValue;
  528. if(m_LibraryOutputPath.size())
  529. {
  530. libpath = m_LibraryOutputPath;
  531. libpath += "lib";
  532. }
  533. else
  534. {
  535. libpath += "/lib";
  536. }
  537. }
  538. else
  539. {
  540. // library is in current Makefile so use lib as a prefix
  541. libpath = m_LibraryOutputPath;
  542. libpath += "lib";
  543. }
  544. // add the library name
  545. libpath += lib2->first;
  546. // add the correct extension
  547. std::string ltname = lib2->first+"_LIBRARY_TYPE";
  548. const char* libType
  549. = m_Makefile->GetDefinition(ltname.c_str());
  550. if(libType && std::string(libType) == "SHARED")
  551. {
  552. libpath += m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  553. }
  554. else
  555. {
  556. libpath += ".a";
  557. }
  558. fout << libpath << " ";
  559. }
  560. }
  561. fout << "\n\n";
  562. emitted.clear();
  563. for(lib2 = libs.begin(); lib2 != libs.end(); ++lib2)
  564. {
  565. // loop over the list of directories that the libraries might
  566. // be in, looking for an ADD_LIBRARY(lib...) line. This would
  567. // be stored in the cache
  568. if( ! emitted.insert(lib2->first).second ) continue;
  569. const char* cacheValue
  570. = m_Makefile->GetDefinition(lib2->first.c_str());
  571. // if cache and not the current directory add a rule, to
  572. // jump into the directory and build for the first time
  573. if(cacheValue
  574. && (strcmp(m_Makefile->GetCurrentOutputDirectory(), cacheValue) != 0))
  575. {
  576. std::string library = "lib";
  577. library += lib2->first;
  578. std::string libpath = cacheValue;
  579. // add the correct extension
  580. std::string ltname = lib2->first+"_LIBRARY_TYPE";
  581. const char* libType
  582. = m_Makefile->GetDefinition(ltname.c_str());
  583. if(libType && std::string(libType) == "SHARED")
  584. {
  585. library += m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  586. }
  587. else
  588. {
  589. library += ".a";
  590. }
  591. if(m_LibraryOutputPath.size())
  592. {
  593. libpath = m_LibraryOutputPath;
  594. }
  595. else
  596. {
  597. libpath += "/";
  598. }
  599. libpath += library;
  600. // put out a rule to build the library if it does not exist
  601. fout << libpath.c_str()
  602. << ":\n\tcd " << cacheValue
  603. << "; ${MAKE} " << m_LibraryOutputPath << library.c_str() << "\n\n";
  604. }
  605. }
  606. }
  607. // output make include flags
  608. void cmUnixMakefileGenerator::OutputMakeFlags(std::ostream& fout)
  609. {
  610. // Output Include paths
  611. fout << "INCLUDE_FLAGS = ";
  612. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  613. std::vector<std::string>::iterator i;
  614. fout << "-I" << m_Makefile->GetStartDirectory() << " ";
  615. for(i = includes.begin(); i != includes.end(); ++i)
  616. {
  617. std::string include = *i;
  618. // Don't output a -I for the standard include path "/usr/include".
  619. // This can cause problems with certain standard library
  620. // implementations because the wrong headers may be found first.
  621. if(include != "/usr/include")
  622. {
  623. fout << "-I" << cmSystemTools::EscapeSpaces(i->c_str()).c_str() << " ";
  624. }
  625. }
  626. fout << m_Makefile->GetDefineFlags();
  627. fout << "\n\n";
  628. fout << "default_target: all\n\n";
  629. // see if there are files to compile in this makefile
  630. // These are used for both libraries and executables
  631. }
  632. // fix up names of directories so they can be used
  633. // as targets in makefiles.
  634. inline std::string FixDirectoryName(const char* dir)
  635. {
  636. std::string s = dir;
  637. // replace ../ with 3 under bars
  638. size_t pos = s.find("../");
  639. if(pos != std::string::npos)
  640. {
  641. s.replace(pos, 3, "___");
  642. }
  643. // replace / directory separators with a single under bar
  644. pos = s.find("/");
  645. while(pos != std::string::npos)
  646. {
  647. s.replace(pos, 1, "_");
  648. pos = s.find("/");
  649. }
  650. return s;
  651. }
  652. void
  653. cmUnixMakefileGenerator::
  654. OutputSubDirectoryVars(std::ostream& fout,
  655. const char* var,
  656. const char* target,
  657. const char* target1,
  658. const char* target2,
  659. const std::vector<std::string>& SubDirectories)
  660. {
  661. if( SubDirectories.size() == 0)
  662. {
  663. return;
  664. }
  665. fout << "# Variable for making " << target << " in subdirectories.\n";
  666. fout << var << " = \\\n";
  667. unsigned int i;
  668. for(i =0; i < SubDirectories.size(); i++)
  669. {
  670. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  671. fout << target << "_" << subdir.c_str();
  672. if(i == SubDirectories.size()-1)
  673. {
  674. fout << " \n\n";
  675. }
  676. else
  677. {
  678. fout << " \\\n";
  679. }
  680. }
  681. fout << "# Targets for making " << target << " in subdirectories.\n";
  682. for(unsigned int i =0; i < SubDirectories.size(); i++)
  683. {
  684. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  685. fout << target << "_" << subdir.c_str() << ":";
  686. const std::set<cmStdString>& subdirDepends = m_Makefile->GetSubdirDepends(SubDirectories[i].c_str());
  687. for(std::set<cmStdString>::const_iterator d = subdirDepends.begin();
  688. d != subdirDepends.end(); ++d)
  689. {
  690. std::string fixed_d = FixDirectoryName(d->c_str());
  691. fout << " " << target << "_" << fixed_d.c_str();
  692. }
  693. fout << "\n";
  694. if(target1)
  695. {
  696. fout << "\t@if test ! -d " << SubDirectories[i].c_str() << "; then ${MAKE} rebuild_cache; fi\n"
  697. "\tcd " << SubDirectories[i].c_str()
  698. << "; ${MAKE} -${MAKEFLAGS} " << target1 << "\n";
  699. }
  700. if(target2)
  701. {
  702. fout << "\t@cd " << SubDirectories[i].c_str()
  703. << "; ${MAKE} -${MAKEFLAGS} " << target2 << "\n";
  704. }
  705. }
  706. fout << "\n\n";
  707. }
  708. // output rules for decending into sub directories
  709. void cmUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  710. {
  711. // Output Sub directory build rules
  712. const std::vector<std::string>& SubDirectories
  713. = m_Makefile->GetSubDirectories();
  714. if( SubDirectories.size() == 0)
  715. {
  716. return;
  717. }
  718. this->OutputSubDirectoryVars(fout, "SUBDIR_BUILD", "build",
  719. "cmake.depends",
  720. "all",
  721. SubDirectories);
  722. this->OutputSubDirectoryVars(fout, "SUBDIR_CLEAN", "clean",
  723. "clean",
  724. 0,
  725. SubDirectories);
  726. this->OutputSubDirectoryVars(fout, "SUBDIR_DEPEND", "depend",
  727. "depend",
  728. 0,
  729. SubDirectories);
  730. this->OutputSubDirectoryVars(fout, "SUBDIR_INSTALL", "install",
  731. "install",
  732. 0,
  733. SubDirectories);
  734. }
  735. // Output the depend information for all the classes
  736. // in the makefile. These would have been generated
  737. // by the class cmMakeDepend GenerateMakefile
  738. void cmUnixMakefileGenerator::OutputObjectDepends(std::ostream& fout)
  739. {
  740. // Iterate over every target.
  741. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  742. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  743. target != targets.end(); ++target)
  744. {
  745. // Iterate over every source for this target.
  746. const std::vector<cmSourceFile>& sources = target->second.GetSourceFiles();
  747. for(std::vector<cmSourceFile>::const_iterator source = sources.begin();
  748. source != sources.end(); ++source)
  749. {
  750. if(!source->IsAHeaderFileOnly())
  751. {
  752. if(!source->GetDepends().empty())
  753. {
  754. fout << source->GetSourceName() << ".o :";
  755. // Iterate through all the dependencies for this source.
  756. for(std::vector<std::string>::const_iterator dep =
  757. source->GetDepends().begin();
  758. dep != source->GetDepends().end(); ++dep)
  759. {
  760. fout << " \\\n" << dep->c_str();
  761. }
  762. fout << "\n\n";
  763. }
  764. }
  765. }
  766. }
  767. }
  768. // Output each custom rule in the following format:
  769. // output: source depends...
  770. // (tab) command...
  771. void cmUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
  772. {
  773. // We may be modifying the source groups temporarily, so make a copy.
  774. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  775. const cmTargets &tgts = m_Makefile->GetTargets();
  776. for(cmTargets::const_iterator tgt = tgts.begin();
  777. tgt != tgts.end(); ++tgt)
  778. {
  779. // add any custom rules to the source groups
  780. for (std::vector<cmCustomCommand>::const_iterator cr =
  781. tgt->second.GetCustomCommands().begin();
  782. cr != tgt->second.GetCustomCommands().end(); ++cr)
  783. {
  784. cmSourceGroup& sourceGroup =
  785. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  786. sourceGroups);
  787. cmCustomCommand cc(*cr);
  788. cc.ExpandVariables(*m_Makefile);
  789. sourceGroup.AddCustomCommand(cc);
  790. }
  791. }
  792. // Loop through every source group.
  793. for(std::vector<cmSourceGroup>::const_iterator sg =
  794. sourceGroups.begin(); sg != sourceGroups.end(); ++sg)
  795. {
  796. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  797. if(buildRules.empty())
  798. { continue; }
  799. std::string name = sg->GetName();
  800. if(name != "")
  801. {
  802. fout << "# Start of source group \"" << name.c_str() << "\"\n";
  803. }
  804. // Loop through each source in the source group.
  805. for(cmSourceGroup::BuildRules::const_iterator cc =
  806. buildRules.begin(); cc != buildRules.end(); ++ cc)
  807. {
  808. std::string source = cc->first;
  809. const cmSourceGroup::Commands& commands = cc->second;
  810. // Loop through every command generating code from the current source.
  811. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  812. c != commands.end(); ++c)
  813. {
  814. std::string command = c->first;
  815. const cmSourceGroup::CommandFiles& commandFiles = c->second;
  816. // if the command has no outputs, then it is a utility command
  817. // with no outputs
  818. if(commandFiles.m_Outputs.size() == 0)
  819. {
  820. fout << source.c_str() << ": ";
  821. // Write out all the dependencies for this rule.
  822. for(std::set<std::string>::const_iterator d =
  823. commandFiles.m_Depends.begin();
  824. d != commandFiles.m_Depends.end(); ++d)
  825. {
  826. std::string dep = cmSystemTools::EscapeSpaces(d->c_str());
  827. fout << " " << dep.c_str();
  828. }
  829. fout << "\n\t" << command.c_str() << "\n\n";
  830. }
  831. // Write a rule for every output generated by this command.
  832. for(std::set<std::string>::const_iterator output =
  833. commandFiles.m_Outputs.begin();
  834. output != commandFiles.m_Outputs.end(); ++output)
  835. {
  836. std::string src = cmSystemTools::EscapeSpaces(source.c_str());
  837. fout << output->c_str() << ": " << src.c_str();
  838. // Write out all the dependencies for this rule.
  839. for(std::set<std::string>::const_iterator d =
  840. commandFiles.m_Depends.begin();
  841. d != commandFiles.m_Depends.end(); ++d)
  842. {
  843. std::string dep = cmSystemTools::EscapeSpaces(d->c_str());
  844. fout << " " << dep.c_str();
  845. }
  846. fout << "\n\t" << command.c_str() << "\n\n";
  847. }
  848. }
  849. }
  850. if(name != "")
  851. {
  852. fout << "# End of source group \"" << name.c_str() << "\"\n\n";
  853. }
  854. }
  855. }
  856. void cmUnixMakefileGenerator::GenerateCacheOnly()
  857. {
  858. cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory());
  859. std::string dest = m_Makefile->GetStartOutputDirectory();
  860. dest += "/Makefile";
  861. std::cout << "cmake: creating : " << dest.c_str() << std::endl;
  862. this->OutputMakefile(dest.c_str());
  863. return;
  864. }
  865. void cmUnixMakefileGenerator::RecursiveGenerateCacheOnly()
  866. {
  867. std::vector<cmMakefile*> makefiles;
  868. m_Makefile->FindSubDirectoryCMakeListsFiles(makefiles);
  869. for(std::vector<cmMakefile*>::iterator i = makefiles.begin();
  870. i != makefiles.end(); ++i)
  871. {
  872. cmMakefile* mf = *i;
  873. if(m_Makefile->GetDefinition("RUN_CONFIGURE"))
  874. {
  875. mf->AddDefinition("RUN_CONFIGURE", true);
  876. }
  877. cmUnixMakefileGenerator* gen = new cmUnixMakefileGenerator;
  878. gen->SetCacheOnlyOn();
  879. gen->SetRecurseOff();
  880. mf->SetMakefileGenerator(gen);
  881. mf->GenerateMakefile();
  882. }
  883. // CLEAN up the makefiles created
  884. for(unsigned int i =0; i < makefiles.size(); ++i)
  885. {
  886. delete makefiles[i];
  887. }
  888. }
  889. void cmUnixMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  890. {
  891. if(strcmp(m_Makefile->GetHomeDirectory(),
  892. m_Makefile->GetHomeOutputDirectory()) == 0)
  893. {
  894. fout << "srcdir = .\n\n";
  895. }
  896. else
  897. {
  898. fout << "srcdir = " << m_Makefile->GetStartDirectory() << "\n";
  899. fout << "VPATH = " << m_Makefile->GetStartDirectory() << "\n";
  900. }
  901. const char* variables =
  902. "# the standard shell for make\n"
  903. "SHELL = /bin/sh\n"
  904. "\n"
  905. "CMAKE_LIB_EXT = @CMAKE_LIB_EXT@\n"
  906. "CMAKE_RANLIB = @CMAKE_RANLIB@\n"
  907. "CMAKE_AR = @CMAKE_AR@\n"
  908. "CMAKE_AR_ARGS = @CMAKE_AR_ARGS@\n"
  909. "CMAKE_C_COMPILER = @CMAKE_C_COMPILER@\n"
  910. "CMAKE_CFLAGS = @CMAKE_C_FLAGS@\n"
  911. "CMAKE_SHLIB_CFLAGS = @CMAKE_SHLIB_CFLAGS@\n"
  912. "\n"
  913. "CMAKE_CXX_COMPILER = @CMAKE_CXX_COMPILER@\n"
  914. "CMAKE_CXXFLAGS = @CMAKE_CXX_FLAGS@ @CMAKE_TEMPLATE_FLAGS@\n"
  915. "\n"
  916. "CMAKE_SHLIB_BUILD_FLAGS = @CMAKE_SHLIB_BUILD_FLAGS@\n"
  917. "CMAKE_SHLIB_LINK_FLAGS = @CMAKE_SHLIB_LINK_FLAGS@\n"
  918. "DL_LIBS = @CMAKE_DL_LIBS@\n"
  919. "SHLIB_LD_LIBS = @CMAKE_SHLIB_LD_LIBS@\n"
  920. "SHLIB_SUFFIX = @CMAKE_SHLIB_SUFFIX@\n"
  921. "THREAD_LIBS = @CMAKE_THREAD_LIBS@\n"
  922. "\n"
  923. "# set up the path to the rulesgen program\n"
  924. "CMAKE_COMMAND = ${CMAKE_COMMAND}\n"
  925. "\n"
  926. "\n"
  927. "\n";
  928. std::string replaceVars = variables;
  929. m_Makefile->ExpandVariablesInString(replaceVars);
  930. fout << replaceVars.c_str();
  931. fout << "CMAKE_CURRENT_SOURCE = " << m_Makefile->GetStartDirectory() << "\n";
  932. fout << "CMAKE_CURRENT_BINARY = " << m_Makefile->GetStartOutputDirectory() << "\n";
  933. }
  934. void cmUnixMakefileGenerator::OutputInstallRules(std::ostream& fout)
  935. {
  936. const char* root
  937. = m_Makefile->GetDefinition("CMAKE_ROOT");
  938. fout << "INSTALL = " << root << "/Templates/install-sh -c\n";
  939. fout << "INSTALL_PROGRAM = ${INSTALL}\n";
  940. fout << "INSTALL_DATA = ${INSTALL} -m 644\n";
  941. const cmTargets &tgts = m_Makefile->GetTargets();
  942. fout << "install: ${SUBDIR_INSTALL}\n";
  943. fout << "\t@echo \"Installing ...\"\n";
  944. const char* prefix
  945. = m_Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  946. if (!prefix)
  947. {
  948. prefix = "/usr/local";
  949. }
  950. for(cmTargets::const_iterator l = tgts.begin();
  951. l != tgts.end(); l++)
  952. {
  953. if (l->second.GetInstallPath() != "")
  954. {
  955. // first make the directories for each target
  956. fout << "\t@if [ ! -d " << prefix << l->second.GetInstallPath() <<
  957. " ] ; then \\\n";
  958. fout << "\t echo \"Making directory " << prefix
  959. << l->second.GetInstallPath() << " \"; \\\n";
  960. fout << "\t mkdir -p " << prefix << l->second.GetInstallPath()
  961. << "; \\\n";
  962. fout << "\t chmod 755 " << prefix << l->second.GetInstallPath()
  963. << "; \\\n";
  964. fout << "\t else true; \\\n";
  965. fout << "\t fi\n";
  966. // now install the target
  967. switch (l->second.GetType())
  968. {
  969. case cmTarget::STATIC_LIBRARY:
  970. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  971. << l->first;
  972. fout << ".a";
  973. fout << " " << prefix << l->second.GetInstallPath() << "\n";
  974. break;
  975. case cmTarget::SHARED_LIBRARY:
  976. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  977. << l->first;
  978. fout << m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  979. fout << " " << prefix << l->second.GetInstallPath() << "\n";
  980. break;
  981. case cmTarget::WIN32_EXECUTABLE:
  982. case cmTarget::EXECUTABLE:
  983. fout << "\t$(INSTALL_PROGRAM) " << m_ExecutableOutputPath
  984. << l->first
  985. << cmSystemTools::GetExecutableExtension()
  986. << " " << prefix << l->second.GetInstallPath() << "\n";
  987. break;
  988. case cmTarget::INSTALL_FILES:
  989. {
  990. const std::vector<std::string> &sf = l->second.GetSourceLists();
  991. std::vector<std::string>::const_iterator i;
  992. for (i = sf.begin(); i != sf.end(); ++i)
  993. {
  994. fout << "\t@ echo \"Installing " << *i << " \"\n";
  995. fout << "\t@if [ -f " << *i << " ] ; then \\\n";
  996. // avoid using install-sh to install install-sh
  997. // does not work on windows....
  998. if(*i == "install-sh")
  999. {
  1000. fout << "\t cp ";
  1001. }
  1002. else
  1003. {
  1004. fout << "\t $(INSTALL_DATA) ";
  1005. }
  1006. fout << *i
  1007. << " " << prefix << l->second.GetInstallPath() << "; \\\n";
  1008. fout << "\t elif [ -f ${srcdir}/" << *i << " ] ; then \\\n";
  1009. // avoid using install-sh to install install-sh
  1010. // does not work on windows....
  1011. if(*i == "install-sh")
  1012. {
  1013. fout << "\t cp ";
  1014. }
  1015. else
  1016. {
  1017. fout << "\t $(INSTALL_DATA) ";
  1018. }
  1019. fout << "${srcdir}/" << *i
  1020. << " " << prefix << l->second.GetInstallPath() << "; \\\n";
  1021. fout << "\telse \\\n";
  1022. fout << "\t echo \" ERROR!!! Unable to find: " << *i
  1023. << " \"; \\\n";
  1024. fout << "\t fi\n";
  1025. }
  1026. }
  1027. break;
  1028. case cmTarget::INSTALL_PROGRAMS:
  1029. {
  1030. const std::vector<std::string> &sf = l->second.GetSourceLists();
  1031. std::vector<std::string>::const_iterator i;
  1032. for (i = sf.begin(); i != sf.end(); ++i)
  1033. {
  1034. fout << "\t@ echo \"Installing " << *i << " \"\n";
  1035. fout << "\t@if [ -f " << *i << " ] ; then \\\n";
  1036. // avoid using install-sh to install install-sh
  1037. // does not work on windows....
  1038. if(*i == "install-sh")
  1039. {
  1040. fout << "\t cp ";
  1041. }
  1042. else
  1043. {
  1044. fout << "\t $(INSTALL_PROGRAM) ";
  1045. }
  1046. fout << *i
  1047. << " " << prefix << l->second.GetInstallPath() << "; \\\n";
  1048. fout << "\t elif [ -f ${srcdir}/" << *i << " ] ; then \\\n";
  1049. // avoid using install-sh to install install-sh
  1050. // does not work on windows....
  1051. if(*i == "install-sh")
  1052. {
  1053. fout << "\t cp ";
  1054. }
  1055. else
  1056. {
  1057. fout << "\t $(INSTALL_PROGRAM) ";
  1058. }
  1059. fout << "${srcdir}/" << *i
  1060. << " " << prefix << l->second.GetInstallPath() << "; \\\n";
  1061. fout << "\telse \\\n";
  1062. fout << "\t echo \" ERROR!!! Unable to find: " << *i
  1063. << " \"; \\\n";
  1064. fout << "\t fi\n";
  1065. }
  1066. }
  1067. break;
  1068. case cmTarget::UTILITY:
  1069. default:
  1070. break;
  1071. }
  1072. }
  1073. }
  1074. }
  1075. void cmUnixMakefileGenerator::OutputMakeRules(std::ostream& fout)
  1076. {
  1077. // only include the cmake.depends and not the Makefile, as
  1078. // building one will cause the other to be built
  1079. this->OutputMakeRule(fout,
  1080. "Default build rule",
  1081. "all",
  1082. "cmake.depends ${TARGETS} ${SUBDIR_BUILD} ${CMAKE_COMMAND}",
  1083. 0);
  1084. this->OutputMakeRule(fout,
  1085. "remove generated files",
  1086. "clean",
  1087. "${SUBDIR_CLEAN}",
  1088. "rm -f ${CLEAN_OBJECT_FILES} ${EXECUTABLES} ${TARGETS}");
  1089. this->OutputMakeRule(fout,
  1090. "Rule to build the cmake.depends and Makefile as side effect",
  1091. "cmake.depends",
  1092. "${CMAKE_COMMAND} ${CMAKE_MAKEFILE_SOURCES} ",
  1093. "${CMAKE_COMMAND} "
  1094. "-S${CMAKE_CURRENT_SOURCE} -O${CMAKE_CURRENT_BINARY} "
  1095. "-H${CMAKE_SOURCE_DIR} -B${CMAKE_BINARY_DIR}");
  1096. this->OutputMakeRule(fout,
  1097. "Rule to force the build of cmake.depends",
  1098. "depend",
  1099. "${SUBDIR_DEPEND}",
  1100. "${CMAKE_COMMAND} "
  1101. "-S${CMAKE_CURRENT_SOURCE} -O${CMAKE_CURRENT_BINARY} "
  1102. "-H${CMAKE_SOURCE_DIR} -B${CMAKE_BINARY_DIR}");
  1103. this->OutputMakeRule(fout,
  1104. "Rebuild the cache",
  1105. "rebuild_cache",
  1106. "${CMAKE_BINARY_DIR}/CMakeCache.txt",
  1107. "${CMAKE_COMMAND} "
  1108. "-H${CMAKE_SOURCE_DIR} -B${CMAKE_BINARY_DIR}");
  1109. this->OutputMakeRule(fout,
  1110. "Rebuild the cache",
  1111. "${CMAKE_BINARY_DIR}/CMakeCache.txt",
  1112. 0,
  1113. "${CMAKE_COMMAND} "
  1114. "-H${CMAKE_SOURCE_DIR} -B${CMAKE_BINARY_DIR}");
  1115. this->OutputMakeRule(fout,
  1116. "Rule to keep make from removing Makefiles "
  1117. "if control-C is hit during a run of cmake.",
  1118. ".PRECIOUS",
  1119. "Makefile cmake.depends",
  1120. 0);
  1121. this->OutputSourceObjectBuildRules(fout);
  1122. // see if there is already a target for a cmake executable in this
  1123. // makefile
  1124. bool buildingCMake = false;
  1125. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1126. for(cmTargets::const_iterator l = targets.begin();
  1127. l != targets.end(); l++)
  1128. {
  1129. if ((l->second.GetType() == cmTarget::EXECUTABLE ||
  1130. l->second.GetType() == cmTarget::WIN32_EXECUTABLE) &&
  1131. l->second.IsInAll())
  1132. {
  1133. if(l->first == "cmake")
  1134. {
  1135. buildingCMake = true;
  1136. }
  1137. }
  1138. }
  1139. // do not put this command in for the cmake project
  1140. if(!buildingCMake)
  1141. {
  1142. this->OutputMakeRule(fout,
  1143. "Rebuild cmake dummy rule",
  1144. "${CMAKE_COMMAND}",
  1145. 0,
  1146. "echo \"cmake might be out of date\"");
  1147. }
  1148. // find ctest
  1149. std::string ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  1150. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1151. ctest += "/";
  1152. ctest += "ctest";
  1153. if (cmSystemTools::FileExists(ctest.c_str()))
  1154. {
  1155. this->OutputMakeRule(fout,
  1156. "run any tests",
  1157. "test",
  1158. "",
  1159. ctest.c_str());
  1160. }
  1161. }
  1162. void cmUnixMakefileGenerator::OutputSourceObjectBuildRules(std::ostream& fout)
  1163. {
  1164. fout << "# Rules to build .o files from their sources:\n";
  1165. std::set<std::string> rules;
  1166. // Iterate over every target.
  1167. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1168. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1169. target != targets.end(); ++target)
  1170. {
  1171. bool shared = (target->second.GetType() == cmTarget::SHARED_LIBRARY);
  1172. std::string exportsDef = "";
  1173. if(shared)
  1174. {
  1175. exportsDef = "-D"+target->first+"_EXPORTS ";
  1176. }
  1177. // Iterate over every source for this target.
  1178. const std::vector<cmSourceFile>& sources = target->second.GetSourceFiles();
  1179. for(std::vector<cmSourceFile>::const_iterator source = sources.begin();
  1180. source != sources.end(); ++source)
  1181. {
  1182. if(!source->IsAHeaderFileOnly())
  1183. {
  1184. std::string shortName;
  1185. std::string sourceName;
  1186. // If the full path to the source file includes this
  1187. // directory, we want to use the relative path for the
  1188. // filename of the object file. Otherwise, we will use just
  1189. // the filename portion.
  1190. if((cmSystemTools::GetFilenamePath(source->GetFullPath()).find(m_Makefile->GetCurrentDirectory()) == 0)
  1191. || (cmSystemTools::GetFilenamePath(source->GetFullPath()).find(m_Makefile->GetCurrentOutputDirectory()) == 0))
  1192. {
  1193. sourceName = source->GetSourceName()+"."+source->GetSourceExtension();
  1194. shortName = source->GetSourceName();
  1195. // The path may be relative. See if a directory needs to be
  1196. // created for the output file. This is a ugly, and perhaps
  1197. // should be moved elsewhere.
  1198. std::string relPath =
  1199. cmSystemTools::GetFilenamePath(source->GetSourceName());
  1200. if(relPath != "")
  1201. {
  1202. std::string outPath = m_Makefile->GetCurrentOutputDirectory();
  1203. outPath += "/"+relPath;
  1204. cmSystemTools::MakeDirectory(outPath.c_str());
  1205. }
  1206. }
  1207. else
  1208. {
  1209. sourceName = source->GetFullPath();
  1210. shortName = cmSystemTools::GetFilenameName(source->GetSourceName());
  1211. }
  1212. // Only output a rule for each .o once.
  1213. if(rules.find(shortName) == rules.end())
  1214. {
  1215. rules.insert(shortName);
  1216. fout << shortName.c_str() << ".o : " << sourceName.c_str() << "\n";
  1217. std::string ext = source->GetSourceExtension();
  1218. if ( ext == "cxx" || ext == "cc" || ext == "cpp" || ext == "C" )
  1219. {
  1220. fout << "\t${CMAKE_CXX_COMPILER} ${CMAKE_CXXFLAGS} " << exportsDef.c_str()
  1221. << (shared? "${CMAKE_SHLIB_CFLAGS} ":"") << "${INCLUDE_FLAGS} -c $< -o $@\n\n";
  1222. }
  1223. else if ( ext == "c" )
  1224. {
  1225. fout << "\t${CMAKE_C_COMPILER} ${CMAKE_CFLAGS} " << exportsDef.c_str()
  1226. << (shared? "${CMAKE_SHLIB_CFLAGS} ":"") << "${INCLUDE_FLAGS} -c $< -o $@\n\n";
  1227. }
  1228. }
  1229. }
  1230. }
  1231. }
  1232. }
  1233. void cmUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  1234. const char* comment,
  1235. const char* target,
  1236. const char* depends,
  1237. const char* command)
  1238. {
  1239. if(!target)
  1240. {
  1241. cmSystemTools::Error("no target for OutputMakeRule");
  1242. return;
  1243. }
  1244. std::string replace;
  1245. if(comment)
  1246. {
  1247. replace = comment;
  1248. m_Makefile->ExpandVariablesInString(replace);
  1249. fout << "# " << comment;
  1250. }
  1251. fout << "\n";
  1252. replace = target;
  1253. m_Makefile->ExpandVariablesInString(replace);
  1254. fout << replace.c_str() << ": ";
  1255. if(depends)
  1256. {
  1257. replace = depends;
  1258. m_Makefile->ExpandVariablesInString(replace);
  1259. fout << replace.c_str();
  1260. }
  1261. fout << "\n";
  1262. if(command)
  1263. {
  1264. replace = command;
  1265. m_Makefile->ExpandVariablesInString(replace);
  1266. fout << "\t" << replace.c_str() << "\n\n";
  1267. }
  1268. fout << "\n";
  1269. }
  1270. void cmUnixMakefileGenerator::SetLocal (bool local)
  1271. {
  1272. if (local)
  1273. {
  1274. m_CacheOnly = false;
  1275. m_Recurse = false;
  1276. }
  1277. else
  1278. {
  1279. m_CacheOnly = true;
  1280. m_Recurse = true;
  1281. }
  1282. }
  1283. void cmUnixMakefileGenerator::ComputeSystemInfo()
  1284. {
  1285. if (m_CacheOnly)
  1286. {
  1287. if(m_Makefile->GetDefinition("CMAKE_CXX_COMPILER"))
  1288. {
  1289. std::string env = "CXX=${CMAKE_CXX_COMPILER}";
  1290. m_Makefile->ExpandVariablesInString(env);
  1291. putenv(const_cast<char*>(env.c_str()));
  1292. env = "CC=${CMAKE_C_COMPILER}";
  1293. m_Makefile->ExpandVariablesInString(env);
  1294. putenv(const_cast<char*>(env.c_str()));
  1295. }
  1296. // currently we run configure shell script here to determine the info
  1297. std::string output;
  1298. std::string cmd = "cd ";
  1299. cmd += m_Makefile->GetHomeOutputDirectory();
  1300. cmd += "; ";
  1301. const char* root
  1302. = m_Makefile->GetDefinition("CMAKE_ROOT");
  1303. cmd += root;
  1304. cmd += "/Templates/configure";
  1305. cmSystemTools::RunCommand(cmd.c_str(), output);
  1306. m_Makefile->AddDefinition("RUN_CONFIGURE", true);
  1307. }
  1308. // now load the settings
  1309. std::string fpath = m_Makefile->GetHomeOutputDirectory();
  1310. fpath += "/CMakeSystemConfig.cmake";
  1311. m_Makefile->ReadListFile(NULL,fpath.c_str());
  1312. }