cmLocalUnixMakefileGenerator3.cxx 106 KB

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