cmLocalGenerator.cxx 112 KB

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