cmLocalUnixMakefileGenerator3.cxx 104 KB

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