cmLocalUnixMakefileGenerator3.cxx 105 KB

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