cmLocalGenerator.cxx 112 KB

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