cmLocalGenerator.cxx 113 KB

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