cmLocalUnixMakefileGenerator.cxx 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmGlobalGenerator.h"
  14. #include "cmLocalUnixMakefileGenerator.h"
  15. #include "cmMakefile.h"
  16. #include "cmSystemTools.h"
  17. #include "cmSourceFile.h"
  18. #include "cmMakeDepend.h"
  19. #include "cmCacheManager.h"
  20. #include "cmGeneratedFileStream.h"
  21. #include <cmsys/RegularExpression.hxx>
  22. cmLocalUnixMakefileGenerator::cmLocalUnixMakefileGenerator()
  23. {
  24. m_WindowsShell = false;
  25. m_IncludeDirective = "include";
  26. m_MakefileVariableSize = 0;
  27. m_IgnoreLibPrefix = false;
  28. m_PassMakeflags = false;
  29. m_UseRelativePaths = false;
  30. }
  31. cmLocalUnixMakefileGenerator::~cmLocalUnixMakefileGenerator()
  32. {
  33. }
  34. void cmLocalUnixMakefileGenerator::Generate(bool fromTheTop)
  35. {
  36. m_UseRelativePaths = m_Makefile->IsOn("CMAKE_USE_RELATIVE_PATHS");
  37. // suppoirt override in output directories
  38. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  39. {
  40. m_LibraryOutputPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  41. if(m_LibraryOutputPath.size())
  42. {
  43. if(m_LibraryOutputPath[m_LibraryOutputPath.size() -1] != '/')
  44. {
  45. m_LibraryOutputPath += "/";
  46. }
  47. if(!cmSystemTools::MakeDirectory(m_LibraryOutputPath.c_str()))
  48. {
  49. cmSystemTools::Error("Error failed create "
  50. "LIBRARY_OUTPUT_PATH directory:",
  51. m_LibraryOutputPath.c_str());
  52. }
  53. m_Makefile->AddLinkDirectory(m_LibraryOutputPath.c_str());
  54. }
  55. }
  56. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  57. {
  58. m_ExecutableOutputPath =
  59. m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  60. if(m_ExecutableOutputPath.size())
  61. {
  62. if(m_ExecutableOutputPath[m_ExecutableOutputPath.size() -1] != '/')
  63. {
  64. m_ExecutableOutputPath += "/";
  65. }
  66. if(!cmSystemTools::MakeDirectory(m_ExecutableOutputPath.c_str()))
  67. {
  68. cmSystemTools::Error("Error failed to create "
  69. "EXECUTABLE_OUTPUT_PATH directory:",
  70. m_ExecutableOutputPath.c_str());
  71. }
  72. m_Makefile->AddLinkDirectory(m_ExecutableOutputPath.c_str());
  73. }
  74. }
  75. if (!fromTheTop)
  76. {
  77. // Generate depends
  78. cmMakeDepend md;
  79. md.SetMakefile(m_Makefile);
  80. md.GenerateMakefileDependencies();
  81. this->ProcessDepends(md);
  82. }
  83. // output the makefile fragment
  84. std::string dest = m_Makefile->GetStartOutputDirectory();
  85. dest += "/Makefile";
  86. this->OutputMakefile(dest.c_str(), !fromTheTop);
  87. }
  88. void
  89. cmLocalUnixMakefileGenerator::AddDependenciesToSourceFile(cmDependInformation const *info,
  90. cmSourceFile *i,
  91. std::set<cmDependInformation const*> *visited)
  92. {
  93. // add info to the visited set
  94. visited->insert(info);
  95. // add this dependency and the recurse
  96. // now recurse with info's dependencies
  97. for(cmDependInformation::DependencySet::const_iterator d =
  98. info->m_DependencySet.begin();
  99. d != info->m_DependencySet.end(); ++d)
  100. {
  101. if (visited->find(*d) == visited->end())
  102. {
  103. if((*d)->m_FullPath != "")
  104. {
  105. i->GetDepends().push_back((*d)->m_FullPath.c_str());
  106. }
  107. this->AddDependenciesToSourceFile(*d,i,visited);
  108. }
  109. }
  110. }
  111. void cmLocalUnixMakefileGenerator::ProcessDepends(const cmMakeDepend &md)
  112. {
  113. // Now create cmDependInformation objects for files in the directory
  114. cmTargets &tgts = m_Makefile->GetTargets();
  115. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  116. {
  117. std::vector<cmSourceFile*> &classes = l->second.GetSourceFiles();
  118. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  119. i != classes.end(); ++i)
  120. {
  121. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  122. {
  123. // get the depends
  124. const cmDependInformation *info =
  125. md.GetDependInformationForSourceFile(*(*i));
  126. // Delete any hints from the source file's dependencies.
  127. (*i)->GetDepends().erase((*i)->GetDepends().begin(), (*i)->GetDepends().end());
  128. // Now add the real dependencies for the file.
  129. if (info)
  130. {
  131. // create visited set
  132. std::set<cmDependInformation const*> visited;
  133. this->AddDependenciesToSourceFile(info,*i, &visited);
  134. }
  135. }
  136. }
  137. }
  138. }
  139. // This is where CMakeTargets.make is generated
  140. void cmLocalUnixMakefileGenerator::OutputMakefile(const char* file,
  141. bool withDepends)
  142. {
  143. // Create sub directories fro aux source directories
  144. std::vector<std::string>& auxSourceDirs =
  145. m_Makefile->GetAuxSourceDirectories();
  146. if( auxSourceDirs.size() )
  147. {
  148. // For the case when this is running as a remote build
  149. // on unix, make the directory
  150. for(std::vector<std::string>::iterator i = auxSourceDirs.begin();
  151. i != auxSourceDirs.end(); ++i)
  152. {
  153. if(i->c_str()[0] != '/')
  154. {
  155. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  156. if(dir.size() && dir[dir.size()-1] != '/')
  157. {
  158. dir += "/";
  159. }
  160. dir += *i;
  161. cmSystemTools::MakeDirectory(dir.c_str());
  162. }
  163. else
  164. {
  165. cmSystemTools::MakeDirectory(i->c_str());
  166. }
  167. }
  168. }
  169. // Create a stream that writes to a temporary file
  170. // then does a copy at the end. This is to allow users
  171. // to hit control-c during the make of the makefile
  172. cmGeneratedFileStream tempFile(file);
  173. tempFile.SetAlwaysCopy(true);
  174. std::ostream& fout = tempFile.GetStream();
  175. if(!fout)
  176. {
  177. cmSystemTools::Error("Error can not open for write: ", file);
  178. cmSystemTools::ReportLastSystemError("");
  179. return;
  180. }
  181. fout << "# CMAKE generated Makefile, DO NOT EDIT!\n"
  182. << "# Generated by \"" << m_GlobalGenerator->GetName() << "\""
  183. << " Generator, CMake Version "
  184. << cmMakefile::GetMajorVersion() << "."
  185. << cmMakefile::GetMinorVersion() << "\n"
  186. << "# Generated from the following files:\n# "
  187. << m_Makefile->GetHomeOutputDirectory() << "/CMakeCache.txt\n";
  188. std::vector<std::string> lfiles = m_Makefile->GetListFiles();
  189. // sort the array
  190. std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  191. // remove duplicates
  192. std::vector<std::string>::iterator new_end =
  193. std::unique(lfiles.begin(), lfiles.end());
  194. lfiles.erase(new_end, lfiles.end());
  195. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  196. i != lfiles.end(); ++i)
  197. {
  198. fout << "# " << i->c_str() << "\n";
  199. }
  200. fout << "\n\n";
  201. fout << "# disable some common implicit rules to speed things up\n";
  202. fout << ".SUFFIXES:\n";
  203. fout << ".SUFFIXES:.hpuxmakemusthaverule\n";
  204. // create a make variable with all of the sources for this Makefile
  205. // for depend purposes.
  206. fout << "CMAKE_MAKEFILE_SOURCES = ";
  207. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  208. i != lfiles.end(); ++i)
  209. {
  210. fout << " " << this->ConvertToRelativeOutputPath(i->c_str());
  211. }
  212. // Add the cache to the list
  213. std::string cacheFile = m_Makefile->GetHomeOutputDirectory();
  214. cacheFile += "/CMakeCache.txt";
  215. fout << " " << this->ConvertToRelativeOutputPath(cacheFile.c_str());
  216. fout << "\n\n\n";
  217. this->OutputMakeVariables(fout);
  218. std::string checkCache = m_Makefile->GetHomeOutputDirectory();
  219. checkCache += "/cmake.check_cache";
  220. checkCache = this->ConvertToRelativeOutputPath(checkCache.c_str());
  221. // most unix makes will pass the command line flags to make down
  222. // to sub invoked makes via an environment variable. However, some
  223. // makes do not support that, so you have to pass the flags explicitly
  224. const char* allRule = "$(MAKE) $(MAKESILENT) all";
  225. if(m_PassMakeflags)
  226. {
  227. allRule = "$(MAKE) $(MAKESILENT) -$(MAKEFLAGS) all";
  228. }
  229. // Set up the default target as the VERY first target, so that make with no arguments will run it
  230. this->
  231. OutputMakeRule(fout,
  232. "Default target executed when no arguments are given to make, first make sure cmake.depends exists, cmake.check_depends is up-to-date, check the sources, then build the all target",
  233. "default_target",
  234. checkCache.c_str(),
  235. "$(MAKE) $(MAKESILENT) cmake.depends",
  236. "$(MAKE) $(MAKESILENT) cmake.check_depends",
  237. "$(MAKE) $(MAKESILENT) -f cmake.check_depends",
  238. allRule);
  239. // Generation of SILENT target must be after default_target.
  240. if(!m_Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  241. {
  242. fout << "# Suppresses display of executed commands\n";
  243. fout << "$(VERBOSE).SILENT:\n\n";
  244. }
  245. this->OutputTargetRules(fout);
  246. this->OutputDependLibs(fout);
  247. this->OutputTargets(fout);
  248. this->OutputSubDirectoryRules(fout);
  249. std::string dependName = m_Makefile->GetStartOutputDirectory();
  250. dependName += "/cmake.depends";
  251. if(withDepends)
  252. {
  253. std::ofstream dependout(dependName.c_str());
  254. if(!dependout)
  255. {
  256. cmSystemTools::Error("Error can not open for write: ", dependName.c_str());
  257. return;
  258. }
  259. dependout << "# .o dependencies in this directory." << std::endl;
  260. std::string checkDepend = m_Makefile->GetStartOutputDirectory();
  261. checkDepend += "/cmake.check_depends";
  262. std::ofstream checkdependout(checkDepend.c_str());
  263. if(!checkdependout)
  264. {
  265. cmSystemTools::Error("Error can not open for write: ", checkDepend.c_str());
  266. return;
  267. }
  268. checkdependout << "# This file is used as a tag file, that all sources depend on. If a source changes, then the rule to rebuild this file will cause cmake.depends to be rebuilt." << std::endl;
  269. // if there were any depends output, then output the check depends
  270. // information inot checkdependout
  271. if(this->OutputObjectDepends(dependout))
  272. {
  273. this->OutputCheckDepends(checkdependout);
  274. }
  275. else
  276. {
  277. checkdependout << "all:\n\t@echo cmake.depends is up-to-date\n";
  278. }
  279. }
  280. this->OutputCustomRules(fout);
  281. this->OutputMakeRules(fout);
  282. // only add the depend include if the depend file exists
  283. if(cmSystemTools::FileExists(dependName.c_str()))
  284. {
  285. fout << m_IncludeDirective << " cmake.depends\n";
  286. }
  287. }
  288. std::string cmLocalUnixMakefileGenerator::GetBaseTargetName(const char* n,
  289. const cmTarget& t)
  290. {
  291. std::string pathPrefix = "";
  292. #ifdef __APPLE__
  293. if ( t.GetPropertyAsBool("MACOSX_BUNDLE") )
  294. {
  295. pathPrefix = n;
  296. pathPrefix += ".app/Contents/MacOS/";
  297. }
  298. #endif
  299. const char* targetPrefix = t.GetProperty("PREFIX");
  300. const char* prefixVar = t.GetPrefixVariable();
  301. // if there is no prefix on the target use the cmake definition
  302. if(!targetPrefix && prefixVar)
  303. {
  304. // first check for a language specific suffix var
  305. const char* ll = t.GetLinkerLanguage(this->GetGlobalGenerator());
  306. if(ll)
  307. {
  308. std::string langPrefix = prefixVar + std::string("_") + ll;
  309. targetPrefix = m_Makefile->GetDefinition(langPrefix.c_str());
  310. }
  311. // if there not a language specific suffix then use the general one
  312. if(!targetPrefix)
  313. {
  314. targetPrefix = m_Makefile->GetSafeDefinition(prefixVar);
  315. }
  316. }
  317. std::string name = pathPrefix + (targetPrefix?targetPrefix:"");
  318. name += n;
  319. return name;
  320. }
  321. // Output the rules for any targets
  322. void cmLocalUnixMakefileGenerator::OutputEcho(std::ostream& fout,
  323. const char *msg)
  324. {
  325. std::string echostring = msg;
  326. // for unix we want to quote the output of echo
  327. // for nmake and borland, the echo should not be quoted
  328. if(strcmp(m_GlobalGenerator->GetName(), "Unix Makefiles") == 0)
  329. {
  330. cmSystemTools::ReplaceString(echostring, "\\\n", " ");
  331. cmSystemTools::ReplaceString(echostring, " \t", " ");
  332. cmSystemTools::ReplaceString(echostring, "\n\t", "\"\n\t@echo \"");
  333. fout << "\t@echo \"" << echostring.c_str() << "\"\n";
  334. }
  335. else
  336. {
  337. cmSystemTools::ReplaceString(echostring, "\n\t", "\n\t@echo ");
  338. fout << "\t@echo " << echostring.c_str() << "\n";
  339. }
  340. }
  341. // Output the rules for any targets
  342. void cmLocalUnixMakefileGenerator::OutputTargetRules(std::ostream& fout)
  343. {
  344. const cmTargets &tgts = m_Makefile->GetTargets();
  345. // add the help target
  346. fout << "help:\n";
  347. this->OutputEcho(fout,"The following are some of the valid targets for this Makefile:");
  348. this->OutputEcho(fout,"... all (the default if no target is provided)");
  349. this->OutputEcho(fout,"... clean");
  350. this->OutputEcho(fout,"... depend");
  351. this->OutputEcho(fout,"... install");
  352. this->OutputEcho(fout,"... rebuild_cache");
  353. // libraries
  354. std::string path;
  355. for(cmTargets::const_iterator l = tgts.begin();
  356. l != tgts.end(); l++)
  357. {
  358. if((l->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  359. (l->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  360. (l->second.GetType() == cmTarget::MODULE_LIBRARY))
  361. {
  362. std::string path2 = m_LibraryOutputPath;
  363. path2 += this->GetFullTargetName(l->first.c_str(), l->second);
  364. path = "... ";
  365. path += this->ConvertToRelativeOutputPath(path2.c_str());
  366. this->OutputEcho(fout,path.c_str());
  367. }
  368. }
  369. // executables
  370. for(cmTargets::const_iterator l = tgts.begin();
  371. l != tgts.end(); l++)
  372. {
  373. if ( l->second.GetType() == cmTarget::EXECUTABLE )
  374. {
  375. path = "... ";
  376. path += l->first + cmSystemTools::GetExecutableExtension();
  377. this->OutputEcho(fout,path.c_str());
  378. }
  379. }
  380. // list utilities last
  381. for(cmTargets::const_iterator l = tgts.begin();
  382. l != tgts.end(); l++)
  383. {
  384. if (l->second.GetType() == cmTarget::UTILITY)
  385. {
  386. path = "... ";
  387. path += l->first;
  388. this->OutputEcho(fout,path.c_str());
  389. }
  390. }
  391. fout << "\n\n";
  392. // for each target add to the list of targets
  393. fout << "TARGETS = ";
  394. // list libraries first
  395. for(cmTargets::const_iterator l = tgts.begin();
  396. l != tgts.end(); l++)
  397. {
  398. if (l->second.IsInAll())
  399. {
  400. if((l->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  401. (l->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  402. (l->second.GetType() == cmTarget::MODULE_LIBRARY))
  403. {
  404. path = m_LibraryOutputPath;
  405. path += this->GetFullTargetName(l->first.c_str(), l->second);
  406. fout << " \\\n"
  407. << this->ConvertToMakeTarget(this->ConvertToRelativeOutputPath(path.c_str()).c_str());
  408. }
  409. }
  410. }
  411. // executables
  412. for(cmTargets::const_iterator l = tgts.begin();
  413. l != tgts.end(); l++)
  414. {
  415. if (l->second.GetType() == cmTarget::EXECUTABLE &&
  416. l->second.IsInAll())
  417. {
  418. path = m_ExecutableOutputPath;
  419. path += this->GetFullTargetName(l->first.c_str(), l->second);
  420. fout << " \\\n" << this->ConvertToMakeTarget(this->ConvertToRelativeOutputPath(path.c_str()).c_str());
  421. }
  422. }
  423. // list utilities last
  424. for(cmTargets::const_iterator l = tgts.begin();
  425. l != tgts.end(); l++)
  426. {
  427. if (l->second.GetType() == cmTarget::UTILITY &&
  428. l->second.IsInAll())
  429. {
  430. fout << " \\\n" << l->first.c_str();
  431. }
  432. }
  433. fout << "\n\n";
  434. // get the classes from the source lists then add them to the groups
  435. for(cmTargets::const_iterator l = tgts.begin();
  436. l != tgts.end(); l++)
  437. {
  438. std::vector<cmSourceFile*> classes = l->second.GetSourceFiles();
  439. if (classes.begin() != classes.end())
  440. {
  441. fout << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS") << " = ";
  442. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  443. i != classes.end(); i++)
  444. {
  445. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  446. !(*i)->GetCustomCommand())
  447. {
  448. std::string outExt =
  449. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  450. (*i)->GetSourceExtension().c_str());
  451. if(outExt.size() && !(*i)->GetPropertyAsBool("EXTERNAL_OBJECT") )
  452. {
  453. fout << "\\\n";
  454. std::string ofname = (*i)->GetSourceName() + outExt;
  455. ofname = this->CreateSafeUniqueObjectFileName(ofname.c_str());
  456. fout << this->ConvertToMakeTarget(this->ConvertToRelativeOutputPath(ofname.c_str()).c_str());
  457. }
  458. }
  459. }
  460. fout << "\n\n";
  461. fout << this->CreateMakeVariable(l->first.c_str(), "_EXTERNAL_OBJS") << " = ";
  462. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  463. i != classes.end(); i++)
  464. {
  465. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  466. !(*i)->GetCustomCommand())
  467. {
  468. std::string outExt =
  469. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  470. (*i)->GetSourceExtension().c_str());
  471. if(outExt.size() && (*i)->GetPropertyAsBool("EXTERNAL_OBJECT") )
  472. {
  473. fout << "\\\n";
  474. fout << this->ConvertToMakeTarget(this->ConvertToRelativeOutputPath((*i)->GetFullPath().c_str()).c_str()) << " ";
  475. }
  476. }
  477. }
  478. fout << "\n\n";
  479. fout << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS_QUOTED") << " = ";
  480. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  481. i != classes.end(); i++)
  482. {
  483. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  484. !(*i)->GetCustomCommand())
  485. {
  486. std::string outExt =
  487. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  488. (*i)->GetSourceExtension().c_str());
  489. if(outExt.size() && !(*i)->GetPropertyAsBool("EXTERNAL_OBJECT") )
  490. {
  491. std::string ofname = (*i)->GetSourceName() + outExt;
  492. ofname = this->CreateSafeUniqueObjectFileName(ofname.c_str());
  493. fout << "\\\n\"" << this->ConvertToMakeTarget(ConvertToRelativeOutputPath(ofname.c_str()).c_str()) << "\" ";
  494. }
  495. }
  496. }
  497. fout << "\n\n";
  498. fout << this->CreateMakeVariable(l->first.c_str(), "_EXTERNAL_OBJS_QUOTED") << " = ";
  499. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  500. i != classes.end(); i++)
  501. {
  502. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  503. !(*i)->GetCustomCommand())
  504. {
  505. std::string outExt =
  506. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  507. (*i)->GetSourceExtension().c_str());
  508. if(outExt.size() && (*i)->GetPropertyAsBool("EXTERNAL_OBJECT") )
  509. {
  510. fout << "\\\n\"" << this->ConvertToMakeTarget(ConvertToRelativeOutputPath((*i)->GetFullPath().c_str()).c_str()) << "\" ";
  511. }
  512. }
  513. }
  514. fout << "\n\n";
  515. }
  516. }
  517. // This list contains extra files created for a target. It includes
  518. // the extra names associated with a versioned shared library.
  519. fout << "TARGET_EXTRAS = ";
  520. for(cmTargets::const_iterator l = tgts.begin();
  521. l != tgts.end(); l++)
  522. {
  523. if (l->second.IsInAll())
  524. {
  525. if(l->second.GetType() == cmTarget::SHARED_LIBRARY ||
  526. l->second.GetType() == cmTarget::MODULE_LIBRARY)
  527. {
  528. std::string targetName;
  529. std::string targetNameSO;
  530. std::string targetNameReal;
  531. std::string targetNameBase;
  532. this->GetLibraryNames(l->first.c_str(), l->second,
  533. targetName, targetNameSO,
  534. targetNameReal, targetNameBase);
  535. if(targetNameSO != targetName)
  536. {
  537. path = m_LibraryOutputPath;
  538. path += targetNameSO;
  539. fout << " \\\n"
  540. << this->ConvertToRelativeOutputPath(path.c_str());
  541. }
  542. if(targetNameReal != targetName &&
  543. targetNameReal != targetNameSO)
  544. {
  545. path = m_LibraryOutputPath;
  546. path += targetNameReal;
  547. fout << " \\\n"
  548. << this->ConvertToRelativeOutputPath(path.c_str());
  549. }
  550. }
  551. }
  552. }
  553. fout << "\n\n";
  554. fout << "CLEAN_OBJECT_FILES = ";
  555. for(cmTargets::const_iterator l = tgts.begin();
  556. l != tgts.end(); l++)
  557. {
  558. std::vector<cmSourceFile*> classes = l->second.GetSourceFiles();
  559. if (classes.begin() != classes.end())
  560. {
  561. fout << "$(" << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS")
  562. << ") ";
  563. }
  564. }
  565. fout << "\n\n";
  566. const char * additional_clean_files =
  567. m_Makefile->GetProperty("ADDITIONAL_MAKE_CLEAN_FILES");
  568. if ( additional_clean_files && strlen(additional_clean_files) > 0 )
  569. {
  570. std::string arg = additional_clean_files;
  571. std::vector<std::string> args;
  572. cmSystemTools::ExpandListArgument(arg, args);
  573. fout << "ADDITIONAL_MAKE_CLEAN_FILES = ";
  574. for(std::vector<std::string>::iterator i = args.begin(); i != args.end(); ++i)
  575. {
  576. fout << this->ConvertToRelativeOutputPath(i->c_str()) << " ";
  577. }
  578. fout << "\n\n";
  579. }
  580. const char * qt_files = m_Makefile->GetDefinition("GENERATED_QT_FILES");
  581. if (qt_files != NULL &&
  582. strlen(m_Makefile->GetDefinition("GENERATED_QT_FILES"))>0)
  583. {
  584. fout << "GENERATED_QT_FILES = ";
  585. fout << qt_files;
  586. fout << "\n\n";
  587. }
  588. }
  589. std::string cmLocalUnixMakefileGenerator::CreatePreBuildRules(
  590. const cmTarget &target, const char* /* targetName */)
  591. {
  592. std::string customRuleCode = "";
  593. bool initNext = false;
  594. for (std::vector<cmCustomCommand>::const_iterator cr =
  595. target.GetPreBuildCommands().begin();
  596. cr != target.GetPreBuildCommands().end(); ++cr)
  597. {
  598. cmCustomCommand cc(*cr);
  599. cc.ExpandVariables(*m_Makefile);
  600. if(initNext)
  601. {
  602. customRuleCode += "\n\t";
  603. }
  604. else
  605. {
  606. initNext = true;
  607. }
  608. std::string command = this->ConvertToRelativeOutputPath(cc.GetCommand().c_str());
  609. customRuleCode += command + " " + cc.GetArguments();
  610. }
  611. return customRuleCode;
  612. }
  613. std::string cmLocalUnixMakefileGenerator::CreatePreLinkRules(
  614. const cmTarget &target, const char* /* targetName */)
  615. {
  616. std::string customRuleCode = "";
  617. bool initNext = false;
  618. for (std::vector<cmCustomCommand>::const_iterator cr =
  619. target.GetPreLinkCommands().begin();
  620. cr != target.GetPreLinkCommands().end(); ++cr)
  621. {
  622. cmCustomCommand cc(*cr);
  623. cc.ExpandVariables(*m_Makefile);
  624. if(initNext)
  625. {
  626. customRuleCode += "\n\t";
  627. }
  628. else
  629. {
  630. initNext = true;
  631. }
  632. std::string command = this->ConvertToRelativeOutputPath(cc.GetCommand().c_str());
  633. customRuleCode += command + " " + cc.GetArguments();
  634. }
  635. return customRuleCode;
  636. }
  637. std::string cmLocalUnixMakefileGenerator::CreatePostBuildRules(
  638. const cmTarget &target, const char* /* targetName */)
  639. {
  640. std::string customRuleCode = "";
  641. bool initNext = false;
  642. for (std::vector<cmCustomCommand>::const_iterator cr =
  643. target.GetPostBuildCommands().begin();
  644. cr != target.GetPostBuildCommands().end(); ++cr)
  645. {
  646. cmCustomCommand cc(*cr);
  647. cc.ExpandVariables(*m_Makefile);
  648. if(initNext)
  649. {
  650. customRuleCode += "\n\t";
  651. }
  652. else
  653. {
  654. initNext = true;
  655. }
  656. std::string command = this->ConvertToRelativeOutputPath(cc.GetCommand().c_str());
  657. customRuleCode += command + " " + cc.GetArguments();
  658. }
  659. return customRuleCode;
  660. }
  661. void cmLocalUnixMakefileGenerator::OutputLibraryRule(std::ostream& fout,
  662. const char* name,
  663. const cmTarget &t,
  664. const char* createVariable,
  665. const char* comment,
  666. const char* linkFlags
  667. )
  668. {
  669. std::string targetName;
  670. std::string targetNameSO;
  671. std::string targetNameReal;
  672. std::string targetNameBase;
  673. this->GetLibraryNames(name, t,
  674. targetName, targetNameSO,
  675. targetNameReal, targetNameBase);
  676. std::string outpath;
  677. std::string outdir;
  678. if(m_UseRelativePaths)
  679. {
  680. outdir = this->ConvertToRelativeOutputPath(m_LibraryOutputPath.c_str());
  681. }
  682. else
  683. {
  684. outdir = m_LibraryOutputPath;
  685. }
  686. if(!m_WindowsShell && m_UseRelativePaths && outdir.size())
  687. {
  688. outpath = "\"`cd ";
  689. }
  690. outpath += outdir;
  691. if(!m_WindowsShell && m_UseRelativePaths && outdir.size())
  692. {
  693. outpath += ";pwd`\"/";
  694. }
  695. if(outdir.size() == 0 && m_UseRelativePaths && !m_WindowsShell)
  696. {
  697. outpath = "\"`pwd`\"/";
  698. }
  699. // The full path versions of the names.
  700. std::string targetFullPath = outpath + targetName;
  701. std::string targetFullPathSO = outpath + targetNameSO;
  702. std::string targetFullPathReal = outpath + targetNameReal;
  703. std::string targetFullPathBase = outpath + targetNameBase;
  704. // If not using relative paths then the output path needs to be
  705. // converted here
  706. if(!m_UseRelativePaths)
  707. {
  708. targetFullPath = this->ConvertToRelativeOutputPath(targetFullPath.c_str());
  709. targetFullPathSO = this->ConvertToRelativeOutputPath(targetFullPathSO.c_str());
  710. targetFullPathReal = this->ConvertToRelativeOutputPath(targetFullPathReal.c_str());
  711. targetFullPathBase = this->ConvertToRelativeOutputPath(targetFullPathBase.c_str());
  712. }
  713. // get the objects that are used to link this library
  714. std::string objs = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS")
  715. + ") $(" + this->CreateMakeVariable(name, "_EXTERNAL_OBJS") + ") ";
  716. std::string objsQuoted = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS_QUOTED")
  717. + ") $(" + this->CreateMakeVariable(name, "_EXTERNAL_OBJS_QUOTED") + ") ";
  718. // create a variable with the objects that this library depends on
  719. std::string depend =
  720. objs + " $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  721. std::vector<std::string> commands;
  722. std::string cmakecommand = this->ConvertToOutputForExisting(
  723. m_Makefile->GetRequiredDefinition("CMAKE_COMMAND"));
  724. // Remove any existing files for this library.
  725. std::string remove = cmakecommand;
  726. remove += " -E remove -f ";
  727. remove += targetFullPathReal;
  728. if(targetFullPathSO != targetFullPathReal)
  729. {
  730. remove += " ";
  731. remove += targetFullPathSO;
  732. }
  733. if(targetFullPath != targetFullPathSO &&
  734. targetFullPath != targetFullPathReal)
  735. {
  736. remove += " ";
  737. remove += targetFullPath;
  738. }
  739. commands.push_back(remove);
  740. // collect custom commands for this target and add them to the list
  741. std::string customCommands = this->CreatePreBuildRules(t, name);
  742. if(customCommands.size() > 0)
  743. {
  744. commands.push_back(customCommands);
  745. }
  746. // collect custom commands for this target and add them to the list
  747. customCommands = this->CreatePreLinkRules(t, name);
  748. if(customCommands.size() > 0)
  749. {
  750. commands.push_back(customCommands);
  751. }
  752. // collect up the build rules
  753. std::vector<std::string> rules;
  754. rules.push_back(m_Makefile->GetRequiredDefinition(createVariable));
  755. // expand multi-command semi-colon separated lists
  756. // of commands into separate commands
  757. cmSystemTools::ExpandList(rules, commands);
  758. // Create necessary symlinks for library.
  759. if(targetFullPath != targetFullPathReal)
  760. {
  761. std::string symlink = cmakecommand;
  762. symlink += " -E cmake_symlink_library ";
  763. symlink += targetFullPathReal;
  764. symlink += " ";
  765. symlink += targetFullPathSO;
  766. symlink += " ";
  767. symlink += targetFullPath;
  768. commands.push_back(symlink);
  769. }
  770. // collect custom commands for this target and add them to the list
  771. customCommands = this->CreatePostBuildRules(t, name);
  772. if(customCommands.size() > 0)
  773. {
  774. commands.push_back(customCommands);
  775. }
  776. // collect up the link libraries
  777. cmOStringStream linklibs;
  778. this->OutputLinkLibraries(linklibs, name, t);
  779. for(std::vector<std::string>::iterator i = commands.begin();
  780. i != commands.end(); ++i)
  781. {
  782. this->ExpandRuleVariables(*i,
  783. t.GetLinkerLanguage(m_GlobalGenerator),
  784. objs.c_str(),
  785. targetFullPathReal.c_str(),
  786. linklibs.str().c_str(),
  787. 0, 0, 0, objsQuoted.c_str(),
  788. targetFullPathBase.c_str(),
  789. targetNameSO.c_str(),
  790. linkFlags);
  791. }
  792. targetFullPath = m_LibraryOutputPath + targetName;
  793. this->OutputMakeRule(fout, comment,
  794. targetFullPath.c_str(),
  795. depend.c_str(),
  796. commands);
  797. depend = targetFullPath;
  798. std::string tgt = this->ConvertToRelativeOutputPath(targetFullPath.c_str());
  799. tgt = this->ConvertToMakeTarget(tgt.c_str());
  800. cmSystemTools::ConvertToUnixSlashes(tgt);
  801. if(tgt.find('/', 0) != tgt.npos)
  802. {
  803. // we need a local target
  804. depend = this->ConvertToRelativeOutputPath(depend.c_str());
  805. std::string target = targetName;
  806. commands.resize(0);
  807. this->OutputMakeRule(fout,
  808. comment,
  809. target.c_str(),
  810. depend.c_str(),
  811. commands);
  812. }
  813. // Add a target with the canonical name (no prefix, suffix or path).
  814. this->OutputMakeRule(fout,
  815. comment,
  816. name,
  817. this->ConvertToRelativeOutputPath(tgt.c_str()).c_str(),
  818. 0);
  819. }
  820. void cmLocalUnixMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  821. const char* name,
  822. const cmTarget &t)
  823. {
  824. const char* linkLanguage = t.GetLinkerLanguage(this->GetGlobalGenerator());
  825. std::string createRule = "CMAKE_";
  826. createRule += linkLanguage;
  827. createRule += "_CREATE_SHARED_LIBRARY";
  828. std::string buildType = m_Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  829. buildType = cmSystemTools::UpperCase(buildType);
  830. std::string linkFlags = m_Makefile->GetSafeDefinition("CMAKE_SHARED_LINKER_FLAGS");
  831. linkFlags += " ";
  832. if(buildType.size())
  833. {
  834. std::string build = "CMAKE_SHARED_LINKER_FLAGS_";
  835. build += buildType;
  836. linkFlags += m_Makefile->GetSafeDefinition(build.c_str());
  837. linkFlags += " ";
  838. }
  839. if(m_Makefile->IsOn("WIN32") && !(m_Makefile->IsOn("CYGWIN") || m_Makefile->IsOn("MINGW")))
  840. {
  841. const std::vector<cmSourceFile*>& sources = t.GetSourceFiles();
  842. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  843. i != sources.end(); ++i)
  844. {
  845. if((*i)->GetSourceExtension() == "def")
  846. {
  847. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  848. linkFlags += this->ConvertToRelativeOutputPath((*i)->GetFullPath().c_str());
  849. linkFlags += " ";
  850. }
  851. }
  852. }
  853. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  854. if(targetLinkFlags)
  855. {
  856. linkFlags += targetLinkFlags;
  857. linkFlags += " ";
  858. }
  859. this->OutputLibraryRule(fout, name, t,
  860. createRule.c_str(),
  861. "shared library",
  862. linkFlags.c_str());
  863. }
  864. void cmLocalUnixMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  865. const char* name,
  866. const cmTarget &t)
  867. {
  868. const char* linkLanguage = t.GetLinkerLanguage(this->GetGlobalGenerator());
  869. std::string createRule = "CMAKE_";
  870. createRule += linkLanguage;
  871. createRule += "_CREATE_SHARED_MODULE";
  872. std::string buildType = m_Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  873. buildType = cmSystemTools::UpperCase(buildType);
  874. std::string linkFlags = m_Makefile->GetSafeDefinition("CMAKE_MODULE_LINKER_FLAGS");
  875. linkFlags += " ";
  876. if(buildType.size())
  877. {
  878. std::string build = "CMAKE_MODULE_LINKER_FLAGS_";
  879. build += buildType;
  880. linkFlags += m_Makefile->GetSafeDefinition(build.c_str());
  881. linkFlags += " ";
  882. }
  883. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  884. if(targetLinkFlags)
  885. {
  886. linkFlags += targetLinkFlags;
  887. linkFlags += " ";
  888. }
  889. this->OutputLibraryRule(fout, name, t,
  890. createRule.c_str(),
  891. "shared module",
  892. linkFlags.c_str());
  893. }
  894. void cmLocalUnixMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  895. const char* name,
  896. const cmTarget &t)
  897. {
  898. const char* linkLanguage = t.GetLinkerLanguage(this->GetGlobalGenerator());
  899. std::string createRule = "CMAKE_";
  900. createRule += linkLanguage;
  901. createRule += "_CREATE_STATIC_LIBRARY";
  902. std::string linkFlags;
  903. const char* targetLinkFlags = t.GetProperty("STATIC_LIBRARY_FLAGS");
  904. if(targetLinkFlags)
  905. {
  906. linkFlags += targetLinkFlags;
  907. linkFlags += " ";
  908. }
  909. this->OutputLibraryRule(fout, name, t,
  910. createRule.c_str(),
  911. "static library",
  912. linkFlags.c_str());
  913. }
  914. void cmLocalUnixMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  915. const char* name,
  916. const cmTarget &t)
  917. {
  918. std::string linkFlags;
  919. std::string buildType = m_Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  920. buildType = cmSystemTools::UpperCase(buildType);
  921. std::string flags;
  922. // Construct the full path to the executable that will be generated.
  923. std::string target = m_ExecutableOutputPath;
  924. if(target.length() == 0)
  925. {
  926. target = m_Makefile->GetCurrentOutputDirectory();
  927. if(target.size() && target[target.size()-1] != '/')
  928. {
  929. target += "/";
  930. }
  931. }
  932. #ifdef __APPLE__
  933. if ( t.GetPropertyAsBool("MACOSX_BUNDLE") )
  934. {
  935. // Make bundle directories
  936. target += name;
  937. target += ".app/Contents/MacOS/";
  938. }
  939. #endif
  940. target += name;
  941. target += cmSystemTools::GetExecutableExtension();
  942. target = this->ConvertToRelativeOutputPath(target.c_str());
  943. bool needsLocalTarget = false;
  944. if(m_UseRelativePaths)
  945. {
  946. cmSystemTools::ConvertToUnixSlashes(target);
  947. std::string tgt = this->ConvertToMakeTarget(target.c_str());
  948. if(tgt.find('/', 0) != tgt.npos)
  949. {
  950. needsLocalTarget = true;
  951. }
  952. target = cmSystemTools::ConvertToOutputPath(target.c_str());
  953. }
  954. else
  955. {
  956. needsLocalTarget = true;
  957. }
  958. std::string objs = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") +
  959. ") $(" + this->CreateMakeVariable(name, "_EXTERNAL_OBJS") + ") ";
  960. std::string depend = "$(";
  961. depend += this->CreateMakeVariable(name, "_SRC_OBJS")
  962. + ") $(" + this->CreateMakeVariable(name, "_EXTERNAL_OBJS")
  963. + ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  964. std::vector<std::string> rules;
  965. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  966. linkFlags += " ";
  967. if(buildType.size())
  968. {
  969. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  970. build += buildType;
  971. linkFlags += m_Makefile->GetSafeDefinition(build.c_str());
  972. linkFlags += " ";
  973. }
  974. const char* linkLanguage = t.GetLinkerLanguage(this->GetGlobalGenerator());
  975. std::string langVar = "CMAKE_";
  976. langVar += linkLanguage;
  977. std::string ruleVar = langVar + "_LINK_EXECUTABLE";
  978. std::string flagsVar = langVar + "_FLAGS";
  979. std::string sharedFlagsVar = "CMAKE_SHARED_LIBRARY_";
  980. sharedFlagsVar += langVar;
  981. sharedFlagsVar += "_FLAGS";
  982. rules.push_back(m_Makefile->GetRequiredDefinition(ruleVar.c_str()));
  983. flags += m_Makefile->GetSafeDefinition(flagsVar.c_str());
  984. flags += " ";
  985. flags += m_Makefile->GetSafeDefinition(sharedFlagsVar.c_str());
  986. flags += " ";
  987. cmOStringStream linklibs;
  988. this->OutputLinkLibraries(linklibs, 0, t);
  989. std::string comment = "executable";
  990. std::vector<std::string> commands;
  991. std::string customCommands = this->CreatePreBuildRules(t, name);
  992. if(customCommands.size() > 0)
  993. {
  994. commands.push_back(customCommands.c_str());
  995. }
  996. customCommands = this->CreatePreLinkRules(t, name);
  997. if(customCommands.size() > 0)
  998. {
  999. commands.push_back(customCommands.c_str());
  1000. }
  1001. cmSystemTools::ExpandList(rules, commands);
  1002. customCommands = this->CreatePostBuildRules(t, name);
  1003. if(customCommands.size() > 0)
  1004. {
  1005. commands.push_back(customCommands.c_str());
  1006. }
  1007. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1008. {
  1009. std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_") + linkLanguage
  1010. + std::string("_FLAGS");
  1011. linkFlags += m_Makefile->GetSafeDefinition(sFlagVar.c_str());
  1012. linkFlags += " ";
  1013. }
  1014. if ( t.GetPropertyAsBool("WIN32_EXECUTABLE") )
  1015. {
  1016. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1017. linkFlags += " ";
  1018. }
  1019. else
  1020. {
  1021. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1022. linkFlags += " ";
  1023. }
  1024. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  1025. if(targetLinkFlags)
  1026. {
  1027. linkFlags += targetLinkFlags;
  1028. linkFlags += " ";
  1029. }
  1030. for(std::vector<std::string>::iterator i = commands.begin();
  1031. i != commands.end(); ++i)
  1032. {
  1033. this->ExpandRuleVariables(*i,
  1034. linkLanguage,
  1035. objs.c_str(),
  1036. target.c_str(),
  1037. linklibs.str().c_str(),
  1038. 0,
  1039. 0,
  1040. flags.c_str(),
  1041. 0,
  1042. 0,
  1043. 0,
  1044. linkFlags.c_str());
  1045. }
  1046. this->OutputMakeRule(fout,
  1047. comment.c_str(),
  1048. target.c_str(),
  1049. depend.c_str(),
  1050. commands);
  1051. // If there is no executable output path, add a rule with the
  1052. // relative path to the executable. This is necessary for
  1053. // try-compile to work in this case.
  1054. if(needsLocalTarget)
  1055. {
  1056. depend = target;
  1057. target = name;
  1058. target += cmSystemTools::GetExecutableExtension();
  1059. target = this->ConvertToRelativeOutputPath(target.c_str());
  1060. commands.resize(0);
  1061. this->OutputMakeRule(fout,
  1062. comment.c_str(),
  1063. target.c_str(),
  1064. depend.c_str(),
  1065. commands);
  1066. }
  1067. // Add a target with the canonical name (no prefix, suffix or path).
  1068. // Note that on some platforms the "local target" added above will
  1069. // actually be the canonical name and will have set "target"
  1070. // correctly. Do not duplicate this target.
  1071. if(target != name)
  1072. {
  1073. this->OutputMakeRule(fout, comment.c_str(), name, target.c_str(), 0);
  1074. }
  1075. }
  1076. void cmLocalUnixMakefileGenerator::OutputUtilityRule(std::ostream& fout,
  1077. const char* name,
  1078. const cmTarget &t)
  1079. {
  1080. const char* cc = 0;
  1081. std::string customCommands = this->CreatePreBuildRules(t, name);
  1082. std::string customCommands2 = this->CreatePreLinkRules(t, name);
  1083. if(customCommands2.size() > 0)
  1084. {
  1085. customCommands += customCommands2;
  1086. }
  1087. customCommands2 = this->CreatePostBuildRules(t, name);
  1088. if(customCommands2.size() > 0)
  1089. {
  1090. customCommands += customCommands2;
  1091. }
  1092. if(customCommands.size() > 0)
  1093. {
  1094. cc = customCommands.c_str();
  1095. }
  1096. std::string comment = "Utility";
  1097. std::string depends;
  1098. depends = "$(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  1099. std::string replaceVars;
  1100. const std::vector<cmCustomCommand> &ccs = t.GetPostBuildCommands();
  1101. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  1102. i != ccs.end(); ++i)
  1103. {
  1104. const std::vector<std::string> & dep = i->GetDepends();
  1105. for(std::vector<std::string>::const_iterator d = dep.begin();
  1106. d != dep.end(); ++d)
  1107. {
  1108. depends += " \\\n";
  1109. replaceVars = *d;
  1110. m_Makefile->ExpandVariablesInString(replaceVars);
  1111. depends += this->ConvertToMakeTarget(this->ConvertToRelativeOutputPath(replaceVars.c_str()).c_str());
  1112. }
  1113. }
  1114. this->OutputMakeRule(fout, comment.c_str(), name,
  1115. depends.c_str(), cc);
  1116. }
  1117. void cmLocalUnixMakefileGenerator::OutputTargets(std::ostream& fout)
  1118. {
  1119. // for each target
  1120. const cmTargets &tgts = m_Makefile->GetTargets();
  1121. for(cmTargets::const_iterator l = tgts.begin();
  1122. l != tgts.end(); l++)
  1123. {
  1124. switch(l->second.GetType())
  1125. {
  1126. case cmTarget::STATIC_LIBRARY:
  1127. this->OutputStaticLibraryRule(fout, l->first.c_str(), l->second);
  1128. break;
  1129. case cmTarget::SHARED_LIBRARY:
  1130. this->OutputSharedLibraryRule(fout, l->first.c_str(), l->second);
  1131. break;
  1132. case cmTarget::MODULE_LIBRARY:
  1133. this->OutputModuleLibraryRule(fout, l->first.c_str(), l->second);
  1134. break;
  1135. case cmTarget::EXECUTABLE:
  1136. this->OutputExecutableRule(fout, l->first.c_str(), l->second);
  1137. break;
  1138. case cmTarget::UTILITY:
  1139. this->OutputUtilityRule(fout, l->first.c_str(), l->second);
  1140. break;
  1141. // This is handled by the OutputCustomRules method
  1142. case cmTarget::INSTALL_FILES:
  1143. // This is handled by the OutputInstallRules method
  1144. case cmTarget::INSTALL_PROGRAMS:
  1145. // This is handled by the OutputInstallRules method
  1146. break;
  1147. }
  1148. }
  1149. }
  1150. // For each target that is an executable or shared library, generate
  1151. // the "<name>_DEPEND_LIBS" variable listing its library dependencies.
  1152. void cmLocalUnixMakefileGenerator::OutputDependLibs(std::ostream& fout)
  1153. {
  1154. // Build a set of libraries that will be linked into any target in
  1155. // this directory.
  1156. std::set<cmStdString> used;
  1157. // for each target
  1158. const cmTargets &tgts = m_Makefile->GetTargets();
  1159. for(cmTargets::const_iterator l = tgts.begin();
  1160. l != tgts.end(); l++)
  1161. {
  1162. // Each dependency should only be emitted once per target.
  1163. std::set<cmStdString> emitted;
  1164. if ((l->second.GetType() == cmTarget::SHARED_LIBRARY)
  1165. || (l->second.GetType() == cmTarget::MODULE_LIBRARY)
  1166. || (l->second.GetType() == cmTarget::STATIC_LIBRARY)
  1167. || (l->second.GetType() == cmTarget::EXECUTABLE)
  1168. || (l->second.GetType() == cmTarget::UTILITY))
  1169. {
  1170. fout << this->CreateMakeVariable(l->first.c_str(), "_DEPEND_LIBS") << " = ";
  1171. // A library should not depend on itself!
  1172. emitted.insert(l->first);
  1173. // for static libraries do not depend on other libraries
  1174. if(l->second.GetType() != cmTarget::STATIC_LIBRARY)
  1175. {
  1176. // Now, look at all link libraries specific to this target.
  1177. const cmTarget::LinkLibraries& tlibs = l->second.GetLinkLibraries();
  1178. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  1179. lib != tlibs.end(); ++lib)
  1180. {
  1181. // Record that this library was used.
  1182. used.insert(lib->first);
  1183. // Don't emit the same library twice for this target.
  1184. if(emitted.insert(lib->first).second)
  1185. {
  1186. // Output this dependency.
  1187. this->OutputLibDepend(fout, lib->first.c_str());
  1188. }
  1189. }
  1190. }
  1191. // for all targets depend on utilities
  1192. // Now, look at all utilities specific to this target.
  1193. const std::set<cmStdString>& tutils = l->second.GetUtilities();
  1194. for(std::set<cmStdString>::const_iterator util = tutils.begin();
  1195. util != tutils.end(); ++util)
  1196. {
  1197. // Record that this utility was used.
  1198. used.insert(*util);
  1199. // Don't emit the same utility twice for this target.
  1200. if(emitted.insert(*util).second)
  1201. {
  1202. // Output this dependency.
  1203. std::string utilType = *util + "_LIBRARY_TYPE";
  1204. const char* libType = m_Makefile->GetDefinition(utilType.c_str());
  1205. if ( libType )
  1206. {
  1207. this->OutputLibDepend(fout, util->c_str());
  1208. }
  1209. else
  1210. {
  1211. this->OutputExeDepend(fout, util->c_str());
  1212. }
  1213. }
  1214. }
  1215. fout << "\n";
  1216. }
  1217. }
  1218. fout << "\n";
  1219. // Loop over the libraries used and make sure there is a rule to
  1220. // build them in this makefile. If the library is in another
  1221. // directory, add a rule to jump to that directory and make sure it
  1222. // exists.
  1223. for(std::set<cmStdString>::const_iterator lib = used.begin();
  1224. lib != used.end(); ++lib)
  1225. {
  1226. // loop over the list of directories that the libraries might
  1227. // be in, looking for an ADD_LIBRARY(lib...) line. This would
  1228. // be stored in the cache
  1229. std::string libPath = *lib + "_CMAKE_PATH";
  1230. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  1231. // if cache and not the current directory add a rule, to
  1232. // jump into the directory and build for the first time
  1233. if(cacheValue && *cacheValue &&
  1234. (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue)))
  1235. {
  1236. // add the correct extension
  1237. std::string ltname = *lib+"_LIBRARY_TYPE";
  1238. const char* libType
  1239. = m_Makefile->GetDefinition(ltname.c_str());
  1240. // if it was a library..
  1241. if (libType)
  1242. {
  1243. std::string library;
  1244. std::string libpath = cacheValue;
  1245. if(libType && std::string(libType) == "SHARED")
  1246. {
  1247. library = m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  1248. library += *lib;
  1249. library += m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1250. }
  1251. else if(libType && std::string(libType) == "MODULE")
  1252. {
  1253. library = m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  1254. library += *lib;
  1255. library += m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1256. }
  1257. else if(libType && std::string(libType) == "STATIC")
  1258. {
  1259. library = m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1260. library += *lib;
  1261. library += m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1262. }
  1263. else
  1264. {
  1265. cmSystemTools::Error("Unknown library type!");
  1266. return;
  1267. }
  1268. if(m_LibraryOutputPath.size())
  1269. {
  1270. libpath = m_LibraryOutputPath;
  1271. }
  1272. else
  1273. {
  1274. libpath += "/";
  1275. }
  1276. libpath += library;
  1277. // put out a rule to build the library if it does not exist
  1278. this->OutputBuildTargetInDir(fout,
  1279. cacheValue,
  1280. library.c_str(),
  1281. libpath.c_str());
  1282. }
  1283. // something other than a library...
  1284. else
  1285. {
  1286. std::string exepath = cacheValue;
  1287. if(m_ExecutableOutputPath.size())
  1288. {
  1289. exepath = m_ExecutableOutputPath;
  1290. }
  1291. else
  1292. {
  1293. exepath += "/";
  1294. }
  1295. std::string fullName = lib->c_str();
  1296. fullName += cmSystemTools::GetExecutableExtension();
  1297. exepath += fullName;
  1298. this->OutputBuildTargetInDir(fout,
  1299. cacheValue,
  1300. fullName.c_str(),
  1301. exepath.c_str());
  1302. }
  1303. }
  1304. }
  1305. }
  1306. void cmLocalUnixMakefileGenerator::OutputBuildTargetInDirWindows(std::ostream& fout,
  1307. const char* path,
  1308. const char* library,
  1309. const char* fullpath)
  1310. {
  1311. std::string jumpBack;
  1312. if(m_UseRelativePaths)
  1313. {
  1314. cmSystemTools::Message("using relative paths??");
  1315. jumpBack = cmSystemTools::RelativePath(cmSystemTools::GetProgramPath(path).c_str(),
  1316. m_Makefile->GetCurrentOutputDirectory());
  1317. }
  1318. else
  1319. {
  1320. jumpBack = m_Makefile->GetCurrentOutputDirectory();
  1321. }
  1322. jumpBack = this->ConvertToOutputForExisting(jumpBack.c_str());
  1323. std::string wpath = this->ConvertToOutputForExisting(path);
  1324. std::string wfullpath = this->ConvertToRelativeOutputPath(fullpath);
  1325. fout << wfullpath
  1326. << ":\n\tcd " << wpath << "\n"
  1327. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.depends\n"
  1328. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.check_depends\n"
  1329. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends\n"
  1330. << "\t$(MAKE) $(MAKESILENT) " << library
  1331. << "\n\tcd " << jumpBack << "\n";
  1332. }
  1333. void cmLocalUnixMakefileGenerator::OutputBuildTargetInDir(std::ostream& fout,
  1334. const char* path,
  1335. const char* library,
  1336. const char* fullpath)
  1337. {
  1338. if(m_WindowsShell)
  1339. {
  1340. this->OutputBuildTargetInDirWindows(fout, path, library, fullpath);
  1341. return;
  1342. }
  1343. fout << this->ConvertToOutputForExisting(fullpath)
  1344. << ":\n\tcd " << this->ConvertToOutputForExisting(path)
  1345. << "; $(MAKE) $(MAKESILENT) cmake.depends"
  1346. << "; $(MAKE) $(MAKESILENT) cmake.check_depends"
  1347. << "; $(MAKE) $(MAKESILENT) -f cmake.check_depends"
  1348. << "; $(MAKE) $(MAKESILENT) "
  1349. << library << "\n\n";
  1350. }
  1351. bool cmLocalUnixMakefileGenerator::SamePath(const char* path1, const char* path2)
  1352. {
  1353. if (strcmp(path1, path2) == 0)
  1354. {
  1355. return true;
  1356. }
  1357. #if defined(_WIN32) || defined(__APPLE__)
  1358. return
  1359. cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path1)) ==
  1360. cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path2));
  1361. #else
  1362. return false;
  1363. #endif
  1364. }
  1365. void cmLocalUnixMakefileGenerator::OutputLibDepend(std::ostream& fout,
  1366. const char* name)
  1367. {
  1368. std::string libPath = name;
  1369. libPath += "_CMAKE_PATH";
  1370. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  1371. if( cacheValue && *cacheValue )
  1372. {
  1373. // if there is a cache value, then this is a library that cmake
  1374. // knows how to build, so we can depend on it
  1375. std::string libpath;
  1376. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  1377. {
  1378. // if the library is not in the current directory, then get the full
  1379. // path to it
  1380. if(m_LibraryOutputPath.size())
  1381. {
  1382. libpath = m_LibraryOutputPath;
  1383. }
  1384. else
  1385. {
  1386. libpath = cacheValue;
  1387. libpath += "/";
  1388. }
  1389. }
  1390. else
  1391. {
  1392. // library is in current Makefile so use lib as a prefix
  1393. libpath = m_LibraryOutputPath;
  1394. }
  1395. // add the correct extension
  1396. std::string ltname = name;
  1397. ltname += "_LIBRARY_TYPE";
  1398. const char* libType = m_Makefile->GetDefinition(ltname.c_str());
  1399. if(libType && std::string(libType) == "SHARED")
  1400. {
  1401. libpath += m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  1402. libpath += name;
  1403. libpath += m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1404. }
  1405. else if (libType && std::string(libType) == "MODULE")
  1406. {
  1407. libpath += m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  1408. libpath += name;
  1409. libpath += m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1410. }
  1411. else if (libType && std::string(libType) == "STATIC")
  1412. {
  1413. libpath += m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1414. libpath += name;
  1415. libpath += m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1416. }
  1417. fout << this->ConvertToRelativeOutputPath(libpath.c_str()) << " ";
  1418. }
  1419. else
  1420. {
  1421. if(cmSystemTools::FileExists(name))
  1422. {
  1423. std::string nameStr = name;
  1424. // if it starts with / or \ or ?:/ or ?:\ then it must be a full path
  1425. if( (nameStr.size() && (nameStr[0] == '/' || nameStr[0] == '\\')) ||
  1426. ((nameStr.size() > 3) && (nameStr[1] == ':') && (nameStr[2] == '/' || nameStr[2] == '\\')))
  1427. {
  1428. fout << this->ConvertToRelativeOutputPath(name) << " ";
  1429. }
  1430. }
  1431. }
  1432. }
  1433. void cmLocalUnixMakefileGenerator::OutputExeDepend(std::ostream& fout,
  1434. const char* name)
  1435. {
  1436. std::string exePath = name;
  1437. exePath += "_CMAKE_PATH";
  1438. const char* cacheValue = m_Makefile->GetDefinition(exePath.c_str());
  1439. if( cacheValue && *cacheValue )
  1440. {
  1441. // if there is a cache value, then this is a executable/utility that cmake
  1442. // knows how to build, so we can depend on it
  1443. std::string exepath;
  1444. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  1445. {
  1446. // if the exe/utility is not in the current directory, then get the full
  1447. // path to it
  1448. if(m_ExecutableOutputPath.size())
  1449. {
  1450. exepath = m_ExecutableOutputPath;
  1451. }
  1452. else
  1453. {
  1454. exepath = cacheValue;
  1455. exepath += "/";
  1456. }
  1457. }
  1458. else
  1459. {
  1460. // library is in current Makefile
  1461. exepath = m_ExecutableOutputPath;
  1462. }
  1463. // add the library name
  1464. exepath += name;
  1465. // add the correct extension
  1466. exepath += cmSystemTools::GetExecutableExtension();
  1467. fout << this->ConvertToRelativeOutputPath(exepath.c_str()) << " ";
  1468. }
  1469. // if it isn't in the cache, it might still be a utility target
  1470. // so check for that
  1471. else
  1472. {
  1473. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1474. if (targets.find(name) != targets.end())
  1475. {
  1476. fout << name << " ";
  1477. }
  1478. }
  1479. }
  1480. // fix up names of directories so they can be used
  1481. // as targets in makefiles.
  1482. inline std::string FixDirectoryName(const char* dir)
  1483. {
  1484. std::string s = dir;
  1485. // replace ../ with 3 under bars
  1486. size_t pos = s.find("../");
  1487. if(pos != std::string::npos)
  1488. {
  1489. s.replace(pos, 3, "___");
  1490. }
  1491. // replace / directory separators with a single under bar
  1492. pos = s.find("/");
  1493. while(pos != std::string::npos)
  1494. {
  1495. s.replace(pos, 1, "_");
  1496. pos = s.find("/");
  1497. }
  1498. return s;
  1499. }
  1500. void cmLocalUnixMakefileGenerator::
  1501. BuildInSubDirectoryWindows(std::ostream& fout,
  1502. const char* directory,
  1503. const char* target1,
  1504. const char* target2,
  1505. bool silent)
  1506. {
  1507. std::string dir;
  1508. if(target1)
  1509. {
  1510. dir = this->ConvertToOutputForExisting(directory);
  1511. if(dir[0] == '\"')
  1512. {
  1513. dir = dir.substr(1, dir.size()-2);
  1514. }
  1515. fout << "\tif not exist \"" << dir << "\\$(NULL)\""
  1516. << " "
  1517. << "$(MAKE) $(MAKESILENT) rebuild_cache\n";
  1518. if (!silent)
  1519. {
  1520. fout << "\t@echo " << directory << ": building " << target1 << "\n";
  1521. }
  1522. fout << "\tcd " << dir << "\n"
  1523. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target1 << "\n";
  1524. }
  1525. if(target2)
  1526. {
  1527. if (!silent)
  1528. {
  1529. fout << "\t@echo " << directory << ": building " << target2 << "\n";
  1530. }
  1531. fout << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target2 << "\n";
  1532. }
  1533. std::string currentDir;
  1534. if(m_UseRelativePaths)
  1535. {
  1536. currentDir = dir;
  1537. cmSystemTools::ConvertToUnixSlashes(currentDir);
  1538. std::string cdback = "..";
  1539. unsigned int i = 0;
  1540. if(currentDir.size() > 2 && currentDir[0] == '.' && currentDir[1] == '/')
  1541. {
  1542. // start past ./ if it starts with ./
  1543. i = 2;
  1544. }
  1545. for(; i < currentDir.size(); ++i)
  1546. {
  1547. if(currentDir[i] == '/')
  1548. {
  1549. cdback += "/..";
  1550. }
  1551. }
  1552. fout << "\tcd " << this->ConvertToOutputForExisting(cdback.c_str()) << "\n\n";
  1553. }
  1554. else
  1555. {
  1556. currentDir = m_Makefile->GetCurrentOutputDirectory();
  1557. fout << "\tcd " << this->ConvertToOutputForExisting(currentDir.c_str()) << "\n\n";
  1558. }
  1559. }
  1560. void cmLocalUnixMakefileGenerator::BuildInSubDirectory(std::ostream& fout,
  1561. const char* dir,
  1562. const char* target1,
  1563. const char* target2,
  1564. bool silent)
  1565. {
  1566. if(m_WindowsShell)
  1567. {
  1568. this->BuildInSubDirectoryWindows(fout, dir, target1, target2, silent);
  1569. return;
  1570. }
  1571. std::string directory = this->ConvertToRelativeOutputPath(dir);
  1572. if(target1)
  1573. {
  1574. fout << "\t@if test ! -d " << directory
  1575. << "; then $(MAKE) rebuild_cache; fi\n";
  1576. if (!silent)
  1577. {
  1578. fout << "\t@echo " << directory << ": building " << target1 << "\n";
  1579. }
  1580. fout << "\t@cd " << directory
  1581. << "; $(MAKE) " << target1 << "\n";
  1582. }
  1583. if(target2)
  1584. {
  1585. if (!silent)
  1586. {
  1587. fout << "\t@echo " << directory << ": building " << target2 << "\n";
  1588. }
  1589. fout << "\t@cd " << directory
  1590. << "; $(MAKE) " << target2 << "\n";
  1591. }
  1592. fout << "\n";
  1593. }
  1594. void
  1595. cmLocalUnixMakefileGenerator::
  1596. OutputSubDirectoryVars(std::ostream& fout,
  1597. const char* var,
  1598. const char* target,
  1599. const char* target1,
  1600. const char* target2,
  1601. const char* depend,
  1602. const std::vector<std::pair<cmStdString, bool> >& SubDirectories,
  1603. bool silent, int order)
  1604. {
  1605. if(!depend)
  1606. {
  1607. depend = "";
  1608. }
  1609. if( SubDirectories.size() == 0)
  1610. {
  1611. return;
  1612. }
  1613. fout << "# Variable for making " << target << " in subdirectories.\n";
  1614. fout << var << " = ";
  1615. unsigned int ii;
  1616. // make sure all the pre-order subdirectories are fist
  1617. // other than that keep the same order that the user specified
  1618. std::vector<std::pair<cmStdString, bool> > orderedDirs;
  1619. // collect pre-order first
  1620. for(ii =0; ii < SubDirectories.size(); ii++)
  1621. {
  1622. if(m_Makefile->IsDirectoryPreOrder(SubDirectories[ii].first.c_str()))
  1623. {
  1624. orderedDirs.push_back(SubDirectories[ii]);
  1625. }
  1626. }
  1627. // now collect post order dirs
  1628. for(ii =0; ii < SubDirectories.size(); ii++)
  1629. {
  1630. if(!m_Makefile->IsDirectoryPreOrder(SubDirectories[ii].first.c_str()))
  1631. {
  1632. orderedDirs.push_back(SubDirectories[ii]);
  1633. }
  1634. }
  1635. for(ii =0; ii < orderedDirs.size(); ii++)
  1636. {
  1637. if(!orderedDirs[ii].second)
  1638. {
  1639. continue;
  1640. }
  1641. if(order == 1 && m_Makefile->IsDirectoryPreOrder(orderedDirs[ii].first.c_str()))
  1642. {
  1643. continue;
  1644. }
  1645. if(order == 2 && !m_Makefile->IsDirectoryPreOrder(orderedDirs[ii].first.c_str()))
  1646. {
  1647. continue;
  1648. }
  1649. fout << " \\\n";
  1650. std::string subdir = FixDirectoryName(orderedDirs[ii].first.c_str());
  1651. fout << target << "_" << subdir.c_str();
  1652. }
  1653. fout << " \n\n";
  1654. fout << "# Targets for making " << target << " in subdirectories.\n";
  1655. std::string last = "";
  1656. for(unsigned int cc =0; cc < orderedDirs.size(); cc++)
  1657. {
  1658. if(!orderedDirs[cc].second)
  1659. {
  1660. continue;
  1661. }
  1662. std::string subdir = FixDirectoryName(orderedDirs[cc].first.c_str());
  1663. if(order == 1 && m_Makefile->IsDirectoryPreOrder(orderedDirs[cc].first.c_str()))
  1664. {
  1665. last = subdir;
  1666. continue;
  1667. }
  1668. if(order == 2 && !m_Makefile->IsDirectoryPreOrder(orderedDirs[cc].first.c_str()))
  1669. {
  1670. last = subdir;
  1671. continue;
  1672. }
  1673. fout << target << "_" << subdir.c_str() << ": " << depend;
  1674. // Make each subdirectory depend on previous one. This forces
  1675. // parallel builds (make -j 2) to build in same order as a single
  1676. // threaded build to avoid dependency problems.
  1677. if(cc > 0)
  1678. {
  1679. fout << " " << target << "_" << last.c_str();
  1680. }
  1681. fout << "\n";
  1682. last = subdir;
  1683. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  1684. dir += "/";
  1685. dir += orderedDirs[cc].first;
  1686. this->BuildInSubDirectory(fout, dir.c_str(),
  1687. target1, target2, silent);
  1688. }
  1689. fout << "\n\n";
  1690. }
  1691. // output rules for decending into sub directories
  1692. void cmLocalUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  1693. {
  1694. // Output Sub directory build rules
  1695. const std::vector<std::pair<cmStdString, bool> >& SubDirectories
  1696. = m_Makefile->GetSubDirectories();
  1697. if( SubDirectories.size() == 0)
  1698. {
  1699. return;
  1700. }
  1701. this->OutputSubDirectoryVars(fout,
  1702. "SUBDIR_BUILD",
  1703. "default_target",
  1704. "default_target",
  1705. 0, "$(TARGETS)",
  1706. SubDirectories,
  1707. false, 1);
  1708. this->OutputSubDirectoryVars(fout,
  1709. "SUBDIR_PREORDER_BUILD",
  1710. "default_target",
  1711. "default_target",
  1712. 0, 0,
  1713. SubDirectories,
  1714. false, 2);
  1715. this->OutputSubDirectoryVars(fout, "SUBDIR_CLEAN", "clean",
  1716. "clean",
  1717. 0, 0,
  1718. SubDirectories);
  1719. this->OutputSubDirectoryVars(fout, "SUBDIR_DEPEND", "depend",
  1720. "depend",
  1721. 0, 0,
  1722. SubDirectories);
  1723. }
  1724. // Output the depend information for all the classes
  1725. // in the makefile. These would have been generated
  1726. // by the class cmMakeDepend GenerateMakefile
  1727. bool cmLocalUnixMakefileGenerator::OutputObjectDepends(std::ostream& fout)
  1728. {
  1729. bool ret = false;
  1730. // Iterate over every target.
  1731. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1732. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1733. target != targets.end(); ++target)
  1734. {
  1735. // Iterate over every source for this target.
  1736. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1737. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1738. source != sources.end(); ++source)
  1739. {
  1740. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1741. {
  1742. if(!(*source)->GetDepends().empty())
  1743. {
  1744. // Iterate through all the dependencies for this source.
  1745. for(std::vector<std::string>::const_iterator dep =
  1746. (*source)->GetDepends().begin();
  1747. dep != (*source)->GetDepends().end(); ++dep)
  1748. {
  1749. std::string s = (*source)->GetSourceName();
  1750. std::string outExt =
  1751. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  1752. (*source)->GetSourceExtension().c_str());
  1753. s += outExt;
  1754. fout << this->ConvertToRelativeOutputPath(s.c_str()) << " : "
  1755. << this->ConvertToRelativeOutputPath(dep->c_str()) << "\n";
  1756. ret = true;
  1757. }
  1758. fout << "\n\n";
  1759. }
  1760. }
  1761. }
  1762. }
  1763. return ret;
  1764. }
  1765. // Output the depend information for all the classes
  1766. // in the makefile. These would have been generated
  1767. // by the class cmMakeDepend GenerateMakefile
  1768. void cmLocalUnixMakefileGenerator::OutputCheckDepends(std::ostream& fout)
  1769. {
  1770. std::set<cmStdString> emittedLowerPath;
  1771. std::set<cmStdString> emitted;
  1772. // Iterate over every target.
  1773. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1774. fout << "# Suppresses display of executed commands\n";
  1775. fout << ".SILENT:\n";
  1776. fout << "# disable some common implicit rules to speed things up\n";
  1777. fout << ".SUFFIXES:\n";
  1778. fout << ".SUFFIXES:.hpuxmakemusthaverule\n";
  1779. this->OutputMakeVariables(fout);
  1780. fout << "default:\n";
  1781. fout << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends all\n"
  1782. << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends cmake.depends\n\n";
  1783. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1784. target != targets.end(); ++target)
  1785. {
  1786. // Iterate over every source for this target.
  1787. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1788. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1789. source != sources.end(); ++source)
  1790. {
  1791. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1792. {
  1793. if(!(*source)->GetDepends().empty())
  1794. {
  1795. for(std::vector<std::string>::const_iterator dep =
  1796. (*source)->GetDepends().begin();
  1797. dep != (*source)->GetDepends().end(); ++dep)
  1798. {
  1799. // do not call CollapseFullPath on dep here, because it already
  1800. // has been done because m_FullPath on cmDependInformation
  1801. // always is it called. If it is called here, network builds are
  1802. // very slow because of the number of stats
  1803. std::string dependfile = this->ConvertToRelativeOutputPath(dep->c_str());
  1804. // use the lower path function to create uniqe names
  1805. std::string lowerpath = this->LowerCasePath(dependfile.c_str());
  1806. if(emittedLowerPath.insert(lowerpath).second)
  1807. {
  1808. emitted.insert(dependfile);
  1809. fout << "all: " << dependfile << "\n";
  1810. }
  1811. }
  1812. }
  1813. }
  1814. }
  1815. }
  1816. fout << "\n\n# if any of these files changes run make dependlocal\n";
  1817. std::set<cmStdString>::iterator i;
  1818. for(i = emitted.begin(); i != emitted.end(); ++i)
  1819. {
  1820. fout << "cmake.depends: " << *i << "\n";
  1821. }
  1822. fout << "cmake.depends: \n"
  1823. << "\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
  1824. fout << "\n\n";
  1825. fout << "# if a .h file is removed then run make dependlocal\n\n";
  1826. for(std::set<cmStdString>::iterator it = emitted.begin();
  1827. it != emitted.end(); ++it)
  1828. {
  1829. fout << *it << ":\n"
  1830. << "\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
  1831. }
  1832. }
  1833. // Output each custom rule in the following format:
  1834. // output: source depends...
  1835. // (tab) command...
  1836. void cmLocalUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
  1837. {
  1838. // we cannot provide multiple rules for a single output
  1839. // so we will keep track of outputs to make sure we don't write
  1840. // two rules. First found wins
  1841. std::set<cmStdString> processedOutputs;
  1842. // first output all custom rules
  1843. const std::vector<cmSourceFile*>& sources = m_Makefile->GetSourceFiles();
  1844. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1845. i != sources.end(); ++i)
  1846. {
  1847. if ((*i)->GetCustomCommand())
  1848. {
  1849. cmCustomCommand *c = (*i)->GetCustomCommand();
  1850. // escape spaces and convert to native slashes path for
  1851. // the command
  1852. std::string comment = c->GetComment();
  1853. std::string command = c->GetCommand();
  1854. cmSystemTools::ReplaceString(command, "/./", "/");
  1855. command = this->ConvertToRelativeOutputPath(command.c_str());
  1856. command += " ";
  1857. // now add the arguments
  1858. command += c->GetArguments();
  1859. std::vector<std::string> depends;
  1860. // Collect out all the dependencies for this rule.
  1861. for(std::vector<std::string>::const_iterator d =
  1862. c->GetDepends().begin();
  1863. d != c->GetDepends().end(); ++d)
  1864. {
  1865. std::string dep = *d;
  1866. m_Makefile->ExpandVariablesInString(dep);
  1867. // watch for target dependencies,
  1868. std::string libPath = dep + "_CMAKE_PATH";
  1869. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  1870. if ( cacheValue && *cacheValue )
  1871. {
  1872. libPath = cacheValue;
  1873. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH") &&
  1874. m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH")[0] != '\0')
  1875. {
  1876. libPath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  1877. }
  1878. libPath += "/";
  1879. libPath += dep;
  1880. libPath += cmSystemTools::GetExecutableExtension();
  1881. dep = libPath;
  1882. }
  1883. cmSystemTools::ReplaceString(dep, "/./", "/");
  1884. cmSystemTools::ReplaceString(dep, "/$(IntDir)/", "/");
  1885. dep = this->ConvertToRelativeOutputPath(dep.c_str());
  1886. depends.push_back(dep.c_str());
  1887. }
  1888. // output rule
  1889. if (processedOutputs.find(c->GetOutput()) == processedOutputs.end())
  1890. {
  1891. this->OutputMakeRule(fout,
  1892. (comment.size()?comment.c_str():"Custom command"),
  1893. c->GetOutput().c_str(),
  1894. depends,
  1895. command.c_str());
  1896. processedOutputs.insert(c->GetOutput());
  1897. }
  1898. else
  1899. {
  1900. cmSystemTools::Error("An output was found with multiple rules on how to build it for output: ",
  1901. c->GetOutput().c_str());
  1902. }
  1903. }
  1904. }
  1905. }
  1906. void cmLocalUnixMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  1907. {
  1908. const char* variables =
  1909. "# the standard shell for make\n"
  1910. "SHELL = /bin/sh\n"
  1911. "\n";
  1912. if(!m_WindowsShell)
  1913. {
  1914. fout << variables;
  1915. }
  1916. else
  1917. {
  1918. fout <<
  1919. "!IF \"$(OS)\" == \"Windows_NT\"\n"
  1920. "NULL=\n"
  1921. "!ELSE \n"
  1922. "NULL=nul\n"
  1923. "!ENDIF \n";
  1924. }
  1925. if(m_MakeSilentFlag.size())
  1926. {
  1927. fout << "MAKESILENT = " << m_MakeSilentFlag << "\n";
  1928. }
  1929. std::string cmakecommand = this->ConvertToOutputForExisting(
  1930. m_Makefile->GetRequiredDefinition("CMAKE_COMMAND"));
  1931. fout << "CMAKE_COMMAND = "
  1932. << cmakecommand
  1933. << "\n";
  1934. fout << "RM = " << cmakecommand.c_str() << " -E remove -f\n";
  1935. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  1936. {
  1937. fout << "CMAKE_EDIT_COMMAND = "
  1938. << this->ConvertToOutputForExisting(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  1939. << "\n";
  1940. }
  1941. fout << "CMAKE_CURRENT_SOURCE = " <<
  1942. this->ConvertToRelativeOutputPath(m_Makefile->GetStartDirectory())
  1943. << "\n";
  1944. fout << "CMAKE_CURRENT_BINARY = " <<
  1945. this->ConvertToRelativeOutputPath(m_Makefile->GetStartOutputDirectory())
  1946. << "\n";
  1947. fout << "CMAKE_SOURCE_DIR = " <<
  1948. this->ConvertToRelativeOutputPath(m_Makefile->GetHomeDirectory())
  1949. << "\n";
  1950. fout << "CMAKE_BINARY_DIR = " <<
  1951. this->ConvertToRelativeOutputPath(m_Makefile->GetHomeOutputDirectory())
  1952. << "\n";
  1953. fout << "\n\n";
  1954. }
  1955. void cmLocalUnixMakefileGenerator::OutputMakeRules(std::ostream& fout)
  1956. {
  1957. this->OutputMakeRule(fout,
  1958. "default build rule",
  1959. "all",
  1960. "cmake.depends $(SUBDIR_PREORDER_BUILD) $(TARGETS) $(SUBDIR_BUILD)",
  1961. 0);
  1962. this->OutputMakeRule(fout,
  1963. "clean generated files",
  1964. "clean",
  1965. "$(SUBDIR_CLEAN)",
  1966. "-@ $(RM) $(CLEAN_OBJECT_FILES) "
  1967. " $(TARGETS) $(TARGET_EXTRAS) $(GENERATED_QT_FILES) $(GENERATED_FLTK_FILES) $(ADDITIONAL_MAKE_CLEAN_FILES)");
  1968. // collect up all the sources
  1969. std::vector<std::string> allsources;
  1970. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1971. for(std::map<cmStdString,cmTarget>::const_iterator target = targets.begin();
  1972. target != targets.end(); ++target)
  1973. {
  1974. // Iterate over every source for this target.
  1975. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1976. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1977. source != sources.end(); ++source)
  1978. {
  1979. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1980. {
  1981. allsources.push_back(this->ConvertToRelativeOutputPath((*source)->GetFullPath().c_str()));
  1982. }
  1983. }
  1984. }
  1985. std::string checkCache = m_Makefile->GetHomeOutputDirectory();
  1986. checkCache += "/cmake.check_cache";
  1987. checkCache = this->ConvertToRelativeOutputPath(checkCache.c_str());
  1988. std::vector<std::string> cmake_depends;
  1989. cmake_depends.push_back("$(CMAKE_MAKEFILE_SOURCES)");
  1990. this->OutputMakeRule(fout,
  1991. "dependencies.",
  1992. "cmake.depends",
  1993. cmake_depends,
  1994. "$(CMAKE_COMMAND) "
  1995. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1996. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1997. this->OutputMakeRule(fout,
  1998. "dependencies",
  1999. "cmake.check_depends",
  2000. allsources,
  2001. "$(CMAKE_COMMAND) "
  2002. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2003. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2004. this->OutputMakeRule(fout,
  2005. "dependencies",
  2006. "depend",
  2007. "$(SUBDIR_DEPEND)",
  2008. "$(CMAKE_COMMAND) "
  2009. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2010. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2011. this->OutputMakeRule(fout,
  2012. "dependencies",
  2013. "dependlocal",
  2014. 0,
  2015. "$(CMAKE_COMMAND) "
  2016. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2017. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2018. this->OutputMakeRule(fout,
  2019. "CMakeCache.txt",
  2020. "rebuild_cache",
  2021. 0,
  2022. "$(CMAKE_COMMAND) "
  2023. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2024. std::vector<std::string> check_cache_depends;
  2025. std::string CMakeCache = m_Makefile->GetHomeOutputDirectory();
  2026. CMakeCache += "/CMakeCache.txt";
  2027. CMakeCache = this->ConvertToRelativeOutputPath(CMakeCache.c_str());
  2028. check_cache_depends.push_back("$(CMAKE_MAKEFILE_SOURCES)");
  2029. this->OutputMakeRule(fout,
  2030. "CMakeCache.txt because out-of-date:",
  2031. checkCache.c_str(),
  2032. check_cache_depends,
  2033. "$(CMAKE_COMMAND) "
  2034. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2035. // if CMAKE_EDIT_COMMAND is defined then add a rule to run it
  2036. // called edit_cache
  2037. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  2038. {
  2039. this->OutputMakeRule(fout,
  2040. "edit CMakeCache.txt",
  2041. "edit_cache",
  2042. 0,
  2043. "$(CMAKE_EDIT_COMMAND) "
  2044. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2045. }
  2046. else
  2047. {
  2048. this->OutputMakeRule(fout,
  2049. "edit CMakeCache.txt",
  2050. "edit_cache",
  2051. 0,
  2052. "$(CMAKE_COMMAND) "
  2053. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) -i");
  2054. }
  2055. std::string cacheFile = m_Makefile->GetHomeOutputDirectory();
  2056. cacheFile += "/CMakeCache.txt";
  2057. cacheFile = this->ConvertToRelativeOutputPath(cacheFile.c_str());
  2058. this->OutputMakeRule(fout,
  2059. "CMakeCache.txt",
  2060. cacheFile.c_str(),
  2061. 0,
  2062. "$(CMAKE_COMMAND) "
  2063. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2064. this->OutputMakeRule(fout,
  2065. "Rule to keep make from removing Makefiles "
  2066. "if control-C is hit during a run of cmake.",
  2067. ".PRECIOUS",
  2068. "Makefile cmake.depends",
  2069. 0);
  2070. this->OutputSourceObjectBuildRules(fout);
  2071. // find ctest
  2072. std::string ctest = m_Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  2073. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2074. ctest += "/";
  2075. ctest += "ctest";
  2076. ctest += cmSystemTools::GetExecutableExtension();
  2077. if(!cmSystemTools::FileExists(ctest.c_str()))
  2078. {
  2079. ctest = m_Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  2080. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2081. ctest += "/Debug/";
  2082. ctest += "ctest";
  2083. ctest += cmSystemTools::GetExecutableExtension();
  2084. }
  2085. if(!cmSystemTools::FileExists(ctest.c_str()))
  2086. {
  2087. ctest = m_Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  2088. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2089. ctest += "/Release/";
  2090. ctest += "ctest";
  2091. ctest += cmSystemTools::GetExecutableExtension();
  2092. }
  2093. if (!cmSystemTools::FileExists(ctest.c_str()))
  2094. {
  2095. if ( !m_Makefile->GetDefinition("PROJECT_NAME") ||
  2096. strcmp(m_Makefile->GetDefinition("PROJECT_NAME"), "CMake") )
  2097. {
  2098. return;
  2099. }
  2100. ctest = m_Makefile->GetRequiredDefinition("EXECUTABLE_OUTPUT_PATH");
  2101. ctest += "/ctest";
  2102. }
  2103. if (m_Makefile->IsOn("CMAKE_TESTING_ENABLED"))
  2104. {
  2105. fout << "ARGS=\n";
  2106. std::string cmd = this->ConvertToOutputForExisting(ctest.c_str());
  2107. cmd += " $(ARGS)";
  2108. this->OutputMakeRule(fout,
  2109. "tests",
  2110. "test",
  2111. "",
  2112. cmd.c_str());
  2113. }
  2114. if(m_Makefile->GetDefinition("CMake_BINARY_DIR"))
  2115. {
  2116. // We are building CMake itself. We cannot use the original
  2117. // executable to install over itself.
  2118. std::string rule = m_Makefile->GetRequiredDefinition("EXECUTABLE_OUTPUT_PATH");
  2119. rule += "/cmake";
  2120. rule = cmSystemTools::ConvertToOutputPath(rule.c_str());
  2121. rule += " -P cmake_install.cmake";
  2122. this->OutputMakeRule(fout, "installation", "install", "", rule.c_str());
  2123. }
  2124. else
  2125. {
  2126. this->OutputMakeRule(fout, "installation", "install", "",
  2127. "$(CMAKE_COMMAND) -P cmake_install.cmake");
  2128. }
  2129. }
  2130. void
  2131. cmLocalUnixMakefileGenerator::
  2132. OutputBuildObjectFromSource(std::ostream& fout,
  2133. const char* shortName,
  2134. const cmSourceFile& source,
  2135. const char* extraCompileFlags,
  2136. bool shared)
  2137. {
  2138. // Header files shouldn't have build rules.
  2139. if(source.GetPropertyAsBool("HEADER_FILE_ONLY") ||
  2140. m_GlobalGenerator->IgnoreFile(source.GetSourceExtension().c_str()))
  2141. {
  2142. return;
  2143. }
  2144. std::string outputExt =
  2145. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  2146. source.GetSourceExtension().c_str());
  2147. std::string objectFile = std::string(shortName) + outputExt;
  2148. objectFile = this->CreateSafeUniqueObjectFileName(objectFile.c_str());
  2149. objectFile = this->ConvertToRelativeOutputPath(objectFile.c_str());
  2150. std::vector<std::string> rules;
  2151. std::string flags;
  2152. if(extraCompileFlags)
  2153. {
  2154. flags += extraCompileFlags;
  2155. }
  2156. std::string sourceFile =
  2157. this->ConvertToRelativeOutputPath(source.GetFullPath().c_str());
  2158. std::string buildType = m_Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  2159. buildType = cmSystemTools::UpperCase(buildType);
  2160. // find out what language the source file is
  2161. const char* lang =
  2162. m_GlobalGenerator->GetLanguageFromExtension(source.GetSourceExtension().c_str());
  2163. // for now if the lang is defined add the rules and flags for it
  2164. std::string comment = outputExt;
  2165. comment += " file";
  2166. if(lang)
  2167. {
  2168. comment += " from ";
  2169. comment += lang;
  2170. comment += ": ";
  2171. if(comment.size() < 18)
  2172. {
  2173. comment.resize(18, ' ');
  2174. }
  2175. }
  2176. if(lang)
  2177. {
  2178. std::string varString = "CMAKE_";
  2179. varString += lang;
  2180. varString += "_COMPILE_OBJECT";
  2181. rules.push_back(m_Makefile->GetRequiredDefinition(varString.c_str()));
  2182. varString = "CMAKE_";
  2183. varString += lang;
  2184. varString += "_FLAGS";
  2185. flags += m_Makefile->GetSafeDefinition(varString.c_str());
  2186. flags += " ";
  2187. if(buildType.size())
  2188. {
  2189. varString += "_";
  2190. varString += buildType;
  2191. flags += m_Makefile->GetSafeDefinition(varString.c_str());
  2192. flags += " ";
  2193. }
  2194. if(shared)
  2195. {
  2196. varString = "CMAKE_SHARED_LIBRARY_";
  2197. varString += lang;
  2198. varString += "_FLAGS";
  2199. flags += m_Makefile->GetSafeDefinition(varString.c_str());
  2200. flags += " ";
  2201. }
  2202. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  2203. {
  2204. varString = "CMAKE_SHARED_BUILD_";
  2205. varString += lang;
  2206. varString += "_FLAGS";
  2207. flags += m_Makefile->GetSafeDefinition(varString.c_str());
  2208. flags += " ";
  2209. }
  2210. }
  2211. else
  2212. {
  2213. // if the language is not defined and should not be ignored,
  2214. // then produce an error
  2215. cmSystemTools::Error("Unexpected file type ",
  2216. sourceFile.c_str());
  2217. }
  2218. flags += this->GetIncludeFlags(lang);
  2219. // expand multi-command semi-colon separated lists
  2220. // of commands into separate commands
  2221. std::vector<std::string> commands;
  2222. cmSystemTools::ExpandList(rules, commands);
  2223. for(std::vector<std::string>::iterator i = commands.begin();
  2224. i != commands.end(); ++i)
  2225. {
  2226. this->ExpandRuleVariables(*i,
  2227. lang,
  2228. 0, // no objects
  2229. 0, // no target
  2230. 0, // no link libs
  2231. sourceFile.c_str(),
  2232. objectFile.c_str(),
  2233. flags.c_str());
  2234. }
  2235. std::vector<std::string> sourceAndDeps;
  2236. sourceAndDeps.push_back(sourceFile);
  2237. // Check for extra object-file dependencies.
  2238. std::vector<std::string> depends;
  2239. const char* additionalDeps = source.GetProperty("OBJECT_DEPENDS");
  2240. if(additionalDeps)
  2241. {
  2242. cmSystemTools::ExpandListArgument(additionalDeps, depends);
  2243. for(std::vector<std::string>::iterator i = depends.begin();
  2244. i != depends.end(); ++i)
  2245. {
  2246. sourceAndDeps.push_back(this->ConvertToRelativeOutputPath(i->c_str()));
  2247. }
  2248. }
  2249. this->OutputMakeRule(fout,
  2250. comment.c_str(),
  2251. objectFile.c_str(),
  2252. sourceAndDeps,
  2253. commands);
  2254. }
  2255. void cmLocalUnixMakefileGenerator::OutputSourceObjectBuildRules(std::ostream& fout)
  2256. {
  2257. fout << "# Rules to build source files :\n\n";
  2258. std::set<cmStdString> rules;
  2259. // Iterate over every target.
  2260. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  2261. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  2262. target != targets.end(); ++target)
  2263. {
  2264. bool shared = ((target->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  2265. (target->second.GetType() == cmTarget::MODULE_LIBRARY));
  2266. std::string exportsDef = "";
  2267. if(shared)
  2268. {
  2269. std::string export_symbol;
  2270. if (const char* custom_export_name = target->second.GetProperty("DEFINE_SYMBOL"))
  2271. {
  2272. export_symbol = custom_export_name;
  2273. }
  2274. else
  2275. {
  2276. std::string in = target->first + "_EXPORTS";
  2277. export_symbol = cmSystemTools::MakeCindentifier(in.c_str());
  2278. }
  2279. exportsDef = "-D"+ export_symbol;
  2280. }
  2281. // Iterate over every source for this target.
  2282. const std::vector<cmSourceFile*>& sources =
  2283. target->second.GetSourceFiles();
  2284. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  2285. source != sources.end(); ++source)
  2286. {
  2287. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  2288. !(*source)->GetCustomCommand())
  2289. {
  2290. std::string shortName;
  2291. std::string sourceName;
  2292. // If the full path to the source file includes this
  2293. // directory, we want to use the relative path for the
  2294. // filename of the object file. Otherwise, we will use just
  2295. // the filename portion.
  2296. if((cmSystemTools::GetFilenamePath(
  2297. (*source)->GetFullPath()).find(
  2298. m_Makefile->GetCurrentDirectory()) == 0)
  2299. || (cmSystemTools::GetFilenamePath(
  2300. (*source)->GetFullPath()).find(
  2301. m_Makefile->GetCurrentOutputDirectory()) == 0))
  2302. {
  2303. sourceName = (*source)->GetSourceName()+"."+
  2304. (*source)->GetSourceExtension();
  2305. shortName = (*source)->GetSourceName();
  2306. // The path may be relative. See if a directory needs to be
  2307. // created for the output file. This is a ugly, and perhaps
  2308. // should be moved elsewhere.
  2309. std::string relPath =
  2310. cmSystemTools::GetFilenamePath((*source)->GetSourceName());
  2311. if(relPath != "")
  2312. {
  2313. std::string outPath = m_Makefile->GetCurrentOutputDirectory();
  2314. outPath += "/"+relPath;
  2315. cmSystemTools::MakeDirectory(outPath.c_str());
  2316. }
  2317. }
  2318. else
  2319. {
  2320. sourceName = (*source)->GetFullPath();
  2321. shortName = cmSystemTools::GetFilenameName((*source)->GetSourceName());
  2322. }
  2323. std::string shortNameWithExt = shortName +
  2324. (*source)->GetSourceExtension();
  2325. // Only output a rule for each .o once.
  2326. std::string compileFlags = exportsDef;
  2327. compileFlags += " ";
  2328. if(rules.find(shortNameWithExt) == rules.end())
  2329. {
  2330. if((*source)->GetProperty("COMPILE_FLAGS"))
  2331. {
  2332. compileFlags += (*source)->GetProperty("COMPILE_FLAGS");
  2333. compileFlags += " ";
  2334. }
  2335. this->OutputBuildObjectFromSource(fout,
  2336. shortName.c_str(),
  2337. *(*source),
  2338. compileFlags.c_str(),
  2339. shared);
  2340. rules.insert(shortNameWithExt);
  2341. }
  2342. }
  2343. }
  2344. }
  2345. }
  2346. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2347. const char* comment,
  2348. const char* target,
  2349. const char* depends,
  2350. const char* command,
  2351. const char* command2,
  2352. const char* command3,
  2353. const char* command4)
  2354. {
  2355. std::vector<std::string> commands;
  2356. if(command)
  2357. {
  2358. commands.push_back(command);
  2359. }
  2360. if(command2)
  2361. {
  2362. commands.push_back(command2);
  2363. }
  2364. if(command3)
  2365. {
  2366. commands.push_back(command3);
  2367. }
  2368. if(command4)
  2369. {
  2370. commands.push_back(command4);
  2371. }
  2372. this->OutputMakeRule(fout, comment, target, depends, commands);
  2373. }
  2374. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2375. const char* comment,
  2376. const char* target,
  2377. const char* depends,
  2378. const std::vector<std::string>& commands)
  2379. {
  2380. std::vector<std::string> depend;
  2381. if(depends)
  2382. {
  2383. depend.push_back(depends);
  2384. }
  2385. this->OutputMakeRule(fout, comment, target, depend, commands);
  2386. }
  2387. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2388. const char* comment,
  2389. const char* target,
  2390. const std::vector<std::string>& depends,
  2391. const char* command)
  2392. {
  2393. std::vector<std::string> commands;
  2394. if(command)
  2395. {
  2396. commands.push_back(command);
  2397. }
  2398. this->OutputMakeRule(fout, comment, target, depends, commands);
  2399. }
  2400. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2401. const char* comment,
  2402. const char* target,
  2403. const std::vector<std::string>& depends,
  2404. const std::vector<std::string>& commands)
  2405. {
  2406. if(!target)
  2407. {
  2408. cmSystemTools::Error("no target for OutputMakeRule");
  2409. return;
  2410. }
  2411. std::string replace;
  2412. if(comment)
  2413. {
  2414. replace = comment;
  2415. m_Makefile->ExpandVariablesInString(replace);
  2416. fout << "#---------------------------------------------------------\n";
  2417. fout << "# " << replace;
  2418. fout << "\n#\n";
  2419. }
  2420. fout << "\n";
  2421. replace = target;
  2422. m_Makefile->ExpandVariablesInString(replace);
  2423. std::string tgt = this->ConvertToRelativeOutputPath(replace.c_str());
  2424. tgt = this->ConvertToMakeTarget(tgt.c_str());
  2425. if(depends.empty())
  2426. {
  2427. fout << tgt.c_str() << " :\n";
  2428. }
  2429. else
  2430. {
  2431. // Split dependencies into multiple rule lines. This allows for
  2432. // very long dependency lists.
  2433. for(std::vector<std::string>::const_iterator dep = depends.begin();
  2434. dep != depends.end(); ++dep)
  2435. {
  2436. replace = *dep;
  2437. m_Makefile->ExpandVariablesInString(replace);
  2438. replace = this->ConvertToMakeTarget(replace.c_str());
  2439. fout << tgt.c_str() << " : " << replace.c_str() << "\n";
  2440. }
  2441. }
  2442. int count = 0;
  2443. for (std::vector<std::string>::const_iterator i = commands.begin();
  2444. i != commands.end(); ++i)
  2445. {
  2446. replace = *i;
  2447. m_Makefile->ExpandVariablesInString(replace);
  2448. if(count == 0 && replace.find_first_not_of(" \t\n\r") != std::string::npos &&
  2449. replace[0] != '-' && replace.find("echo") != 0
  2450. && replace.find("$(MAKE)") != 0)
  2451. {
  2452. std::string echostring = "Building ";
  2453. echostring += comment;
  2454. echostring += " ";
  2455. echostring += target;
  2456. echostring += "...";
  2457. this->OutputEcho(fout,echostring.c_str());
  2458. }
  2459. fout << "\t" << replace.c_str() << "\n";
  2460. count++;
  2461. }
  2462. fout << "\n";
  2463. }
  2464. std::string cmLocalUnixMakefileGenerator::LowerCasePath(const char* path)
  2465. {
  2466. #ifdef _WIN32
  2467. return cmSystemTools::LowerCase(path);
  2468. #else
  2469. return std::string(path);
  2470. #endif
  2471. }
  2472. std::string&
  2473. cmLocalUnixMakefileGenerator::CreateSafeUniqueObjectFileName(const char* sin)
  2474. {
  2475. if ( m_Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES") )
  2476. {
  2477. std::map<cmStdString,cmStdString>::iterator it = m_UniqueObjectNamesMap.find(sin);
  2478. if ( it == m_UniqueObjectNamesMap.end() )
  2479. {
  2480. std::string ssin = sin;
  2481. bool done;
  2482. int cc = 0;
  2483. char rpstr[100];
  2484. sprintf(rpstr, "_p_");
  2485. cmSystemTools::ReplaceString(ssin, "+", rpstr);
  2486. std::string sssin = sin;
  2487. do
  2488. {
  2489. done = true;
  2490. for ( it = m_UniqueObjectNamesMap.begin();
  2491. it != m_UniqueObjectNamesMap.end();
  2492. ++ it )
  2493. {
  2494. if ( it->second == ssin )
  2495. {
  2496. done = false;
  2497. }
  2498. }
  2499. if ( done )
  2500. {
  2501. break;
  2502. }
  2503. sssin = ssin;
  2504. cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
  2505. sprintf(rpstr, "_p%d_", cc++);
  2506. }
  2507. while ( !done );
  2508. m_UniqueObjectNamesMap[sin] = ssin;
  2509. }
  2510. }
  2511. else
  2512. {
  2513. m_UniqueObjectNamesMap[sin] = sin;
  2514. }
  2515. return m_UniqueObjectNamesMap[sin];
  2516. }
  2517. std::string
  2518. cmLocalUnixMakefileGenerator::CreateMakeVariable(const char* sin, const char* s2in)
  2519. {
  2520. std::string s = sin;
  2521. std::string s2 = s2in;
  2522. std::string unmodified = s;
  2523. unmodified += s2;
  2524. // if there is no restriction on the length of make variables
  2525. // and there are no "." charactors in the string, then return the
  2526. // unmodified combination.
  2527. if(!m_MakefileVariableSize && unmodified.find('.') == s.npos)
  2528. {
  2529. return unmodified;
  2530. }
  2531. // see if the variable has been defined before and return
  2532. // the modified version of the variable
  2533. std::map<cmStdString, cmStdString>::iterator i = m_MakeVariableMap.find(unmodified);
  2534. if(i != m_MakeVariableMap.end())
  2535. {
  2536. return i->second;
  2537. }
  2538. // start with the unmodified variable
  2539. std::string ret = unmodified;
  2540. // if this there is no value for m_MakefileVariableSize then
  2541. // the string must have bad characters in it
  2542. if(!m_MakefileVariableSize)
  2543. {
  2544. cmSystemTools::ReplaceString(ret, ".", "_");
  2545. int ni = 0;
  2546. char buffer[5];
  2547. // make sure the _ version is not already used, if
  2548. // it is used then add number to the end of the variable
  2549. while(m_ShortMakeVariableMap.count(ret) && ni < 1000)
  2550. {
  2551. ++ni;
  2552. sprintf(buffer, "%04d", ni);
  2553. ret = unmodified + buffer;
  2554. }
  2555. m_ShortMakeVariableMap[ret] = "1";
  2556. m_MakeVariableMap[unmodified] = ret;
  2557. return ret;
  2558. }
  2559. // if the string is greater the 32 chars it is an invalid vairable name
  2560. // for borland make
  2561. if(static_cast<int>(ret.size()) > m_MakefileVariableSize)
  2562. {
  2563. int keep = m_MakefileVariableSize - 8;
  2564. int size = keep + 3;
  2565. std::string str1 = s;
  2566. std::string str2 = s2;
  2567. // we must shorten the combined string by 4 charactors
  2568. // keep no more than 24 charactors from the second string
  2569. if(static_cast<int>(str2.size()) > keep)
  2570. {
  2571. str2 = str2.substr(0, keep);
  2572. }
  2573. if(static_cast<int>(str1.size()) + static_cast<int>(str2.size()) > size)
  2574. {
  2575. str1 = str1.substr(0, size - str2.size());
  2576. }
  2577. char buffer[5];
  2578. int ni = 0;
  2579. sprintf(buffer, "%04d", ni);
  2580. ret = str1 + str2 + buffer;
  2581. while(m_ShortMakeVariableMap.count(ret) && ni < 1000)
  2582. {
  2583. ++ni;
  2584. sprintf(buffer, "%04d", ni);
  2585. ret = str1 + str2 + buffer;
  2586. }
  2587. if(ni == 1000)
  2588. {
  2589. cmSystemTools::Error("Borland makefile variable length too long");
  2590. return unmodified;
  2591. }
  2592. // once an unused variable is found
  2593. m_ShortMakeVariableMap[ret] = "1";
  2594. }
  2595. // always make an entry into the unmodified to variable map
  2596. m_MakeVariableMap[unmodified] = ret;
  2597. return ret;
  2598. }
  2599. void cmLocalUnixMakefileGenerator::GetLibraryNames(const char* n,
  2600. const cmTarget& t,
  2601. std::string& name,
  2602. std::string& soName,
  2603. std::string& realName,
  2604. std::string& baseName)
  2605. {
  2606. // Check for library version properties.
  2607. const char* version = t.GetProperty("VERSION");
  2608. const char* soversion = t.GetProperty("SOVERSION");
  2609. if((t.GetType() != cmTarget::SHARED_LIBRARY &&
  2610. t.GetType() != cmTarget::MODULE_LIBRARY) ||
  2611. !m_Makefile->GetDefinition("CMAKE_SHARED_LIBRARY_SONAME_C_FLAG"))
  2612. {
  2613. // Versioning is supported only for shared libraries and modules,
  2614. // and then only when the platform supports an soname flag.
  2615. version = 0;
  2616. soversion = 0;
  2617. }
  2618. if(version && !soversion)
  2619. {
  2620. // The soversion must be set if the library version is set. Use
  2621. // the library version as the soversion.
  2622. soversion = version;
  2623. }
  2624. // The library name.
  2625. name = this->GetFullTargetName(n, t);
  2626. // The library's soname.
  2627. soName = name;
  2628. if(soversion)
  2629. {
  2630. soName += ".";
  2631. soName += soversion;
  2632. }
  2633. // The library's real name on disk.
  2634. realName = name;
  2635. if(version)
  2636. {
  2637. realName += ".";
  2638. realName += version;
  2639. }
  2640. else if(soversion)
  2641. {
  2642. realName += ".";
  2643. realName += soversion;
  2644. }
  2645. // The library name without extension.
  2646. baseName = this->GetBaseTargetName(n, t);
  2647. }
  2648. std::string cmLocalUnixMakefileGenerator::ConvertToMakeTarget(const char* tgt)
  2649. {
  2650. // Make targets should not have a leading './' for a file in the
  2651. // directory containing the makefile.
  2652. std::string ret = tgt;
  2653. if(ret.size() > 2 &&
  2654. (ret[0] == '.') &&
  2655. ( (ret[1] == '/') || ret[1] == '\\'))
  2656. {
  2657. std::string upath = ret;
  2658. cmSystemTools::ConvertToUnixSlashes(upath);
  2659. if(upath.find(2, '/') == upath.npos)
  2660. {
  2661. ret = ret.substr(2, ret.size()-2);
  2662. }
  2663. }
  2664. return ret;
  2665. }