cmUnixMakefileGenerator.cxx 53 KB

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