cmLocalGenerator.cxx 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmLocalGenerator.h"
  11. #include "cmComputeLinkInformation.h"
  12. #include "cmGeneratedFileStream.h"
  13. #include "cmGlobalGenerator.h"
  14. #include "cmInstallGenerator.h"
  15. #include "cmInstallFilesGenerator.h"
  16. #include "cmInstallScriptGenerator.h"
  17. #include "cmInstallTargetGenerator.h"
  18. #include "cmMakefile.h"
  19. #include "cmSourceFile.h"
  20. #include "cmTest.h"
  21. #include "cmTestGenerator.h"
  22. #include "cmVersion.h"
  23. #include "cmake.h"
  24. #if defined(CMAKE_BUILD_WITH_CMAKE)
  25. # define CM_LG_ENCODE_OBJECT_NAMES
  26. # include <cmsys/MD5.h>
  27. #endif
  28. #include <cmsys/System.h>
  29. #include <ctype.h> // for isalpha
  30. #include <assert.h>
  31. #if defined(__HAIKU__)
  32. #include <StorageKit.h>
  33. #endif
  34. cmLocalGenerator::cmLocalGenerator()
  35. {
  36. this->Makefile = 0; // moved to after set on global
  37. this->Parent = 0;
  38. this->WindowsShell = false;
  39. this->WindowsVSIDE = false;
  40. this->WatcomWMake = false;
  41. this->MinGWMake = false;
  42. this->NMake = false;
  43. this->MSYSShell = false;
  44. this->LinkScriptShell = false;
  45. this->IgnoreLibPrefix = false;
  46. this->UseRelativePaths = false;
  47. this->Configured = false;
  48. this->EmitUniversalBinaryFlags = true;
  49. this->IsMakefileGenerator = false;
  50. this->RelativePathsConfigured = false;
  51. this->PathConversionsSetup = false;
  52. this->BackwardsCompatibility = 0;
  53. this->BackwardsCompatibilityFinal = false;
  54. }
  55. cmLocalGenerator::~cmLocalGenerator()
  56. {
  57. delete this->Makefile;
  58. }
  59. //----------------------------------------------------------------------------
  60. class cmLocalGeneratorCurrent
  61. {
  62. cmGlobalGenerator* GG;
  63. cmLocalGenerator* LG;
  64. public:
  65. cmLocalGeneratorCurrent(cmLocalGenerator* lg)
  66. {
  67. this->GG = lg->GetGlobalGenerator();
  68. this->LG = this->GG->GetCurrentLocalGenerator();
  69. this->GG->SetCurrentLocalGenerator(lg);
  70. }
  71. ~cmLocalGeneratorCurrent()
  72. {
  73. this->GG->SetCurrentLocalGenerator(this->LG);
  74. }
  75. };
  76. //----------------------------------------------------------------------------
  77. void cmLocalGenerator::Configure()
  78. {
  79. // Manage the global generator's current local generator.
  80. cmLocalGeneratorCurrent clg(this);
  81. static_cast<void>(clg);
  82. // make sure the CMakeFiles dir is there
  83. std::string filesDir = this->Makefile->GetStartOutputDirectory();
  84. filesDir += cmake::GetCMakeFilesDirectory();
  85. cmSystemTools::MakeDirectory(filesDir.c_str());
  86. // find & read the list file
  87. this->ReadInputFile();
  88. // at the end of the ReadListFile handle any old style subdirs
  89. // first get all the subdirectories
  90. std::vector<cmLocalGenerator *> subdirs = this->GetChildren();
  91. // for each subdir recurse
  92. std::vector<cmLocalGenerator *>::iterator sdi = subdirs.begin();
  93. for (; sdi != subdirs.end(); ++sdi)
  94. {
  95. if (!(*sdi)->Configured)
  96. {
  97. this->Makefile->ConfigureSubDirectory(*sdi);
  98. }
  99. }
  100. // Check whether relative paths should be used for optionally
  101. // relative paths.
  102. this->UseRelativePaths = this->Makefile->IsOn("CMAKE_USE_RELATIVE_PATHS");
  103. this->ComputeObjectMaxPath();
  104. this->Configured = true;
  105. }
  106. //----------------------------------------------------------------------------
  107. void cmLocalGenerator::ComputeObjectMaxPath()
  108. {
  109. // Choose a maximum object file name length.
  110. #if defined(_WIN32) || defined(__CYGWIN__)
  111. this->ObjectPathMax = 250;
  112. #else
  113. this->ObjectPathMax = 1000;
  114. #endif
  115. const char* plen = this->Makefile->GetDefinition("CMAKE_OBJECT_PATH_MAX");
  116. if(plen && *plen)
  117. {
  118. unsigned int pmax;
  119. if(sscanf(plen, "%u", &pmax) == 1)
  120. {
  121. if(pmax >= 128)
  122. {
  123. this->ObjectPathMax = pmax;
  124. }
  125. else
  126. {
  127. cmOStringStream w;
  128. w << "CMAKE_OBJECT_PATH_MAX is set to " << pmax
  129. << ", which is less than the minimum of 128. "
  130. << "The value will be ignored.";
  131. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  132. }
  133. }
  134. else
  135. {
  136. cmOStringStream w;
  137. w << "CMAKE_OBJECT_PATH_MAX is set to \"" << plen
  138. << "\", which fails to parse as a positive integer. "
  139. << "The value will be ignored.";
  140. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  141. }
  142. }
  143. this->ObjectMaxPathViolations.clear();
  144. }
  145. //----------------------------------------------------------------------------
  146. void cmLocalGenerator::ReadInputFile()
  147. {
  148. // Look for the CMakeLists.txt file.
  149. std::string currentStart = this->Makefile->GetStartDirectory();
  150. currentStart += "/CMakeLists.txt";
  151. if(cmSystemTools::FileExists(currentStart.c_str(), true))
  152. {
  153. this->Makefile->ReadListFile(currentStart.c_str());
  154. return;
  155. }
  156. if(!this->Parent)
  157. {
  158. return;
  159. }
  160. // The file is missing. Check policy CMP0014.
  161. cmMakefile* mf = this->Parent->GetMakefile();
  162. cmOStringStream e;
  163. e << "The source directory\n"
  164. << " " << this->Makefile->GetStartDirectory() << "\n"
  165. << "does not contain a CMakeLists.txt file.";
  166. switch (mf->GetPolicyStatus(cmPolicies::CMP0014))
  167. {
  168. case cmPolicies::WARN:
  169. // Print the warning.
  170. e << "\n"
  171. << "CMake does not support this case but it used "
  172. << "to work accidentally and is being allowed for "
  173. << "compatibility."
  174. << "\n"
  175. << mf->GetPolicies()->GetPolicyWarning(cmPolicies::CMP0014);
  176. mf->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  177. case cmPolicies::OLD:
  178. // OLD behavior does not warn.
  179. return;
  180. case cmPolicies::REQUIRED_IF_USED:
  181. case cmPolicies::REQUIRED_ALWAYS:
  182. e << "\n"
  183. << mf->GetPolicies()->GetRequiredPolicyError(cmPolicies::CMP0014);
  184. case cmPolicies::NEW:
  185. // NEW behavior prints the error.
  186. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  187. break;
  188. }
  189. }
  190. void cmLocalGenerator::SetupPathConversions()
  191. {
  192. // Setup the current output directory components for use by
  193. // Convert
  194. std::string outdir;
  195. outdir =
  196. cmSystemTools::CollapseFullPath(this->Makefile->GetHomeDirectory());
  197. cmSystemTools::SplitPath(outdir.c_str(), this->HomeDirectoryComponents);
  198. outdir =
  199. cmSystemTools::CollapseFullPath(this->Makefile->GetStartDirectory());
  200. cmSystemTools::SplitPath(outdir.c_str(), this->StartDirectoryComponents);
  201. outdir = cmSystemTools::CollapseFullPath
  202. (this->Makefile->GetHomeOutputDirectory());
  203. cmSystemTools::SplitPath(outdir.c_str(),
  204. this->HomeOutputDirectoryComponents);
  205. outdir = cmSystemTools::CollapseFullPath
  206. (this->Makefile->GetStartOutputDirectory());
  207. cmSystemTools::SplitPath(outdir.c_str(),
  208. this->StartOutputDirectoryComponents);
  209. }
  210. void cmLocalGenerator::SetGlobalGenerator(cmGlobalGenerator *gg)
  211. {
  212. this->GlobalGenerator = gg;
  213. this->Makefile = new cmMakefile;
  214. this->Makefile->SetLocalGenerator(this);
  215. // setup the home directories
  216. this->Makefile->GetProperties().SetCMakeInstance(gg->GetCMakeInstance());
  217. this->Makefile->SetHomeDirectory(
  218. gg->GetCMakeInstance()->GetHomeDirectory());
  219. this->Makefile->SetHomeOutputDirectory(
  220. gg->GetCMakeInstance()->GetHomeOutputDirectory());
  221. }
  222. void cmLocalGenerator::ConfigureFinalPass()
  223. {
  224. this->Makefile->ConfigureFinalPass();
  225. }
  226. void cmLocalGenerator::TraceDependencies()
  227. {
  228. // Generate the rule files for each target.
  229. cmTargets& targets = this->Makefile->GetTargets();
  230. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  231. {
  232. const char* projectFilename = 0;
  233. if (this->IsMakefileGenerator == false) // only use of this variable
  234. {
  235. projectFilename = t->second.GetName();
  236. }
  237. t->second.TraceDependencies(projectFilename);
  238. }
  239. }
  240. void cmLocalGenerator::GenerateTestFiles()
  241. {
  242. if ( !this->Makefile->IsOn("CMAKE_TESTING_ENABLED") )
  243. {
  244. return;
  245. }
  246. // Compute the set of configurations.
  247. std::vector<std::string> configurationTypes;
  248. const char* config =
  249. this->Makefile->GetConfigurations(configurationTypes, false);
  250. std::string file = this->Makefile->GetStartOutputDirectory();
  251. file += "/";
  252. file += "CTestTestfile.cmake";
  253. cmGeneratedFileStream fout(file.c_str());
  254. fout.SetCopyIfDifferent(true);
  255. fout << "# CMake generated Testfile for " << std::endl
  256. << "# Source directory: "
  257. << this->Makefile->GetStartDirectory() << std::endl
  258. << "# Build directory: "
  259. << this->Makefile->GetStartOutputDirectory() << std::endl
  260. << "# " << std::endl
  261. << "# This file includes the relevant testing commands "
  262. << "required for " << std::endl
  263. << "# testing this directory and lists subdirectories to "
  264. << "be tested as well." << std::endl;
  265. const char* testIncludeFile =
  266. this->Makefile->GetProperty("TEST_INCLUDE_FILE");
  267. if ( testIncludeFile )
  268. {
  269. fout << "INCLUDE(\"" << testIncludeFile << "\")" << std::endl;
  270. }
  271. // Ask each test generator to write its code.
  272. std::vector<cmTestGenerator*> const&
  273. testers = this->Makefile->GetTestGenerators();
  274. for(std::vector<cmTestGenerator*>::const_iterator gi = testers.begin();
  275. gi != testers.end(); ++gi)
  276. {
  277. (*gi)->Generate(fout, config, configurationTypes);
  278. }
  279. if ( this->Children.size())
  280. {
  281. size_t i;
  282. for(i = 0; i < this->Children.size(); ++i)
  283. {
  284. fout << "SUBDIRS(";
  285. std::string outP =
  286. this->Children[i]->GetMakefile()->GetStartOutputDirectory();
  287. fout << this->Convert(outP.c_str(),START_OUTPUT);
  288. fout << ")" << std::endl;
  289. }
  290. }
  291. }
  292. //----------------------------------------------------------------------------
  293. void cmLocalGenerator::GenerateInstallRules()
  294. {
  295. // Compute the install prefix.
  296. const char* prefix = this->Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  297. #if defined(_WIN32) && !defined(__CYGWIN__)
  298. std::string prefix_win32;
  299. if(!prefix)
  300. {
  301. if(!cmSystemTools::GetEnv("SystemDrive", prefix_win32))
  302. {
  303. prefix_win32 = "C:";
  304. }
  305. const char* project_name = this->Makefile->GetDefinition("PROJECT_NAME");
  306. if(project_name && project_name[0])
  307. {
  308. prefix_win32 += "/Program Files/";
  309. prefix_win32 += project_name;
  310. }
  311. else
  312. {
  313. prefix_win32 += "/InstalledCMakeProject";
  314. }
  315. prefix = prefix_win32.c_str();
  316. }
  317. #elif defined(__HAIKU__)
  318. if (!prefix)
  319. {
  320. BPath dir;
  321. if (find_directory(B_COMMON_DIRECTORY, &dir) == B_OK)
  322. {
  323. prefix = dir.Path();
  324. }
  325. else
  326. {
  327. prefix = "/boot/common";
  328. }
  329. }
  330. #else
  331. if (!prefix)
  332. {
  333. prefix = "/usr/local";
  334. }
  335. #endif
  336. // Compute the set of configurations.
  337. std::vector<std::string> configurationTypes;
  338. const char* config =
  339. this->Makefile->GetConfigurations(configurationTypes, false);
  340. // Choose a default install configuration.
  341. const char* default_config = config;
  342. const char* default_order[] = {"RELEASE", "MINSIZEREL",
  343. "RELWITHDEBINFO", "DEBUG", 0};
  344. for(const char** c = default_order; *c && !default_config; ++c)
  345. {
  346. for(std::vector<std::string>::iterator i = configurationTypes.begin();
  347. i != configurationTypes.end(); ++i)
  348. {
  349. if(cmSystemTools::UpperCase(*i) == *c)
  350. {
  351. default_config = i->c_str();
  352. }
  353. }
  354. }
  355. if(!default_config && !configurationTypes.empty())
  356. {
  357. default_config = configurationTypes[0].c_str();
  358. }
  359. if(!default_config)
  360. {
  361. default_config = "Release";
  362. }
  363. // Create the install script file.
  364. std::string file = this->Makefile->GetStartOutputDirectory();
  365. std::string homedir = this->Makefile->GetHomeOutputDirectory();
  366. std::string currdir = this->Makefile->GetCurrentOutputDirectory();
  367. cmSystemTools::ConvertToUnixSlashes(file);
  368. cmSystemTools::ConvertToUnixSlashes(homedir);
  369. cmSystemTools::ConvertToUnixSlashes(currdir);
  370. int toplevel_install = 0;
  371. if ( currdir == homedir )
  372. {
  373. toplevel_install = 1;
  374. }
  375. file += "/cmake_install.cmake";
  376. cmGeneratedFileStream fout(file.c_str());
  377. fout.SetCopyIfDifferent(true);
  378. // Write the header.
  379. fout << "# Install script for directory: "
  380. << this->Makefile->GetCurrentDirectory() << std::endl << std::endl;
  381. fout << "# Set the install prefix" << std::endl
  382. << "IF(NOT DEFINED CMAKE_INSTALL_PREFIX)" << std::endl
  383. << " SET(CMAKE_INSTALL_PREFIX \"" << prefix << "\")" << std::endl
  384. << "ENDIF(NOT DEFINED CMAKE_INSTALL_PREFIX)" << std::endl
  385. << "STRING(REGEX REPLACE \"/$\" \"\" CMAKE_INSTALL_PREFIX "
  386. << "\"${CMAKE_INSTALL_PREFIX}\")" << std::endl
  387. << std::endl;
  388. // Write support code for generating per-configuration install rules.
  389. fout <<
  390. "# Set the install configuration name.\n"
  391. "IF(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)\n"
  392. " IF(BUILD_TYPE)\n"
  393. " STRING(REGEX REPLACE \"^[^A-Za-z0-9_]+\" \"\"\n"
  394. " CMAKE_INSTALL_CONFIG_NAME \"${BUILD_TYPE}\")\n"
  395. " ELSE(BUILD_TYPE)\n"
  396. " SET(CMAKE_INSTALL_CONFIG_NAME \"" << default_config << "\")\n"
  397. " ENDIF(BUILD_TYPE)\n"
  398. " MESSAGE(STATUS \"Install configuration: "
  399. "\\\"${CMAKE_INSTALL_CONFIG_NAME}\\\"\")\n"
  400. "ENDIF(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)\n"
  401. "\n";
  402. // Write support code for dealing with component-specific installs.
  403. fout <<
  404. "# Set the component getting installed.\n"
  405. "IF(NOT CMAKE_INSTALL_COMPONENT)\n"
  406. " IF(COMPONENT)\n"
  407. " MESSAGE(STATUS \"Install component: \\\"${COMPONENT}\\\"\")\n"
  408. " SET(CMAKE_INSTALL_COMPONENT \"${COMPONENT}\")\n"
  409. " ELSE(COMPONENT)\n"
  410. " SET(CMAKE_INSTALL_COMPONENT)\n"
  411. " ENDIF(COMPONENT)\n"
  412. "ENDIF(NOT CMAKE_INSTALL_COMPONENT)\n"
  413. "\n";
  414. // Copy user-specified install options to the install code.
  415. if(const char* so_no_exe =
  416. this->Makefile->GetDefinition("CMAKE_INSTALL_SO_NO_EXE"))
  417. {
  418. fout <<
  419. "# Install shared libraries without execute permission?\n"
  420. "IF(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)\n"
  421. " SET(CMAKE_INSTALL_SO_NO_EXE \"" << so_no_exe << "\")\n"
  422. "ENDIF(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)\n"
  423. "\n";
  424. }
  425. // Ask each install generator to write its code.
  426. std::vector<cmInstallGenerator*> const& installers =
  427. this->Makefile->GetInstallGenerators();
  428. for(std::vector<cmInstallGenerator*>::const_iterator
  429. gi = installers.begin();
  430. gi != installers.end(); ++gi)
  431. {
  432. (*gi)->Generate(fout, config, configurationTypes);
  433. }
  434. // Write rules from old-style specification stored in targets.
  435. this->GenerateTargetInstallRules(fout, config, configurationTypes);
  436. // Include install scripts from subdirectories.
  437. if(!this->Children.empty())
  438. {
  439. fout << "IF(NOT CMAKE_INSTALL_LOCAL_ONLY)\n";
  440. fout << " # Include the install script for each subdirectory.\n";
  441. for(std::vector<cmLocalGenerator*>::const_iterator
  442. ci = this->Children.begin(); ci != this->Children.end(); ++ci)
  443. {
  444. if(!(*ci)->GetMakefile()->GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  445. {
  446. std::string odir = (*ci)->GetMakefile()->GetStartOutputDirectory();
  447. cmSystemTools::ConvertToUnixSlashes(odir);
  448. fout << " INCLUDE(\"" << odir.c_str()
  449. << "/cmake_install.cmake\")" << std::endl;
  450. }
  451. }
  452. fout << "\n";
  453. fout << "ENDIF(NOT CMAKE_INSTALL_LOCAL_ONLY)\n\n";
  454. }
  455. // Record the install manifest.
  456. if ( toplevel_install )
  457. {
  458. fout <<
  459. "IF(CMAKE_INSTALL_COMPONENT)\n"
  460. " SET(CMAKE_INSTALL_MANIFEST \"install_manifest_"
  461. "${CMAKE_INSTALL_COMPONENT}.txt\")\n"
  462. "ELSE(CMAKE_INSTALL_COMPONENT)\n"
  463. " SET(CMAKE_INSTALL_MANIFEST \"install_manifest.txt\")\n"
  464. "ENDIF(CMAKE_INSTALL_COMPONENT)\n\n";
  465. fout
  466. << "FILE(WRITE \""
  467. << homedir.c_str() << "/${CMAKE_INSTALL_MANIFEST}\" "
  468. << "\"\")" << std::endl;
  469. fout
  470. << "FOREACH(file ${CMAKE_INSTALL_MANIFEST_FILES})" << std::endl
  471. << " FILE(APPEND \""
  472. << homedir.c_str() << "/${CMAKE_INSTALL_MANIFEST}\" "
  473. << "\"${file}\\n\")" << std::endl
  474. << "ENDFOREACH(file)" << std::endl;
  475. }
  476. }
  477. //----------------------------------------------------------------------------
  478. void cmLocalGenerator::GenerateTargetManifest()
  479. {
  480. // Collect the set of configuration types.
  481. std::vector<std::string> configNames;
  482. this->Makefile->GetConfigurations(configNames);
  483. // Add our targets to the manifest for each configuration.
  484. cmTargets& targets = this->Makefile->GetTargets();
  485. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  486. {
  487. cmTarget& target = t->second;
  488. if(configNames.empty())
  489. {
  490. target.GenerateTargetManifest(0);
  491. }
  492. else
  493. {
  494. for(std::vector<std::string>::iterator ci = configNames.begin();
  495. ci != configNames.end(); ++ci)
  496. {
  497. const char* config = ci->c_str();
  498. target.GenerateTargetManifest(config);
  499. }
  500. }
  501. }
  502. }
  503. void cmLocalGenerator::AddCustomCommandToCreateObject(const char* ofname,
  504. const char* lang,
  505. cmSourceFile& source,
  506. cmGeneratorTarget& target)
  507. {
  508. std::string objectDir = cmSystemTools::GetFilenamePath(std::string(ofname));
  509. objectDir = this->Convert(objectDir.c_str(),START_OUTPUT,SHELL);
  510. std::string objectFile = this->Convert(ofname,START_OUTPUT,SHELL);
  511. std::string sourceFile =
  512. this->Convert(source.GetFullPath().c_str(),START_OUTPUT,SHELL,true);
  513. std::string varString = "CMAKE_";
  514. varString += lang;
  515. varString += "_COMPILE_OBJECT";
  516. std::vector<std::string> rules;
  517. rules.push_back(this->Makefile->GetRequiredDefinition(varString.c_str()));
  518. varString = "CMAKE_";
  519. varString += lang;
  520. varString += "_FLAGS";
  521. std::string flags;
  522. flags += this->Makefile->GetSafeDefinition(varString.c_str());
  523. flags += " ";
  524. {
  525. std::vector<std::string> includes;
  526. this->GetIncludeDirectories(includes, &target, lang);
  527. flags += this->GetIncludeFlags(includes, lang);
  528. }
  529. flags += this->Makefile->GetDefineFlags();
  530. // Construct the command lines.
  531. cmCustomCommandLines commandLines;
  532. std::vector<std::string> commands;
  533. cmSystemTools::ExpandList(rules, commands);
  534. cmLocalGenerator::RuleVariables vars;
  535. vars.Language = lang;
  536. vars.Source = sourceFile.c_str();
  537. vars.Object = objectFile.c_str();
  538. vars.ObjectDir = objectDir.c_str();
  539. vars.Flags = flags.c_str();
  540. for(std::vector<std::string>::iterator i = commands.begin();
  541. i != commands.end(); ++i)
  542. {
  543. // Expand the full command line string.
  544. this->ExpandRuleVariables(*i, vars);
  545. // Parse the string to get the custom command line.
  546. cmCustomCommandLine commandLine;
  547. std::vector<cmStdString> cmd = cmSystemTools::ParseArguments(i->c_str());
  548. for(std::vector<cmStdString>::iterator a = cmd.begin();
  549. a != cmd.end(); ++a)
  550. {
  551. commandLine.push_back(*a);
  552. }
  553. // Store this command line.
  554. commandLines.push_back(commandLine);
  555. }
  556. // Check for extra object-file dependencies.
  557. std::vector<std::string> depends;
  558. const char* additionalDeps = source.GetProperty("OBJECT_DEPENDS");
  559. if(additionalDeps)
  560. {
  561. cmSystemTools::ExpandListArgument(additionalDeps, depends);
  562. }
  563. // Generate a meaningful comment for the command.
  564. std::string comment = "Building ";
  565. comment += lang;
  566. comment += " object ";
  567. comment += this->Convert(ofname, START_OUTPUT);
  568. // Add the custom command to build the object file.
  569. this->Makefile->AddCustomCommandToOutput(
  570. ofname,
  571. depends,
  572. source.GetFullPath().c_str(),
  573. commandLines,
  574. comment.c_str(),
  575. this->Makefile->GetStartOutputDirectory()
  576. );
  577. }
  578. void cmLocalGenerator::AddBuildTargetRule(const char* llang,
  579. cmGeneratorTarget& target)
  580. {
  581. cmStdString objs;
  582. std::vector<std::string> objVector;
  583. // Add all the sources outputs to the depends of the target
  584. std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
  585. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  586. i != classes.end(); ++i)
  587. {
  588. cmSourceFile* sf = *i;
  589. if(!sf->GetCustomCommand() &&
  590. !sf->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  591. !sf->GetPropertyAsBool("EXTERNAL_OBJECT"))
  592. {
  593. std::string dir_max;
  594. dir_max += this->Makefile->GetCurrentOutputDirectory();
  595. dir_max += "/";
  596. std::string obj = this->GetObjectFileNameWithoutTarget(*sf, dir_max);
  597. if(!obj.empty())
  598. {
  599. std::string ofname = this->Makefile->GetCurrentOutputDirectory();
  600. ofname += "/";
  601. ofname += obj;
  602. objVector.push_back(ofname);
  603. this->AddCustomCommandToCreateObject(ofname.c_str(),
  604. llang, *(*i), target);
  605. objs += this->Convert(ofname.c_str(),START_OUTPUT,MAKEFILE);
  606. objs += " ";
  607. }
  608. }
  609. }
  610. std::string createRule = "CMAKE_";
  611. createRule += llang;
  612. createRule += target.GetCreateRuleVariable();
  613. std::string targetName = target.Target->GetFullName();
  614. // Executable :
  615. // Shared Library:
  616. // Static Library:
  617. // Shared Module:
  618. std::string linkLibs; // should be set
  619. std::string frameworkPath;
  620. std::string linkPath;
  621. std::string flags; // should be set
  622. std::string linkFlags; // should be set
  623. this->GetTargetFlags(linkLibs, frameworkPath, linkPath, flags, linkFlags,
  624. &target);
  625. linkLibs = frameworkPath + linkPath + linkLibs;
  626. cmLocalGenerator::RuleVariables vars;
  627. vars.Language = llang;
  628. vars.Objects = objs.c_str();
  629. vars.ObjectDir = ".";
  630. vars.Target = targetName.c_str();
  631. vars.LinkLibraries = linkLibs.c_str();
  632. vars.Flags = flags.c_str();
  633. vars.LinkFlags = linkFlags.c_str();
  634. std::string langFlags;
  635. this->AddLanguageFlags(langFlags, llang, 0);
  636. this->AddArchitectureFlags(langFlags, &target, llang, 0);
  637. vars.LanguageCompileFlags = langFlags.c_str();
  638. cmCustomCommandLines commandLines;
  639. std::vector<std::string> rules;
  640. rules.push_back(this->Makefile->GetRequiredDefinition(createRule.c_str()));
  641. std::vector<std::string> commands;
  642. cmSystemTools::ExpandList(rules, commands);
  643. for(std::vector<std::string>::iterator i = commands.begin();
  644. i != commands.end(); ++i)
  645. {
  646. // Expand the full command line string.
  647. this->ExpandRuleVariables(*i, vars);
  648. // Parse the string to get the custom command line.
  649. cmCustomCommandLine commandLine;
  650. std::vector<cmStdString> cmd = cmSystemTools::ParseArguments(i->c_str());
  651. for(std::vector<cmStdString>::iterator a = cmd.begin();
  652. a != cmd.end(); ++a)
  653. {
  654. commandLine.push_back(*a);
  655. }
  656. // Store this command line.
  657. commandLines.push_back(commandLine);
  658. }
  659. std::string targetFullPath = target.Target->GetFullPath();
  660. // Generate a meaningful comment for the command.
  661. std::string comment = "Linking ";
  662. comment += llang;
  663. comment += " target ";
  664. comment += this->Convert(targetFullPath.c_str(), START_OUTPUT);
  665. this->Makefile->AddCustomCommandToOutput(
  666. targetFullPath.c_str(),
  667. objVector,
  668. 0,
  669. commandLines,
  670. comment.c_str(),
  671. this->Makefile->GetStartOutputDirectory()
  672. );
  673. target.Target->AddSourceFile
  674. (this->Makefile->GetSource(targetFullPath.c_str()));
  675. }
  676. void cmLocalGenerator
  677. ::CreateCustomTargetsAndCommands(std::set<cmStdString> const& lang)
  678. {
  679. cmGeneratorTargetsType tgts = this->Makefile->GetGeneratorTargets();
  680. for(cmGeneratorTargetsType::iterator l = tgts.begin();
  681. l != tgts.end(); l++)
  682. {
  683. if (l->first->IsImported())
  684. {
  685. continue;
  686. }
  687. cmGeneratorTarget& target = *l->second;
  688. switch(target.GetType())
  689. {
  690. case cmTarget::STATIC_LIBRARY:
  691. case cmTarget::SHARED_LIBRARY:
  692. case cmTarget::MODULE_LIBRARY:
  693. case cmTarget::EXECUTABLE:
  694. {
  695. const char* llang = target.Target->GetLinkerLanguage();
  696. if(!llang)
  697. {
  698. cmSystemTools::Error
  699. ("CMake can not determine linker language for target: ",
  700. target.Target->GetName());
  701. return;
  702. }
  703. // if the language is not in the set lang then create custom
  704. // commands to build the target
  705. if(lang.count(llang) == 0)
  706. {
  707. this->AddBuildTargetRule(llang, target);
  708. }
  709. }
  710. break;
  711. default:
  712. break;
  713. }
  714. }
  715. }
  716. // List of variables that are replaced when
  717. // rules are expanced. These variables are
  718. // replaced in the form <var> with GetSafeDefinition(var).
  719. // ${LANG} is replaced in the variable first with all enabled
  720. // languages.
  721. static const char* ruleReplaceVars[] =
  722. {
  723. "CMAKE_${LANG}_COMPILER",
  724. "CMAKE_SHARED_LIBRARY_CREATE_${LANG}_FLAGS",
  725. "CMAKE_SHARED_MODULE_CREATE_${LANG}_FLAGS",
  726. "CMAKE_SHARED_MODULE_${LANG}_FLAGS",
  727. "CMAKE_SHARED_LIBRARY_${LANG}_FLAGS",
  728. "CMAKE_${LANG}_LINK_FLAGS",
  729. "CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG",
  730. "CMAKE_${LANG}_ARCHIVE",
  731. "CMAKE_AR",
  732. "CMAKE_CURRENT_SOURCE_DIR",
  733. "CMAKE_CURRENT_BINARY_DIR",
  734. "CMAKE_RANLIB",
  735. "CMAKE_LINKER",
  736. 0
  737. };
  738. std::string
  739. cmLocalGenerator::ExpandRuleVariable(std::string const& variable,
  740. const RuleVariables& replaceValues)
  741. {
  742. if(replaceValues.LinkFlags)
  743. {
  744. if(variable == "LINK_FLAGS")
  745. {
  746. return replaceValues.LinkFlags;
  747. }
  748. }
  749. if(replaceValues.Flags)
  750. {
  751. if(variable == "FLAGS")
  752. {
  753. return replaceValues.Flags;
  754. }
  755. }
  756. if(replaceValues.Source)
  757. {
  758. if(variable == "SOURCE")
  759. {
  760. return replaceValues.Source;
  761. }
  762. }
  763. if(replaceValues.PreprocessedSource)
  764. {
  765. if(variable == "PREPROCESSED_SOURCE")
  766. {
  767. return replaceValues.PreprocessedSource;
  768. }
  769. }
  770. if(replaceValues.AssemblySource)
  771. {
  772. if(variable == "ASSEMBLY_SOURCE")
  773. {
  774. return replaceValues.AssemblySource;
  775. }
  776. }
  777. if(replaceValues.Object)
  778. {
  779. if(variable == "OBJECT")
  780. {
  781. return replaceValues.Object;
  782. }
  783. }
  784. if(replaceValues.ObjectDir)
  785. {
  786. if(variable == "OBJECT_DIR")
  787. {
  788. return replaceValues.ObjectDir;
  789. }
  790. }
  791. if(replaceValues.Objects)
  792. {
  793. if(variable == "OBJECTS")
  794. {
  795. return replaceValues.Objects;
  796. }
  797. }
  798. if(replaceValues.ObjectsQuoted)
  799. {
  800. if(variable == "OBJECTS_QUOTED")
  801. {
  802. return replaceValues.ObjectsQuoted;
  803. }
  804. }
  805. if(replaceValues.Defines && variable == "DEFINES")
  806. {
  807. return replaceValues.Defines;
  808. }
  809. if(replaceValues.TargetPDB )
  810. {
  811. if(variable == "TARGET_PDB")
  812. {
  813. return replaceValues.TargetPDB;
  814. }
  815. }
  816. if(replaceValues.DependencyFile )
  817. {
  818. if(variable == "DEP_FILE")
  819. {
  820. return replaceValues.DependencyFile;
  821. }
  822. }
  823. if(replaceValues.Target)
  824. {
  825. if(variable == "TARGET_QUOTED")
  826. {
  827. std::string targetQuoted = replaceValues.Target;
  828. if(targetQuoted.size() && targetQuoted[0] != '\"')
  829. {
  830. targetQuoted = '\"';
  831. targetQuoted += replaceValues.Target;
  832. targetQuoted += '\"';
  833. }
  834. return targetQuoted;
  835. }
  836. if(variable == "TARGET_UNQUOTED")
  837. {
  838. std::string unquoted = replaceValues.Target;
  839. std::string::size_type sz = unquoted.size();
  840. if(sz > 2 && unquoted[0] == '\"' && unquoted[sz-1] == '\"')
  841. {
  842. unquoted = unquoted.substr(1, sz-2);
  843. }
  844. return unquoted;
  845. }
  846. if(replaceValues.LanguageCompileFlags)
  847. {
  848. if(variable == "LANGUAGE_COMPILE_FLAGS")
  849. {
  850. return replaceValues.LanguageCompileFlags;
  851. }
  852. }
  853. if(replaceValues.Target)
  854. {
  855. if(variable == "TARGET")
  856. {
  857. return replaceValues.Target;
  858. }
  859. }
  860. if(variable == "TARGET_IMPLIB")
  861. {
  862. return this->TargetImplib;
  863. }
  864. if(variable == "TARGET_VERSION_MAJOR")
  865. {
  866. if(replaceValues.TargetVersionMajor)
  867. {
  868. return replaceValues.TargetVersionMajor;
  869. }
  870. else
  871. {
  872. return "0";
  873. }
  874. }
  875. if(variable == "TARGET_VERSION_MINOR")
  876. {
  877. if(replaceValues.TargetVersionMinor)
  878. {
  879. return replaceValues.TargetVersionMinor;
  880. }
  881. else
  882. {
  883. return "0";
  884. }
  885. }
  886. if(replaceValues.Target)
  887. {
  888. if(variable == "TARGET_BASE")
  889. {
  890. // Strip the last extension off the target name.
  891. std::string targetBase = replaceValues.Target;
  892. std::string::size_type pos = targetBase.rfind(".");
  893. if(pos != targetBase.npos)
  894. {
  895. return targetBase.substr(0, pos);
  896. }
  897. else
  898. {
  899. return targetBase;
  900. }
  901. }
  902. }
  903. }
  904. if(variable == "TARGET_SONAME" || variable == "SONAME_FLAG" ||
  905. variable == "TARGET_INSTALLNAME_DIR")
  906. {
  907. // All these variables depend on TargetSOName
  908. if(replaceValues.TargetSOName)
  909. {
  910. if(variable == "TARGET_SONAME")
  911. {
  912. return replaceValues.TargetSOName;
  913. }
  914. if(variable == "SONAME_FLAG" && replaceValues.SONameFlag)
  915. {
  916. return replaceValues.SONameFlag;
  917. }
  918. if(replaceValues.TargetInstallNameDir &&
  919. variable == "TARGET_INSTALLNAME_DIR")
  920. {
  921. return replaceValues.TargetInstallNameDir;
  922. }
  923. }
  924. return "";
  925. }
  926. if(replaceValues.LinkLibraries)
  927. {
  928. if(variable == "LINK_LIBRARIES")
  929. {
  930. return replaceValues.LinkLibraries;
  931. }
  932. }
  933. if(replaceValues.Language)
  934. {
  935. if(variable == "LANGUAGE")
  936. {
  937. return replaceValues.Language;
  938. }
  939. }
  940. if(replaceValues.CMTarget)
  941. {
  942. if(variable == "TARGET_NAME")
  943. {
  944. return replaceValues.CMTarget->GetName();
  945. }
  946. if(variable == "TARGET_TYPE")
  947. {
  948. return cmTarget::GetTargetTypeName(replaceValues.CMTarget->GetType());
  949. }
  950. }
  951. if(replaceValues.Output)
  952. {
  953. if(variable == "OUTPUT")
  954. {
  955. return replaceValues.Output;
  956. }
  957. }
  958. if(variable == "CMAKE_COMMAND")
  959. {
  960. const char* cmcommand =
  961. this->GetMakefile()->GetDefinition("CMAKE_COMMAND");
  962. return this->Convert(cmcommand, FULL, SHELL);
  963. }
  964. std::vector<std::string> enabledLanguages;
  965. this->GlobalGenerator->GetEnabledLanguages(enabledLanguages);
  966. // loop over language specific replace variables
  967. int pos = 0;
  968. while(ruleReplaceVars[pos])
  969. {
  970. for(std::vector<std::string>::iterator i = enabledLanguages.begin();
  971. i != enabledLanguages.end(); ++i)
  972. {
  973. const char* lang = i->c_str();
  974. std::string actualReplace = ruleReplaceVars[pos];
  975. // If this is the compiler then look for the extra variable
  976. // _COMPILER_ARG1 which must be the first argument to the compiler
  977. const char* compilerArg1 = 0;
  978. const char* compilerTarget = 0;
  979. const char* compilerOptionTarget = 0;
  980. if(actualReplace == "CMAKE_${LANG}_COMPILER")
  981. {
  982. std::string arg1 = actualReplace + "_ARG1";
  983. cmSystemTools::ReplaceString(arg1, "${LANG}", lang);
  984. compilerArg1 = this->Makefile->GetDefinition(arg1.c_str());
  985. compilerTarget
  986. = this->Makefile->GetDefinition(
  987. (std::string("CMAKE_") + lang + "_COMPILER_TARGET").c_str());
  988. compilerOptionTarget
  989. = this->Makefile->GetDefinition(
  990. (std::string("CMAKE_") + lang +
  991. "_COMPILE_OPTION_TARGET").c_str());
  992. }
  993. if(actualReplace.find("${LANG}") != actualReplace.npos)
  994. {
  995. cmSystemTools::ReplaceString(actualReplace, "${LANG}", lang);
  996. }
  997. if(actualReplace == variable)
  998. {
  999. std::string replace =
  1000. this->Makefile->GetSafeDefinition(variable.c_str());
  1001. // if the variable is not a FLAG then treat it like a path
  1002. if(variable.find("_FLAG") == variable.npos)
  1003. {
  1004. std::string ret = this->ConvertToOutputForExisting(replace.c_str());
  1005. // if there is a required first argument to the compiler add it
  1006. // to the compiler string
  1007. if(compilerArg1)
  1008. {
  1009. ret += " ";
  1010. ret += compilerArg1;
  1011. }
  1012. if (compilerTarget && compilerOptionTarget)
  1013. {
  1014. ret += compilerOptionTarget;
  1015. ret += compilerTarget;
  1016. }
  1017. return ret;
  1018. }
  1019. return replace;
  1020. }
  1021. }
  1022. pos++;
  1023. }
  1024. return variable;
  1025. }
  1026. void
  1027. cmLocalGenerator::ExpandRuleVariables(std::string& s,
  1028. const RuleVariables& replaceValues)
  1029. {
  1030. this->InsertRuleLauncher(s, replaceValues.CMTarget,
  1031. replaceValues.RuleLauncher);
  1032. std::string::size_type start = s.find('<');
  1033. // no variables to expand
  1034. if(start == s.npos)
  1035. {
  1036. return;
  1037. }
  1038. std::string::size_type pos = 0;
  1039. std::string expandedInput;
  1040. while(start != s.npos && start < s.size()-2)
  1041. {
  1042. std::string::size_type end = s.find('>', start);
  1043. // if we find a < with no > we are done
  1044. if(end == s.npos)
  1045. {
  1046. return;
  1047. }
  1048. char c = s[start+1];
  1049. // if the next char after the < is not A-Za-z then
  1050. // skip it and try to find the next < in the string
  1051. if(!isalpha(c))
  1052. {
  1053. start = s.find('<', start+1);
  1054. }
  1055. else
  1056. {
  1057. // extract the var
  1058. std::string var = s.substr(start+1, end - start-1);
  1059. std::string replace = this->ExpandRuleVariable(var,
  1060. replaceValues);
  1061. expandedInput += s.substr(pos, start-pos);
  1062. expandedInput += replace;
  1063. // move to next one
  1064. start = s.find('<', start+var.size()+2);
  1065. pos = end+1;
  1066. }
  1067. }
  1068. // add the rest of the input
  1069. expandedInput += s.substr(pos, s.size()-pos);
  1070. s = expandedInput;
  1071. }
  1072. //----------------------------------------------------------------------------
  1073. const char* cmLocalGenerator::GetRuleLauncher(cmTarget* target,
  1074. const char* prop)
  1075. {
  1076. if(target)
  1077. {
  1078. return target->GetProperty(prop);
  1079. }
  1080. else
  1081. {
  1082. return this->Makefile->GetProperty(prop);
  1083. }
  1084. }
  1085. //----------------------------------------------------------------------------
  1086. void cmLocalGenerator::InsertRuleLauncher(std::string& s, cmTarget* target,
  1087. const char* prop)
  1088. {
  1089. if(const char* val = this->GetRuleLauncher(target, prop))
  1090. {
  1091. cmOStringStream wrapped;
  1092. wrapped << val << " " << s;
  1093. s = wrapped.str();
  1094. }
  1095. }
  1096. //----------------------------------------------------------------------------
  1097. std::string
  1098. cmLocalGenerator::ConvertToOutputForExistingCommon(const char* remote,
  1099. std::string const& result)
  1100. {
  1101. // If this is a windows shell, the result has a space, and the path
  1102. // already exists, we can use a short-path to reference it without a
  1103. // space.
  1104. if(this->WindowsShell && result.find(' ') != result.npos &&
  1105. cmSystemTools::FileExists(remote))
  1106. {
  1107. std::string tmp;
  1108. if(cmSystemTools::GetShortPath(remote, tmp))
  1109. {
  1110. return this->Convert(tmp.c_str(), NONE, SHELL, true);
  1111. }
  1112. }
  1113. // Otherwise, leave it unchanged.
  1114. return result;
  1115. }
  1116. //----------------------------------------------------------------------------
  1117. std::string
  1118. cmLocalGenerator::ConvertToOutputForExisting(const char* remote,
  1119. RelativeRoot local)
  1120. {
  1121. // Perform standard conversion.
  1122. std::string result = this->Convert(remote, local, SHELL, true);
  1123. // Consider short-path.
  1124. return this->ConvertToOutputForExistingCommon(remote, result);
  1125. }
  1126. //----------------------------------------------------------------------------
  1127. std::string
  1128. cmLocalGenerator::ConvertToOutputForExisting(RelativeRoot remote,
  1129. const char* local)
  1130. {
  1131. // Perform standard conversion.
  1132. std::string result = this->Convert(remote, local, SHELL, true);
  1133. // Consider short-path.
  1134. const char* remotePath = this->GetRelativeRootPath(remote);
  1135. return this->ConvertToOutputForExistingCommon(remotePath, result);
  1136. }
  1137. //----------------------------------------------------------------------------
  1138. std::string
  1139. cmLocalGenerator::ConvertToIncludeReference(std::string const& path)
  1140. {
  1141. return this->ConvertToOutputForExisting(path.c_str());
  1142. }
  1143. //----------------------------------------------------------------------------
  1144. std::string cmLocalGenerator::GetIncludeFlags(
  1145. const std::vector<std::string> &includes,
  1146. const char* lang, bool forResponseFile,
  1147. const char *config)
  1148. {
  1149. if(!lang)
  1150. {
  1151. return "";
  1152. }
  1153. cmOStringStream includeFlags;
  1154. std::string flagVar = "CMAKE_INCLUDE_FLAG_";
  1155. flagVar += lang;
  1156. const char* includeFlag =
  1157. this->Makefile->GetSafeDefinition(flagVar.c_str());
  1158. flagVar = "CMAKE_INCLUDE_FLAG_SEP_";
  1159. flagVar += lang;
  1160. const char* sep = this->Makefile->GetDefinition(flagVar.c_str());
  1161. bool quotePaths = false;
  1162. if(this->Makefile->GetDefinition("CMAKE_QUOTE_INCLUDE_PATHS"))
  1163. {
  1164. quotePaths = true;
  1165. }
  1166. bool repeatFlag = true;
  1167. // should the include flag be repeated like ie. -IA -IB
  1168. if(!sep)
  1169. {
  1170. sep = " ";
  1171. }
  1172. else
  1173. {
  1174. // if there is a separator then the flag is not repeated but is only
  1175. // given once i.e. -classpath a:b:c
  1176. repeatFlag = false;
  1177. }
  1178. // Support special system include flag if it is available and the
  1179. // normal flag is repeated for each directory.
  1180. std::string sysFlagVar = "CMAKE_INCLUDE_SYSTEM_FLAG_";
  1181. sysFlagVar += lang;
  1182. const char* sysIncludeFlag = 0;
  1183. if(repeatFlag)
  1184. {
  1185. sysIncludeFlag = this->Makefile->GetDefinition(sysFlagVar.c_str());
  1186. }
  1187. bool flagUsed = false;
  1188. std::set<cmStdString> emitted;
  1189. #ifdef __APPLE__
  1190. emitted.insert("/System/Library/Frameworks");
  1191. #endif
  1192. std::vector<std::string>::const_iterator i;
  1193. for(i = includes.begin(); i != includes.end(); ++i)
  1194. {
  1195. if(this->Makefile->IsOn("APPLE")
  1196. && cmSystemTools::IsPathToFramework(i->c_str()))
  1197. {
  1198. std::string frameworkDir = *i;
  1199. frameworkDir += "/../";
  1200. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  1201. if(emitted.insert(frameworkDir).second)
  1202. {
  1203. OutputFormat format = forResponseFile? RESPONSE : SHELL;
  1204. includeFlags
  1205. << "-F" << this->Convert(frameworkDir.c_str(),
  1206. START_OUTPUT, format, true)
  1207. << " ";
  1208. }
  1209. continue;
  1210. }
  1211. std::string include = *i;
  1212. if(!flagUsed || repeatFlag)
  1213. {
  1214. if(sysIncludeFlag &&
  1215. this->Makefile->IsSystemIncludeDirectory(i->c_str(), config))
  1216. {
  1217. includeFlags << sysIncludeFlag;
  1218. }
  1219. else
  1220. {
  1221. includeFlags << includeFlag;
  1222. }
  1223. flagUsed = true;
  1224. }
  1225. std::string includePath;
  1226. if(forResponseFile)
  1227. {
  1228. includePath = this->Convert(i->c_str(), START_OUTPUT,
  1229. RESPONSE, true);
  1230. }
  1231. else
  1232. {
  1233. includePath = this->ConvertToIncludeReference(*i);
  1234. }
  1235. if(quotePaths && includePath.size() && includePath[0] != '\"')
  1236. {
  1237. includeFlags << "\"";
  1238. }
  1239. includeFlags << includePath;
  1240. if(quotePaths && includePath.size() && includePath[0] != '\"')
  1241. {
  1242. includeFlags << "\"";
  1243. }
  1244. includeFlags << sep;
  1245. }
  1246. std::string flags = includeFlags.str();
  1247. // remove trailing separators
  1248. if((sep[0] != ' ') && flags.size()>0 && flags[flags.size()-1] == sep[0])
  1249. {
  1250. flags[flags.size()-1] = ' ';
  1251. }
  1252. return flags;
  1253. }
  1254. //----------------------------------------------------------------------------
  1255. void cmLocalGenerator::AddCompileOptions(
  1256. std::string& flags, cmTarget* target,
  1257. const char* lang, const char* config
  1258. )
  1259. {
  1260. std::string langFlagRegexVar = std::string("CMAKE_")+lang+"_FLAG_REGEX";
  1261. if(const char* langFlagRegexStr =
  1262. this->Makefile->GetDefinition(langFlagRegexVar.c_str()))
  1263. {
  1264. // Filter flags acceptable to this language.
  1265. cmsys::RegularExpression r(langFlagRegexStr);
  1266. std::vector<std::string> opts;
  1267. if(const char* targetFlags = target->GetProperty("COMPILE_FLAGS"))
  1268. {
  1269. cmSystemTools::ParseWindowsCommandLine(targetFlags, opts);
  1270. }
  1271. target->GetCompileOptions(opts, config);
  1272. for(std::vector<std::string>::const_iterator i = opts.begin();
  1273. i != opts.end(); ++i)
  1274. {
  1275. if(r.find(i->c_str()))
  1276. {
  1277. // (Re-)Escape this flag. COMPILE_FLAGS were already parsed
  1278. // as a command line above, and COMPILE_OPTIONS are escaped.
  1279. this->AppendFlagEscape(flags, i->c_str());
  1280. }
  1281. }
  1282. }
  1283. else
  1284. {
  1285. // Use all flags.
  1286. if(const char* targetFlags = target->GetProperty("COMPILE_FLAGS"))
  1287. {
  1288. // COMPILE_FLAGS are not escaped for historical reasons.
  1289. this->AppendFlags(flags, targetFlags);
  1290. }
  1291. std::vector<std::string> opts; // TODO: Emitted.
  1292. target->GetCompileOptions(opts, config);
  1293. for(std::vector<std::string>::const_iterator i = opts.begin();
  1294. i != opts.end(); ++i)
  1295. {
  1296. // COMPILE_OPTIONS are escaped.
  1297. this->AppendFlagEscape(flags, i->c_str());
  1298. }
  1299. }
  1300. }
  1301. //----------------------------------------------------------------------------
  1302. void cmLocalGenerator::GetIncludeDirectories(std::vector<std::string>& dirs,
  1303. cmGeneratorTarget* target,
  1304. const char* lang,
  1305. const char *config,
  1306. bool stripImplicitInclDirs
  1307. )
  1308. {
  1309. // Need to decide whether to automatically include the source and
  1310. // binary directories at the beginning of the include path.
  1311. bool includeSourceDir = false;
  1312. bool includeBinaryDir = false;
  1313. // When automatic include directories are requested for a build then
  1314. // include the source and binary directories at the beginning of the
  1315. // include path to approximate include file behavior for an
  1316. // in-source build. This does not account for the case of a source
  1317. // file in a subdirectory of the current source directory but we
  1318. // cannot fix this because not all native build tools support
  1319. // per-source-file include paths.
  1320. if(this->Makefile->IsOn("CMAKE_INCLUDE_CURRENT_DIR"))
  1321. {
  1322. includeSourceDir = true;
  1323. includeBinaryDir = true;
  1324. }
  1325. // CMake versions below 2.0 would add the source tree to the -I path
  1326. // automatically. Preserve compatibility.
  1327. if(this->NeedBackwardsCompatibility(1,9))
  1328. {
  1329. includeSourceDir = true;
  1330. }
  1331. // Hack for VTK 4.0 - 4.4 which depend on the old behavior but do
  1332. // not set the backwards compatibility level automatically.
  1333. const char* vtkSourceDir =
  1334. this->Makefile->GetDefinition("VTK_SOURCE_DIR");
  1335. if(vtkSourceDir)
  1336. {
  1337. const char* vtk_major =
  1338. this->Makefile->GetDefinition("VTK_MAJOR_VERSION");
  1339. const char* vtk_minor =
  1340. this->Makefile->GetDefinition("VTK_MINOR_VERSION");
  1341. vtk_major = vtk_major? vtk_major : "4";
  1342. vtk_minor = vtk_minor? vtk_minor : "4";
  1343. int vmajor = 0;
  1344. int vminor = 0;
  1345. if(sscanf(vtk_major, "%d", &vmajor) &&
  1346. sscanf(vtk_minor, "%d", &vminor) && vmajor == 4 && vminor <= 4)
  1347. {
  1348. includeSourceDir = true;
  1349. }
  1350. }
  1351. // Do not repeat an include path.
  1352. std::set<cmStdString> emitted;
  1353. // Store the automatic include paths.
  1354. if(includeBinaryDir)
  1355. {
  1356. if(emitted.find(
  1357. this->Makefile->GetStartOutputDirectory()) == emitted.end())
  1358. {
  1359. dirs.push_back(this->Makefile->GetStartOutputDirectory());
  1360. emitted.insert(this->Makefile->GetStartOutputDirectory());
  1361. }
  1362. }
  1363. if(includeSourceDir)
  1364. {
  1365. if(emitted.find(this->Makefile->GetStartDirectory()) == emitted.end())
  1366. {
  1367. dirs.push_back(this->Makefile->GetStartDirectory());
  1368. emitted.insert(this->Makefile->GetStartDirectory());
  1369. }
  1370. }
  1371. if(!target)
  1372. {
  1373. return;
  1374. }
  1375. std::vector<std::string> implicitDirs;
  1376. // Load implicit include directories for this language.
  1377. std::string impDirVar = "CMAKE_";
  1378. impDirVar += lang;
  1379. impDirVar += "_IMPLICIT_INCLUDE_DIRECTORIES";
  1380. if(const char* value = this->Makefile->GetDefinition(impDirVar.c_str()))
  1381. {
  1382. std::vector<std::string> impDirVec;
  1383. cmSystemTools::ExpandListArgument(value, impDirVec);
  1384. for(std::vector<std::string>::const_iterator i = impDirVec.begin();
  1385. i != impDirVec.end(); ++i)
  1386. {
  1387. emitted.insert(*i);
  1388. if (!stripImplicitInclDirs)
  1389. {
  1390. implicitDirs.push_back(*i);
  1391. }
  1392. }
  1393. }
  1394. // Get the target-specific include directories.
  1395. std::vector<std::string> includes;
  1396. includes = target->GetIncludeDirectories(config);
  1397. // Support putting all the in-project include directories first if
  1398. // it is requested by the project.
  1399. if(this->Makefile->IsOn("CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE"))
  1400. {
  1401. const char* topSourceDir = this->Makefile->GetHomeDirectory();
  1402. const char* topBinaryDir = this->Makefile->GetHomeOutputDirectory();
  1403. for(std::vector<std::string>::const_iterator i = includes.begin();
  1404. i != includes.end(); ++i)
  1405. {
  1406. // Emit this directory only if it is a subdirectory of the
  1407. // top-level source or binary tree.
  1408. if(cmSystemTools::ComparePath(i->c_str(), topSourceDir) ||
  1409. cmSystemTools::ComparePath(i->c_str(), topBinaryDir) ||
  1410. cmSystemTools::IsSubDirectory(i->c_str(), topSourceDir) ||
  1411. cmSystemTools::IsSubDirectory(i->c_str(), topBinaryDir))
  1412. {
  1413. if(emitted.insert(*i).second)
  1414. {
  1415. dirs.push_back(*i);
  1416. }
  1417. }
  1418. }
  1419. }
  1420. // Construct the final ordered include directory list.
  1421. for(std::vector<std::string>::const_iterator i = includes.begin();
  1422. i != includes.end(); ++i)
  1423. {
  1424. if(emitted.insert(*i).second)
  1425. {
  1426. dirs.push_back(*i);
  1427. }
  1428. }
  1429. for(std::vector<std::string>::const_iterator i = implicitDirs.begin();
  1430. i != implicitDirs.end(); ++i)
  1431. {
  1432. if(std::find(includes.begin(), includes.end(), *i) != includes.end())
  1433. {
  1434. dirs.push_back(*i);
  1435. }
  1436. }
  1437. }
  1438. void cmLocalGenerator::GetTargetFlags(std::string& linkLibs,
  1439. std::string& flags,
  1440. std::string& linkFlags,
  1441. std::string& frameworkPath,
  1442. std::string& linkPath,
  1443. cmGeneratorTarget* target)
  1444. {
  1445. std::string buildType =
  1446. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1447. buildType = cmSystemTools::UpperCase(buildType);
  1448. const char* libraryLinkVariable =
  1449. "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
  1450. switch(target->GetType())
  1451. {
  1452. case cmTarget::STATIC_LIBRARY:
  1453. {
  1454. const char* targetLinkFlags =
  1455. target->GetProperty("STATIC_LIBRARY_FLAGS");
  1456. if(targetLinkFlags)
  1457. {
  1458. linkFlags += targetLinkFlags;
  1459. linkFlags += " ";
  1460. }
  1461. if(!buildType.empty())
  1462. {
  1463. std::string build = "STATIC_LIBRARY_FLAGS_";
  1464. build += buildType;
  1465. targetLinkFlags = target->GetProperty(build.c_str());
  1466. if(targetLinkFlags)
  1467. {
  1468. linkFlags += targetLinkFlags;
  1469. linkFlags += " ";
  1470. }
  1471. }
  1472. }
  1473. break;
  1474. case cmTarget::MODULE_LIBRARY:
  1475. libraryLinkVariable = "CMAKE_MODULE_LINKER_FLAGS";
  1476. case cmTarget::SHARED_LIBRARY:
  1477. {
  1478. linkFlags = this->Makefile->GetSafeDefinition(libraryLinkVariable);
  1479. linkFlags += " ";
  1480. if(!buildType.empty())
  1481. {
  1482. std::string build = libraryLinkVariable;
  1483. build += "_";
  1484. build += buildType;
  1485. linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
  1486. linkFlags += " ";
  1487. }
  1488. if(this->Makefile->IsOn("WIN32") &&
  1489. !(this->Makefile->IsOn("CYGWIN") || this->Makefile->IsOn("MINGW")))
  1490. {
  1491. const std::vector<cmSourceFile*>& sources = target->GetSourceFiles();
  1492. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1493. i != sources.end(); ++i)
  1494. {
  1495. cmSourceFile* sf = *i;
  1496. if(sf->GetExtension() == "def")
  1497. {
  1498. linkFlags +=
  1499. this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1500. linkFlags += this->Convert(sf->GetFullPath().c_str(),
  1501. FULL, SHELL);
  1502. linkFlags += " ";
  1503. }
  1504. }
  1505. }
  1506. const char* targetLinkFlags = target->GetProperty("LINK_FLAGS");
  1507. if(targetLinkFlags)
  1508. {
  1509. linkFlags += targetLinkFlags;
  1510. linkFlags += " ";
  1511. }
  1512. if(!buildType.empty())
  1513. {
  1514. std::string configLinkFlags = "LINK_FLAGS_";
  1515. configLinkFlags += buildType;
  1516. targetLinkFlags = target->GetProperty(configLinkFlags.c_str());
  1517. if(targetLinkFlags)
  1518. {
  1519. linkFlags += targetLinkFlags;
  1520. linkFlags += " ";
  1521. }
  1522. }
  1523. this->OutputLinkLibraries(linkLibs, frameworkPath, linkPath,
  1524. *target, false);
  1525. }
  1526. break;
  1527. case cmTarget::EXECUTABLE:
  1528. {
  1529. linkFlags +=
  1530. this->Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  1531. linkFlags += " ";
  1532. if(!buildType.empty())
  1533. {
  1534. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  1535. build += buildType;
  1536. linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
  1537. linkFlags += " ";
  1538. }
  1539. const char* linkLanguage = target->Target->GetLinkerLanguage();
  1540. if(!linkLanguage)
  1541. {
  1542. cmSystemTools::Error
  1543. ("CMake can not determine linker language for target: ",
  1544. target->Target->GetName());
  1545. return;
  1546. }
  1547. this->AddLanguageFlags(flags, linkLanguage, buildType.c_str());
  1548. this->OutputLinkLibraries(linkLibs, frameworkPath, linkPath,
  1549. *target, false);
  1550. if(cmSystemTools::IsOn
  1551. (this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1552. {
  1553. std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_")
  1554. + linkLanguage + std::string("_FLAGS");
  1555. linkFlags += this->Makefile->GetSafeDefinition(sFlagVar.c_str());
  1556. linkFlags += " ";
  1557. }
  1558. if ( target->GetPropertyAsBool("WIN32_EXECUTABLE") )
  1559. {
  1560. linkFlags +=
  1561. this->Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1562. linkFlags += " ";
  1563. }
  1564. else
  1565. {
  1566. linkFlags +=
  1567. this->Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1568. linkFlags += " ";
  1569. }
  1570. if (target->Target->IsExecutableWithExports())
  1571. {
  1572. std::string exportFlagVar = "CMAKE_EXE_EXPORTS_";
  1573. exportFlagVar += linkLanguage;
  1574. exportFlagVar += "_FLAG";
  1575. linkFlags +=
  1576. this->Makefile->GetSafeDefinition(exportFlagVar.c_str());
  1577. linkFlags += " ";
  1578. }
  1579. const char* targetLinkFlags = target->GetProperty("LINK_FLAGS");
  1580. if(targetLinkFlags)
  1581. {
  1582. linkFlags += targetLinkFlags;
  1583. linkFlags += " ";
  1584. }
  1585. if(!buildType.empty())
  1586. {
  1587. std::string configLinkFlags = "LINK_FLAGS_";
  1588. configLinkFlags += buildType;
  1589. targetLinkFlags = target->GetProperty(configLinkFlags.c_str());
  1590. if(targetLinkFlags)
  1591. {
  1592. linkFlags += targetLinkFlags;
  1593. linkFlags += " ";
  1594. }
  1595. }
  1596. }
  1597. break;
  1598. default:
  1599. break;
  1600. }
  1601. }
  1602. std::string cmLocalGenerator::ConvertToLinkReference(std::string const& lib)
  1603. {
  1604. #if defined(_WIN32) && !defined(__CYGWIN__)
  1605. // Work-ardound command line parsing limitations in MSVC 6.0 and
  1606. // Watcom.
  1607. if(this->Makefile->IsOn("MSVC60") || this->Makefile->IsOn("WATCOM"))
  1608. {
  1609. // Search for the last space.
  1610. std::string::size_type pos = lib.rfind(' ');
  1611. if(pos != lib.npos)
  1612. {
  1613. // Find the slash after the last space, if any.
  1614. pos = lib.find('/', pos);
  1615. // Convert the portion of the path with a space to a short path.
  1616. std::string sp;
  1617. if(cmSystemTools::GetShortPath(lib.substr(0, pos).c_str(), sp))
  1618. {
  1619. // Append the rest of the path with no space.
  1620. sp += lib.substr(pos);
  1621. // Convert to an output path.
  1622. return this->Convert(sp.c_str(), NONE, SHELL);
  1623. }
  1624. }
  1625. }
  1626. #endif
  1627. // Normal behavior.
  1628. return this->Convert(lib.c_str(), START_OUTPUT, SHELL);
  1629. }
  1630. /**
  1631. * Output the linking rules on a command line. For executables,
  1632. * targetLibrary should be a NULL pointer. For libraries, it should point
  1633. * to the name of the library. This will not link a library against itself.
  1634. */
  1635. void cmLocalGenerator::OutputLinkLibraries(std::string& linkLibraries,
  1636. std::string& frameworkPath,
  1637. std::string& linkPath,
  1638. cmGeneratorTarget &tgt,
  1639. bool relink)
  1640. {
  1641. cmOStringStream fout;
  1642. const char* config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  1643. cmComputeLinkInformation* pcli = tgt.Target->GetLinkInformation(config);
  1644. if(!pcli)
  1645. {
  1646. return;
  1647. }
  1648. cmComputeLinkInformation& cli = *pcli;
  1649. // Collect library linking flags command line options.
  1650. std::string linkLibs;
  1651. const char* linkLanguage = cli.GetLinkLanguage();
  1652. std::string libPathFlag =
  1653. this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
  1654. std::string libPathTerminator =
  1655. this->Makefile->GetSafeDefinition("CMAKE_LIBRARY_PATH_TERMINATOR");
  1656. // Flags to link an executable to shared libraries.
  1657. std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
  1658. linkFlagsVar += linkLanguage;
  1659. linkFlagsVar += "_FLAGS";
  1660. if( tgt.GetType() == cmTarget::EXECUTABLE )
  1661. {
  1662. linkLibs = this->Makefile->GetSafeDefinition(linkFlagsVar.c_str());
  1663. linkLibs += " ";
  1664. }
  1665. // Append the framework search path flags.
  1666. std::vector<std::string> const& fwDirs = cli.GetFrameworkPaths();
  1667. for(std::vector<std::string>::const_iterator fdi = fwDirs.begin();
  1668. fdi != fwDirs.end(); ++fdi)
  1669. {
  1670. frameworkPath += "-F";
  1671. frameworkPath += this->Convert(fdi->c_str(), NONE, SHELL, false);
  1672. frameworkPath += " ";
  1673. }
  1674. // Append the library search path flags.
  1675. std::vector<std::string> const& libDirs = cli.GetDirectories();
  1676. for(std::vector<std::string>::const_iterator libDir = libDirs.begin();
  1677. libDir != libDirs.end(); ++libDir)
  1678. {
  1679. std::string libpath = this->ConvertToOutputForExisting(libDir->c_str());
  1680. linkPath += " " + libPathFlag;
  1681. linkPath += libpath;
  1682. linkPath += libPathTerminator;
  1683. linkPath += " ";
  1684. }
  1685. // Append the link items.
  1686. typedef cmComputeLinkInformation::ItemVector ItemVector;
  1687. ItemVector const& items = cli.GetItems();
  1688. for(ItemVector::const_iterator li = items.begin(); li != items.end(); ++li)
  1689. {
  1690. if(li->IsPath)
  1691. {
  1692. linkLibs += this->ConvertToLinkReference(li->Value);
  1693. }
  1694. else
  1695. {
  1696. linkLibs += li->Value;
  1697. }
  1698. linkLibs += " ";
  1699. }
  1700. // Write the library flags to the build rule.
  1701. fout << linkLibs;
  1702. // Get the RPATH entries.
  1703. std::vector<std::string> runtimeDirs;
  1704. cli.GetRPath(runtimeDirs, relink);
  1705. // Check what kind of rpath flags to use.
  1706. if(cli.GetRuntimeSep().empty())
  1707. {
  1708. // Each rpath entry gets its own option ("-R a -R b -R c")
  1709. std::string rpath;
  1710. for(std::vector<std::string>::iterator ri = runtimeDirs.begin();
  1711. ri != runtimeDirs.end(); ++ri)
  1712. {
  1713. rpath += cli.GetRuntimeFlag();
  1714. rpath += this->Convert(ri->c_str(), NONE, SHELL, false);
  1715. rpath += " ";
  1716. }
  1717. fout << rpath;
  1718. }
  1719. else
  1720. {
  1721. // All rpath entries are combined ("-Wl,-rpath,a:b:c").
  1722. std::string rpath = cli.GetRPathString(relink);
  1723. // Store the rpath option in the stream.
  1724. if(!rpath.empty())
  1725. {
  1726. fout << cli.GetRuntimeFlag();
  1727. fout << this->EscapeForShell(rpath.c_str(), true);
  1728. fout << " ";
  1729. }
  1730. }
  1731. // Add the linker runtime search path if any.
  1732. std::string rpath_link = cli.GetRPathLinkString();
  1733. if(!cli.GetRPathLinkFlag().empty() && !rpath_link.empty())
  1734. {
  1735. fout << cli.GetRPathLinkFlag();
  1736. fout << this->EscapeForShell(rpath_link.c_str(), true);
  1737. fout << " ";
  1738. }
  1739. // Add standard libraries for this language.
  1740. std::string standardLibsVar = "CMAKE_";
  1741. standardLibsVar += cli.GetLinkLanguage();
  1742. standardLibsVar += "_STANDARD_LIBRARIES";
  1743. if(const char* stdLibs =
  1744. this->Makefile->GetDefinition(standardLibsVar.c_str()))
  1745. {
  1746. fout << stdLibs << " ";
  1747. }
  1748. linkLibraries = fout.str();
  1749. }
  1750. //----------------------------------------------------------------------------
  1751. void cmLocalGenerator::AddArchitectureFlags(std::string& flags,
  1752. cmGeneratorTarget* target,
  1753. const char *lang,
  1754. const char* config)
  1755. {
  1756. // Only add Mac OS X specific flags on Darwin platforms (OSX and iphone):
  1757. if(!this->Makefile->IsOn("APPLE"))
  1758. {
  1759. return;
  1760. }
  1761. if(this->EmitUniversalBinaryFlags)
  1762. {
  1763. std::vector<std::string> archs;
  1764. target->GetAppleArchs(config, archs);
  1765. const char* sysroot = this->Makefile->GetDefinition("CMAKE_OSX_SYSROOT");
  1766. if(sysroot && sysroot[0] == '/' && !sysroot[1])
  1767. { sysroot = 0; }
  1768. std::string sysrootFlagVar =
  1769. std::string("CMAKE_") + lang + "_SYSROOT_FLAG";
  1770. const char* sysrootFlag =
  1771. this->Makefile->GetDefinition(sysrootFlagVar.c_str());
  1772. const char* deploymentTarget =
  1773. this->Makefile->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
  1774. std::string deploymentTargetFlagVar =
  1775. std::string("CMAKE_") + lang + "_OSX_DEPLOYMENT_TARGET_FLAG";
  1776. const char* deploymentTargetFlag =
  1777. this->Makefile->GetDefinition(deploymentTargetFlagVar.c_str());
  1778. if(!archs.empty() && lang && (lang[0] =='C' || lang[0] == 'F'))
  1779. {
  1780. for(std::vector<std::string>::iterator i = archs.begin();
  1781. i != archs.end(); ++i)
  1782. {
  1783. flags += " -arch ";
  1784. flags += *i;
  1785. }
  1786. }
  1787. if(sysrootFlag && *sysrootFlag && sysroot && *sysroot)
  1788. {
  1789. flags += " ";
  1790. flags += sysrootFlag;
  1791. flags += " ";
  1792. flags += sysroot;
  1793. }
  1794. if (deploymentTargetFlag && *deploymentTargetFlag &&
  1795. deploymentTarget && *deploymentTarget)
  1796. {
  1797. flags += " ";
  1798. flags += deploymentTargetFlag;
  1799. flags += deploymentTarget;
  1800. }
  1801. }
  1802. }
  1803. //----------------------------------------------------------------------------
  1804. void cmLocalGenerator::AddLanguageFlags(std::string& flags,
  1805. const char* lang,
  1806. const char* config)
  1807. {
  1808. // Add language-specific flags.
  1809. std::string flagsVar = "CMAKE_";
  1810. flagsVar += lang;
  1811. flagsVar += "_FLAGS";
  1812. this->AddConfigVariableFlags(flags, flagsVar.c_str(), config);
  1813. }
  1814. //----------------------------------------------------------------------------
  1815. bool cmLocalGenerator::GetRealDependency(const char* inName,
  1816. const char* config,
  1817. std::string& dep)
  1818. {
  1819. // Older CMake code may specify the dependency using the target
  1820. // output file rather than the target name. Such code would have
  1821. // been written before there was support for target properties that
  1822. // modify the name so stripping down to just the file name should
  1823. // produce the target name in this case.
  1824. std::string name = cmSystemTools::GetFilenameName(inName);
  1825. // If the input name is the empty string, there is no real
  1826. // dependency. Short-circuit the other checks:
  1827. if(name == "")
  1828. {
  1829. return false;
  1830. }
  1831. if(cmSystemTools::GetFilenameLastExtension(name) == ".exe")
  1832. {
  1833. name = cmSystemTools::GetFilenameWithoutLastExtension(name);
  1834. }
  1835. // Look for a CMake target with the given name.
  1836. if(cmTarget* target = this->Makefile->FindTargetToUse(name.c_str()))
  1837. {
  1838. // make sure it is not just a coincidence that the target name
  1839. // found is part of the inName
  1840. if(cmSystemTools::FileIsFullPath(inName))
  1841. {
  1842. std::string tLocation;
  1843. if(target->GetType() >= cmTarget::EXECUTABLE &&
  1844. target->GetType() <= cmTarget::MODULE_LIBRARY)
  1845. {
  1846. tLocation = target->GetLocation(config);
  1847. tLocation = cmSystemTools::GetFilenamePath(tLocation);
  1848. tLocation = cmSystemTools::CollapseFullPath(tLocation.c_str());
  1849. }
  1850. std::string depLocation = cmSystemTools::GetFilenamePath(
  1851. std::string(inName));
  1852. depLocation = cmSystemTools::CollapseFullPath(depLocation.c_str());
  1853. if(depLocation != tLocation)
  1854. {
  1855. // it is a full path to a depend that has the same name
  1856. // as a target but is in a different location so do not use
  1857. // the target as the depend
  1858. dep = inName;
  1859. return true;
  1860. }
  1861. }
  1862. switch (target->GetType())
  1863. {
  1864. case cmTarget::EXECUTABLE:
  1865. case cmTarget::STATIC_LIBRARY:
  1866. case cmTarget::SHARED_LIBRARY:
  1867. case cmTarget::MODULE_LIBRARY:
  1868. case cmTarget::UNKNOWN_LIBRARY:
  1869. dep = target->GetLocation(config);
  1870. return true;
  1871. case cmTarget::OBJECT_LIBRARY:
  1872. // An object library has no single file on which to depend.
  1873. // This was listed to get the target-level dependency.
  1874. return false;
  1875. case cmTarget::UTILITY:
  1876. case cmTarget::GLOBAL_TARGET:
  1877. // A utility target has no file on which to depend. This was listed
  1878. // only to get the target-level dependency.
  1879. return false;
  1880. }
  1881. }
  1882. // The name was not that of a CMake target. It must name a file.
  1883. if(cmSystemTools::FileIsFullPath(inName))
  1884. {
  1885. // This is a full path. Return it as given.
  1886. dep = inName;
  1887. return true;
  1888. }
  1889. // Check for a source file in this directory that matches the
  1890. // dependency.
  1891. if(cmSourceFile* sf = this->Makefile->GetSource(inName))
  1892. {
  1893. dep = sf->GetFullPath();
  1894. return true;
  1895. }
  1896. // Treat the name as relative to the source directory in which it
  1897. // was given.
  1898. dep = this->Makefile->GetCurrentDirectory();
  1899. dep += "/";
  1900. dep += inName;
  1901. return true;
  1902. }
  1903. //----------------------------------------------------------------------------
  1904. void cmLocalGenerator::AddSharedFlags(std::string& flags,
  1905. const char* lang,
  1906. bool shared)
  1907. {
  1908. std::string flagsVar;
  1909. // Add flags for dealing with shared libraries for this language.
  1910. if(shared)
  1911. {
  1912. flagsVar = "CMAKE_SHARED_LIBRARY_";
  1913. flagsVar += lang;
  1914. flagsVar += "_FLAGS";
  1915. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  1916. }
  1917. }
  1918. static void AddVisibilityCompileOption(std::string &flags, cmTarget* target,
  1919. cmLocalGenerator *lg, const char *lang)
  1920. {
  1921. std::string l(lang);
  1922. std::string compileOption = "CMAKE_" + l + "_COMPILE_OPTIONS_VISIBILITY";
  1923. const char *opt = lg->GetMakefile()->GetDefinition(compileOption.c_str());
  1924. if (!opt)
  1925. {
  1926. return;
  1927. }
  1928. std::string flagDefine = l + "_VISIBILITY_PRESET";
  1929. const char *prop = target->GetProperty(flagDefine.c_str());
  1930. if (!prop)
  1931. {
  1932. return;
  1933. }
  1934. if (strcmp(prop, "hidden") != 0
  1935. && strcmp(prop, "default") != 0
  1936. && strcmp(prop, "protected") != 0
  1937. && strcmp(prop, "internal") != 0 )
  1938. {
  1939. cmOStringStream e;
  1940. e << "Target " << target->GetName() << " uses unsupported value \""
  1941. << prop << "\" for " << flagDefine << ".";
  1942. cmSystemTools::Error(e.str().c_str());
  1943. return;
  1944. }
  1945. std::string option = std::string(opt) + prop;
  1946. lg->AppendFlags(flags, option.c_str());
  1947. }
  1948. static void AddInlineVisibilityCompileOption(std::string &flags,
  1949. cmTarget* target,
  1950. cmLocalGenerator *lg)
  1951. {
  1952. std::string compileOption
  1953. = "CMAKE_CXX_COMPILE_OPTIONS_VISIBILITY_INLINES_HIDDEN";
  1954. const char *opt = lg->GetMakefile()->GetDefinition(compileOption.c_str());
  1955. if (!opt)
  1956. {
  1957. return;
  1958. }
  1959. bool prop = target->GetPropertyAsBool("VISIBILITY_INLINES_HIDDEN");
  1960. if (!prop)
  1961. {
  1962. return;
  1963. }
  1964. lg->AppendFlags(flags, opt);
  1965. }
  1966. //----------------------------------------------------------------------------
  1967. void cmLocalGenerator
  1968. ::AddVisibilityPresetFlags(std::string &flags, cmTarget* target,
  1969. const char *lang)
  1970. {
  1971. int targetType = target->GetType();
  1972. bool suitableTarget = ((targetType == cmTarget::SHARED_LIBRARY)
  1973. || (targetType == cmTarget::MODULE_LIBRARY)
  1974. || (target->IsExecutableWithExports()));
  1975. if (!suitableTarget)
  1976. {
  1977. return;
  1978. }
  1979. if (!lang)
  1980. {
  1981. return;
  1982. }
  1983. AddVisibilityCompileOption(flags, target, this, lang);
  1984. AddInlineVisibilityCompileOption(flags, target, this);
  1985. }
  1986. //----------------------------------------------------------------------------
  1987. void cmLocalGenerator::AddCMP0018Flags(std::string &flags, cmTarget* target,
  1988. std::string const& lang,
  1989. const char *config)
  1990. {
  1991. int targetType = target->GetType();
  1992. bool shared = ((targetType == cmTarget::SHARED_LIBRARY) ||
  1993. (targetType == cmTarget::MODULE_LIBRARY));
  1994. if (this->GetShouldUseOldFlags(shared, lang))
  1995. {
  1996. this->AddSharedFlags(flags, lang.c_str(), shared);
  1997. }
  1998. else
  1999. {
  2000. if (target->GetType() == cmTarget::OBJECT_LIBRARY)
  2001. {
  2002. if (target->GetPropertyAsBool("POSITION_INDEPENDENT_CODE"))
  2003. {
  2004. this->AddPositionIndependentFlags(flags, lang, targetType);
  2005. }
  2006. return;
  2007. }
  2008. if (target->GetLinkInterfaceDependentBoolProperty(
  2009. "POSITION_INDEPENDENT_CODE",
  2010. config))
  2011. {
  2012. this->AddPositionIndependentFlags(flags, lang, targetType);
  2013. }
  2014. if (shared)
  2015. {
  2016. this->AppendFeatureOptions(flags, lang.c_str(), "DLL");
  2017. }
  2018. }
  2019. }
  2020. //----------------------------------------------------------------------------
  2021. bool cmLocalGenerator::GetShouldUseOldFlags(bool shared,
  2022. const std::string &lang) const
  2023. {
  2024. std::string originalFlags =
  2025. this->GlobalGenerator->GetSharedLibFlagsForLanguage(lang);
  2026. if (shared)
  2027. {
  2028. std::string flagsVar = "CMAKE_SHARED_LIBRARY_";
  2029. flagsVar += lang;
  2030. flagsVar += "_FLAGS";
  2031. const char* flags =
  2032. this->Makefile->GetSafeDefinition(flagsVar.c_str());
  2033. if (flags && flags != originalFlags)
  2034. {
  2035. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0018))
  2036. {
  2037. case cmPolicies::WARN:
  2038. {
  2039. cmOStringStream e;
  2040. e << "Variable " << flagsVar << " has been modified. CMake "
  2041. "will ignore the POSITION_INDEPENDENT_CODE target property for "
  2042. "shared libraries and will use the " << flagsVar << " variable "
  2043. "instead. This may cause errors if the original content of "
  2044. << flagsVar << " was removed.\n"
  2045. << this->Makefile->GetPolicies()->GetPolicyWarning(
  2046. cmPolicies::CMP0018);
  2047. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  2048. // fall through to OLD behaviour
  2049. }
  2050. case cmPolicies::OLD:
  2051. return true;
  2052. case cmPolicies::REQUIRED_IF_USED:
  2053. case cmPolicies::REQUIRED_ALWAYS:
  2054. case cmPolicies::NEW:
  2055. default:
  2056. return false;
  2057. }
  2058. }
  2059. }
  2060. return false;
  2061. }
  2062. //----------------------------------------------------------------------------
  2063. void cmLocalGenerator::AddPositionIndependentFlags(std::string& flags,
  2064. std::string const& lang,
  2065. int targetType)
  2066. {
  2067. const char* picFlags = 0;
  2068. if(targetType == cmTarget::EXECUTABLE)
  2069. {
  2070. std::string flagsVar = "CMAKE_";
  2071. flagsVar += lang;
  2072. flagsVar += "_COMPILE_OPTIONS_PIE";
  2073. picFlags = this->Makefile->GetSafeDefinition(flagsVar.c_str());
  2074. }
  2075. if (!picFlags)
  2076. {
  2077. std::string flagsVar = "CMAKE_";
  2078. flagsVar += lang;
  2079. flagsVar += "_COMPILE_OPTIONS_PIC";
  2080. picFlags = this->Makefile->GetSafeDefinition(flagsVar.c_str());
  2081. }
  2082. if (picFlags)
  2083. {
  2084. std::vector<std::string> options;
  2085. cmSystemTools::ExpandListArgument(picFlags, options);
  2086. for(std::vector<std::string>::const_iterator oi = options.begin();
  2087. oi != options.end(); ++oi)
  2088. {
  2089. this->AppendFlagEscape(flags, oi->c_str());
  2090. }
  2091. }
  2092. }
  2093. //----------------------------------------------------------------------------
  2094. void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
  2095. const char* var,
  2096. const char* config)
  2097. {
  2098. // Add the flags from the variable itself.
  2099. std::string flagsVar = var;
  2100. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  2101. // Add the flags from the build-type specific variable.
  2102. if(config && *config)
  2103. {
  2104. flagsVar += "_";
  2105. flagsVar += cmSystemTools::UpperCase(config);
  2106. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  2107. }
  2108. }
  2109. //----------------------------------------------------------------------------
  2110. void cmLocalGenerator::AppendFlags(std::string& flags,
  2111. const char* newFlags)
  2112. {
  2113. if(newFlags && *newFlags)
  2114. {
  2115. std::string newf = newFlags;
  2116. if(flags.size())
  2117. {
  2118. flags += " ";
  2119. }
  2120. flags += newFlags;
  2121. }
  2122. }
  2123. //----------------------------------------------------------------------------
  2124. void cmLocalGenerator::AppendFlagEscape(std::string& flags,
  2125. const char* rawFlag)
  2126. {
  2127. this->AppendFlags(flags, this->EscapeForShell(rawFlag).c_str());
  2128. }
  2129. //----------------------------------------------------------------------------
  2130. void cmLocalGenerator::AppendDefines(std::set<std::string>& defines,
  2131. const char* defines_list)
  2132. {
  2133. // Short-circuit if there are no definitions.
  2134. if(!defines_list)
  2135. {
  2136. return;
  2137. }
  2138. // Expand the list of definitions.
  2139. std::vector<std::string> defines_vec;
  2140. cmSystemTools::ExpandListArgument(defines_list, defines_vec);
  2141. for(std::vector<std::string>::const_iterator di = defines_vec.begin();
  2142. di != defines_vec.end(); ++di)
  2143. {
  2144. // Skip unsupported definitions.
  2145. if(!this->CheckDefinition(*di))
  2146. {
  2147. continue;
  2148. }
  2149. defines.insert(*di);
  2150. }
  2151. }
  2152. //----------------------------------------------------------------------------
  2153. void cmLocalGenerator::JoinDefines(const std::set<std::string>& defines,
  2154. std::string &definesString,
  2155. const char* lang)
  2156. {
  2157. // Lookup the define flag for the current language.
  2158. std::string dflag = "-D";
  2159. if(lang)
  2160. {
  2161. std::string defineFlagVar = "CMAKE_";
  2162. defineFlagVar += lang;
  2163. defineFlagVar += "_DEFINE_FLAG";
  2164. const char* df = this->Makefile->GetDefinition(defineFlagVar.c_str());
  2165. if(df && *df)
  2166. {
  2167. dflag = df;
  2168. }
  2169. }
  2170. std::set<std::string>::const_iterator defineIt = defines.begin();
  2171. const std::set<std::string>::const_iterator defineEnd = defines.end();
  2172. const char* itemSeparator = definesString.empty() ? "" : " ";
  2173. for( ; defineIt != defineEnd; ++defineIt)
  2174. {
  2175. // Append the definition with proper escaping.
  2176. std::string def = dflag;
  2177. if(this->WatcomWMake)
  2178. {
  2179. // The Watcom compiler does its own command line parsing instead
  2180. // of using the windows shell rules. Definitions are one of
  2181. // -DNAME
  2182. // -DNAME=<cpp-token>
  2183. // -DNAME="c-string with spaces and other characters(?@#$)"
  2184. //
  2185. // Watcom will properly parse each of these cases from the
  2186. // command line without any escapes. However we still have to
  2187. // get the '$' and '#' characters through WMake as '$$' and
  2188. // '$#'.
  2189. for(const char* c = defineIt->c_str(); *c; ++c)
  2190. {
  2191. if(*c == '$' || *c == '#')
  2192. {
  2193. def += '$';
  2194. }
  2195. def += *c;
  2196. }
  2197. }
  2198. else
  2199. {
  2200. // Make the definition appear properly on the command line. Use
  2201. // -DNAME="value" instead of -D"NAME=value" to help VS6 parser.
  2202. std::string::size_type eq = defineIt->find("=");
  2203. def += defineIt->substr(0, eq);
  2204. if(eq != defineIt->npos)
  2205. {
  2206. def += "=";
  2207. def += this->EscapeForShell(defineIt->c_str() + eq + 1, true);
  2208. }
  2209. }
  2210. definesString += itemSeparator;
  2211. itemSeparator = " ";
  2212. definesString += def;
  2213. }
  2214. }
  2215. //----------------------------------------------------------------------------
  2216. void cmLocalGenerator::AppendFeatureOptions(
  2217. std::string& flags, const char* lang, const char* feature)
  2218. {
  2219. std::string optVar = "CMAKE_";
  2220. optVar += lang;
  2221. optVar += "_COMPILE_OPTIONS_";
  2222. optVar += feature;
  2223. if(const char* optionList = this->Makefile->GetDefinition(optVar.c_str()))
  2224. {
  2225. std::vector<std::string> options;
  2226. cmSystemTools::ExpandListArgument(optionList, options);
  2227. for(std::vector<std::string>::const_iterator oi = options.begin();
  2228. oi != options.end(); ++oi)
  2229. {
  2230. this->AppendFlagEscape(flags, oi->c_str());
  2231. }
  2232. }
  2233. }
  2234. //----------------------------------------------------------------------------
  2235. std::string
  2236. cmLocalGenerator::ConstructComment(const cmCustomCommand& cc,
  2237. const char* default_comment)
  2238. {
  2239. // Check for a comment provided with the command.
  2240. if(cc.GetComment())
  2241. {
  2242. return cc.GetComment();
  2243. }
  2244. // Construct a reasonable default comment if possible.
  2245. if(!cc.GetOutputs().empty())
  2246. {
  2247. std::string comment;
  2248. comment = "Generating ";
  2249. const char* sep = "";
  2250. for(std::vector<std::string>::const_iterator o = cc.GetOutputs().begin();
  2251. o != cc.GetOutputs().end(); ++o)
  2252. {
  2253. comment += sep;
  2254. comment += this->Convert(o->c_str(), cmLocalGenerator::START_OUTPUT);
  2255. sep = ", ";
  2256. }
  2257. return comment;
  2258. }
  2259. // Otherwise use the provided default.
  2260. return default_comment;
  2261. }
  2262. //----------------------------------------------------------------------------
  2263. std::string
  2264. cmLocalGenerator::ConvertToOptionallyRelativeOutputPath(const char* remote)
  2265. {
  2266. return this->Convert(remote, START_OUTPUT, SHELL, true);
  2267. }
  2268. //----------------------------------------------------------------------------
  2269. const char* cmLocalGenerator::GetRelativeRootPath(RelativeRoot relroot)
  2270. {
  2271. switch (relroot)
  2272. {
  2273. case HOME: return this->Makefile->GetHomeDirectory();
  2274. case START: return this->Makefile->GetStartDirectory();
  2275. case HOME_OUTPUT: return this->Makefile->GetHomeOutputDirectory();
  2276. case START_OUTPUT: return this->Makefile->GetStartOutputDirectory();
  2277. default: break;
  2278. }
  2279. return 0;
  2280. }
  2281. //----------------------------------------------------------------------------
  2282. std::string cmLocalGenerator::Convert(const char* source,
  2283. RelativeRoot relative,
  2284. OutputFormat output,
  2285. bool optional)
  2286. {
  2287. // Make sure the relative path conversion components are set.
  2288. if(!this->PathConversionsSetup)
  2289. {
  2290. this->SetupPathConversions();
  2291. this->PathConversionsSetup = true;
  2292. }
  2293. // Convert the path to a relative path.
  2294. std::string result = source;
  2295. if (!optional || this->UseRelativePaths)
  2296. {
  2297. switch (relative)
  2298. {
  2299. case HOME:
  2300. //result = cmSystemTools::CollapseFullPath(result.c_str());
  2301. result = this->ConvertToRelativePath(this->HomeDirectoryComponents,
  2302. result.c_str());
  2303. break;
  2304. case START:
  2305. //result = cmSystemTools::CollapseFullPath(result.c_str());
  2306. result = this->ConvertToRelativePath(this->StartDirectoryComponents,
  2307. result.c_str());
  2308. break;
  2309. case HOME_OUTPUT:
  2310. //result = cmSystemTools::CollapseFullPath(result.c_str());
  2311. result =
  2312. this->ConvertToRelativePath(this->HomeOutputDirectoryComponents,
  2313. result.c_str());
  2314. break;
  2315. case START_OUTPUT:
  2316. //result = cmSystemTools::CollapseFullPath(result.c_str());
  2317. result =
  2318. this->ConvertToRelativePath(this->StartOutputDirectoryComponents,
  2319. result.c_str());
  2320. break;
  2321. case FULL:
  2322. result = cmSystemTools::CollapseFullPath(result.c_str());
  2323. break;
  2324. case NONE:
  2325. break;
  2326. }
  2327. }
  2328. return this->ConvertToOutputFormat(result.c_str(), output);
  2329. }
  2330. //----------------------------------------------------------------------------
  2331. std::string cmLocalGenerator::ConvertToOutputFormat(const char* source,
  2332. OutputFormat output)
  2333. {
  2334. std::string result = source;
  2335. // Convert it to an output path.
  2336. if (output == MAKEFILE)
  2337. {
  2338. result = cmSystemTools::ConvertToOutputPath(result.c_str());
  2339. }
  2340. else if( output == SHELL)
  2341. {
  2342. // For the MSYS shell convert drive letters to posix paths, so
  2343. // that c:/some/path becomes /c/some/path. This is needed to
  2344. // avoid problems with the shell path translation.
  2345. if(this->MSYSShell && !this->LinkScriptShell)
  2346. {
  2347. if(result.size() > 2 && result[1] == ':')
  2348. {
  2349. result[1] = result[0];
  2350. result[0] = '/';
  2351. }
  2352. }
  2353. if(this->WindowsShell)
  2354. {
  2355. std::string::size_type pos = 0;
  2356. while((pos = result.find('/', pos)) != std::string::npos)
  2357. {
  2358. result[pos] = '\\';
  2359. pos++;
  2360. }
  2361. }
  2362. result = this->EscapeForShell(result.c_str(), true, false);
  2363. }
  2364. else if(output == RESPONSE)
  2365. {
  2366. result = this->EscapeForShell(result.c_str(), false, false);
  2367. }
  2368. return result;
  2369. }
  2370. //----------------------------------------------------------------------------
  2371. std::string cmLocalGenerator::Convert(RelativeRoot remote,
  2372. const char* local,
  2373. OutputFormat output,
  2374. bool optional)
  2375. {
  2376. const char* remotePath = this->GetRelativeRootPath(remote);
  2377. // The relative root must have a path (i.e. not FULL or NONE)
  2378. assert(remotePath != 0);
  2379. if(local && (!optional || this->UseRelativePaths))
  2380. {
  2381. std::vector<std::string> components;
  2382. cmSystemTools::SplitPath(local, components);
  2383. std::string result = this->ConvertToRelativePath(components, remotePath);
  2384. return this->ConvertToOutputFormat(result.c_str(), output);
  2385. }
  2386. else
  2387. {
  2388. return this->ConvertToOutputFormat(remotePath, output);
  2389. }
  2390. }
  2391. //----------------------------------------------------------------------------
  2392. std::string cmLocalGenerator::FindRelativePathTopSource()
  2393. {
  2394. // Relative path conversion within a single tree managed by CMake is
  2395. // safe. We can use our parent relative path top if and only if
  2396. // this is a subdirectory of that top.
  2397. if(cmLocalGenerator* parent = this->GetParent())
  2398. {
  2399. std::string parentTop = parent->FindRelativePathTopSource();
  2400. if(cmSystemTools::IsSubDirectory(
  2401. this->Makefile->GetStartDirectory(), parentTop.c_str()))
  2402. {
  2403. return parentTop;
  2404. }
  2405. }
  2406. // Otherwise this directory itself is the new top.
  2407. return this->Makefile->GetStartDirectory();
  2408. }
  2409. //----------------------------------------------------------------------------
  2410. std::string cmLocalGenerator::FindRelativePathTopBinary()
  2411. {
  2412. // Relative path conversion within a single tree managed by CMake is
  2413. // safe. We can use our parent relative path top if and only if
  2414. // this is a subdirectory of that top.
  2415. if(cmLocalGenerator* parent = this->GetParent())
  2416. {
  2417. std::string parentTop = parent->FindRelativePathTopBinary();
  2418. if(cmSystemTools::IsSubDirectory(
  2419. this->Makefile->GetStartOutputDirectory(), parentTop.c_str()))
  2420. {
  2421. return parentTop;
  2422. }
  2423. }
  2424. // Otherwise this directory itself is the new top.
  2425. return this->Makefile->GetStartOutputDirectory();
  2426. }
  2427. //----------------------------------------------------------------------------
  2428. void cmLocalGenerator::ConfigureRelativePaths()
  2429. {
  2430. // Relative path conversion inside the source tree is not used to
  2431. // construct relative paths passed to build tools so it is safe to
  2432. // even when the source is a network path.
  2433. std::string source = this->FindRelativePathTopSource();
  2434. this->RelativePathTopSource = source;
  2435. // The current working directory on Windows cannot be a network
  2436. // path. Therefore relative paths cannot work when the binary tree
  2437. // is a network path.
  2438. std::string binary = this->FindRelativePathTopBinary();
  2439. if(binary.size() < 2 || binary.substr(0, 2) != "//")
  2440. {
  2441. this->RelativePathTopBinary = binary;
  2442. }
  2443. else
  2444. {
  2445. this->RelativePathTopBinary = "";
  2446. }
  2447. }
  2448. //----------------------------------------------------------------------------
  2449. static bool cmLocalGeneratorNotAbove(const char* a, const char* b)
  2450. {
  2451. return (cmSystemTools::ComparePath(a, b) ||
  2452. cmSystemTools::IsSubDirectory(a, b));
  2453. }
  2454. //----------------------------------------------------------------------------
  2455. std::string
  2456. cmLocalGenerator::ConvertToRelativePath(const std::vector<std::string>& local,
  2457. const char* in_remote, bool force)
  2458. {
  2459. // The path should never be quoted.
  2460. assert(in_remote[0] != '\"');
  2461. // The local path should never have a trailing slash.
  2462. assert(local.size() > 0 && !(local[local.size()-1] == ""));
  2463. // If the path is already relative then just return the path.
  2464. if(!cmSystemTools::FileIsFullPath(in_remote))
  2465. {
  2466. return in_remote;
  2467. }
  2468. // Make sure relative path conversion is configured.
  2469. if(!this->RelativePathsConfigured)
  2470. {
  2471. this->ConfigureRelativePaths();
  2472. this->RelativePathsConfigured = true;
  2473. }
  2474. if(!force)
  2475. {
  2476. // Skip conversion if the path and local are not both in the source
  2477. // or both in the binary tree.
  2478. std::string local_path = cmSystemTools::JoinPath(local);
  2479. if(!((cmLocalGeneratorNotAbove(local_path.c_str(),
  2480. this->RelativePathTopBinary.c_str()) &&
  2481. cmLocalGeneratorNotAbove(in_remote,
  2482. this->RelativePathTopBinary.c_str())) ||
  2483. (cmLocalGeneratorNotAbove(local_path.c_str(),
  2484. this->RelativePathTopSource.c_str()) &&
  2485. cmLocalGeneratorNotAbove(in_remote,
  2486. this->RelativePathTopSource.c_str()))))
  2487. {
  2488. return in_remote;
  2489. }
  2490. }
  2491. // Identify the longest shared path component between the remote
  2492. // path and the local path.
  2493. std::vector<std::string> remote;
  2494. cmSystemTools::SplitPath(in_remote, remote);
  2495. unsigned int common=0;
  2496. while(common < remote.size() &&
  2497. common < local.size() &&
  2498. cmSystemTools::ComparePath(remote[common].c_str(),
  2499. local[common].c_str()))
  2500. {
  2501. ++common;
  2502. }
  2503. // If no part of the path is in common then return the full path.
  2504. if(common == 0)
  2505. {
  2506. return in_remote;
  2507. }
  2508. // If the entire path is in common then just return a ".".
  2509. if(common == remote.size() &&
  2510. common == local.size())
  2511. {
  2512. return ".";
  2513. }
  2514. // If the entire path is in common except for a trailing slash then
  2515. // just return a "./".
  2516. if(common+1 == remote.size() &&
  2517. remote[common].size() == 0 &&
  2518. common == local.size())
  2519. {
  2520. return "./";
  2521. }
  2522. // Construct the relative path.
  2523. std::string relative;
  2524. // First add enough ../ to get up to the level of the shared portion
  2525. // of the path. Leave off the trailing slash. Note that the last
  2526. // component of local will never be empty because local should never
  2527. // have a trailing slash.
  2528. for(unsigned int i=common; i < local.size(); ++i)
  2529. {
  2530. relative += "..";
  2531. if(i < local.size()-1)
  2532. {
  2533. relative += "/";
  2534. }
  2535. }
  2536. // Now add the portion of the destination path that is not included
  2537. // in the shared portion of the path. Add a slash the first time
  2538. // only if there was already something in the path. If there was a
  2539. // trailing slash in the input then the last iteration of the loop
  2540. // will add a slash followed by an empty string which will preserve
  2541. // the trailing slash in the output.
  2542. for(unsigned int i=common; i < remote.size(); ++i)
  2543. {
  2544. if(relative.size() > 0)
  2545. {
  2546. relative += "/";
  2547. }
  2548. relative += remote[i];
  2549. }
  2550. // Finally return the path.
  2551. return relative;
  2552. }
  2553. //----------------------------------------------------------------------------
  2554. void
  2555. cmLocalGenerator
  2556. ::GenerateTargetInstallRules(
  2557. std::ostream& os, const char* config,
  2558. std::vector<std::string> const& configurationTypes)
  2559. {
  2560. // Convert the old-style install specification from each target to
  2561. // an install generator and run it.
  2562. cmTargets& tgts = this->Makefile->GetTargets();
  2563. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); ++l)
  2564. {
  2565. // Include the user-specified pre-install script for this target.
  2566. if(const char* preinstall = l->second.GetProperty("PRE_INSTALL_SCRIPT"))
  2567. {
  2568. cmInstallScriptGenerator g(preinstall, false, 0);
  2569. g.Generate(os, config, configurationTypes);
  2570. }
  2571. // Install this target if a destination is given.
  2572. if(l->second.GetInstallPath() != "")
  2573. {
  2574. // Compute the full install destination. Note that converting
  2575. // to unix slashes also removes any trailing slash.
  2576. // We also skip over the leading slash given by the user.
  2577. std::string destination = l->second.GetInstallPath().substr(1);
  2578. cmSystemTools::ConvertToUnixSlashes(destination);
  2579. if(destination.empty())
  2580. {
  2581. destination = ".";
  2582. }
  2583. // Generate the proper install generator for this target type.
  2584. switch(l->second.GetType())
  2585. {
  2586. case cmTarget::EXECUTABLE:
  2587. case cmTarget::STATIC_LIBRARY:
  2588. case cmTarget::MODULE_LIBRARY:
  2589. {
  2590. // Use a target install generator.
  2591. cmInstallTargetGenerator g(l->second, destination.c_str(), false);
  2592. g.Generate(os, config, configurationTypes);
  2593. }
  2594. break;
  2595. case cmTarget::SHARED_LIBRARY:
  2596. {
  2597. #if defined(_WIN32) || defined(__CYGWIN__)
  2598. // Special code to handle DLL. Install the import library
  2599. // to the normal destination and the DLL to the runtime
  2600. // destination.
  2601. cmInstallTargetGenerator g1(l->second, destination.c_str(), true);
  2602. g1.Generate(os, config, configurationTypes);
  2603. // We also skip over the leading slash given by the user.
  2604. destination = l->second.GetRuntimeInstallPath().substr(1);
  2605. cmSystemTools::ConvertToUnixSlashes(destination);
  2606. cmInstallTargetGenerator g2(l->second, destination.c_str(), false);
  2607. g2.Generate(os, config, configurationTypes);
  2608. #else
  2609. // Use a target install generator.
  2610. cmInstallTargetGenerator g(l->second, destination.c_str(), false);
  2611. g.Generate(os, config, configurationTypes);
  2612. #endif
  2613. }
  2614. break;
  2615. default:
  2616. break;
  2617. }
  2618. }
  2619. // Include the user-specified post-install script for this target.
  2620. if(const char* postinstall = l->second.GetProperty("POST_INSTALL_SCRIPT"))
  2621. {
  2622. cmInstallScriptGenerator g(postinstall, false, 0);
  2623. g.Generate(os, config, configurationTypes);
  2624. }
  2625. }
  2626. }
  2627. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  2628. static std::string cmLocalGeneratorMD5(const char* input)
  2629. {
  2630. char md5out[32];
  2631. cmsysMD5* md5 = cmsysMD5_New();
  2632. cmsysMD5_Initialize(md5);
  2633. cmsysMD5_Append(md5, reinterpret_cast<unsigned char const*>(input), -1);
  2634. cmsysMD5_FinalizeHex(md5, md5out);
  2635. cmsysMD5_Delete(md5);
  2636. return std::string(md5out, 32);
  2637. }
  2638. static bool
  2639. cmLocalGeneratorShortenObjectName(std::string& objName,
  2640. std::string::size_type max_len)
  2641. {
  2642. // Replace the beginning of the path portion of the object name with
  2643. // its own md5 sum.
  2644. std::string::size_type pos = objName.find('/', objName.size()-max_len+32);
  2645. if(pos != objName.npos)
  2646. {
  2647. std::string md5name = cmLocalGeneratorMD5(objName.substr(0, pos).c_str());
  2648. md5name += objName.substr(pos);
  2649. objName = md5name;
  2650. // The object name is now short enough.
  2651. return true;
  2652. }
  2653. else
  2654. {
  2655. // The object name could not be shortened enough.
  2656. return false;
  2657. }
  2658. }
  2659. static
  2660. bool cmLocalGeneratorCheckObjectName(std::string& objName,
  2661. std::string::size_type dir_len,
  2662. std::string::size_type max_total_len)
  2663. {
  2664. // Enforce the maximum file name length if possible.
  2665. std::string::size_type max_obj_len = max_total_len;
  2666. if(dir_len < max_total_len)
  2667. {
  2668. max_obj_len = max_total_len - dir_len;
  2669. if(objName.size() > max_obj_len)
  2670. {
  2671. // The current object file name is too long. Try to shorten it.
  2672. return cmLocalGeneratorShortenObjectName(objName, max_obj_len);
  2673. }
  2674. else
  2675. {
  2676. // The object file name is short enough.
  2677. return true;
  2678. }
  2679. }
  2680. else
  2681. {
  2682. // The build directory in which the object will be stored is
  2683. // already too deep.
  2684. return false;
  2685. }
  2686. }
  2687. #endif
  2688. //----------------------------------------------------------------------------
  2689. std::string&
  2690. cmLocalGenerator
  2691. ::CreateSafeUniqueObjectFileName(const char* sin,
  2692. std::string const& dir_max)
  2693. {
  2694. // Look for an existing mapped name for this object file.
  2695. std::map<cmStdString,cmStdString>::iterator it =
  2696. this->UniqueObjectNamesMap.find(sin);
  2697. // If no entry exists create one.
  2698. if(it == this->UniqueObjectNamesMap.end())
  2699. {
  2700. // Start with the original name.
  2701. std::string ssin = sin;
  2702. // Avoid full paths by removing leading slashes.
  2703. std::string::size_type pos = 0;
  2704. for(;pos < ssin.size() && ssin[pos] == '/'; ++pos)
  2705. {
  2706. }
  2707. ssin = ssin.substr(pos);
  2708. // Avoid full paths by removing colons.
  2709. cmSystemTools::ReplaceString(ssin, ":", "_");
  2710. // Avoid relative paths that go up the tree.
  2711. cmSystemTools::ReplaceString(ssin, "../", "__/");
  2712. // Avoid spaces.
  2713. cmSystemTools::ReplaceString(ssin, " ", "_");
  2714. // Mangle the name if necessary.
  2715. if(this->Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES"))
  2716. {
  2717. bool done;
  2718. int cc = 0;
  2719. char rpstr[100];
  2720. sprintf(rpstr, "_p_");
  2721. cmSystemTools::ReplaceString(ssin, "+", rpstr);
  2722. std::string sssin = sin;
  2723. do
  2724. {
  2725. done = true;
  2726. for ( it = this->UniqueObjectNamesMap.begin();
  2727. it != this->UniqueObjectNamesMap.end();
  2728. ++ it )
  2729. {
  2730. if ( it->second == ssin )
  2731. {
  2732. done = false;
  2733. }
  2734. }
  2735. if ( done )
  2736. {
  2737. break;
  2738. }
  2739. sssin = ssin;
  2740. cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
  2741. sprintf(rpstr, "_p%d_", cc++);
  2742. }
  2743. while ( !done );
  2744. }
  2745. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  2746. if(!cmLocalGeneratorCheckObjectName(ssin, dir_max.size(),
  2747. this->ObjectPathMax))
  2748. {
  2749. // Warn if this is the first time the path has been seen.
  2750. if(this->ObjectMaxPathViolations.insert(dir_max).second)
  2751. {
  2752. cmOStringStream m;
  2753. m << "The object file directory\n"
  2754. << " " << dir_max << "\n"
  2755. << "has " << dir_max.size() << " characters. "
  2756. << "The maximum full path to an object file is "
  2757. << this->ObjectPathMax << " characters "
  2758. << "(see CMAKE_OBJECT_PATH_MAX). "
  2759. << "Object file\n"
  2760. << " " << ssin << "\n"
  2761. << "cannot be safely placed under this directory. "
  2762. << "The build may not work correctly.";
  2763. this->Makefile->IssueMessage(cmake::WARNING, m.str());
  2764. }
  2765. }
  2766. #else
  2767. (void)dir_max;
  2768. #endif
  2769. // Insert the newly mapped object file name.
  2770. std::map<cmStdString, cmStdString>::value_type e(sin, ssin);
  2771. it = this->UniqueObjectNamesMap.insert(e).first;
  2772. }
  2773. // Return the map entry.
  2774. return it->second;
  2775. }
  2776. //----------------------------------------------------------------------------
  2777. std::string
  2778. cmLocalGenerator
  2779. ::GetObjectFileNameWithoutTarget(const cmSourceFile& source,
  2780. std::string const& dir_max,
  2781. bool* hasSourceExtension)
  2782. {
  2783. // Construct the object file name using the full path to the source
  2784. // file which is its only unique identification.
  2785. const char* fullPath = source.GetFullPath().c_str();
  2786. // Try referencing the source relative to the source tree.
  2787. std::string relFromSource = this->Convert(fullPath, START);
  2788. assert(!relFromSource.empty());
  2789. bool relSource = !cmSystemTools::FileIsFullPath(relFromSource.c_str());
  2790. bool subSource = relSource && relFromSource[0] != '.';
  2791. // Try referencing the source relative to the binary tree.
  2792. std::string relFromBinary = this->Convert(fullPath, START_OUTPUT);
  2793. assert(!relFromBinary.empty());
  2794. bool relBinary = !cmSystemTools::FileIsFullPath(relFromBinary.c_str());
  2795. bool subBinary = relBinary && relFromBinary[0] != '.';
  2796. // Select a nice-looking reference to the source file to construct
  2797. // the object file name.
  2798. std::string objectName;
  2799. if((relSource && !relBinary) || (subSource && !subBinary))
  2800. {
  2801. objectName = relFromSource;
  2802. }
  2803. else if((relBinary && !relSource) || (subBinary && !subSource))
  2804. {
  2805. objectName = relFromBinary;
  2806. }
  2807. else if(relFromBinary.length() < relFromSource.length())
  2808. {
  2809. objectName = relFromBinary;
  2810. }
  2811. else
  2812. {
  2813. objectName = relFromSource;
  2814. }
  2815. // if it is still a full path check for the try compile case
  2816. // try compile never have in source sources, and should not
  2817. // have conflicting source file names in the same target
  2818. if(cmSystemTools::FileIsFullPath(objectName.c_str()))
  2819. {
  2820. if(this->GetGlobalGenerator()->GetCMakeInstance()->GetIsInTryCompile())
  2821. {
  2822. objectName = cmSystemTools::GetFilenameName(source.GetFullPath());
  2823. }
  2824. }
  2825. // Replace the original source file extension with the object file
  2826. // extension.
  2827. bool keptSourceExtension = true;
  2828. if(!source.GetPropertyAsBool("KEEP_EXTENSION"))
  2829. {
  2830. // Decide whether this language wants to replace the source
  2831. // extension with the object extension. For CMake 2.4
  2832. // compatibility do this by default.
  2833. bool replaceExt = this->NeedBackwardsCompatibility(2, 4);
  2834. if(!replaceExt)
  2835. {
  2836. if(const char* lang = source.GetLanguage())
  2837. {
  2838. std::string repVar = "CMAKE_";
  2839. repVar += lang;
  2840. repVar += "_OUTPUT_EXTENSION_REPLACE";
  2841. replaceExt = this->Makefile->IsOn(repVar.c_str());
  2842. }
  2843. }
  2844. // Remove the source extension if it is to be replaced.
  2845. if(replaceExt)
  2846. {
  2847. keptSourceExtension = false;
  2848. std::string::size_type dot_pos = objectName.rfind(".");
  2849. if(dot_pos != std::string::npos)
  2850. {
  2851. objectName = objectName.substr(0, dot_pos);
  2852. }
  2853. }
  2854. // Store the new extension.
  2855. objectName +=
  2856. this->GlobalGenerator->GetLanguageOutputExtension(source);
  2857. }
  2858. if(hasSourceExtension)
  2859. {
  2860. *hasSourceExtension = keptSourceExtension;
  2861. }
  2862. // Convert to a safe name.
  2863. return this->CreateSafeUniqueObjectFileName(objectName.c_str(), dir_max);
  2864. }
  2865. //----------------------------------------------------------------------------
  2866. const char*
  2867. cmLocalGenerator
  2868. ::GetSourceFileLanguage(const cmSourceFile& source)
  2869. {
  2870. return source.GetLanguage();
  2871. }
  2872. //----------------------------------------------------------------------------
  2873. std::string cmLocalGenerator::EscapeForShellOldStyle(const char* str)
  2874. {
  2875. std::string result;
  2876. #if defined(_WIN32) && !defined(__CYGWIN__)
  2877. // if there are spaces
  2878. std::string temp = str;
  2879. if (temp.find(" ") != std::string::npos &&
  2880. temp.find("\"")==std::string::npos)
  2881. {
  2882. result = "\"";
  2883. result += str;
  2884. result += "\"";
  2885. return result;
  2886. }
  2887. return str;
  2888. #else
  2889. for(const char* ch = str; *ch != '\0'; ++ch)
  2890. {
  2891. if(*ch == ' ')
  2892. {
  2893. result += '\\';
  2894. }
  2895. result += *ch;
  2896. }
  2897. return result;
  2898. #endif
  2899. }
  2900. //----------------------------------------------------------------------------
  2901. static bool cmLocalGeneratorIsShellOperator(const char* str)
  2902. {
  2903. if(strcmp(str, "<") == 0 ||
  2904. strcmp(str, ">") == 0 ||
  2905. strcmp(str, "<<") == 0 ||
  2906. strcmp(str, ">>") == 0 ||
  2907. strcmp(str, "|") == 0 ||
  2908. strcmp(str, "||") == 0 ||
  2909. strcmp(str, "&&") == 0 ||
  2910. strcmp(str, "&>") == 0 ||
  2911. strcmp(str, "1>") == 0 ||
  2912. strcmp(str, "2>") == 0 ||
  2913. strcmp(str, "2>&1") == 0 ||
  2914. strcmp(str, "1>&2") == 0)
  2915. {
  2916. return true;
  2917. }
  2918. return false;
  2919. }
  2920. //----------------------------------------------------------------------------
  2921. std::string cmLocalGenerator::EscapeForShell(const char* str, bool makeVars,
  2922. bool forEcho)
  2923. {
  2924. // Do not escape shell operators.
  2925. if(cmLocalGeneratorIsShellOperator(str))
  2926. {
  2927. return str;
  2928. }
  2929. // Compute the flags for the target shell environment.
  2930. int flags = 0;
  2931. if(this->WindowsVSIDE)
  2932. {
  2933. flags |= cmsysSystem_Shell_Flag_VSIDE;
  2934. }
  2935. else if(!this->LinkScriptShell)
  2936. {
  2937. flags |= cmsysSystem_Shell_Flag_Make;
  2938. }
  2939. if(makeVars)
  2940. {
  2941. flags |= cmsysSystem_Shell_Flag_AllowMakeVariables;
  2942. }
  2943. if(forEcho)
  2944. {
  2945. flags |= cmsysSystem_Shell_Flag_EchoWindows;
  2946. }
  2947. if(this->WatcomWMake)
  2948. {
  2949. flags |= cmsysSystem_Shell_Flag_WatcomWMake;
  2950. }
  2951. if(this->MinGWMake)
  2952. {
  2953. flags |= cmsysSystem_Shell_Flag_MinGWMake;
  2954. }
  2955. if(this->NMake)
  2956. {
  2957. flags |= cmsysSystem_Shell_Flag_NMake;
  2958. }
  2959. // Compute the buffer size needed.
  2960. int size = (this->WindowsShell ?
  2961. cmsysSystem_Shell_GetArgumentSizeForWindows(str, flags) :
  2962. cmsysSystem_Shell_GetArgumentSizeForUnix(str, flags));
  2963. // Compute the shell argument itself.
  2964. std::vector<char> arg(size);
  2965. if(this->WindowsShell)
  2966. {
  2967. cmsysSystem_Shell_GetArgumentForWindows(str, &arg[0], flags);
  2968. }
  2969. else
  2970. {
  2971. cmsysSystem_Shell_GetArgumentForUnix(str, &arg[0], flags);
  2972. }
  2973. return std::string(&arg[0]);
  2974. }
  2975. //----------------------------------------------------------------------------
  2976. std::string cmLocalGenerator::EscapeForCMake(const char* str)
  2977. {
  2978. // Always double-quote the argument to take care of most escapes.
  2979. std::string result = "\"";
  2980. for(const char* c = str; *c; ++c)
  2981. {
  2982. if(*c == '"')
  2983. {
  2984. // Escape the double quote to avoid ending the argument.
  2985. result += "\\\"";
  2986. }
  2987. else if(*c == '$')
  2988. {
  2989. // Escape the dollar to avoid expanding variables.
  2990. result += "\\$";
  2991. }
  2992. else if(*c == '\\')
  2993. {
  2994. // Escape the backslash to avoid other escapes.
  2995. result += "\\\\";
  2996. }
  2997. else
  2998. {
  2999. // Other characters will be parsed correctly.
  3000. result += *c;
  3001. }
  3002. }
  3003. result += "\"";
  3004. return result;
  3005. }
  3006. //----------------------------------------------------------------------------
  3007. cmLocalGenerator::FortranFormat
  3008. cmLocalGenerator::GetFortranFormat(const char* value)
  3009. {
  3010. FortranFormat format = FortranFormatNone;
  3011. if(value && *value)
  3012. {
  3013. std::vector<std::string> fmt;
  3014. cmSystemTools::ExpandListArgument(value, fmt);
  3015. for(std::vector<std::string>::iterator fi = fmt.begin();
  3016. fi != fmt.end(); ++fi)
  3017. {
  3018. if(*fi == "FIXED")
  3019. {
  3020. format = FortranFormatFixed;
  3021. }
  3022. if(*fi == "FREE")
  3023. {
  3024. format = FortranFormatFree;
  3025. }
  3026. }
  3027. }
  3028. return format;
  3029. }
  3030. //----------------------------------------------------------------------------
  3031. std::string
  3032. cmLocalGenerator::GetTargetDirectory(cmTarget const&) const
  3033. {
  3034. cmSystemTools::Error("GetTargetDirectory"
  3035. " called on cmLocalGenerator");
  3036. return "";
  3037. }
  3038. //----------------------------------------------------------------------------
  3039. unsigned int cmLocalGenerator::GetBackwardsCompatibility()
  3040. {
  3041. // The computed version may change until the project is fully
  3042. // configured.
  3043. if(!this->BackwardsCompatibilityFinal)
  3044. {
  3045. unsigned int major = 0;
  3046. unsigned int minor = 0;
  3047. unsigned int patch = 0;
  3048. if(const char* value
  3049. = this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY"))
  3050. {
  3051. switch(sscanf(value, "%u.%u.%u", &major, &minor, &patch))
  3052. {
  3053. case 2: patch = 0; break;
  3054. case 1: minor = 0; patch = 0; break;
  3055. default: break;
  3056. }
  3057. }
  3058. this->BackwardsCompatibility = CMake_VERSION_ENCODE(major, minor, patch);
  3059. this->BackwardsCompatibilityFinal = this->Configured;
  3060. }
  3061. return this->BackwardsCompatibility;
  3062. }
  3063. //----------------------------------------------------------------------------
  3064. bool cmLocalGenerator::NeedBackwardsCompatibility(unsigned int major,
  3065. unsigned int minor,
  3066. unsigned int patch)
  3067. {
  3068. // Check the policy to decide whether to pay attention to this
  3069. // variable.
  3070. switch(this->Makefile->GetPolicyStatus(cmPolicies::CMP0001))
  3071. {
  3072. case cmPolicies::WARN:
  3073. // WARN is just OLD without warning because user code does not
  3074. // always affect whether this check is done.
  3075. case cmPolicies::OLD:
  3076. // Old behavior is to check the variable.
  3077. break;
  3078. case cmPolicies::NEW:
  3079. // New behavior is to ignore the variable.
  3080. return false;
  3081. case cmPolicies::REQUIRED_IF_USED:
  3082. case cmPolicies::REQUIRED_ALWAYS:
  3083. // This will never be the case because the only way to require
  3084. // the setting is to require the user to specify version policy
  3085. // 2.6 or higher. Once we add that requirement then this whole
  3086. // method can be removed anyway.
  3087. return false;
  3088. }
  3089. // Compatibility is needed if CMAKE_BACKWARDS_COMPATIBILITY is set
  3090. // equal to or lower than the given version.
  3091. unsigned int actual_compat = this->GetBackwardsCompatibility();
  3092. return (actual_compat &&
  3093. actual_compat <= CMake_VERSION_ENCODE(major, minor, patch));
  3094. }
  3095. //----------------------------------------------------------------------------
  3096. bool cmLocalGenerator::CheckDefinition(std::string const& define) const
  3097. {
  3098. // Many compilers do not support -DNAME(arg)=sdf so we disable it.
  3099. bool function_style = false;
  3100. for(const char* c = define.c_str(); *c && *c != '='; ++c)
  3101. {
  3102. if(*c == '(')
  3103. {
  3104. function_style = true;
  3105. break;
  3106. }
  3107. }
  3108. if(function_style)
  3109. {
  3110. cmOStringStream e;
  3111. e << "WARNING: Function-style preprocessor definitions may not be "
  3112. << "passed on the compiler command line because many compilers "
  3113. << "do not support it.\n"
  3114. << "CMake is dropping a preprocessor definition: " << define << "\n"
  3115. << "Consider defining the macro in a (configured) header file.\n";
  3116. cmSystemTools::Message(e.str().c_str());
  3117. return false;
  3118. }
  3119. // Many compilers do not support # in the value so we disable it.
  3120. if(define.find_first_of("#") != define.npos)
  3121. {
  3122. cmOStringStream e;
  3123. e << "WARNING: Preprocessor definitions containing '#' may not be "
  3124. << "passed on the compiler command line because many compilers "
  3125. << "do not support it.\n"
  3126. << "CMake is dropping a preprocessor definition: " << define << "\n"
  3127. << "Consider defining the macro in a (configured) header file.\n";
  3128. cmSystemTools::Message(e.str().c_str());
  3129. return false;
  3130. }
  3131. // Assume it is supported.
  3132. return true;
  3133. }
  3134. //----------------------------------------------------------------------------
  3135. static void cmLGInfoProp(cmMakefile* mf, cmTarget* target, const char* prop)
  3136. {
  3137. if(const char* val = target->GetProperty(prop))
  3138. {
  3139. mf->AddDefinition(prop, val);
  3140. }
  3141. }
  3142. //----------------------------------------------------------------------------
  3143. void cmLocalGenerator::GenerateAppleInfoPList(cmTarget* target,
  3144. const char* targetName,
  3145. const char* fname)
  3146. {
  3147. // Find the Info.plist template.
  3148. const char* in = target->GetProperty("MACOSX_BUNDLE_INFO_PLIST");
  3149. std::string inFile = (in && *in)? in : "MacOSXBundleInfo.plist.in";
  3150. if(!cmSystemTools::FileIsFullPath(inFile.c_str()))
  3151. {
  3152. std::string inMod = this->Makefile->GetModulesFile(inFile.c_str());
  3153. if(!inMod.empty())
  3154. {
  3155. inFile = inMod;
  3156. }
  3157. }
  3158. if(!cmSystemTools::FileExists(inFile.c_str(), true))
  3159. {
  3160. cmOStringStream e;
  3161. e << "Target " << target->GetName() << " Info.plist template \""
  3162. << inFile << "\" could not be found.";
  3163. cmSystemTools::Error(e.str().c_str());
  3164. return;
  3165. }
  3166. // Convert target properties to variables in an isolated makefile
  3167. // scope to configure the file. If properties are set they will
  3168. // override user make variables. If not the configuration will fall
  3169. // back to the directory-level values set by the user.
  3170. cmMakefile* mf = this->Makefile;
  3171. mf->PushScope();
  3172. mf->AddDefinition("MACOSX_BUNDLE_EXECUTABLE_NAME", targetName);
  3173. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_INFO_STRING");
  3174. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_ICON_FILE");
  3175. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_GUI_IDENTIFIER");
  3176. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_LONG_VERSION_STRING");
  3177. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_BUNDLE_NAME");
  3178. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_SHORT_VERSION_STRING");
  3179. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_BUNDLE_VERSION");
  3180. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_COPYRIGHT");
  3181. mf->ConfigureFile(inFile.c_str(), fname, false, false, false);
  3182. mf->PopScope();
  3183. }
  3184. //----------------------------------------------------------------------------
  3185. void cmLocalGenerator::GenerateFrameworkInfoPList(cmTarget* target,
  3186. const char* targetName,
  3187. const char* fname)
  3188. {
  3189. // Find the Info.plist template.
  3190. const char* in = target->GetProperty("MACOSX_FRAMEWORK_INFO_PLIST");
  3191. std::string inFile = (in && *in)? in : "MacOSXFrameworkInfo.plist.in";
  3192. if(!cmSystemTools::FileIsFullPath(inFile.c_str()))
  3193. {
  3194. std::string inMod = this->Makefile->GetModulesFile(inFile.c_str());
  3195. if(!inMod.empty())
  3196. {
  3197. inFile = inMod;
  3198. }
  3199. }
  3200. if(!cmSystemTools::FileExists(inFile.c_str(), true))
  3201. {
  3202. cmOStringStream e;
  3203. e << "Target " << target->GetName() << " Info.plist template \""
  3204. << inFile << "\" could not be found.";
  3205. cmSystemTools::Error(e.str().c_str());
  3206. return;
  3207. }
  3208. // Convert target properties to variables in an isolated makefile
  3209. // scope to configure the file. If properties are set they will
  3210. // override user make variables. If not the configuration will fall
  3211. // back to the directory-level values set by the user.
  3212. cmMakefile* mf = this->Makefile;
  3213. mf->PushScope();
  3214. mf->AddDefinition("MACOSX_FRAMEWORK_NAME", targetName);
  3215. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_ICON_FILE");
  3216. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_IDENTIFIER");
  3217. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_SHORT_VERSION_STRING");
  3218. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_BUNDLE_VERSION");
  3219. mf->ConfigureFile(inFile.c_str(), fname, false, false, false);
  3220. mf->PopScope();
  3221. }