cmUnixMakefileGenerator.cxx 46 KB

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