cmLocalGenerator.cxx 109 KB

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