cmUnixMakefileGenerator.cxx 41 KB

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