cmUnixMakefileGenerator.cxx 60 KB

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