cmUnixMakefileGenerator.cxx 53 KB

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