cmUnixMakefileGenerator.cxx 48 KB

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