cmLocalUnixMakefileGenerator3.cxx 101 KB

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