cmLocalUnixMakefileGenerator3.cxx 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877
  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 "cmLocalUnixMakefileGenerator3.h"
  14. #include "cmDepends.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmGlobalGenerator.h"
  17. #include "cmMakefile.h"
  18. #include "cmSourceFile.h"
  19. #include "cmake.h"
  20. // Include dependency scanners for supported languages. Only the
  21. // C/C++ scanner is needed for bootstrapping CMake.
  22. #include "cmDependsC.h"
  23. #ifdef CMAKE_BUILD_WITH_CMAKE
  24. # include "cmDependsFortran.h"
  25. # include "cmDependsJava.h"
  26. #endif
  27. #include <memory> // auto_ptr
  28. #include <queue>
  29. // TODO: Add "help" target.
  30. // TODO: Identify remaining relative path violations.
  31. // TODO: Need test for separate executable/library output path.
  32. //----------------------------------------------------------------------------
  33. cmLocalUnixMakefileGenerator3::cmLocalUnixMakefileGenerator3()
  34. {
  35. m_WindowsShell = false;
  36. m_IncludeDirective = "include";
  37. m_MakefileVariableSize = 0;
  38. m_IgnoreLibPrefix = false;
  39. m_PassMakeflags = false;
  40. m_EchoNeedsQuote = true;
  41. }
  42. //----------------------------------------------------------------------------
  43. cmLocalUnixMakefileGenerator3::~cmLocalUnixMakefileGenerator3()
  44. {
  45. }
  46. //----------------------------------------------------------------------------
  47. void cmLocalUnixMakefileGenerator3::Generate()
  48. {
  49. // Setup our configuration variables for this directory.
  50. this->ConfigureOutputPaths();
  51. // Generate the rule files for each target.
  52. const cmTargets& targets = m_Makefile->GetTargets();
  53. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  54. {
  55. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  56. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  57. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  58. (t->second.GetType() == cmTarget::MODULE_LIBRARY))
  59. {
  60. this->WriteTargetRuleFiles(t->second);
  61. }
  62. else if(t->second.GetType() == cmTarget::UTILITY)
  63. {
  64. this->WriteUtilityRuleFiles(t->second);
  65. }
  66. }
  67. this->WriteCustomCommands();
  68. // Write the cmake file with information for this directory.
  69. this->WriteDirectoryInformationFile();
  70. }
  71. //----------------------------------------------------------------------------
  72. void cmLocalUnixMakefileGenerator3::ConfigureOutputPaths()
  73. {
  74. // Format the library and executable output paths.
  75. if(const char* libOut = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  76. {
  77. m_LibraryOutputPath = libOut;
  78. this->FormatOutputPath(m_LibraryOutputPath, "LIBRARY");
  79. }
  80. if(const char* exeOut = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  81. {
  82. m_ExecutableOutputPath = exeOut;
  83. this->FormatOutputPath(m_ExecutableOutputPath, "EXECUTABLE");
  84. }
  85. }
  86. //----------------------------------------------------------------------------
  87. void cmLocalUnixMakefileGenerator3::FormatOutputPath(std::string& path,
  88. const char* name)
  89. {
  90. if(!path.empty())
  91. {
  92. // Convert the output path to a full path in case it is
  93. // specified as a relative path. Treat a relative path as
  94. // relative to the current output directory for this makefile.
  95. path =
  96. cmSystemTools::CollapseFullPath(path.c_str(),
  97. m_Makefile->GetStartOutputDirectory());
  98. // Add a trailing slash for easy appending later.
  99. if(path.empty() || path[path.size()-1] != '/')
  100. {
  101. path += "/";
  102. }
  103. // Make sure the output path exists on disk.
  104. if(!cmSystemTools::MakeDirectory(path.c_str()))
  105. {
  106. cmSystemTools::Error("Error failed to create ",
  107. name, "_OUTPUT_PATH directory:", path.c_str());
  108. }
  109. // Add this as a link directory automatically.
  110. m_Makefile->AddLinkDirectory(path.c_str());
  111. }
  112. }
  113. void cmLocalUnixMakefileGenerator3::WriteCustomCommands()
  114. {
  115. // Generate the rule files for each custom command.
  116. const std::vector<cmSourceFile*>& sources = m_Makefile->GetSourceFiles();
  117. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  118. i != sources.end(); ++i)
  119. {
  120. if(const cmCustomCommand* cc = (*i)->GetCustomCommand())
  121. {
  122. this->GenerateCustomRuleFile(*cc);
  123. }
  124. }
  125. // generate the includes
  126. std::string ruleFileName = "CMakeCustomRules.dir/build.make";
  127. // Open the rule file. This should be copy-if-different because the
  128. // rules may depend on this file itself.
  129. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  130. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  131. ruleFileStream.SetCopyIfDifferent(true);
  132. if(!ruleFileStream)
  133. {
  134. return;
  135. }
  136. this->WriteDisclaimer(ruleFileStream);
  137. std::string relPath = this->GetHomeRelativeOutputPath();
  138. std::string objTarget;
  139. for(std::set<cmStdString>::const_iterator i = m_CustomRuleFiles.begin();
  140. i != m_CustomRuleFiles.end(); ++i)
  141. {
  142. objTarget = relPath;
  143. objTarget += *i;
  144. ruleFileStream
  145. << m_IncludeDirective << " "
  146. << this->ConvertToOutputForExisting(objTarget.c_str()).c_str()
  147. << "\n";
  148. }
  149. // now do the clean
  150. ruleFileName = "CMakeCustomRules.dir/clean.make";
  151. // Open the rule file. This should be copy-if-different because the
  152. // rules may depend on this file itself.
  153. ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  154. cmGeneratedFileStream ruleFileStream2(ruleFileNameFull.c_str());
  155. ruleFileStream2.SetCopyIfDifferent(true);
  156. if(!ruleFileStream2)
  157. {
  158. return;
  159. }
  160. this->WriteDisclaimer(ruleFileStream2);
  161. std::vector<std::string> cleanFiles;
  162. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  163. i != sources.end(); ++i)
  164. {
  165. if(const cmCustomCommand* cc = (*i)->GetCustomCommand())
  166. {
  167. cleanFiles.push_back(cc->GetOutput());
  168. }
  169. }
  170. if (cleanFiles.size())
  171. {
  172. std::vector<std::string> commands;
  173. std::vector<std::string> depends;
  174. this->AppendCleanCommand(commands, cleanFiles);
  175. std::string dir = m_Makefile->GetStartOutputDirectory();
  176. dir += "/CMakeCustomRules.dir/clean";
  177. dir = cmSystemTools::RelativePath(m_Makefile->GetHomeOutputDirectory(), dir.c_str());
  178. dir = cmSystemTools::ConvertToOutputPath(dir.c_str());
  179. this->WriteMakeRule(ruleFileStream2,
  180. "Clean the output of this custom command.",
  181. dir.c_str(), depends, commands);
  182. // do the include
  183. commands.clear();
  184. depends.push_back(dir);
  185. this->WriteMakeRule(ruleFileStream2,
  186. "Clean the output of this custom command.",
  187. "clean", depends, commands);
  188. }
  189. }
  190. //----------------------------------------------------------------------------
  191. void cmLocalUnixMakefileGenerator3::WriteDirectoryInformationFile()
  192. {
  193. std::string infoFileName = m_Makefile->GetStartOutputDirectory();
  194. infoFileName += "/CMakeDirectoryInformation.cmake";
  195. // Open the output file.
  196. cmGeneratedFileStream infoFileStream(infoFileName.c_str());
  197. if(!infoFileStream)
  198. {
  199. return;
  200. }
  201. // Write the do not edit header.
  202. this->WriteDisclaimer(infoFileStream);
  203. // Tell the dependency scanner to use unix paths if necessary.
  204. if(cmSystemTools::GetForceUnixPaths())
  205. {
  206. infoFileStream
  207. << "# Force unix paths in dependencies.\n"
  208. << "SET(CMAKE_FORCE_UNIX_PATHS 1)\n"
  209. << "\n";
  210. }
  211. // Store the include search path for this directory.
  212. infoFileStream
  213. << "# The C and CXX include file search paths:\n";
  214. infoFileStream
  215. << "SET(CMAKE_C_INCLUDE_PATH\n";
  216. std::vector<std::string> includeDirs;
  217. this->GetIncludeDirectories(includeDirs);
  218. for(std::vector<std::string>::iterator i = includeDirs.begin();
  219. i != includeDirs.end(); ++i)
  220. {
  221. infoFileStream
  222. << " \"" << this->ConvertToRelativePath(i->c_str()).c_str() << "\"\n";
  223. }
  224. infoFileStream
  225. << " )\n";
  226. infoFileStream
  227. << "SET(CMAKE_CXX_INCLUDE_PATH ${CMAKE_C_INCLUDE_PATH})\n";
  228. // Store the include regular expressions for this directory.
  229. infoFileStream
  230. << "\n"
  231. << "# The C and CXX include file regular expressions for this directory.\n";
  232. infoFileStream
  233. << "SET(CMAKE_C_INCLUDE_REGEX_SCAN ";
  234. this->WriteCMakeArgument(infoFileStream,
  235. m_Makefile->GetIncludeRegularExpression());
  236. infoFileStream
  237. << ")\n";
  238. infoFileStream
  239. << "SET(CMAKE_C_INCLUDE_REGEX_COMPLAIN ";
  240. this->WriteCMakeArgument(infoFileStream,
  241. m_Makefile->GetComplainRegularExpression());
  242. infoFileStream
  243. << ")\n";
  244. infoFileStream
  245. << "SET(CMAKE_CXX_INCLUDE_REGEX_SCAN ${CMAKE_C_INCLUDE_REGEX_SCAN})\n";
  246. infoFileStream
  247. << "SET(CMAKE_CXX_INCLUDE_REGEX_COMPLAIN ${CMAKE_C_INCLUDE_REGEX_COMPLAIN})\n";
  248. }
  249. //----------------------------------------------------------------------------
  250. std::string
  251. cmLocalUnixMakefileGenerator3
  252. ::ConvertToFullPath(const std::string& localPath)
  253. {
  254. std::string dir = m_Makefile->GetStartOutputDirectory();
  255. dir += "/";
  256. dir += localPath;
  257. return dir;
  258. }
  259. //----------------------------------------------------------------------------
  260. void cmLocalUnixMakefileGenerator3::WriteDisclaimer(std::ostream& os)
  261. {
  262. os
  263. << "# CMAKE generated file: DO NOT EDIT!\n"
  264. << "# Generated by \"" << m_GlobalGenerator->GetName() << "\""
  265. << " Generator, CMake Version "
  266. << cmMakefile::GetMajorVersion() << "."
  267. << cmMakefile::GetMinorVersion() << "\n\n";
  268. }
  269. std::string cmLocalUnixMakefileGenerator3::GetHomeRelativeOutputPath()
  270. {
  271. // Include the rule file for each object.
  272. std::string relPath =
  273. cmSystemTools::RelativePath(m_Makefile->GetHomeOutputDirectory(),
  274. m_Makefile->GetStartOutputDirectory());
  275. if (relPath.size())
  276. {
  277. relPath += "/";
  278. }
  279. return relPath;
  280. }
  281. //----------------------------------------------------------------------------
  282. void
  283. cmLocalUnixMakefileGenerator3
  284. ::WriteTargetRuleFiles(const cmTarget& target)
  285. {
  286. // Create a directory for this target.
  287. std::string dir = this->GetTargetDirectory(target);
  288. cmSystemTools::MakeDirectory(this->ConvertToFullPath(dir).c_str());
  289. // First generate the object rule files. Save a list of all object
  290. // files for this target.
  291. std::vector<std::string> objects;
  292. std::vector<std::string> external_objects;
  293. std::vector<std::string> provides_requires;
  294. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  295. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  296. source != sources.end(); ++source)
  297. {
  298. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  299. !(*source)->GetCustomCommand())
  300. {
  301. if(!m_GlobalGenerator->IgnoreFile((*source)->GetSourceExtension().c_str()))
  302. {
  303. // Generate this object file's rule file.
  304. this->WriteObjectRuleFiles(target, *(*source), objects,provides_requires);
  305. }
  306. else if((*source)->GetPropertyAsBool("EXTERNAL_OBJECT"))
  307. {
  308. // This is an external object file. Just add it.
  309. external_objects.push_back((*source)->GetFullPath());
  310. }
  311. }
  312. }
  313. // Generate the build-time dependencies file for this target.
  314. std::string depBase = dir;
  315. depBase += "/";
  316. depBase += target.GetName();
  317. // Construct the rule file name.
  318. std::string ruleFileName = dir;
  319. ruleFileName += "/build.make";
  320. // Open the rule file. This should be copy-if-different because the
  321. // rules may depend on this file itself.
  322. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  323. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  324. ruleFileStream.SetCopyIfDifferent(true);
  325. if(!ruleFileStream)
  326. {
  327. return;
  328. }
  329. this->WriteDisclaimer(ruleFileStream);
  330. // Include the rule file for each object.
  331. std::string relPath = this->GetHomeRelativeOutputPath();
  332. std::string objTarget;
  333. if(!objects.empty())
  334. {
  335. ruleFileStream
  336. << "# Include make rules for object files.\n";
  337. for(std::vector<std::string>::const_iterator obj = objects.begin();
  338. obj != objects.end(); ++obj)
  339. {
  340. objTarget = relPath;
  341. objTarget += *obj;
  342. objTarget += ".build.make";
  343. ruleFileStream
  344. << m_IncludeDirective << " "
  345. << this->ConvertToOutputForExisting(objTarget.c_str()).c_str()
  346. << "\n";
  347. }
  348. ruleFileStream
  349. << "\n";
  350. }
  351. // Write the rule for this target type.
  352. switch(target.GetType())
  353. {
  354. case cmTarget::STATIC_LIBRARY:
  355. this->WriteStaticLibraryRule(ruleFileStream, ruleFileName.c_str(),
  356. target, objects, external_objects,
  357. provides_requires);
  358. break;
  359. case cmTarget::SHARED_LIBRARY:
  360. this->WriteSharedLibraryRule(ruleFileStream, ruleFileName.c_str(),
  361. target, objects, external_objects,
  362. provides_requires);
  363. break;
  364. case cmTarget::MODULE_LIBRARY:
  365. this->WriteModuleLibraryRule(ruleFileStream, ruleFileName.c_str(),
  366. target, objects, external_objects,
  367. provides_requires);
  368. break;
  369. case cmTarget::EXECUTABLE:
  370. this->WriteExecutableRule(ruleFileStream, ruleFileName.c_str(),
  371. target, objects, external_objects,
  372. provides_requires);
  373. break;
  374. default:
  375. break;
  376. }
  377. }
  378. //----------------------------------------------------------------------------
  379. void
  380. cmLocalUnixMakefileGenerator3
  381. ::WriteObjectDependFile(std::string &obj,
  382. const char * lang,
  383. const cmSourceFile& source,
  384. std::vector<std::string>& depends,
  385. std::string& depMakeFile)
  386. {
  387. // TODO: what the heck is this?
  388. // Generate the build-time dependencies file for this object file.
  389. std::string depMarkFile;
  390. if(!this->GenerateDependsMakeFile(lang, obj.c_str(),
  391. depMakeFile, depMarkFile))
  392. {
  393. cmSystemTools::Error("No dependency checker available for language \"",
  394. lang, "\".");
  395. return;
  396. }
  397. // Open the rule file for writing. This should be copy-if-different
  398. // because the rules may depend on this file itself.
  399. std::string ruleFileName = obj;
  400. ruleFileName += ".depend.make";
  401. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  402. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  403. ruleFileStream.SetCopyIfDifferent(true);
  404. if(!ruleFileStream)
  405. {
  406. return;
  407. }
  408. this->WriteDisclaimer(ruleFileStream);
  409. ruleFileStream
  410. << "# depend stage file for object file " << obj.c_str() << ".\n\n";
  411. // Create the list of dependencies known at cmake time. These are
  412. // shared between the object file and dependency scanning rule.
  413. depends.push_back(source.GetFullPath());
  414. if(const char* objectDeps = source.GetProperty("OBJECT_DEPENDS"))
  415. {
  416. std::vector<std::string> deps;
  417. cmSystemTools::ExpandListArgument(objectDeps, deps);
  418. for(std::vector<std::string>::iterator i = deps.begin();
  419. i != deps.end(); ++i)
  420. {
  421. depends.push_back(i->c_str());
  422. }
  423. }
  424. this->AppendRuleDepend(depends, ruleFileNameFull.c_str());
  425. // Write the dependency generation rule.
  426. std::string relativeObj = this->GetHomeRelativeOutputPath();
  427. relativeObj += obj;
  428. std::vector<std::string> commands;
  429. std::string depEcho = "Scanning ";
  430. depEcho += lang;
  431. depEcho += " dependencies of ";
  432. depEcho += this->ConvertToRelativeOutputPath(relativeObj.c_str());
  433. this->AppendEcho(commands, depEcho.c_str());
  434. // Add a command to call CMake to scan dependencies. CMake will
  435. // touch the corresponding depends file after scanning dependencies.
  436. cmOStringStream depCmd;
  437. // TODO: Account for source file properties and directory-level
  438. // definitions when scanning for dependencies.
  439. depCmd << "$(CMAKE_COMMAND) -E cmake_depends " << lang << " "
  440. << relativeObj.c_str() << " "
  441. << m_GlobalGenerator->ConvertToHomeRelativeOutputPath
  442. (source.GetFullPath().c_str());
  443. commands.push_back(depCmd.str());
  444. // compute the target
  445. std::string relPath = this->GetHomeRelativeOutputPath();
  446. relPath += depMarkFile;
  447. // Write the rule.
  448. this->WriteMakeRule(ruleFileStream, 0,
  449. relPath.c_str(), depends, commands);
  450. }
  451. //----------------------------------------------------------------------------
  452. void
  453. cmLocalUnixMakefileGenerator3
  454. ::WriteObjectBuildFile(std::string &obj,
  455. const char *lang,
  456. const cmTarget& target,
  457. const cmSourceFile& source,
  458. std::vector<std::string>& depends,
  459. std::string &depMakeFile,
  460. std::vector<std::string>& provides_requires)
  461. {
  462. // Open the rule file for writing. This should be copy-if-different
  463. // because the rules may depend on this file itself.
  464. std::string ruleFileName = obj;
  465. ruleFileName += ".build.make";
  466. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  467. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  468. ruleFileStream.SetCopyIfDifferent(true);
  469. if(!ruleFileStream)
  470. {
  471. return;
  472. }
  473. this->WriteDisclaimer(ruleFileStream);
  474. ruleFileStream
  475. << "# Rule file for object file " << obj.c_str() << ".\n\n";
  476. // Include the dependencies for the target.
  477. std::string depPath = this->GetHomeRelativeOutputPath();
  478. depPath += depMakeFile;
  479. depMakeFile = m_GlobalGenerator->ConvertToHomeRelativeOutputPath(depPath.c_str());
  480. ruleFileStream
  481. << "# Include any dependencies generated for this rule.\n"
  482. << m_IncludeDirective << " "
  483. << depMakeFile
  484. << "\n\n";
  485. // Write the build rule.
  486. {
  487. // Build the set of compiler flags.
  488. std::string flags;
  489. // Add the export symbol definition for shared library objects.
  490. bool shared = ((target.GetType() == cmTarget::SHARED_LIBRARY) ||
  491. (target.GetType() == cmTarget::MODULE_LIBRARY));
  492. if(shared)
  493. {
  494. flags += "-D";
  495. if(const char* custom_export_name = target.GetProperty("DEFINE_SYMBOL"))
  496. {
  497. flags += custom_export_name;
  498. }
  499. else
  500. {
  501. std::string in = target.GetName();
  502. in += "_EXPORTS";
  503. flags += cmSystemTools::MakeCindentifier(in.c_str());
  504. }
  505. }
  506. // Add flags from source file properties.
  507. this->AppendFlags(flags, source.GetProperty("COMPILE_FLAGS"));
  508. // Add language-specific flags.
  509. this->AddLanguageFlags(flags, lang);
  510. // Add shared-library flags if needed.
  511. this->AddSharedFlags(flags, lang, shared);
  512. // Add include directory flags.
  513. this->AppendFlags(flags, this->GetIncludeFlags(lang));
  514. // Get the output paths for source and object files.
  515. std::string sourceFile =
  516. this->ConvertToOptionallyRelativeOutputPath(source.GetFullPath().c_str());
  517. std::string objectFile =
  518. this->ConvertToRelativeOutputPath(obj.c_str());
  519. // Construct the build message.
  520. std::string relativeObj = this->GetHomeRelativeOutputPath();
  521. relativeObj += obj;
  522. std::vector<std::string> commands;
  523. std::string buildEcho = "Building ";
  524. buildEcho += lang;
  525. buildEcho += " object ";
  526. buildEcho += relativeObj;
  527. this->AppendEcho(commands, buildEcho.c_str());
  528. // Construct the compile rules.
  529. std::string compileRuleVar = "CMAKE_";
  530. compileRuleVar += lang;
  531. compileRuleVar += "_COMPILE_OBJECT";
  532. std::string compileRule =
  533. m_Makefile->GetRequiredDefinition(compileRuleVar.c_str());
  534. cmSystemTools::ExpandListArgument(compileRule, commands);
  535. // Expand placeholders in the commands.
  536. for(std::vector<std::string>::iterator i = commands.begin();
  537. i != commands.end(); ++i)
  538. {
  539. this->ExpandRuleVariables(*i,
  540. lang,
  541. 0, // no objects
  542. 0, // no target
  543. 0, // no link libs
  544. sourceFile.c_str(),
  545. relativeObj.c_str(),
  546. flags.c_str());
  547. }
  548. // Write the rule.
  549. this->WriteMakeRule(ruleFileStream, 0,
  550. relativeObj.c_str(), depends, commands);
  551. }
  552. // If the language needs provides-requires mode, create the
  553. // corresponding targets.
  554. if(strcmp(lang, "Fortran") == 0)
  555. {
  556. std::string objectRequires = obj;
  557. std::string objectProvides = obj;
  558. objectRequires += ".requires";
  559. objectProvides += ".provides";
  560. {
  561. // Add the provides target to build the object file.
  562. std::vector<std::string> no_commands;
  563. std::vector<std::string> p_depends;
  564. p_depends.push_back(obj);
  565. this->WriteMakeRule(ruleFileStream, 0,
  566. objectProvides.c_str(), p_depends, no_commands);
  567. }
  568. // Add this to the set of provides-requires objects on the target.
  569. provides_requires.push_back(objectRequires);
  570. }
  571. }
  572. //----------------------------------------------------------------------------
  573. void
  574. cmLocalUnixMakefileGenerator3
  575. ::WriteObjectRuleFiles(const cmTarget& target, const cmSourceFile& source,
  576. std::vector<std::string>& objects,
  577. std::vector<std::string>& provides_requires)
  578. {
  579. // Identify the language of the source file.
  580. const char* lang = this->GetSourceFileLanguage(source);
  581. if(!lang)
  582. {
  583. // If language is not known, this is an error.
  584. cmSystemTools::Error("Source file \"", source.GetFullPath().c_str(),
  585. "\" has unknown type.");
  586. return;
  587. }
  588. // Get the full path name of the object file.
  589. std::string obj = this->GetObjectFileName(target, source);
  590. // Avoid generating duplicate rules.
  591. if(m_ObjectFiles.find(obj) == m_ObjectFiles.end())
  592. {
  593. m_ObjectFiles.insert(obj);
  594. }
  595. else
  596. {
  597. cmOStringStream err;
  598. err << "Warning: Source file \""
  599. << source.GetSourceName().c_str() << "."
  600. << source.GetSourceExtension().c_str()
  601. << "\" is listed multiple times for target \"" << target.GetName()
  602. << "\".";
  603. cmSystemTools::Message(err.str().c_str(), "Warning");
  604. return;
  605. }
  606. // Create the directory containing the object file. This may be a
  607. // subdirectory under the target's directory.
  608. std::string dir = cmSystemTools::GetFilenamePath(obj.c_str());
  609. cmSystemTools::MakeDirectory(this->ConvertToFullPath(dir).c_str());
  610. // Save this in the target's list of object files.
  611. objects.push_back(obj);
  612. std::string relativeObj = this->GetHomeRelativeOutputPath();
  613. relativeObj += obj;
  614. // we compute some depends when writing the depend.make that we will also
  615. // use in the build.make, same with depMakeFile
  616. std::vector<std::string> depends;
  617. std::string depMakeFile;
  618. // generate the depends rule file
  619. this->WriteObjectDependFile(obj, lang, source, depends, depMakeFile);
  620. // generate the build rule file
  621. this->WriteObjectBuildFile(obj, lang, target, source, depends, depMakeFile,
  622. provides_requires);
  623. // The object file should be checked for dependency integrity.
  624. m_CheckDependFiles[lang].insert(relativeObj);
  625. // If the language needs provides-requires mode, create the
  626. // corresponding targets.
  627. /*
  628. if(strcmp(lang, "Fortran") == 0)
  629. {
  630. std::string objectRequires = obj;
  631. std::string objectProvides = obj;
  632. objectRequires += ".requires";
  633. objectProvides += ".provides";
  634. // Add the provides target to build the object file.
  635. std::vector<std::string> no_commands;
  636. std::vector<std::string> p_depends;
  637. p_depends.push_back(obj);
  638. this->WriteMakeRule(ruleFileStream, 0,
  639. objectProvides.c_str(), p_depends, no_commands);
  640. // Add this to the set of provides-requires objects on the target.
  641. provides_requires.push_back(objectRequires);
  642. }
  643. */
  644. }
  645. //----------------------------------------------------------------------------
  646. void
  647. cmLocalUnixMakefileGenerator3
  648. ::GenerateCustomRuleFile(const cmCustomCommand& cc)
  649. {
  650. // Create a directory for custom rule files.
  651. std::string dir = "CMakeCustomRules.dir";
  652. cmSystemTools::MakeDirectory(this->ConvertToFullPath(dir).c_str());
  653. // Convert the output name to a relative path if possible.
  654. std::string output = this->ConvertToRelativePath(cc.GetOutput());
  655. // Construct the name of the rule file by transforming the output
  656. // name to a valid file name. Since the output is already a file
  657. // everything but the path characters is valid.
  658. std::string customName = output;
  659. cmSystemTools::ReplaceString(customName, "../", "___");
  660. cmSystemTools::ReplaceString(customName, "/", "_");
  661. cmSystemTools::ReplaceString(customName, ":", "_");
  662. std::string ruleFileName = dir;
  663. ruleFileName += "/";
  664. ruleFileName += customName;
  665. ruleFileName += ".build.make";
  666. // If this is a duplicate rule produce an error.
  667. if(m_CustomRuleFiles.find(ruleFileName) != m_CustomRuleFiles.end())
  668. {
  669. cmSystemTools::Error("An output was found with multiple rules on how to build it for output: ",
  670. cc.GetOutput());
  671. return;
  672. }
  673. m_CustomRuleFiles.insert(ruleFileName);
  674. // what is the relative path to the rule file
  675. std::string relRuleFile = this->GetHomeRelativeOutputPath();
  676. relRuleFile += ruleFileName;
  677. // Open the rule file. This should be copy-if-different because the
  678. // rules may depend on this file itself.
  679. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  680. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  681. ruleFileStream.SetCopyIfDifferent(true);
  682. if(!ruleFileStream)
  683. {
  684. return;
  685. }
  686. this->WriteDisclaimer(ruleFileStream);
  687. ruleFileStream
  688. << "# Custom command rule file for " << output.c_str() << ".\n\n";
  689. // Collect the commands.
  690. std::vector<std::string> commands;
  691. std::string preEcho = "Generating ";
  692. preEcho += output;
  693. this->AppendEcho(commands, preEcho.c_str());
  694. this->AppendCustomCommand(commands, cc);
  695. // Collect the dependencies.
  696. std::vector<std::string> depends;
  697. this->AppendCustomDepend(depends, cc);
  698. // Add a dependency on the rule file itself.
  699. this->AppendRuleDepend(depends, relRuleFile.c_str());
  700. // Write the rule.
  701. const char* comment = 0;
  702. if(cc.GetComment() && *cc.GetComment())
  703. {
  704. comment = cc.GetComment();
  705. }
  706. this->WriteMakeRule(ruleFileStream, comment,
  707. cc.GetOutput(), depends, commands);
  708. }
  709. //----------------------------------------------------------------------------
  710. void
  711. cmLocalUnixMakefileGenerator3
  712. ::WriteUtilityRuleFiles(const cmTarget& target)
  713. {
  714. // Create a directory for this target.
  715. std::string dir = this->GetTargetDirectory(target);
  716. cmSystemTools::MakeDirectory(this->ConvertToFullPath(dir).c_str());
  717. // Construct the name of the rule file.
  718. std::string ruleFileName = dir;
  719. ruleFileName += "/build.make";
  720. // Open the rule file. This should be copy-if-different because the
  721. // rules may depend on this file itself.
  722. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  723. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  724. ruleFileStream.SetCopyIfDifferent(true);
  725. if(!ruleFileStream)
  726. {
  727. return;
  728. }
  729. this->WriteDisclaimer(ruleFileStream);
  730. ruleFileStream
  731. << "# Utility rule file for " << target.GetName() << ".\n\n";
  732. // Collect the commands and dependencies.
  733. std::vector<std::string> commands;
  734. std::vector<std::string> depends;
  735. // Utility targets store their rules in pre- and post-build commands.
  736. this->AppendCustomDepends(depends, target.GetPreBuildCommands());
  737. this->AppendCustomDepends(depends, target.GetPostBuildCommands());
  738. this->AppendCustomCommands(commands, target.GetPreBuildCommands());
  739. this->AppendCustomCommands(commands, target.GetPostBuildCommands());
  740. // Add dependencies on targets that must be built first.
  741. this->AppendTargetDepends(depends, target);
  742. // Add a dependency on the rule file itself.
  743. std::string relPath = this->GetHomeRelativeOutputPath();
  744. std::string objTarget = relPath;
  745. objTarget += ruleFileName;
  746. this->AppendRuleDepend(depends, objTarget.c_str());
  747. // Write the rule.
  748. this->WriteMakeRule(ruleFileStream, 0,
  749. target.GetName(), depends, commands);
  750. // Write convenience targets.
  751. dir = m_Makefile->GetStartOutputDirectory();
  752. dir += "/";
  753. dir += this->GetTargetDirectory(target);
  754. std::string buildTargetRuleName = dir;
  755. buildTargetRuleName += "/build";
  756. buildTargetRuleName =
  757. m_GlobalGenerator->ConvertToHomeRelativeOutputPath(buildTargetRuleName.c_str());
  758. this->WriteConvenienceRule(ruleFileStream, target.GetName(),
  759. buildTargetRuleName.c_str());
  760. }
  761. //----------------------------------------------------------------------------
  762. bool
  763. cmLocalUnixMakefileGenerator3
  764. ::GenerateDependsMakeFile(const std::string& lang, const char* objFile,
  765. std::string& depMakeFile, std::string& depMarkFile)
  766. {
  767. // Construct a checker for the given language.
  768. std::auto_ptr<cmDepends>
  769. checker(this->GetDependsChecker(lang,
  770. m_Makefile->GetStartOutputDirectory(),
  771. objFile, false));
  772. if(checker.get())
  773. {
  774. // Save the make and mark file names.
  775. depMakeFile = checker->GetMakeFileName();
  776. depMarkFile = checker->GetMarkFileName();
  777. // Todo is this required???
  778. // Check the dependencies.
  779. checker->Check();
  780. return true;
  781. }
  782. return false;
  783. }
  784. //----------------------------------------------------------------------------
  785. void
  786. cmLocalUnixMakefileGenerator3
  787. ::WriteMakeRule(std::ostream& os,
  788. const char* comment,
  789. const char* target,
  790. const std::vector<std::string>& depends,
  791. const std::vector<std::string>& commands)
  792. {
  793. // Make sure there is a target.
  794. if(!target || !*target)
  795. {
  796. cmSystemTools::Error("No target for WriteMakeRule!");
  797. return;
  798. }
  799. std::string replace;
  800. // Write the comment describing the rule in the makefile.
  801. if(comment)
  802. {
  803. replace = comment;
  804. std::string::size_type lpos = 0;
  805. std::string::size_type rpos;
  806. while((rpos = replace.find('\n', lpos)) != std::string::npos)
  807. {
  808. os << "# " << replace.substr(lpos, rpos-lpos) << "\n";
  809. lpos = rpos+1;
  810. }
  811. os << "# " << replace.substr(lpos) << "\n";
  812. }
  813. // Construct the left hand side of the rule.
  814. replace = target;
  815. std::string tgt = m_GlobalGenerator->ConvertToHomeRelativeOutputPath(replace.c_str());
  816. tgt = this->ConvertToMakeTarget(tgt.c_str());
  817. const char* space = "";
  818. if(tgt.size() == 1)
  819. {
  820. // Add a space before the ":" to avoid drive letter confusion on
  821. // Windows.
  822. space = " ";
  823. }
  824. // Write the rule.
  825. if(depends.empty())
  826. {
  827. // No dependencies. The commands will always run.
  828. os << tgt.c_str() << space << ":\n";
  829. }
  830. else
  831. {
  832. // Split dependencies into multiple rule lines. This allows for
  833. // very long dependency lists even on older make implementations.
  834. for(std::vector<std::string>::const_iterator dep = depends.begin();
  835. dep != depends.end(); ++dep)
  836. {
  837. replace = *dep;
  838. replace = m_GlobalGenerator->ConvertToHomeRelativeOutputPath(replace.c_str());
  839. replace = this->ConvertToMakeTarget(replace.c_str());
  840. os << tgt.c_str() << space << ": " << replace.c_str() << "\n";
  841. }
  842. }
  843. // Write the list of commands.
  844. for(std::vector<std::string>::const_iterator i = commands.begin();
  845. i != commands.end(); ++i)
  846. {
  847. replace = *i;
  848. os << "\t" << replace.c_str() << "\n";
  849. }
  850. os << "\n";
  851. }
  852. //----------------------------------------------------------------------------
  853. void cmLocalUnixMakefileGenerator3::WriteDivider(std::ostream& os)
  854. {
  855. os
  856. << "#======================================"
  857. << "=======================================\n";
  858. }
  859. //----------------------------------------------------------------------------
  860. void
  861. cmLocalUnixMakefileGenerator3
  862. ::WriteMakeVariables(std::ostream& makefileStream)
  863. {
  864. this->WriteDivider(makefileStream);
  865. makefileStream
  866. << "# Set environment variables for the build.\n"
  867. << "\n";
  868. if(m_WindowsShell)
  869. {
  870. makefileStream
  871. << "!IF \"$(OS)\" == \"Windows_NT\"\n"
  872. << "NULL=\n"
  873. << "!ELSE\n"
  874. << "NULL=nul\n"
  875. << "!ENDIF\n";
  876. }
  877. else
  878. {
  879. makefileStream
  880. << "# The shell in which to execute make rules.\n"
  881. << "SHELL = /bin/sh\n"
  882. << "\n";
  883. }
  884. if(m_Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  885. {
  886. makefileStream
  887. << "# Produce verbose output by default.\n"
  888. << "VERBOSE = 1\n"
  889. << "\n";
  890. }
  891. std::string cmakecommand =
  892. this->ConvertToOutputForExisting(
  893. m_Makefile->GetRequiredDefinition("CMAKE_COMMAND"));
  894. makefileStream
  895. << "# The CMake executable.\n"
  896. << "CMAKE_COMMAND = "
  897. << m_GlobalGenerator->ConvertToHomeRelativeOutputPath
  898. (cmakecommand.c_str()).c_str() << "\n"
  899. << "\n";
  900. makefileStream
  901. << "# The command to remove a file.\n"
  902. << "RM = "
  903. << this->ConvertToRelativeOutputPath(cmakecommand.c_str()).c_str()
  904. << " -E remove -f\n"
  905. << "\n";
  906. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  907. {
  908. makefileStream
  909. << "# The program to use to edit the cache.\n"
  910. << "CMAKE_EDIT_COMMAND = "
  911. << (this->ConvertToOutputForExisting(
  912. m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))) << "\n"
  913. << "\n";
  914. }
  915. makefileStream
  916. << "# The top-level source directory on which CMake was run.\n"
  917. << "CMAKE_SOURCE_DIR = "
  918. << this->ConvertToRelativeOutputPath(m_Makefile->GetHomeDirectory())
  919. << "\n"
  920. << "\n";
  921. makefileStream
  922. << "# The top-level build directory on which CMake was run.\n"
  923. << "CMAKE_BINARY_DIR = "
  924. << this->ConvertToRelativeOutputPath(m_Makefile->GetHomeOutputDirectory())
  925. << "\n"
  926. << "\n";
  927. }
  928. void cmLocalUnixMakefileGenerator3::WriteTargetIncludes(std::ostream& makefileStream,
  929. const char *file,
  930. const char *rule)
  931. {
  932. std::vector<std::string> depends;
  933. std::vector<std::string> no_commands;
  934. // if this is the build rules also include the custom commands if there
  935. // were any
  936. if (!strcmp(rule,"build") && m_CustomRuleFiles.size())
  937. {
  938. // do the include
  939. std::string dir = m_Makefile->GetStartOutputDirectory();
  940. dir += "/CMakeCustomRules.dir/build.make";
  941. dir = cmSystemTools::RelativePath(m_Makefile->GetHomeOutputDirectory(), dir.c_str());
  942. dir = cmSystemTools::ConvertToOutputPath(dir.c_str());
  943. makefileStream
  944. << m_IncludeDirective << " "
  945. << this->ConvertToOutputForExisting(dir.c_str()).c_str()
  946. << "\n";
  947. }
  948. // if this is the clean rules also include the custom commands if there
  949. // were any
  950. const char* clean_no_custom = m_Makefile->GetProperty("CLEAN_NO_CUSTOM");
  951. if (!strcmp(rule,"clean") && cmSystemTools::IsOff(clean_no_custom))
  952. {
  953. // do the include
  954. std::string dir = m_Makefile->GetStartOutputDirectory();
  955. dir += "/CMakeCustomRules.dir/clean.make";
  956. dir = cmSystemTools::RelativePath(m_Makefile->GetHomeOutputDirectory(), dir.c_str());
  957. dir = cmSystemTools::ConvertToOutputPath(dir.c_str());
  958. makefileStream
  959. << m_IncludeDirective << " "
  960. << this->ConvertToOutputForExisting(dir.c_str()).c_str()
  961. << "\n";
  962. }
  963. for (cmTargets::const_iterator l = m_Makefile->GetTargets().begin();
  964. l != m_Makefile->GetTargets().end(); l++)
  965. {
  966. if((l->second.GetType() == cmTarget::EXECUTABLE) ||
  967. (l->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  968. (l->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  969. (l->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  970. (l->second.GetType() == cmTarget::UTILITY && !strcmp(rule,"build")))
  971. {
  972. // do the include
  973. std::string tname = this->GetRelativeTargetDirectory(l->second);
  974. tname += "/";
  975. tname += file;
  976. makefileStream
  977. << m_IncludeDirective << " "
  978. << this->ConvertToOutputForExisting(tname.c_str()).c_str()
  979. << "\n";
  980. // Add this to the list of depends rules in this directory.
  981. if(l->second.IsInAll())
  982. {
  983. // add the dependency
  984. tname = this->GetRelativeTargetDirectory(l->second);
  985. tname += "/";
  986. tname += rule;
  987. depends.clear();
  988. depends.push_back(tname);
  989. this->WriteMakeRule(makefileStream, 0,
  990. rule, depends, no_commands);
  991. }
  992. }
  993. }
  994. }
  995. //----------------------------------------------------------------------------
  996. void
  997. cmLocalUnixMakefileGenerator3
  998. ::WriteLocalCleanRule(std::ostream& makefileStream)
  999. {
  1000. // Collect a list of extra files to clean in this directory.
  1001. std::vector<std::string> files;
  1002. // Look for files registered for cleaning in this directory.
  1003. if(const char* additional_clean_files =
  1004. m_Makefile->GetProperty("ADDITIONAL_MAKE_CLEAN_FILES"))
  1005. {
  1006. cmSystemTools::ExpandListArgument(additional_clean_files, files);
  1007. }
  1008. // Write the local clean rule for this directory.
  1009. if(!files.empty())
  1010. {
  1011. // Have extra files to clean. Write the action to remove them.
  1012. std::string cleanTarget = this->GetHomeRelativeOutputPath();
  1013. cleanTarget += "clean.local";
  1014. std::vector<std::string> no_depends;
  1015. std::vector<std::string> commands;
  1016. this->AppendCleanCommand(commands, files);
  1017. this->WriteMakeRule(makefileStream,
  1018. "Clean extra files in this directory.",
  1019. cleanTarget.c_str(), no_depends, commands);
  1020. commands.clear();
  1021. no_depends.push_back(cleanTarget);
  1022. this->WriteMakeRule(makefileStream, 0,
  1023. "clean", no_depends, commands);
  1024. }
  1025. }
  1026. //----------------------------------------------------------------------------
  1027. void
  1028. cmLocalUnixMakefileGenerator3
  1029. ::WriteSpecialTargetsTop(std::ostream& makefileStream)
  1030. {
  1031. this->WriteDivider(makefileStream);
  1032. makefileStream
  1033. << "# Special targets provided by cmake.\n"
  1034. << "\n";
  1035. // Write the main entry point target. This must be the VERY first
  1036. // target so that make with no arguments will run it.
  1037. {
  1038. // Just depend on the all target to drive the build.
  1039. std::vector<std::string> depends;
  1040. std::vector<std::string> no_commands;
  1041. depends.push_back("all");
  1042. // Write the rule.
  1043. this->WriteMakeRule(makefileStream,
  1044. "Default target executed when no arguments are "
  1045. "given to make.",
  1046. "default_target",
  1047. depends,
  1048. no_commands);
  1049. }
  1050. // Write special "test" target to run ctest.
  1051. if(m_Makefile->IsOn("CMAKE_TESTING_ENABLED"))
  1052. {
  1053. std::string ctest;
  1054. if(m_Makefile->GetDefinition("CMake_BINARY_DIR"))
  1055. {
  1056. // We are building CMake itself. Use the ctest that comes with
  1057. // this version of CMake instead of the one used to build it.
  1058. ctest = m_ExecutableOutputPath;
  1059. ctest += "ctest";
  1060. ctest += cmSystemTools::GetExecutableExtension();
  1061. ctest = this->ConvertToRelativeOutputPath(ctest.c_str());
  1062. }
  1063. else
  1064. {
  1065. // We are building another project. Use the ctest that comes with
  1066. // the CMake building it.
  1067. ctest = m_Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  1068. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1069. ctest += "/";
  1070. ctest += "ctest";
  1071. ctest += cmSystemTools::GetExecutableExtension();
  1072. ctest = this->ConvertToOutputForExisting(ctest.c_str());
  1073. }
  1074. std::vector<std::string> no_depends;
  1075. std::vector<std::string> commands;
  1076. this->AppendEcho(commands, "Running tests...");
  1077. std::string cmd = ctest;
  1078. cmd += " $(ARGS)";
  1079. commands.push_back(cmd);
  1080. this->WriteMakeRule(makefileStream,
  1081. "Special rule to drive testing with ctest.",
  1082. "test", no_depends, commands);
  1083. }
  1084. // Write special "install" target to run cmake_install.cmake script.
  1085. {
  1086. std::vector<std::string> depends;
  1087. std::vector<std::string> commands;
  1088. std::string cmd;
  1089. if(m_Makefile->GetDefinition("CMake_BINARY_DIR"))
  1090. {
  1091. // We are building CMake itself. We cannot use the original
  1092. // executable to install over itself.
  1093. cmd = m_ExecutableOutputPath;
  1094. cmd += "cmake";
  1095. cmd = this->ConvertToRelativeOutputPath(cmd.c_str());
  1096. }
  1097. else
  1098. {
  1099. cmd = "$(CMAKE_COMMAND)";
  1100. }
  1101. cmd += " -P cmake_install.cmake";
  1102. commands.push_back(cmd);
  1103. const char* noall =
  1104. m_Makefile->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
  1105. if(!noall || cmSystemTools::IsOff(noall))
  1106. {
  1107. // Drive the build before installing.
  1108. depends.push_back("all");
  1109. }
  1110. this->WriteMakeRule(makefileStream,
  1111. "Special rule to run installation script.",
  1112. "install", depends, commands);
  1113. }
  1114. // Write special "rebuild_cache" target to re-run cmake.
  1115. {
  1116. std::vector<std::string> no_depends;
  1117. std::vector<std::string> commands;
  1118. this->AppendEcho(commands, "Running CMake to regenerate build system...");
  1119. commands.push_back(
  1120. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1121. this->WriteMakeRule(makefileStream,
  1122. "Special rule to re-run CMake using make.",
  1123. "rebuild_cache",
  1124. no_depends,
  1125. commands);
  1126. }
  1127. // Use CMAKE_EDIT_COMMAND for the edit_cache rule if it is defined.
  1128. // Otherwise default to the interactive command-line interface.
  1129. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  1130. {
  1131. std::vector<std::string> no_depends;
  1132. std::vector<std::string> commands;
  1133. this->AppendEcho(commands, "Running CMake cache editor...");
  1134. commands.push_back(
  1135. "$(CMAKE_EDIT_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1136. this->WriteMakeRule(makefileStream,
  1137. "Special rule to re-run CMake cache editor using make.",
  1138. "edit_cache",
  1139. no_depends,
  1140. commands);
  1141. }
  1142. else
  1143. {
  1144. std::vector<std::string> no_depends;
  1145. std::vector<std::string> commands;
  1146. this->AppendEcho(commands,
  1147. "Running interactive CMake command-line interface...");
  1148. commands.push_back(
  1149. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) -i");
  1150. this->WriteMakeRule(makefileStream,
  1151. "Special rule to re-run CMake cache editor using make.",
  1152. "edit_cache",
  1153. no_depends,
  1154. commands);
  1155. }
  1156. }
  1157. //----------------------------------------------------------------------------
  1158. void
  1159. cmLocalUnixMakefileGenerator3
  1160. ::WriteSpecialTargetsBottom(std::ostream& makefileStream)
  1161. {
  1162. this->WriteDivider(makefileStream);
  1163. makefileStream
  1164. << "# Special targets to cleanup operation of make.\n"
  1165. << "\n";
  1166. // Write special "cmake_check_build_system" target to run cmake with
  1167. // the --check-build-system flag.
  1168. {
  1169. // Build command to run CMake to check if anything needs regenerating.
  1170. std::string cmakefileName = m_Makefile->GetStartOutputDirectory();
  1171. cmakefileName += "/Makefile.cmake";
  1172. std::string runRule =
  1173. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
  1174. runRule += " --check-build-system ";
  1175. runRule += this->ConvertToRelativeOutputPath(cmakefileName.c_str());
  1176. std::vector<std::string> no_depends;
  1177. std::vector<std::string> commands;
  1178. commands.push_back(runRule);
  1179. this->WriteMakeRule(makefileStream,
  1180. "Special rule to run CMake to check the build system "
  1181. "integrity.\n"
  1182. "No rule that depends on this can have "
  1183. "commands that come from listfiles\n"
  1184. "because they might be regenerated.",
  1185. "cmake_check_build_system",
  1186. no_depends,
  1187. commands);
  1188. }
  1189. std::vector<std::string> no_commands;
  1190. // Write special target to silence make output. This must be after
  1191. // the default target in case VERBOSE is set (which changes the
  1192. // name). The setting of CMAKE_VERBOSE_MAKEFILE to ON will cause a
  1193. // "VERBOSE=1" to be added as a make variable which will change the
  1194. // name of this special target. This gives a make-time choice to
  1195. // the user.
  1196. std::vector<std::string> no_depends;
  1197. this->WriteMakeRule(makefileStream,
  1198. "Suppress display of executed commands.",
  1199. "$(VERBOSE).SILENT",
  1200. no_depends,
  1201. no_commands);
  1202. // Special target to cleanup operation of make tool.
  1203. std::vector<std::string> depends;
  1204. this->WriteMakeRule(makefileStream,
  1205. "Disable implicit rules so canoncical targets will work.",
  1206. ".SUFFIXES",
  1207. depends,
  1208. no_commands);
  1209. // Add a fake suffix to keep HP happy. Must be max 32 chars for SGI make.
  1210. depends.push_back(".hpux_make_needs_suffix_list");
  1211. this->WriteMakeRule(makefileStream, 0,
  1212. ".SUFFIXES", depends, no_commands);
  1213. }
  1214. //----------------------------------------------------------------------------
  1215. void
  1216. cmLocalUnixMakefileGenerator3
  1217. ::WriteConvenienceRule(std::ostream& ruleFileStream,
  1218. const char* realTarget,
  1219. const char* helpTarget)
  1220. {
  1221. // A rule is only needed if the names are different.
  1222. if(strcmp(realTarget, helpTarget) != 0)
  1223. {
  1224. // The helper target depends on the real target.
  1225. std::vector<std::string> depends;
  1226. depends.push_back(realTarget);
  1227. // There are no commands.
  1228. std::vector<std::string> no_commands;
  1229. // Write the rule.
  1230. this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  1231. helpTarget, depends, no_commands);
  1232. }
  1233. }
  1234. //----------------------------------------------------------------------------
  1235. void
  1236. cmLocalUnixMakefileGenerator3
  1237. ::WriteExecutableRule(std::ostream& ruleFileStream,
  1238. const char* ruleFileName,
  1239. const cmTarget& target,
  1240. const std::vector<std::string>& objects,
  1241. const std::vector<std::string>& external_objects,
  1242. const std::vector<std::string>& provides_requires)
  1243. {
  1244. // Write the dependency generation rule.
  1245. this->WriteTargetDependRule(ruleFileName, target, objects);
  1246. std::vector<std::string> commands;
  1247. std::string relPath = this->GetHomeRelativeOutputPath();
  1248. std::string objTarget;
  1249. // Build list of dependencies.
  1250. std::vector<std::string> depends;
  1251. for(std::vector<std::string>::const_iterator obj = objects.begin();
  1252. obj != objects.end(); ++obj)
  1253. {
  1254. objTarget = relPath;
  1255. objTarget += *obj;
  1256. depends.push_back(objTarget);
  1257. }
  1258. // Add dependencies on targets that must be built first.
  1259. this->AppendTargetDepends(depends, target);
  1260. // Add a dependency on the rule file itself.
  1261. objTarget = relPath;
  1262. objTarget += ruleFileName;
  1263. this->AppendRuleDepend(depends, objTarget.c_str());
  1264. std::vector<std::string> cleanFiles;
  1265. // Construct the full path to the executable that will be generated.
  1266. std::string targetFullPath = m_ExecutableOutputPath;
  1267. if(targetFullPath.length() == 0)
  1268. {
  1269. targetFullPath = m_Makefile->GetStartOutputDirectory();
  1270. targetFullPath += "/";
  1271. }
  1272. #ifdef __APPLE__
  1273. if(target.GetPropertyAsBool("MACOSX_BUNDLE"))
  1274. {
  1275. // Make bundle directories
  1276. targetFullPath += target.GetName();
  1277. targetFullPath += ".app/Contents/MacOS/";
  1278. }
  1279. #endif
  1280. targetFullPath += target.GetName();
  1281. targetFullPath += cmSystemTools::GetExecutableExtension();
  1282. // Convert to the output path to use in constructing commands.
  1283. std::string targetOutPath =
  1284. m_GlobalGenerator->ConvertToHomeRelativeOutputPath(targetFullPath.c_str());
  1285. // Get the language to use for linking this executable.
  1286. const char* linkLanguage =
  1287. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1288. // Make sure we have a link language.
  1289. if(!linkLanguage)
  1290. {
  1291. cmSystemTools::Error("Cannot determine link language for target \"",
  1292. target.GetName(), "\".");
  1293. return;
  1294. }
  1295. // Add the link message.
  1296. std::string buildEcho = "Linking ";
  1297. buildEcho += linkLanguage;
  1298. buildEcho += " executable ";
  1299. buildEcho += targetOutPath;
  1300. this->AppendEcho(commands, buildEcho.c_str());
  1301. // Build a list of compiler flags and linker flags.
  1302. std::string flags;
  1303. std::string linkFlags;
  1304. // Add flags to create an executable.
  1305. this->AddConfigVariableFlags(linkFlags, "CMAKE_EXE_LINKER_FLAGS");
  1306. if(target.GetPropertyAsBool("WIN32_EXECUTABLE"))
  1307. {
  1308. this->AppendFlags(linkFlags,
  1309. m_Makefile->GetDefinition("CMAKE_CREATE_WIN32_EXE"));
  1310. }
  1311. else
  1312. {
  1313. this->AppendFlags(linkFlags,
  1314. m_Makefile->GetDefinition("CMAKE_CREATE_CONSOLE_EXE"));
  1315. }
  1316. // Add language-specific flags.
  1317. this->AddLanguageFlags(flags, linkLanguage);
  1318. // Add flags to deal with shared libraries. Any library being
  1319. // linked in might be shared, so always use shared flags for an
  1320. // executable.
  1321. this->AddSharedFlags(flags, linkLanguage, true);
  1322. // Add target-specific linker flags.
  1323. this->AppendFlags(linkFlags, target.GetProperty("LINK_FLAGS"));
  1324. // Add the pre-build and pre-link rules.
  1325. this->AppendCustomCommands(commands, target.GetPreBuildCommands());
  1326. this->AppendCustomCommands(commands, target.GetPreLinkCommands());
  1327. // Construct the main link rule.
  1328. std::string linkRuleVar = "CMAKE_";
  1329. linkRuleVar += linkLanguage;
  1330. linkRuleVar += "_LINK_EXECUTABLE";
  1331. std::string linkRule = m_Makefile->GetRequiredDefinition(linkRuleVar.c_str());
  1332. cmSystemTools::ExpandListArgument(linkRule, commands);
  1333. // Add the post-build rules.
  1334. this->AppendCustomCommands(commands, target.GetPostBuildCommands());
  1335. // Collect up flags to link in needed libraries.
  1336. cmOStringStream linklibs;
  1337. this->OutputLinkLibraries(linklibs, 0, target);
  1338. // Construct object file lists that may be needed to expand the
  1339. // rule.
  1340. std::string variableName;
  1341. std::string variableNameExternal;
  1342. this->WriteObjectsVariable(ruleFileStream, target, objects, external_objects,
  1343. variableName, variableNameExternal);
  1344. std::string buildObjs = "$(";
  1345. buildObjs += variableName;
  1346. buildObjs += ") $(";
  1347. buildObjs += variableNameExternal;
  1348. buildObjs += ")";
  1349. std::string cleanObjs = "$(";
  1350. cleanObjs += variableName;
  1351. cleanObjs += ")";
  1352. // Expand placeholders in the commands.
  1353. for(std::vector<std::string>::iterator i = commands.begin();
  1354. i != commands.end(); ++i)
  1355. {
  1356. this->ExpandRuleVariables(*i,
  1357. linkLanguage,
  1358. buildObjs.c_str(),
  1359. targetOutPath.c_str(),
  1360. linklibs.str().c_str(),
  1361. 0,
  1362. 0,
  1363. flags.c_str(),
  1364. 0,
  1365. 0,
  1366. 0,
  1367. linkFlags.c_str());
  1368. }
  1369. // Write the build rule.
  1370. this->WriteMakeRule(ruleFileStream, 0,
  1371. targetFullPath.c_str(), depends, commands);
  1372. // Write convenience targets.
  1373. std::string dir = m_Makefile->GetStartOutputDirectory();
  1374. dir += "/";
  1375. dir += this->GetTargetDirectory(target);
  1376. std::string buildTargetRuleName = dir;
  1377. buildTargetRuleName += "/build";
  1378. buildTargetRuleName =
  1379. m_GlobalGenerator->ConvertToHomeRelativeOutputPath(buildTargetRuleName.c_str());
  1380. this->WriteConvenienceRule(ruleFileStream, targetFullPath.c_str(),
  1381. buildTargetRuleName.c_str());
  1382. // Write clean target
  1383. cleanFiles.push_back(cleanObjs);
  1384. cleanFiles.push_back(targetOutPath.c_str());
  1385. this->WriteTargetCleanRule(ruleFileName, target, cleanFiles, objects, external_objects);
  1386. }
  1387. //----------------------------------------------------------------------------
  1388. void
  1389. cmLocalUnixMakefileGenerator3
  1390. ::WriteStaticLibraryRule(std::ostream& ruleFileStream,
  1391. const char* ruleFileName,
  1392. const cmTarget& target,
  1393. const std::vector<std::string>& objects,
  1394. const std::vector<std::string>& external_objects,
  1395. const std::vector<std::string>& provides_requires)
  1396. {
  1397. const char* linkLanguage =
  1398. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1399. std::string linkRuleVar = "CMAKE_";
  1400. linkRuleVar += linkLanguage;
  1401. linkRuleVar += "_CREATE_STATIC_LIBRARY";
  1402. std::string extraFlags;
  1403. this->AppendFlags(extraFlags, target.GetProperty("STATIC_LIBRARY_FLAGS"));
  1404. this->WriteLibraryRule(ruleFileStream, ruleFileName, target,
  1405. objects, external_objects,
  1406. linkRuleVar.c_str(), extraFlags.c_str(),
  1407. provides_requires);
  1408. }
  1409. //----------------------------------------------------------------------------
  1410. void
  1411. cmLocalUnixMakefileGenerator3
  1412. ::WriteSharedLibraryRule(std::ostream& ruleFileStream,
  1413. const char* ruleFileName,
  1414. const cmTarget& target,
  1415. const std::vector<std::string>& objects,
  1416. const std::vector<std::string>& external_objects,
  1417. const std::vector<std::string>& provides_requires)
  1418. {
  1419. const char* linkLanguage =
  1420. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1421. std::string linkRuleVar = "CMAKE_";
  1422. linkRuleVar += linkLanguage;
  1423. linkRuleVar += "_CREATE_SHARED_LIBRARY";
  1424. std::string extraFlags;
  1425. this->AppendFlags(extraFlags, target.GetProperty("LINK_FLAGS"));
  1426. this->AddConfigVariableFlags(extraFlags, "CMAKE_SHARED_LINKER_FLAGS");
  1427. if(m_Makefile->IsOn("WIN32") && !(m_Makefile->IsOn("CYGWIN") || m_Makefile->IsOn("MINGW")))
  1428. {
  1429. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  1430. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1431. i != sources.end(); ++i)
  1432. {
  1433. if((*i)->GetSourceExtension() == "def")
  1434. {
  1435. extraFlags += " ";
  1436. extraFlags += m_Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1437. extraFlags +=
  1438. m_GlobalGenerator->ConvertToHomeRelativeOutputPath((*i)->GetFullPath().c_str());
  1439. }
  1440. }
  1441. }
  1442. this->WriteLibraryRule(ruleFileStream, ruleFileName, target,
  1443. objects, external_objects,
  1444. linkRuleVar.c_str(), extraFlags.c_str(),
  1445. provides_requires);
  1446. }
  1447. //----------------------------------------------------------------------------
  1448. void
  1449. cmLocalUnixMakefileGenerator3
  1450. ::WriteModuleLibraryRule(std::ostream& ruleFileStream,
  1451. const char* ruleFileName,
  1452. const cmTarget& target,
  1453. const std::vector<std::string>& objects,
  1454. const std::vector<std::string>& external_objects,
  1455. const std::vector<std::string>& provides_requires)
  1456. {
  1457. const char* linkLanguage =
  1458. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1459. std::string linkRuleVar = "CMAKE_";
  1460. linkRuleVar += linkLanguage;
  1461. linkRuleVar += "_CREATE_SHARED_MODULE";
  1462. std::string extraFlags;
  1463. this->AppendFlags(extraFlags, target.GetProperty("LINK_FLAGS"));
  1464. this->AddConfigVariableFlags(extraFlags, "CMAKE_MODULE_LINKER_FLAGS");
  1465. // TODO: .def files should be supported here also.
  1466. this->WriteLibraryRule(ruleFileStream, ruleFileName, target,
  1467. objects, external_objects,
  1468. linkRuleVar.c_str(), extraFlags.c_str(),
  1469. provides_requires);
  1470. }
  1471. //----------------------------------------------------------------------------
  1472. void
  1473. cmLocalUnixMakefileGenerator3
  1474. ::WriteLibraryRule(std::ostream& ruleFileStream,
  1475. const char* ruleFileName,
  1476. const cmTarget& target,
  1477. const std::vector<std::string>& objects,
  1478. const std::vector<std::string>& external_objects,
  1479. const char* linkRuleVar,
  1480. const char* extraFlags,
  1481. const std::vector<std::string>& provides_requires)
  1482. {
  1483. // Write the dependency generation rule.
  1484. this->WriteTargetDependRule(ruleFileName, target, objects);
  1485. // TODO: Merge the methods that call this method to avoid
  1486. // code duplication.
  1487. std::vector<std::string> commands;
  1488. std::string relPath = this->GetHomeRelativeOutputPath();
  1489. std::string objTarget;
  1490. // Build list of dependencies.
  1491. std::vector<std::string> depends;
  1492. for(std::vector<std::string>::const_iterator obj = objects.begin();
  1493. obj != objects.end(); ++obj)
  1494. {
  1495. objTarget = relPath;
  1496. objTarget += *obj;
  1497. depends.push_back(objTarget);
  1498. }
  1499. // Add dependencies on targets that must be built first.
  1500. this->AppendTargetDepends(depends, target);
  1501. // Add a dependency on the rule file itself.
  1502. objTarget = relPath;
  1503. objTarget += ruleFileName;
  1504. this->AppendRuleDepend(depends, objTarget.c_str());
  1505. std::vector<std::string> cleanFiles;
  1506. // from here up is the same for exe or lib
  1507. for(std::vector<std::string>::const_iterator obj = external_objects.begin();
  1508. obj != external_objects.end(); ++obj)
  1509. {
  1510. depends.push_back(*obj);
  1511. }
  1512. // Get the language to use for linking this library.
  1513. const char* linkLanguage =
  1514. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1515. // Make sure we have a link language.
  1516. if(!linkLanguage)
  1517. {
  1518. cmSystemTools::Error("Cannot determine link language for target \"",
  1519. target.GetName(), "\".");
  1520. return;
  1521. }
  1522. // Create set of linking flags.
  1523. std::string linkFlags;
  1524. this->AppendFlags(linkFlags, extraFlags);
  1525. // Construct the name of the library.
  1526. std::string targetName;
  1527. std::string targetNameSO;
  1528. std::string targetNameReal;
  1529. std::string targetNameBase;
  1530. target.GetLibraryNames(m_Makefile,
  1531. targetName, targetNameSO,
  1532. targetNameReal, targetNameBase);
  1533. // Construct the full path version of the names.
  1534. std::string outpath = m_LibraryOutputPath;
  1535. if(outpath.length() == 0)
  1536. {
  1537. outpath = m_Makefile->GetStartOutputDirectory();
  1538. outpath += "/";
  1539. }
  1540. std::string targetFullPath = outpath + targetName;
  1541. std::string targetFullPathSO = outpath + targetNameSO;
  1542. std::string targetFullPathReal = outpath + targetNameReal;
  1543. std::string targetFullPathBase = outpath + targetNameBase;
  1544. // Construct the output path version of the names for use in command
  1545. // arguments.
  1546. std::string targetOutPath =
  1547. m_GlobalGenerator->ConvertToHomeRelativeOutputPath(targetFullPath.c_str());
  1548. std::string targetOutPathSO =
  1549. m_GlobalGenerator->ConvertToHomeRelativeOutputPath(targetFullPathSO.c_str());
  1550. std::string targetOutPathReal =
  1551. m_GlobalGenerator->ConvertToHomeRelativeOutputPath(targetFullPathReal.c_str());
  1552. std::string targetOutPathBase =
  1553. m_GlobalGenerator->ConvertToHomeRelativeOutputPath(targetFullPathBase.c_str());
  1554. // Add the link message.
  1555. std::string buildEcho = "Linking ";
  1556. buildEcho += linkLanguage;
  1557. switch(target.GetType())
  1558. {
  1559. case cmTarget::STATIC_LIBRARY:
  1560. buildEcho += " static library "; break;
  1561. case cmTarget::SHARED_LIBRARY:
  1562. buildEcho += " shared library "; break;
  1563. case cmTarget::MODULE_LIBRARY:
  1564. buildEcho += " shared module "; break;
  1565. default:
  1566. buildEcho += " library "; break;
  1567. }
  1568. buildEcho += targetOutPath.c_str();
  1569. this->AppendEcho(commands, buildEcho.c_str());
  1570. // Construct a list of files associated with this library that may
  1571. // need to be cleaned.
  1572. {
  1573. std::string cleanStaticName;
  1574. std::string cleanSharedName;
  1575. std::string cleanSharedSOName;
  1576. std::string cleanSharedRealName;
  1577. target.GetLibraryCleanNames(m_Makefile,
  1578. cleanStaticName,
  1579. cleanSharedName,
  1580. cleanSharedSOName,
  1581. cleanSharedRealName);
  1582. std::string cleanFullStaticName = outpath + cleanStaticName;
  1583. std::string cleanFullSharedName = outpath + cleanSharedName;
  1584. std::string cleanFullSharedSOName = outpath + cleanSharedSOName;
  1585. std::string cleanFullSharedRealName = outpath + cleanSharedRealName;
  1586. cleanFiles.push_back(cleanFullStaticName);
  1587. if(cleanSharedRealName != cleanStaticName)
  1588. {
  1589. cleanFiles.push_back(cleanFullSharedRealName);
  1590. }
  1591. if(cleanSharedSOName != cleanStaticName &&
  1592. cleanSharedSOName != cleanSharedRealName)
  1593. {
  1594. cleanFiles.push_back(cleanFullSharedSOName);
  1595. }
  1596. if(cleanSharedName != cleanStaticName &&
  1597. cleanSharedName != cleanSharedSOName &&
  1598. cleanSharedName != cleanSharedRealName)
  1599. {
  1600. cleanFiles.push_back(cleanFullSharedName);
  1601. }
  1602. }
  1603. // Add a command to remove any existing files for this library.
  1604. this->AppendCleanCommand(commands, cleanFiles);
  1605. // Add the pre-build and pre-link rules.
  1606. this->AppendCustomCommands(commands, target.GetPreBuildCommands());
  1607. this->AppendCustomCommands(commands, target.GetPreLinkCommands());
  1608. // Construct the main link rule.
  1609. std::string linkRule = m_Makefile->GetRequiredDefinition(linkRuleVar);
  1610. cmSystemTools::ExpandListArgument(linkRule, commands);
  1611. // Add a rule to create necessary symlinks for the library.
  1612. if(targetOutPath != targetOutPathReal)
  1613. {
  1614. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  1615. symlink += targetOutPathReal;
  1616. symlink += " ";
  1617. symlink += targetOutPathSO;
  1618. symlink += " ";
  1619. symlink += targetOutPath;
  1620. commands.push_back(symlink);
  1621. }
  1622. // Add the post-build rules.
  1623. this->AppendCustomCommands(commands, target.GetPostBuildCommands());
  1624. // Collect up flags to link in needed libraries.
  1625. cmOStringStream linklibs;
  1626. this->OutputLinkLibraries(linklibs, target.GetName(), target);
  1627. // Construct object file lists that may be needed to expand the
  1628. // rule.
  1629. std::string variableName;
  1630. std::string variableNameExternal;
  1631. this->WriteObjectsVariable(ruleFileStream, target, objects, external_objects,
  1632. variableName, variableNameExternal);
  1633. std::string buildObjs = "$(";
  1634. buildObjs += variableName;
  1635. buildObjs += ") $(";
  1636. buildObjs += variableNameExternal;
  1637. buildObjs += ")";
  1638. std::string cleanObjs = "$(";
  1639. cleanObjs += variableName;
  1640. cleanObjs += ")";
  1641. // Expand placeholders in the commands.
  1642. for(std::vector<std::string>::iterator i = commands.begin();
  1643. i != commands.end(); ++i)
  1644. {
  1645. this->ExpandRuleVariables(*i,
  1646. linkLanguage,
  1647. buildObjs.c_str(),
  1648. targetOutPathReal.c_str(),
  1649. linklibs.str().c_str(),
  1650. 0, 0, 0, buildObjs.c_str(),
  1651. targetOutPathBase.c_str(),
  1652. targetNameSO.c_str(),
  1653. linkFlags.c_str());
  1654. }
  1655. // from here down is the same for exe or lib
  1656. // Write the build rule.
  1657. this->WriteMakeRule(ruleFileStream, 0,
  1658. targetFullPath.c_str(), depends, commands);
  1659. // Write convenience targets.
  1660. std::string dir = m_Makefile->GetStartOutputDirectory();
  1661. dir += "/";
  1662. dir += this->GetTargetDirectory(target);
  1663. std::string buildTargetRuleName = dir;
  1664. buildTargetRuleName += "/build";
  1665. buildTargetRuleName =
  1666. m_GlobalGenerator->ConvertToHomeRelativeOutputPath(buildTargetRuleName.c_str());
  1667. this->WriteConvenienceRule(ruleFileStream, targetFullPath.c_str(),
  1668. buildTargetRuleName.c_str());
  1669. // Write clean target
  1670. cleanFiles.push_back(cleanObjs);
  1671. cleanFiles.push_back(targetOutPath.c_str());
  1672. this->WriteTargetCleanRule(ruleFileName, target, cleanFiles, objects, external_objects);
  1673. }
  1674. //----------------------------------------------------------------------------
  1675. void
  1676. cmLocalUnixMakefileGenerator3
  1677. ::WriteObjectsVariable(std::ostream& ruleFileStream,
  1678. const cmTarget& target,
  1679. const std::vector<std::string>& objects,
  1680. const std::vector<std::string>& external_objects,
  1681. std::string& variableName,
  1682. std::string& variableNameExternal)
  1683. {
  1684. // Write a make variable assignment that lists all objects for the
  1685. // target.
  1686. variableName = this->CreateMakeVariable(target.GetName(), "_OBJECTS");
  1687. ruleFileStream
  1688. << "# Object files for target " << target.GetName() << "\n"
  1689. << variableName.c_str() << " =";
  1690. std::string relPath = this->GetHomeRelativeOutputPath();
  1691. std::string object;
  1692. for(std::vector<std::string>::const_iterator i = objects.begin();
  1693. i != objects.end(); ++i)
  1694. {
  1695. object = relPath;
  1696. object += *i;
  1697. ruleFileStream
  1698. << " \\\n"
  1699. << this->ConvertToQuotedOutputPath(object.c_str());
  1700. }
  1701. ruleFileStream
  1702. << "\n";
  1703. // Write a make variable assignment that lists all external objects
  1704. // for the target.
  1705. variableNameExternal = this->CreateMakeVariable(target.GetName(),
  1706. "_EXTERNAL_OBJECTS");
  1707. ruleFileStream
  1708. << "\n"
  1709. << "# External object files for target " << target.GetName() << "\n"
  1710. << variableNameExternal.c_str() << " =";
  1711. for(std::vector<std::string>::const_iterator i = external_objects.begin();
  1712. i != external_objects.end(); ++i)
  1713. {
  1714. std::string object = m_GlobalGenerator->ConvertToHomeRelativePath(i->c_str());
  1715. ruleFileStream
  1716. << " \\\n"
  1717. << this->ConvertToQuotedOutputPath(object.c_str());
  1718. }
  1719. ruleFileStream
  1720. << "\n"
  1721. << "\n";
  1722. }
  1723. //----------------------------------------------------------------------------
  1724. void
  1725. cmLocalUnixMakefileGenerator3
  1726. ::WriteTargetDependRule(const char* ruleFileName,
  1727. const cmTarget& target,
  1728. const std::vector<std::string>& objects)
  1729. {
  1730. std::string dir = cmSystemTools::GetFilenamePath(ruleFileName);
  1731. std::string dependFileName = dir;
  1732. dependFileName += "/depend.make";
  1733. // Open the rule file. This should be copy-if-different because the
  1734. // rules may depend on this file itself.
  1735. std::string ruleFileNameFull = this->ConvertToFullPath(dependFileName);
  1736. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  1737. ruleFileStream.SetCopyIfDifferent(true);
  1738. if(!ruleFileStream)
  1739. {
  1740. return;
  1741. }
  1742. this->WriteDisclaimer(ruleFileStream);
  1743. std::vector<std::string> depends;
  1744. std::vector<std::string> no_commands;
  1745. // Construct the name of the dependency generation target.
  1746. std::string depTarget = this->GetRelativeTargetDirectory(target);
  1747. depTarget += "/depend";
  1748. // This target drives dependency generation for all object files.
  1749. std::string relPath = this->GetHomeRelativeOutputPath();
  1750. std::string objTarget;
  1751. for(std::vector<std::string>::const_iterator obj = objects.begin();
  1752. obj != objects.end(); ++obj)
  1753. {
  1754. objTarget = relPath;
  1755. objTarget += *obj;
  1756. objTarget += ".depend";
  1757. depends.push_back(objTarget);
  1758. }
  1759. // Write the rule.
  1760. this->WriteMakeRule(ruleFileStream, 0,
  1761. depTarget.c_str(), depends, no_commands);
  1762. // Include the rule file for each object.
  1763. if(!objects.empty())
  1764. {
  1765. ruleFileStream
  1766. << "# Include depend rules for object files.\n";
  1767. for(std::vector<std::string>::const_iterator obj = objects.begin();
  1768. obj != objects.end(); ++obj)
  1769. {
  1770. objTarget = relPath;
  1771. objTarget += *obj;
  1772. objTarget += ".depend.make";
  1773. ruleFileStream
  1774. << m_IncludeDirective << " "
  1775. << this->ConvertToOutputForExisting(objTarget.c_str()).c_str()
  1776. << "\n";
  1777. }
  1778. ruleFileStream << "\n";
  1779. }
  1780. }
  1781. //----------------------------------------------------------------------------
  1782. void
  1783. cmLocalUnixMakefileGenerator3
  1784. ::WriteTargetCleanRule(const char *ruleFileName,
  1785. const cmTarget& target,
  1786. const std::vector<std::string>& files,
  1787. const std::vector<std::string>& objects,
  1788. const std::vector<std::string>& external_objects)
  1789. {
  1790. std::string dir = cmSystemTools::GetFilenamePath(ruleFileName);
  1791. std::string cleanFileName = dir;
  1792. cleanFileName += "/clean.make";
  1793. // Open the rule file. This should be copy-if-different because the
  1794. // rules may depend on this file itself.
  1795. std::string ruleFileNameFull = this->ConvertToFullPath(cleanFileName);
  1796. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  1797. ruleFileStream.SetCopyIfDifferent(true);
  1798. if(!ruleFileStream)
  1799. {
  1800. return;
  1801. }
  1802. this->WriteDisclaimer(ruleFileStream);
  1803. std::string variableName;
  1804. std::string variableNameExternal;
  1805. this->WriteObjectsVariable(ruleFileStream, target, objects, external_objects,
  1806. variableName, variableNameExternal);
  1807. std::vector<std::string> no_depends;
  1808. std::vector<std::string> commands;
  1809. // Construct the clean target name.
  1810. std::string cleanTarget = this->GetRelativeTargetDirectory(target);
  1811. cleanTarget += "/clean";
  1812. // Construct the clean command.
  1813. this->AppendCleanCommand(commands, files);
  1814. // Write the rule.
  1815. this->WriteMakeRule(ruleFileStream, 0,
  1816. cleanTarget.c_str(), no_depends, commands);
  1817. }
  1818. //----------------------------------------------------------------------------
  1819. void
  1820. cmLocalUnixMakefileGenerator3
  1821. ::WriteCMakeArgument(std::ostream& os, const char* s)
  1822. {
  1823. // Write the given string to the stream with escaping to get it back
  1824. // into CMake through the lexical scanner.
  1825. os << "\"";
  1826. for(const char* c = s; *c; ++c)
  1827. {
  1828. if(*c == '\\')
  1829. {
  1830. os << "\\\\";
  1831. }
  1832. else if(*c == '"')
  1833. {
  1834. os << "\\\"";
  1835. }
  1836. else
  1837. {
  1838. os << *c;
  1839. }
  1840. }
  1841. os << "\"";
  1842. }
  1843. //----------------------------------------------------------------------------
  1844. std::string
  1845. cmLocalUnixMakefileGenerator3::GetTargetDirectory(const cmTarget& target)
  1846. {
  1847. std::string dir = target.GetName();
  1848. dir += ".dir";
  1849. return dir;
  1850. }
  1851. //----------------------------------------------------------------------------
  1852. std::string
  1853. cmLocalUnixMakefileGenerator3::GetRelativeTargetDirectory(const cmTarget& target)
  1854. {
  1855. std::string dir = m_Makefile->GetStartOutputDirectory();
  1856. dir += "/";
  1857. dir += this->GetTargetDirectory(target);
  1858. dir = cmSystemTools::RelativePath(m_Makefile->GetHomeOutputDirectory(), dir.c_str());
  1859. return cmSystemTools::ConvertToOutputPath(dir.c_str());
  1860. }
  1861. //----------------------------------------------------------------------------
  1862. std::string
  1863. cmLocalUnixMakefileGenerator3
  1864. ::GetSubdirTargetName(const char* pass, const char* subdir)
  1865. {
  1866. // Convert the subdirectory name to a relative path to keep it short.
  1867. std::string reldir = this->ConvertToRelativePath(subdir);
  1868. // Convert the subdirectory name to a valid make target name.
  1869. std::string s = pass;
  1870. s += "_";
  1871. s += reldir;
  1872. // Replace "../" with 3 underscores. This allows one .. at the beginning.
  1873. size_t pos = s.find("../");
  1874. if(pos != std::string::npos)
  1875. {
  1876. s.replace(pos, 3, "___");
  1877. }
  1878. // Replace "/" directory separators with a single underscore.
  1879. while((pos = s.find('/')) != std::string::npos)
  1880. {
  1881. s.replace(pos, 1, "_");
  1882. }
  1883. // Replace ":" drive specifier with a single underscore
  1884. while((pos = s.find(':')) != std::string::npos)
  1885. {
  1886. s.replace(pos, 1, "_");
  1887. }
  1888. return s;
  1889. }
  1890. //----------------------------------------------------------------------------
  1891. std::string
  1892. cmLocalUnixMakefileGenerator3
  1893. ::GetObjectFileName(const cmTarget& target,
  1894. const cmSourceFile& source)
  1895. {
  1896. // If the full path to the source file includes this directory,
  1897. // we want to use the relative path for the filename of the
  1898. // object file. Otherwise, we will use just the filename
  1899. // portion.
  1900. std::string objectName;
  1901. if((cmSystemTools::GetFilenamePath(
  1902. source.GetFullPath()).find(
  1903. m_Makefile->GetCurrentDirectory()) == 0)
  1904. || (cmSystemTools::GetFilenamePath(
  1905. source.GetFullPath()).find(
  1906. m_Makefile->GetStartOutputDirectory()) == 0))
  1907. {
  1908. objectName = source.GetSourceName();
  1909. }
  1910. else
  1911. {
  1912. objectName = cmSystemTools::GetFilenameName(source.GetSourceName());
  1913. }
  1914. // Append the object file extension.
  1915. objectName +=
  1916. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  1917. source.GetSourceExtension().c_str());
  1918. // Convert to a safe name.
  1919. objectName = this->CreateSafeUniqueObjectFileName(objectName.c_str());
  1920. // Prepend the target directory.
  1921. std::string obj = this->GetTargetDirectory(target);
  1922. obj += "/";
  1923. obj += objectName;
  1924. return obj;
  1925. }
  1926. //----------------------------------------------------------------------------
  1927. const char*
  1928. cmLocalUnixMakefileGenerator3
  1929. ::GetSourceFileLanguage(const cmSourceFile& source)
  1930. {
  1931. // Identify the language of the source file.
  1932. return (m_GlobalGenerator
  1933. ->GetLanguageFromExtension(source.GetSourceExtension().c_str()));
  1934. }
  1935. //----------------------------------------------------------------------------
  1936. std::string
  1937. cmLocalUnixMakefileGenerator3::ConvertToRelativeOutputPath(const char* p)
  1938. {
  1939. // Convert the path to a relative path.
  1940. std::string relative = this->ConvertToRelativePath(p);
  1941. // Now convert it to an output path.
  1942. return cmSystemTools::ConvertToOutputPath(relative.c_str());
  1943. }
  1944. //----------------------------------------------------------------------------
  1945. std::string
  1946. cmLocalUnixMakefileGenerator3::ConvertToQuotedOutputPath(const char* p)
  1947. {
  1948. // Split the path into its components.
  1949. std::vector<std::string> components;
  1950. cmSystemTools::SplitPath(p, components);
  1951. // Return an empty path if there are no components.
  1952. if(components.empty())
  1953. {
  1954. return "\"\"";
  1955. }
  1956. // Choose a slash direction and fix root component.
  1957. const char* slash = "/";
  1958. #if defined(_WIN32) && !defined(__CYGWIN__)
  1959. if(!cmSystemTools::GetForceUnixPaths())
  1960. {
  1961. slash = "\\";
  1962. for(std::string::iterator i = components[0].begin();
  1963. i != components[0].end(); ++i)
  1964. {
  1965. if(*i == '/')
  1966. {
  1967. *i = '\\';
  1968. }
  1969. }
  1970. }
  1971. #endif
  1972. // Begin the quoted result with the root component.
  1973. std::string result = "\"";
  1974. result += components[0];
  1975. // Now add the rest of the components separated by the proper slash
  1976. // direction for this platform.
  1977. bool first = true;
  1978. for(unsigned int i=1; i < components.size(); ++i)
  1979. {
  1980. // Only the last component can be empty to avoid double slashes.
  1981. if(components[i].length() > 0 || (i == (components.size()-1)))
  1982. {
  1983. if(!first)
  1984. {
  1985. result += slash;
  1986. }
  1987. result += components[i];
  1988. first = false;
  1989. }
  1990. }
  1991. // Close the quoted result.
  1992. result += "\"";
  1993. return result;
  1994. }
  1995. //----------------------------------------------------------------------------
  1996. void
  1997. cmLocalUnixMakefileGenerator3
  1998. ::AppendTargetDepends(std::vector<std::string>& depends,
  1999. const cmTarget& target)
  2000. {
  2001. // Do not bother with dependencies for static libraries.
  2002. if(target.GetType() == cmTarget::STATIC_LIBRARY)
  2003. {
  2004. return;
  2005. }
  2006. // Keep track of dependencies already listed.
  2007. std::set<cmStdString> emitted;
  2008. // A target should not depend on itself.
  2009. emitted.insert(target.GetName());
  2010. // Loop over all library dependencies.
  2011. const cmTarget::LinkLibraries& tlibs = target.GetLinkLibraries();
  2012. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  2013. lib != tlibs.end(); ++lib)
  2014. {
  2015. // Don't emit the same library twice for this target.
  2016. if(emitted.insert(lib->first).second)
  2017. {
  2018. // Add this dependency.
  2019. this->AppendAnyDepend(depends, lib->first.c_str());
  2020. }
  2021. }
  2022. // Loop over all utility dependencies.
  2023. const std::set<cmStdString>& tutils = target.GetUtilities();
  2024. for(std::set<cmStdString>::const_iterator util = tutils.begin();
  2025. util != tutils.end(); ++util)
  2026. {
  2027. // Don't emit the same utility twice for this target.
  2028. if(emitted.insert(*util).second)
  2029. {
  2030. // Add this dependency.
  2031. this->AppendAnyDepend(depends, util->c_str());
  2032. }
  2033. }
  2034. }
  2035. //----------------------------------------------------------------------------
  2036. void
  2037. cmLocalUnixMakefileGenerator3
  2038. ::AppendAnyDepend(std::vector<std::string>& depends, const char* name,
  2039. bool assume_unknown_is_file)
  2040. {
  2041. // There are a few cases for the name of the target:
  2042. // - CMake target in this directory: depend on it.
  2043. // - CMake target in another directory: depend and add jump-and-build.
  2044. // - Full path to a file: depend on it.
  2045. // - Other format (like -lm): do nothing.
  2046. // If it is an executable or library target there will be a
  2047. // definition for it.
  2048. std::string dirVar = name;
  2049. dirVar += "_CMAKE_PATH";
  2050. const char* dir = m_Makefile->GetDefinition(dirVar.c_str());
  2051. if(dir && *dir)
  2052. {
  2053. // This is a CMake target somewhere in this project.
  2054. // Get the type of the library. If it does not have a type then
  2055. // it is an executable.
  2056. std::string typeVar = name;
  2057. typeVar += "_LIBRARY_TYPE";
  2058. const char* libType = m_Makefile->GetDefinition(typeVar.c_str());
  2059. // Get the output path for this target type.
  2060. std::string tgtOutputPath;
  2061. if(libType)
  2062. {
  2063. tgtOutputPath = m_LibraryOutputPath;
  2064. }
  2065. else
  2066. {
  2067. tgtOutputPath = m_ExecutableOutputPath;
  2068. }
  2069. // Get the path to the target.
  2070. std::string tgtPath;
  2071. if(tgtOutputPath.size())
  2072. {
  2073. tgtPath = tgtOutputPath;
  2074. }
  2075. else
  2076. {
  2077. tgtPath = dir;
  2078. tgtPath += "/";
  2079. }
  2080. // Add the name of the targets's file. This depends on the type
  2081. // of the target.
  2082. std::string prefix;
  2083. std::string suffix;
  2084. if(!libType)
  2085. {
  2086. suffix = cmSystemTools::GetExecutableExtension();
  2087. }
  2088. else if(strcmp(libType, "SHARED") == 0)
  2089. {
  2090. prefix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  2091. suffix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  2092. }
  2093. else if(strcmp(libType, "MODULE") == 0)
  2094. {
  2095. prefix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  2096. suffix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  2097. }
  2098. else if(strcmp(libType, "STATIC") == 0)
  2099. {
  2100. prefix = m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  2101. suffix = m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  2102. }
  2103. tgtPath += prefix;
  2104. tgtPath += name;
  2105. tgtPath += suffix;
  2106. // Add a dependency on the target.
  2107. depends.push_back(tgtPath.c_str());
  2108. }
  2109. else if(m_Makefile->GetTargets().find(name) !=
  2110. m_Makefile->GetTargets().end())
  2111. {
  2112. // This is a CMake target that is not an executable or library.
  2113. // It must be in this directory, so just depend on the name
  2114. // directly.
  2115. depends.push_back(name);
  2116. }
  2117. else if(cmSystemTools::FileIsFullPath(name))
  2118. {
  2119. // This is a path to a file. Just trust the listfile author that
  2120. // it will be present or there is a rule to build it.
  2121. depends.push_back(cmSystemTools::CollapseFullPath(name));
  2122. }
  2123. else if(assume_unknown_is_file)
  2124. {
  2125. // Just assume this is a file or make target that will be present.
  2126. depends.push_back(name);
  2127. }
  2128. }
  2129. //----------------------------------------------------------------------------
  2130. void
  2131. cmLocalUnixMakefileGenerator3
  2132. ::AppendRuleDepend(std::vector<std::string>& depends,
  2133. const char* ruleFileName)
  2134. {
  2135. // Add a dependency on the rule file itself unless an option to skip
  2136. // it is specifically enabled by the user or project.
  2137. const char* nodep = m_Makefile->GetDefinition("CMAKE_SKIP_RULE_DEPENDENCY");
  2138. if(!nodep || cmSystemTools::IsOff(nodep))
  2139. {
  2140. depends.push_back(ruleFileName);
  2141. }
  2142. }
  2143. //----------------------------------------------------------------------------
  2144. void
  2145. cmLocalUnixMakefileGenerator3
  2146. ::AppendCustomDepends(std::vector<std::string>& depends,
  2147. const std::vector<cmCustomCommand>& ccs)
  2148. {
  2149. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  2150. i != ccs.end(); ++i)
  2151. {
  2152. this->AppendCustomDepend(depends, *i);
  2153. }
  2154. }
  2155. //----------------------------------------------------------------------------
  2156. void
  2157. cmLocalUnixMakefileGenerator3
  2158. ::AppendCustomDepend(std::vector<std::string>& depends,
  2159. const cmCustomCommand& cc)
  2160. {
  2161. for(std::vector<std::string>::const_iterator d = cc.GetDepends().begin();
  2162. d != cc.GetDepends().end(); ++d)
  2163. {
  2164. // Add this dependency.
  2165. this->AppendAnyDepend(depends, d->c_str(), true);
  2166. }
  2167. }
  2168. //----------------------------------------------------------------------------
  2169. void
  2170. cmLocalUnixMakefileGenerator3
  2171. ::AppendCustomCommands(std::vector<std::string>& commands,
  2172. const std::vector<cmCustomCommand>& ccs)
  2173. {
  2174. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  2175. i != ccs.end(); ++i)
  2176. {
  2177. this->AppendCustomCommand(commands, *i);
  2178. }
  2179. }
  2180. //----------------------------------------------------------------------------
  2181. void
  2182. cmLocalUnixMakefileGenerator3
  2183. ::AppendCustomCommand(std::vector<std::string>& commands,
  2184. const cmCustomCommand& cc)
  2185. {
  2186. // TODO: Convert outputs/dependencies (arguments?) to relative paths.
  2187. // Add each command line to the set of commands.
  2188. for(cmCustomCommandLines::const_iterator cl = cc.GetCommandLines().begin();
  2189. cl != cc.GetCommandLines().end(); ++cl)
  2190. {
  2191. // Build the command line in a single string.
  2192. const cmCustomCommandLine& commandLine = *cl;
  2193. std::string cmd = commandLine[0];
  2194. cmSystemTools::ReplaceString(cmd, "/./", "/");
  2195. cmd = m_GlobalGenerator->ConvertToHomeRelativePath(cmd.c_str());
  2196. if(cmd.find("/") == cmd.npos &&
  2197. commandLine[0].find("/") != cmd.npos)
  2198. {
  2199. // Add a leading "./" for executables in the current directory.
  2200. cmd = "./" + cmd;
  2201. }
  2202. cmd = cmSystemTools::ConvertToOutputPath(cmd.c_str());
  2203. for(unsigned int j=1; j < commandLine.size(); ++j)
  2204. {
  2205. cmd += " ";
  2206. cmd += cmSystemTools::EscapeSpaces(commandLine[j].c_str());
  2207. }
  2208. commands.push_back(cmd);
  2209. }
  2210. }
  2211. //----------------------------------------------------------------------------
  2212. void
  2213. cmLocalUnixMakefileGenerator3
  2214. ::AppendCleanCommand(std::vector<std::string>& commands,
  2215. const std::vector<std::string>& files)
  2216. {
  2217. if(!files.empty())
  2218. {
  2219. std::string remove = "$(CMAKE_COMMAND) -E remove -f";
  2220. for(std::vector<std::string>::const_iterator f = files.begin();
  2221. f != files.end(); ++f)
  2222. {
  2223. remove += " ";
  2224. remove += this->ConvertToRelativeOutputPath(f->c_str());
  2225. }
  2226. commands.push_back(remove);
  2227. }
  2228. }
  2229. //----------------------------------------------------------------------------
  2230. void
  2231. cmLocalUnixMakefileGenerator3::AppendEcho(std::vector<std::string>& commands,
  2232. const char* text)
  2233. {
  2234. // Echo one line at a time.
  2235. std::string line;
  2236. for(const char* c = text;; ++c)
  2237. {
  2238. if(*c == '\n' || *c == '\0')
  2239. {
  2240. // Avoid writing a blank last line on end-of-string.
  2241. if(*c != '\0' || !line.empty())
  2242. {
  2243. // Add a command to echo this line.
  2244. std::string cmd = "@echo ";
  2245. if(m_EchoNeedsQuote)
  2246. {
  2247. cmd += "\"";
  2248. }
  2249. cmd += line;
  2250. if(m_EchoNeedsQuote)
  2251. {
  2252. cmd += "\"";
  2253. }
  2254. commands.push_back(cmd);
  2255. }
  2256. // Reset the line to emtpy.
  2257. line = "";
  2258. // Terminate on end-of-string.
  2259. if(*c == '\0')
  2260. {
  2261. return;
  2262. }
  2263. }
  2264. else if(*c != '\r')
  2265. {
  2266. // Append this character to the current line.
  2267. line += *c;
  2268. }
  2269. }
  2270. }
  2271. //============================================================================
  2272. //----------------------------------------------------------------------------
  2273. bool
  2274. cmLocalUnixMakefileGenerator3::SamePath(const char* path1, const char* path2)
  2275. {
  2276. if (strcmp(path1, path2) == 0)
  2277. {
  2278. return true;
  2279. }
  2280. #if defined(_WIN32) || defined(__APPLE__)
  2281. return
  2282. (cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path1)) ==
  2283. cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path2)));
  2284. #else
  2285. return false;
  2286. #endif
  2287. }
  2288. //----------------------------------------------------------------------------
  2289. // take a tgt path and convert it into a make target, it could be full, or relative
  2290. std::string
  2291. cmLocalUnixMakefileGenerator3
  2292. ::ConvertToMakeTarget(const char* tgt)
  2293. {
  2294. // Make targets should not have a leading './' for a file in the
  2295. // directory containing the makefile.
  2296. std::string ret = tgt;
  2297. if(ret.size() > 2 && (ret[0] == '.') &&
  2298. ( (ret[1] == '/') || ret[1] == '\\'))
  2299. {
  2300. std::string upath = ret;
  2301. cmSystemTools::ConvertToUnixSlashes(upath);
  2302. if(upath.find(2, '/') == upath.npos)
  2303. {
  2304. ret = ret.substr(2, ret.size()-2);
  2305. }
  2306. }
  2307. return ret;
  2308. }
  2309. //----------------------------------------------------------------------------
  2310. std::string&
  2311. cmLocalUnixMakefileGenerator3::CreateSafeUniqueObjectFileName(const char* sin)
  2312. {
  2313. if ( m_Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES") )
  2314. {
  2315. std::map<cmStdString,cmStdString>::iterator it = m_UniqueObjectNamesMap.find(sin);
  2316. if ( it == m_UniqueObjectNamesMap.end() )
  2317. {
  2318. std::string ssin = sin;
  2319. bool done;
  2320. int cc = 0;
  2321. char rpstr[100];
  2322. sprintf(rpstr, "_p_");
  2323. cmSystemTools::ReplaceString(ssin, "+", rpstr);
  2324. std::string sssin = sin;
  2325. do
  2326. {
  2327. done = true;
  2328. for ( it = m_UniqueObjectNamesMap.begin();
  2329. it != m_UniqueObjectNamesMap.end();
  2330. ++ it )
  2331. {
  2332. if ( it->second == ssin )
  2333. {
  2334. done = false;
  2335. }
  2336. }
  2337. if ( done )
  2338. {
  2339. break;
  2340. }
  2341. sssin = ssin;
  2342. cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
  2343. sprintf(rpstr, "_p%d_", cc++);
  2344. }
  2345. while ( !done );
  2346. m_UniqueObjectNamesMap[sin] = ssin;
  2347. }
  2348. }
  2349. else
  2350. {
  2351. m_UniqueObjectNamesMap[sin] = sin;
  2352. }
  2353. return m_UniqueObjectNamesMap[sin];
  2354. }
  2355. //----------------------------------------------------------------------------
  2356. std::string
  2357. cmLocalUnixMakefileGenerator3
  2358. ::CreateMakeVariable(const char* sin, const char* s2in)
  2359. {
  2360. std::string s = sin;
  2361. std::string s2 = s2in;
  2362. std::string unmodified = s;
  2363. unmodified += s2;
  2364. // if there is no restriction on the length of make variables
  2365. // and there are no "." charactors in the string, then return the
  2366. // unmodified combination.
  2367. if(!m_MakefileVariableSize && unmodified.find('.') == s.npos)
  2368. {
  2369. return unmodified;
  2370. }
  2371. // see if the variable has been defined before and return
  2372. // the modified version of the variable
  2373. std::map<cmStdString, cmStdString>::iterator i = m_MakeVariableMap.find(unmodified);
  2374. if(i != m_MakeVariableMap.end())
  2375. {
  2376. return i->second;
  2377. }
  2378. // start with the unmodified variable
  2379. std::string ret = unmodified;
  2380. // if this there is no value for m_MakefileVariableSize then
  2381. // the string must have bad characters in it
  2382. if(!m_MakefileVariableSize)
  2383. {
  2384. cmSystemTools::ReplaceString(ret, ".", "_");
  2385. int ni = 0;
  2386. char buffer[5];
  2387. // make sure the _ version is not already used, if
  2388. // it is used then add number to the end of the variable
  2389. while(m_ShortMakeVariableMap.count(ret) && ni < 1000)
  2390. {
  2391. ++ni;
  2392. sprintf(buffer, "%04d", ni);
  2393. ret = unmodified + buffer;
  2394. }
  2395. m_ShortMakeVariableMap[ret] = "1";
  2396. m_MakeVariableMap[unmodified] = ret;
  2397. return ret;
  2398. }
  2399. // if the string is greater the 32 chars it is an invalid vairable name
  2400. // for borland make
  2401. if(static_cast<int>(ret.size()) > m_MakefileVariableSize)
  2402. {
  2403. int keep = m_MakefileVariableSize - 8;
  2404. int size = keep + 3;
  2405. std::string str1 = s;
  2406. std::string str2 = s2;
  2407. // we must shorten the combined string by 4 charactors
  2408. // keep no more than 24 charactors from the second string
  2409. if(static_cast<int>(str2.size()) > keep)
  2410. {
  2411. str2 = str2.substr(0, keep);
  2412. }
  2413. if(static_cast<int>(str1.size()) + static_cast<int>(str2.size()) > size)
  2414. {
  2415. str1 = str1.substr(0, size - str2.size());
  2416. }
  2417. char buffer[5];
  2418. int ni = 0;
  2419. sprintf(buffer, "%04d", ni);
  2420. ret = str1 + str2 + buffer;
  2421. while(m_ShortMakeVariableMap.count(ret) && ni < 1000)
  2422. {
  2423. ++ni;
  2424. sprintf(buffer, "%04d", ni);
  2425. ret = str1 + str2 + buffer;
  2426. }
  2427. if(ni == 1000)
  2428. {
  2429. cmSystemTools::Error("Borland makefile variable length too long");
  2430. return unmodified;
  2431. }
  2432. // once an unused variable is found
  2433. m_ShortMakeVariableMap[ret] = "1";
  2434. }
  2435. // always make an entry into the unmodified to variable map
  2436. m_MakeVariableMap[unmodified] = ret;
  2437. return ret;
  2438. }
  2439. //============================================================================
  2440. //----------------------------------------------------------------------------
  2441. cmDepends*
  2442. cmLocalUnixMakefileGenerator3::GetDependsChecker(const std::string& lang,
  2443. const char* dir,
  2444. const char* objFile,
  2445. bool verbose)
  2446. {
  2447. if(lang == "C" || lang == "CXX" || lang == "RC")
  2448. {
  2449. return new cmDependsC(dir, objFile, verbose);
  2450. }
  2451. #ifdef CMAKE_BUILD_WITH_CMAKE
  2452. else if(lang == "Fortran")
  2453. {
  2454. return new cmDependsFortran(dir, objFile, verbose);
  2455. }
  2456. else if(lang == "Java")
  2457. {
  2458. return new cmDependsJava(dir, objFile, verbose);
  2459. }
  2460. #endif
  2461. return 0;
  2462. }
  2463. //----------------------------------------------------------------------------
  2464. bool
  2465. cmLocalUnixMakefileGenerator3
  2466. ::ScanDependencies(std::vector<std::string> const& args)
  2467. {
  2468. // Format of arguments is:
  2469. // $(CMAKE_COMMAND), cmake_depends, <lang>, <obj>, <src>
  2470. // The caller has ensured that all required arguments exist.
  2471. // The language for which we are scanning dependencies.
  2472. std::string const& lang = args[2];
  2473. // The file to which to write dependencies.
  2474. const char* objFile = args[3].c_str();
  2475. // The source file at which to start the scan.
  2476. const char* srcFile = args[4].c_str();
  2477. // Read the directory information file.
  2478. cmake cm;
  2479. cmGlobalGenerator gg;
  2480. gg.SetCMakeInstance(&cm);
  2481. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  2482. lg->SetGlobalGenerator(&gg);
  2483. cmMakefile* mf = lg->GetMakefile();
  2484. bool haveDirectoryInfo = false;
  2485. if(mf->ReadListFile(0, "CMakeDirectoryInformation.cmake") &&
  2486. !cmSystemTools::GetErrorOccuredFlag())
  2487. {
  2488. haveDirectoryInfo = true;
  2489. }
  2490. // Test whether we need to force Unix paths.
  2491. if(haveDirectoryInfo)
  2492. {
  2493. if(const char* force = mf->GetDefinition("CMAKE_FORCE_UNIX_PATHS"))
  2494. {
  2495. if(!cmSystemTools::IsOff(force))
  2496. {
  2497. cmSystemTools::SetForceUnixPaths(true);
  2498. }
  2499. }
  2500. }
  2501. // Get the set of include directories.
  2502. std::vector<std::string> includes;
  2503. if(haveDirectoryInfo)
  2504. {
  2505. std::string includePathVar = "CMAKE_";
  2506. includePathVar += lang;
  2507. includePathVar += "_INCLUDE_PATH";
  2508. if(const char* includePath = mf->GetDefinition(includePathVar.c_str()))
  2509. {
  2510. cmSystemTools::ExpandListArgument(includePath, includes);
  2511. }
  2512. }
  2513. // Get the include file regular expression.
  2514. std::string includeRegexScan = "^.*$";
  2515. std::string includeRegexComplain = "^$";
  2516. if(haveDirectoryInfo)
  2517. {
  2518. std::string scanRegexVar = "CMAKE_";
  2519. scanRegexVar += lang;
  2520. scanRegexVar += "_INCLUDE_REGEX_SCAN";
  2521. if(const char* scanRegex = mf->GetDefinition(scanRegexVar.c_str()))
  2522. {
  2523. includeRegexScan = scanRegex;
  2524. }
  2525. std::string complainRegexVar = "CMAKE_";
  2526. complainRegexVar += lang;
  2527. complainRegexVar += "_INCLUDE_REGEX_COMPLAIN";
  2528. if(const char* complainRegex = mf->GetDefinition(complainRegexVar.c_str()))
  2529. {
  2530. includeRegexComplain = complainRegex;
  2531. }
  2532. }
  2533. // Dispatch the scan for each language.
  2534. if(lang == "C" || lang == "CXX" || lang == "RC")
  2535. {
  2536. // TODO: Handle RC (resource files) dependencies correctly.
  2537. cmDependsC scanner(".", objFile, srcFile, includes,
  2538. includeRegexScan.c_str(), includeRegexComplain.c_str());
  2539. return scanner.Write();
  2540. }
  2541. #ifdef CMAKE_BUILD_WITH_CMAKE
  2542. else if(lang == "Fortran")
  2543. {
  2544. cmDependsFortran scanner(".", objFile, srcFile, includes);
  2545. return scanner.Write();
  2546. }
  2547. else if(lang == "Java")
  2548. {
  2549. cmDependsJava scanner(".", objFile, srcFile);
  2550. return scanner.Write();
  2551. }
  2552. #endif
  2553. return false;
  2554. }
  2555. //----------------------------------------------------------------------------
  2556. void cmLocalUnixMakefileGenerator3::CheckDependencies(cmMakefile* mf,
  2557. bool verbose)
  2558. {
  2559. // Get the list of languages that may have sources to check.
  2560. const char* langDef = mf->GetDefinition("CMAKE_DEPENDS_LANGUAGES");
  2561. if(!langDef)
  2562. {
  2563. return;
  2564. }
  2565. std::vector<std::string> languages;
  2566. cmSystemTools::ExpandListArgument(langDef, languages);
  2567. // For each language get the set of files to check.
  2568. for(std::vector<std::string>::iterator l = languages.begin();
  2569. l != languages.end(); ++l)
  2570. {
  2571. std::string depCheck = "CMAKE_DEPENDS_CHECK_";
  2572. depCheck += *l;
  2573. if(const char* fileDef = mf->GetDefinition(depCheck.c_str()))
  2574. {
  2575. // Check each file. The current working directory is already
  2576. // correct.
  2577. std::vector<std::string> files;
  2578. cmSystemTools::ExpandListArgument(fileDef, files);
  2579. for(std::vector<std::string>::iterator f = files.begin();
  2580. f != files.end(); ++f)
  2581. {
  2582. // Construct a checker for the given language.
  2583. std::auto_ptr<cmDepends>
  2584. checker(cmLocalUnixMakefileGenerator3
  2585. ::GetDependsChecker(*l, ".", f->c_str(), verbose));
  2586. if(checker.get())
  2587. {
  2588. checker->Check();
  2589. }
  2590. }
  2591. }
  2592. }
  2593. }