cmLocalVisualStudio6Generator.cxx 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012
  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 "cmGlobalGenerator.h"
  11. #include "cmLocalVisualStudio6Generator.h"
  12. #include "cmMakefile.h"
  13. #include "cmSystemTools.h"
  14. #include "cmSourceFile.h"
  15. #include "cmGeneratorTarget.h"
  16. #include "cmCustomCommandGenerator.h"
  17. #include "cmake.h"
  18. #include "cmComputeLinkInformation.h"
  19. #include <cmsys/RegularExpression.hxx>
  20. #include <cmsys/FStream.hxx>
  21. cmLocalVisualStudio6Generator
  22. ::cmLocalVisualStudio6Generator(cmGlobalGenerator* gg,
  23. cmLocalGenerator* parent,
  24. cmState::Snapshot snapshot):
  25. cmLocalVisualStudioGenerator(gg, parent, snapshot)
  26. {
  27. }
  28. cmLocalVisualStudio6Generator::~cmLocalVisualStudio6Generator()
  29. {
  30. }
  31. //----------------------------------------------------------------------------
  32. // Helper class to write build events.
  33. class cmLocalVisualStudio6Generator::EventWriter
  34. {
  35. public:
  36. EventWriter(cmLocalVisualStudio6Generator* lg,
  37. const std::string& config, std::string& code):
  38. LG(lg), Config(config), Code(code), First(true) {}
  39. void Start(const char* event)
  40. {
  41. this->First = true;
  42. this->Event = event;
  43. }
  44. void Finish()
  45. {
  46. this->Code += (this->First? "" : "\n");
  47. }
  48. void Write(std::vector<cmCustomCommand> const& ccs)
  49. {
  50. for(std::vector<cmCustomCommand>::const_iterator ci = ccs.begin();
  51. ci != ccs.end(); ++ci)
  52. {
  53. this->Write(*ci);
  54. }
  55. }
  56. void Write(cmCustomCommand const& cc)
  57. {
  58. cmCustomCommandGenerator ccg(cc, this->Config, this->LG);
  59. if(this->First)
  60. {
  61. this->Code += this->Event + "_Cmds=";
  62. this->First = false;
  63. }
  64. else
  65. {
  66. this->Code += "\\\n\t";
  67. }
  68. this->Code += this->LG->ConstructScript(ccg, "\\\n\t");
  69. }
  70. private:
  71. cmLocalVisualStudio6Generator* LG;
  72. std::string Config;
  73. std::string& Code;
  74. bool First;
  75. std::string Event;
  76. };
  77. void cmLocalVisualStudio6Generator::AddCMakeListsRules()
  78. {
  79. cmTargets &tgts = this->Makefile->GetTargets();
  80. for(cmTargets::iterator l = tgts.begin();
  81. l != tgts.end(); l++)
  82. {
  83. if (l->second.GetType() == cmTarget::INTERFACE_LIBRARY
  84. || l->second.GetType() == cmTarget::GLOBAL_TARGET)
  85. {
  86. continue;
  87. }
  88. // Add a rule to regenerate the build system when the target
  89. // specification source changes.
  90. const char* suppRegenRule =
  91. this->Makefile->GetDefinition("CMAKE_SUPPRESS_REGENERATION");
  92. if (!cmSystemTools::IsOn(suppRegenRule))
  93. {
  94. this->AddDSPBuildRule(l->second);
  95. }
  96. }
  97. }
  98. void cmLocalVisualStudio6Generator::Generate()
  99. {
  100. this->OutputDSPFile();
  101. }
  102. void cmLocalVisualStudio6Generator::OutputDSPFile()
  103. {
  104. // If not an in source build, then create the output directory
  105. if(strcmp(this->Makefile->GetCurrentBinaryDirectory(),
  106. this->Makefile->GetHomeDirectory()) != 0)
  107. {
  108. if(!cmSystemTools::MakeDirectory
  109. (this->Makefile->GetCurrentBinaryDirectory()))
  110. {
  111. cmSystemTools::Error("Error creating directory ",
  112. this->Makefile->GetCurrentBinaryDirectory());
  113. }
  114. }
  115. // Create the DSP or set of DSP's for libraries and executables
  116. cmTargets &tgts = this->Makefile->GetTargets();
  117. // build any targets
  118. for(cmTargets::iterator l = tgts.begin();
  119. l != tgts.end(); l++)
  120. {
  121. switch(l->second.GetType())
  122. {
  123. case cmTarget::STATIC_LIBRARY:
  124. case cmTarget::OBJECT_LIBRARY:
  125. this->SetBuildType(STATIC_LIBRARY, l->first.c_str(), l->second);
  126. break;
  127. case cmTarget::SHARED_LIBRARY:
  128. case cmTarget::MODULE_LIBRARY:
  129. this->SetBuildType(DLL, l->first.c_str(), l->second);
  130. break;
  131. case cmTarget::EXECUTABLE:
  132. this->SetBuildType(EXECUTABLE,l->first.c_str(), l->second);
  133. break;
  134. case cmTarget::UTILITY:
  135. case cmTarget::GLOBAL_TARGET:
  136. this->SetBuildType(UTILITY, l->first.c_str(), l->second);
  137. break;
  138. case cmTarget::INTERFACE_LIBRARY:
  139. continue;
  140. default:
  141. cmSystemTools::Error("Bad target type: ", l->first.c_str());
  142. break;
  143. }
  144. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  145. // so don't build a projectfile for it
  146. const char* path =
  147. l->second.GetProperty("EXTERNAL_MSPROJECT");
  148. if(!path)
  149. {
  150. // check to see if the dsp is going into a sub-directory
  151. std::string::size_type pos = l->first.rfind('/');
  152. if(pos != std::string::npos)
  153. {
  154. std::string dir = this->Makefile->GetCurrentBinaryDirectory();
  155. dir += "/";
  156. dir += l->first.substr(0, pos);
  157. if(!cmSystemTools::MakeDirectory(dir.c_str()))
  158. {
  159. cmSystemTools::Error("Error creating directory: ", dir.c_str());
  160. }
  161. }
  162. this->CreateSingleDSP(l->first.c_str(),l->second);
  163. }
  164. }
  165. }
  166. // Utility function to make a valid VS6 *.dsp filename out
  167. // of a CMake target name:
  168. //
  169. extern std::string GetVS6TargetName(const std::string& targetName);
  170. void cmLocalVisualStudio6Generator::CreateSingleDSP(const std::string& lname,
  171. cmTarget &target)
  172. {
  173. // add to the list of projects
  174. std::string pname = GetVS6TargetName(lname);
  175. // create the dsp.cmake file
  176. std::string fname;
  177. fname = this->Makefile->GetCurrentBinaryDirectory();
  178. fname += "/";
  179. fname += pname;
  180. fname += ".dsp";
  181. // save the name of the real dsp file
  182. std::string realDSP = fname;
  183. fname += ".cmake";
  184. cmsys::ofstream fout(fname.c_str());
  185. if(!fout)
  186. {
  187. cmSystemTools::Error("Error Writing ", fname.c_str());
  188. cmSystemTools::ReportLastSystemError("");
  189. }
  190. this->WriteDSPFile(fout,pname.c_str(),target);
  191. fout.close();
  192. // if the dsp file has changed, then write it.
  193. cmSystemTools::CopyFileIfDifferent(fname.c_str(), realDSP.c_str());
  194. }
  195. void cmLocalVisualStudio6Generator::AddDSPBuildRule(cmTarget& tgt)
  196. {
  197. std::string dspname = GetVS6TargetName(tgt.GetName());
  198. dspname += ".dsp.cmake";
  199. cmCustomCommandLine commandLine;
  200. commandLine.push_back(cmSystemTools::GetCMakeCommand());
  201. std::string makefileIn = this->Makefile->GetCurrentSourceDirectory();
  202. makefileIn += "/";
  203. makefileIn += "CMakeLists.txt";
  204. if(!cmSystemTools::FileExists(makefileIn.c_str()))
  205. {
  206. return;
  207. }
  208. std::string comment = "Building Custom Rule ";
  209. comment += makefileIn;
  210. std::string args;
  211. args = "-H";
  212. args += this->Makefile->GetHomeDirectory();
  213. commandLine.push_back(args);
  214. args = "-B";
  215. args += this->Makefile->GetHomeOutputDirectory();
  216. commandLine.push_back(args);
  217. std::vector<std::string> const& listFiles = this->Makefile->GetListFiles();
  218. cmCustomCommandLines commandLines;
  219. commandLines.push_back(commandLine);
  220. const char* no_working_directory = 0;
  221. this->Makefile->AddCustomCommandToOutput(dspname.c_str(), listFiles,
  222. makefileIn.c_str(), commandLines,
  223. comment.c_str(),
  224. no_working_directory, true);
  225. if(this->Makefile->GetSource(makefileIn.c_str()))
  226. {
  227. tgt.AddSource(makefileIn);
  228. }
  229. else
  230. {
  231. cmSystemTools::Error("Error adding rule for ", makefileIn.c_str());
  232. }
  233. }
  234. void cmLocalVisualStudio6Generator::WriteDSPFile(std::ostream& fout,
  235. const std::string& libName,
  236. cmTarget &target)
  237. {
  238. // For utility targets need custom command since pre- and post-
  239. // build does not do anything in Visual Studio 6. In order for the
  240. // rules to run in the correct order as custom commands, we need
  241. // special care for dependencies. The first rule must depend on all
  242. // the dependencies of all the rules. The later rules must each
  243. // depend only on the previous rule.
  244. if ((target.GetType() == cmTarget::UTILITY ||
  245. target.GetType() == cmTarget::GLOBAL_TARGET) &&
  246. (!target.GetPreBuildCommands().empty() ||
  247. !target.GetPostBuildCommands().empty()))
  248. {
  249. // Accumulate the dependencies of all the commands.
  250. std::vector<std::string> depends;
  251. for (std::vector<cmCustomCommand>::const_iterator cr =
  252. target.GetPreBuildCommands().begin();
  253. cr != target.GetPreBuildCommands().end(); ++cr)
  254. {
  255. depends.insert(depends.end(),
  256. cr->GetDepends().begin(), cr->GetDepends().end());
  257. }
  258. for (std::vector<cmCustomCommand>::const_iterator cr =
  259. target.GetPostBuildCommands().begin();
  260. cr != target.GetPostBuildCommands().end(); ++cr)
  261. {
  262. depends.insert(depends.end(),
  263. cr->GetDepends().begin(), cr->GetDepends().end());
  264. }
  265. // Add the pre- and post-build commands in order.
  266. int count = 1;
  267. for (std::vector<cmCustomCommand>::const_iterator cr =
  268. target.GetPreBuildCommands().begin();
  269. cr != target.GetPreBuildCommands().end(); ++cr)
  270. {
  271. this->AddUtilityCommandHack(target, count++, depends, *cr);
  272. }
  273. for (std::vector<cmCustomCommand>::const_iterator cr =
  274. target.GetPostBuildCommands().begin();
  275. cr != target.GetPostBuildCommands().end(); ++cr)
  276. {
  277. this->AddUtilityCommandHack(target, count++, depends, *cr);
  278. }
  279. }
  280. // We may be modifying the source groups temporarily, so make a copy.
  281. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  282. cmGeneratorTarget* gt =
  283. this->GlobalGenerator->GetGeneratorTarget(&target);
  284. // get the classes from the source lists then add them to the groups
  285. std::vector<cmSourceFile*> classes;
  286. if (!gt->GetConfigCommonSourceFiles(classes))
  287. {
  288. return;
  289. }
  290. // now all of the source files have been properly assigned to the target
  291. // now stick them into source groups using the reg expressions
  292. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  293. i != classes.end(); i++)
  294. {
  295. if (!(*i)->GetObjectLibrary().empty())
  296. {
  297. continue;
  298. }
  299. // Add the file to the list of sources.
  300. std::string source = (*i)->GetFullPath();
  301. cmSourceGroup* sourceGroup =
  302. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  303. sourceGroup->AssignSource(*i);
  304. // while we are at it, if it is a .rule file then for visual studio 6 we
  305. // must generate it
  306. if ((*i)->GetPropertyAsBool("__CMAKE_RULE"))
  307. {
  308. if(!cmSystemTools::FileExists(source.c_str()))
  309. {
  310. cmSystemTools::ReplaceString(source, "$(IntDir)/", "");
  311. // Make sure the path exists for the file
  312. std::string path = cmSystemTools::GetFilenamePath(source);
  313. cmSystemTools::MakeDirectory(path.c_str());
  314. #if defined(_WIN32) || defined(__CYGWIN__)
  315. cmsys::ofstream sourceFout(source.c_str(),
  316. std::ios::binary | std::ios::out
  317. | std::ios::trunc);
  318. #else
  319. cmsys::ofstream sourceFout(source.c_str(),
  320. std::ios::out | std::ios::trunc);
  321. #endif
  322. if(sourceFout)
  323. {
  324. sourceFout.write("# generated from CMake",22);
  325. sourceFout.flush();
  326. sourceFout.close();
  327. }
  328. }
  329. }
  330. }
  331. // Write the DSP file's header.
  332. this->WriteDSPHeader(fout, libName, target, sourceGroups);
  333. // Loop through every source group.
  334. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  335. sg != sourceGroups.end(); ++sg)
  336. {
  337. this->WriteGroup(&(*sg), target, fout, libName);
  338. }
  339. // Write the DSP file's footer.
  340. this->WriteDSPFooter(fout);
  341. }
  342. void cmLocalVisualStudio6Generator
  343. ::WriteGroup(const cmSourceGroup *sg, cmTarget& target,
  344. std::ostream &fout, const std::string& libName)
  345. {
  346. cmGeneratorTarget* gt =
  347. this->GlobalGenerator->GetGeneratorTarget(&target);
  348. const std::vector<const cmSourceFile *> &sourceFiles =
  349. sg->GetSourceFiles();
  350. // If the group is empty, don't write it at all.
  351. if(sourceFiles.empty() && sg->GetGroupChildren().empty())
  352. {
  353. return;
  354. }
  355. // If the group has a name, write the header.
  356. std::string name = sg->GetName();
  357. if(name != "")
  358. {
  359. this->WriteDSPBeginGroup(fout, name.c_str(), "");
  360. }
  361. // Loop through each source in the source group.
  362. for(std::vector<const cmSourceFile *>::const_iterator sf =
  363. sourceFiles.begin(); sf != sourceFiles.end(); ++sf)
  364. {
  365. if (!(*sf)->GetObjectLibrary().empty())
  366. {
  367. continue;
  368. }
  369. std::string source = (*sf)->GetFullPath();
  370. const cmCustomCommand *command =
  371. (*sf)->GetCustomCommand();
  372. std::string compileFlags;
  373. std::vector<std::string> depends;
  374. std::string objectNameDir;
  375. if(gt->HasExplicitObjectName(*sf))
  376. {
  377. objectNameDir = cmSystemTools::GetFilenamePath(gt->GetObjectName(*sf));
  378. }
  379. // Add per-source file flags.
  380. if(const char* cflags = (*sf)->GetProperty("COMPILE_FLAGS"))
  381. {
  382. compileFlags += cflags;
  383. }
  384. const std::string& lang = this->GetSourceFileLanguage(*(*sf));
  385. if(lang == "CXX")
  386. {
  387. // force a C++ file type
  388. compileFlags += " /TP ";
  389. }
  390. else if(lang == "C")
  391. {
  392. // force to c file type
  393. compileFlags += " /TC ";
  394. }
  395. // Add per-source and per-configuration preprocessor definitions.
  396. std::map<std::string, std::string> cdmap;
  397. {
  398. std::set<std::string> targetCompileDefinitions;
  399. this->AppendDefines(targetCompileDefinitions,
  400. (*sf)->GetProperty("COMPILE_DEFINITIONS"));
  401. this->JoinDefines(targetCompileDefinitions, compileFlags, lang);
  402. }
  403. if(const char* cdefs = (*sf)->GetProperty("COMPILE_DEFINITIONS_DEBUG"))
  404. {
  405. std::set<std::string> debugCompileDefinitions;
  406. this->AppendDefines(debugCompileDefinitions, cdefs);
  407. this->JoinDefines(debugCompileDefinitions, cdmap["DEBUG"], lang);
  408. }
  409. if(const char* cdefs = (*sf)->GetProperty("COMPILE_DEFINITIONS_RELEASE"))
  410. {
  411. std::set<std::string> releaseCompileDefinitions;
  412. this->AppendDefines(releaseCompileDefinitions, cdefs);
  413. this->JoinDefines(releaseCompileDefinitions, cdmap["RELEASE"], lang);
  414. }
  415. if(const char* cdefs =
  416. (*sf)->GetProperty("COMPILE_DEFINITIONS_MINSIZEREL"))
  417. {
  418. std::set<std::string> minsizerelCompileDefinitions;
  419. this->AppendDefines(minsizerelCompileDefinitions, cdefs);
  420. this->JoinDefines(minsizerelCompileDefinitions, cdmap["MINSIZEREL"],
  421. lang);
  422. }
  423. if(const char* cdefs =
  424. (*sf)->GetProperty("COMPILE_DEFINITIONS_RELWITHDEBINFO"))
  425. {
  426. std::set<std::string> relwithdebinfoCompileDefinitions;
  427. this->AppendDefines(relwithdebinfoCompileDefinitions, cdefs);
  428. this->JoinDefines(relwithdebinfoCompileDefinitions,
  429. cdmap["RELWITHDEBINFO"], lang);
  430. }
  431. bool excludedFromBuild =
  432. (!lang.empty() && (*sf)->GetPropertyAsBool("HEADER_FILE_ONLY"));
  433. // Check for extra object-file dependencies.
  434. const char* dependsValue = (*sf)->GetProperty("OBJECT_DEPENDS");
  435. if(dependsValue)
  436. {
  437. cmSystemTools::ExpandListArgument(dependsValue, depends);
  438. }
  439. if (GetVS6TargetName(source) != libName ||
  440. target.GetType() == cmTarget::UTILITY ||
  441. target.GetType() == cmTarget::GLOBAL_TARGET)
  442. {
  443. fout << "# Begin Source File\n\n";
  444. // Tell MS-Dev what the source is. If the compiler knows how to
  445. // build it, then it will.
  446. fout << "SOURCE=" <<
  447. this->ConvertToOutputFormat(source.c_str(), SHELL) << "\n\n";
  448. if(!depends.empty())
  449. {
  450. // Write out the dependencies for the rule.
  451. fout << "USERDEP__HACK=";
  452. for(std::vector<std::string>::const_iterator d = depends.begin();
  453. d != depends.end(); ++d)
  454. {
  455. fout << "\\\n\t" <<
  456. this->ConvertToOutputFormat(d->c_str(), SHELL);
  457. }
  458. fout << "\n";
  459. }
  460. if (command)
  461. {
  462. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  463. this->WriteCustomRule(fout, source.c_str(), *command, flags);
  464. }
  465. else if(!compileFlags.empty() || !objectNameDir.empty() ||
  466. excludedFromBuild || !cdmap.empty())
  467. {
  468. for(std::vector<std::string>::iterator i
  469. = this->Configurations.begin();
  470. i != this->Configurations.end(); ++i)
  471. {
  472. // Strip the subdirectory name out of the configuration name.
  473. std::string config = this->GetConfigName(*i);
  474. if (i == this->Configurations.begin())
  475. {
  476. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  477. }
  478. else
  479. {
  480. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  481. }
  482. if(excludedFromBuild)
  483. {
  484. fout << "# PROP Exclude_From_Build 1\n";
  485. }
  486. if(!compileFlags.empty())
  487. {
  488. fout << "\n# ADD CPP " << compileFlags << "\n\n";
  489. }
  490. std::map<std::string, std::string>::iterator cdi =
  491. cdmap.find(cmSystemTools::UpperCase(config));
  492. if(cdi != cdmap.end() && !cdi->second.empty())
  493. {
  494. fout << "\n# ADD CPP " << cdi->second << "\n\n";
  495. }
  496. if(!objectNameDir.empty())
  497. {
  498. // Setup an alternate object file directory.
  499. fout << "\n# PROP Intermediate_Dir \""
  500. << config << "/" << objectNameDir << "\"\n\n";
  501. }
  502. }
  503. fout << "!ENDIF\n\n";
  504. }
  505. fout << "# End Source File\n";
  506. }
  507. }
  508. std::vector<cmSourceGroup> const& children = sg->GetGroupChildren();
  509. for(unsigned int i=0;i<children.size();++i)
  510. {
  511. this->WriteGroup(&children[i], target, fout, libName);
  512. }
  513. // If the group has a name, write the footer.
  514. if(name != "")
  515. {
  516. this->WriteDSPEndGroup(fout);
  517. }
  518. }
  519. void
  520. cmLocalVisualStudio6Generator
  521. ::AddUtilityCommandHack(cmTarget& target, int count,
  522. std::vector<std::string>& depends,
  523. const cmCustomCommand& origCommand)
  524. {
  525. // Create a fake output that forces the rule to run.
  526. char* output = new char[(strlen(this->Makefile->GetCurrentBinaryDirectory())
  527. + target.GetName().size() + 30)];
  528. sprintf(output,"%s/%s_force_%i", this->Makefile->GetCurrentBinaryDirectory(),
  529. target.GetName().c_str(), count);
  530. const char* comment = origCommand.GetComment();
  531. if(!comment && origCommand.GetOutputs().empty())
  532. {
  533. comment = "<hack>";
  534. }
  535. // Add the rule with the given dependencies and commands.
  536. std::string no_main_dependency = "";
  537. if(cmSourceFile* outsf =
  538. this->Makefile->AddCustomCommandToOutput(
  539. output, depends, no_main_dependency,
  540. origCommand.GetCommandLines(), comment,
  541. origCommand.GetWorkingDirectory().c_str()))
  542. {
  543. target.AddSource(outsf->GetFullPath());
  544. }
  545. // Replace the dependencies with the output of this rule so that the
  546. // next rule added will run after this one.
  547. depends.clear();
  548. depends.push_back(output);
  549. // Free the fake output name.
  550. delete [] output;
  551. }
  552. void
  553. cmLocalVisualStudio6Generator
  554. ::WriteCustomRule(std::ostream& fout,
  555. const char* source,
  556. const cmCustomCommand& command,
  557. const char* flags)
  558. {
  559. // Write the rule for each configuration.
  560. std::vector<std::string>::iterator i;
  561. for(i = this->Configurations.begin(); i != this->Configurations.end(); ++i)
  562. {
  563. std::string config = this->GetConfigName(*i);
  564. cmCustomCommandGenerator ccg(command, config, this);
  565. std::string comment =
  566. this->ConstructComment(ccg, "Building Custom Rule $(InputPath)");
  567. if(comment == "<hack>")
  568. {
  569. comment = "";
  570. }
  571. std::string script =
  572. this->ConstructScript(ccg, "\\\n\t");
  573. if (i == this->Configurations.begin())
  574. {
  575. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  576. }
  577. else
  578. {
  579. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  580. }
  581. if(flags)
  582. {
  583. fout << "\n# ADD CPP " << flags << "\n\n";
  584. }
  585. // Write out the dependencies for the rule.
  586. fout << "USERDEP__HACK=";
  587. for(std::vector<std::string>::const_iterator d =
  588. ccg.GetDepends().begin();
  589. d != ccg.GetDepends().end();
  590. ++d)
  591. {
  592. // Lookup the real name of the dependency in case it is a CMake target.
  593. std::string dep;
  594. if(this->GetRealDependency(d->c_str(), config.c_str(), dep))
  595. {
  596. fout << "\\\n\t" <<
  597. this->ConvertToOutputFormat(dep.c_str(), SHELL);
  598. }
  599. }
  600. fout << "\n";
  601. fout << "# PROP Ignore_Default_Tool 1\n";
  602. fout << "# Begin Custom Build -";
  603. if(!comment.empty())
  604. {
  605. fout << " " << comment.c_str();
  606. }
  607. fout << "\n\n";
  608. if(ccg.GetOutputs().empty())
  609. {
  610. fout << source
  611. << "_force : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  612. fout << script.c_str() << "\n\n";
  613. }
  614. else
  615. {
  616. for(std::vector<std::string>::const_iterator o =
  617. ccg.GetOutputs().begin();
  618. o != ccg.GetOutputs().end();
  619. ++o)
  620. {
  621. // Write a rule for every output generated by this command.
  622. fout << this->ConvertToOutputFormat(o->c_str(), SHELL)
  623. << " : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  624. fout << script.c_str() << "\n\n";
  625. }
  626. }
  627. fout << "# End Custom Build\n\n";
  628. }
  629. fout << "!ENDIF\n\n";
  630. }
  631. void cmLocalVisualStudio6Generator::WriteDSPBeginGroup(std::ostream& fout,
  632. const char* group,
  633. const char* filter)
  634. {
  635. fout << "# Begin Group \"" << group << "\"\n"
  636. "# PROP Default_Filter \"" << filter << "\"\n";
  637. }
  638. void cmLocalVisualStudio6Generator::WriteDSPEndGroup(std::ostream& fout)
  639. {
  640. fout << "# End Group\n";
  641. }
  642. void cmLocalVisualStudio6Generator::SetBuildType(BuildType b,
  643. const std::string& libName,
  644. cmTarget& target)
  645. {
  646. std::string root= this->Makefile->GetRequiredDefinition("CMAKE_ROOT");
  647. const char *def=
  648. this->Makefile->GetDefinition( "MSPROJECT_TEMPLATE_DIRECTORY");
  649. if( def)
  650. {
  651. root = def;
  652. }
  653. else
  654. {
  655. root += "/Templates";
  656. }
  657. switch(b)
  658. {
  659. case WIN32_EXECUTABLE:
  660. break;
  661. case STATIC_LIBRARY:
  662. this->DSPHeaderTemplate = root;
  663. this->DSPHeaderTemplate += "/staticLibHeader.dsptemplate";
  664. this->DSPFooterTemplate = root;
  665. this->DSPFooterTemplate += "/staticLibFooter.dsptemplate";
  666. break;
  667. case DLL:
  668. this->DSPHeaderTemplate = root;
  669. this->DSPHeaderTemplate += "/DLLHeader.dsptemplate";
  670. this->DSPFooterTemplate = root;
  671. this->DSPFooterTemplate += "/DLLFooter.dsptemplate";
  672. break;
  673. case EXECUTABLE:
  674. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  675. {
  676. this->DSPHeaderTemplate = root;
  677. this->DSPHeaderTemplate += "/EXEWinHeader.dsptemplate";
  678. this->DSPFooterTemplate = root;
  679. this->DSPFooterTemplate += "/EXEFooter.dsptemplate";
  680. }
  681. else
  682. {
  683. this->DSPHeaderTemplate = root;
  684. this->DSPHeaderTemplate += "/EXEHeader.dsptemplate";
  685. this->DSPFooterTemplate = root;
  686. this->DSPFooterTemplate += "/EXEFooter.dsptemplate";
  687. }
  688. break;
  689. case UTILITY:
  690. this->DSPHeaderTemplate = root;
  691. this->DSPHeaderTemplate += "/UtilityHeader.dsptemplate";
  692. this->DSPFooterTemplate = root;
  693. this->DSPFooterTemplate += "/UtilityFooter.dsptemplate";
  694. break;
  695. }
  696. // once the build type is set, determine what configurations are
  697. // possible
  698. cmsys::ifstream fin(this->DSPHeaderTemplate.c_str());
  699. cmsys::RegularExpression reg("# Name ");
  700. if(!fin)
  701. {
  702. cmSystemTools::Error("Error Reading ", this->DSPHeaderTemplate.c_str());
  703. }
  704. // reset this->Configurations
  705. this->Configurations.erase(this->Configurations.begin(),
  706. this->Configurations.end());
  707. // now add all the configurations possible
  708. std::string vs6name = GetVS6TargetName(libName);
  709. std::string line;
  710. while(cmSystemTools::GetLineFromStream(fin, line))
  711. {
  712. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME", vs6name.c_str());
  713. if (reg.find(line))
  714. {
  715. this->Configurations.push_back(line.substr(reg.end()));
  716. }
  717. }
  718. }
  719. //----------------------------------------------------------------------------
  720. cmsys::auto_ptr<cmCustomCommand>
  721. cmLocalVisualStudio6Generator::MaybeCreateOutputDir(cmTarget& target,
  722. const std::string& config)
  723. {
  724. cmsys::auto_ptr<cmCustomCommand> pcc;
  725. // VS6 forgets to create the output directory for archives if it
  726. // differs from the intermediate directory.
  727. if(target.GetType() != cmTarget::STATIC_LIBRARY) { return pcc; }
  728. std::string outDir = target.GetDirectory(config, false);
  729. // Add a pre-link event to create the directory.
  730. cmCustomCommandLine command;
  731. command.push_back(cmSystemTools::GetCMakeCommand());
  732. command.push_back("-E");
  733. command.push_back("make_directory");
  734. command.push_back(outDir);
  735. std::vector<std::string> no_output;
  736. std::vector<std::string> no_byproducts;
  737. std::vector<std::string> no_depends;
  738. cmCustomCommandLines commands;
  739. commands.push_back(command);
  740. pcc.reset(new cmCustomCommand(0, no_output, no_byproducts,
  741. no_depends, commands, 0, 0));
  742. pcc->SetEscapeOldStyle(false);
  743. pcc->SetEscapeAllowMakeVars(true);
  744. return pcc;
  745. }
  746. // look for custom rules on a target and collect them together
  747. std::string
  748. cmLocalVisualStudio6Generator::CreateTargetRules(cmTarget &target,
  749. const std::string& configName,
  750. const std::string& /* libName */)
  751. {
  752. if (target.GetType() >= cmTarget::UTILITY )
  753. {
  754. return "";
  755. }
  756. std::string customRuleCode = "# Begin Special Build Tool\n";
  757. EventWriter event(this, configName, customRuleCode);
  758. // Write the pre-build and pre-link together (VS6 does not support both).
  759. event.Start("PreLink");
  760. event.Write(target.GetPreBuildCommands());
  761. event.Write(target.GetPreLinkCommands());
  762. cmsys::auto_ptr<cmCustomCommand> pcc(
  763. this->MaybeCreateImplibDir(target, configName, false));
  764. if(pcc.get())
  765. {
  766. event.Write(*pcc);
  767. }
  768. pcc = this->MaybeCreateOutputDir(target, configName);
  769. if(pcc.get())
  770. {
  771. event.Write(*pcc);
  772. }
  773. event.Finish();
  774. // Write the post-build rules.
  775. event.Start("PostBuild");
  776. event.Write(target.GetPostBuildCommands());
  777. event.Finish();
  778. customRuleCode += "# End Special Build Tool\n";
  779. return customRuleCode;
  780. }
  781. inline std::string removeQuotes(const std::string& s)
  782. {
  783. if(s[0] == '\"' && s[s.size()-1] == '\"')
  784. {
  785. return s.substr(1, s.size()-2);
  786. }
  787. return s;
  788. }
  789. std::string
  790. cmLocalVisualStudio6Generator::GetTargetIncludeOptions(cmTarget &target,
  791. const std::string& config)
  792. {
  793. std::string includeOptions;
  794. // Setup /I and /LIBPATH options for the resulting DSP file. VS 6
  795. // truncates long include paths so make it as short as possible if
  796. // the length threatens this problem.
  797. unsigned int maxIncludeLength = 3000;
  798. bool useShortPath = false;
  799. cmGeneratorTarget* gt =
  800. this->GlobalGenerator->GetGeneratorTarget(&target);
  801. for(int j=0; j < 2; ++j)
  802. {
  803. std::vector<std::string> includes;
  804. this->GetIncludeDirectories(includes, gt, "C", config);
  805. std::vector<std::string>::iterator i;
  806. for(i = includes.begin(); i != includes.end(); ++i)
  807. {
  808. std::string tmp =
  809. this->ConvertToOutputFormat(i->c_str(), SHELL);
  810. if(useShortPath)
  811. {
  812. cmSystemTools::GetShortPath(tmp.c_str(), tmp);
  813. }
  814. includeOptions += " /I ";
  815. // quote if not already quoted
  816. if (tmp[0] != '"')
  817. {
  818. includeOptions += "\"";
  819. includeOptions += tmp;
  820. includeOptions += "\"";
  821. }
  822. else
  823. {
  824. includeOptions += tmp;
  825. }
  826. }
  827. if(j == 0 && includeOptions.size() > maxIncludeLength)
  828. {
  829. includeOptions = "";
  830. useShortPath = true;
  831. }
  832. else
  833. {
  834. break;
  835. }
  836. }
  837. return includeOptions;
  838. }
  839. // Code in blocks surrounded by a test for this definition is needed
  840. // only for compatibility with user project's replacement DSP
  841. // templates. The CMake templates no longer use them.
  842. #define CM_USE_OLD_VS6
  843. void cmLocalVisualStudio6Generator
  844. ::WriteDSPHeader(std::ostream& fout,
  845. const std::string& libName, cmTarget &target,
  846. std::vector<cmSourceGroup> &)
  847. {
  848. bool targetBuilds = (target.GetType() >= cmTarget::EXECUTABLE &&
  849. target.GetType() <= cmTarget::MODULE_LIBRARY);
  850. #ifdef CM_USE_OLD_VS6
  851. // Lookup the library and executable output directories.
  852. std::string libPath;
  853. if(this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  854. {
  855. libPath = this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  856. }
  857. std::string exePath;
  858. if(this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  859. {
  860. exePath = this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  861. }
  862. // Make sure there are trailing slashes.
  863. if(!libPath.empty())
  864. {
  865. if(libPath[libPath.size()-1] != '/')
  866. {
  867. libPath += "/";
  868. }
  869. }
  870. if(!exePath.empty())
  871. {
  872. if(exePath[exePath.size()-1] != '/')
  873. {
  874. exePath += "/";
  875. }
  876. }
  877. std::set<std::string> pathEmitted;
  878. // determine the link directories
  879. std::string libOptions;
  880. std::string libDebugOptions;
  881. std::string libOptimizedOptions;
  882. std::string libMultiLineOptions;
  883. std::string libMultiLineOptionsForDebug;
  884. std::string libMultiLineDebugOptions;
  885. std::string libMultiLineOptimizedOptions;
  886. if(!libPath.empty())
  887. {
  888. std::string lpath =
  889. this->ConvertToOutputFormat(libPath.c_str(), SHELL);
  890. if(lpath.empty())
  891. {
  892. lpath = ".";
  893. }
  894. std::string lpathIntDir = libPath + "$(INTDIR)";
  895. lpathIntDir =
  896. this->ConvertToOutputFormat(lpathIntDir.c_str(), SHELL);
  897. if(pathEmitted.insert(lpath).second)
  898. {
  899. libOptions += " /LIBPATH:";
  900. libOptions += lpathIntDir;
  901. libOptions += " ";
  902. libOptions += " /LIBPATH:";
  903. libOptions += lpath;
  904. libOptions += " ";
  905. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  906. libMultiLineOptions += lpathIntDir;
  907. libMultiLineOptions += " ";
  908. libMultiLineOptions += " /LIBPATH:";
  909. libMultiLineOptions += lpath;
  910. libMultiLineOptions += " \n";
  911. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  912. libMultiLineOptionsForDebug += lpathIntDir;
  913. libMultiLineOptionsForDebug += " ";
  914. libMultiLineOptionsForDebug += " /LIBPATH:";
  915. libMultiLineOptionsForDebug += lpath;
  916. libMultiLineOptionsForDebug += " \n";
  917. }
  918. }
  919. if(!exePath.empty())
  920. {
  921. std::string lpath =
  922. this->ConvertToOutputFormat(exePath.c_str(), SHELL);
  923. if(lpath.empty())
  924. {
  925. lpath = ".";
  926. }
  927. std::string lpathIntDir = exePath + "$(INTDIR)";
  928. lpathIntDir =
  929. this->ConvertToOutputFormat(lpathIntDir.c_str(), SHELL);
  930. if(pathEmitted.insert(lpath).second)
  931. {
  932. libOptions += " /LIBPATH:";
  933. libOptions += lpathIntDir;
  934. libOptions += " ";
  935. libOptions += " /LIBPATH:";
  936. libOptions += lpath;
  937. libOptions += " ";
  938. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  939. libMultiLineOptions += lpathIntDir;
  940. libMultiLineOptions += " ";
  941. libMultiLineOptions += " /LIBPATH:";
  942. libMultiLineOptions += lpath;
  943. libMultiLineOptions += " \n";
  944. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  945. libMultiLineOptionsForDebug += lpathIntDir;
  946. libMultiLineOptionsForDebug += " ";
  947. libMultiLineOptionsForDebug += " /LIBPATH:";
  948. libMultiLineOptionsForDebug += lpath;
  949. libMultiLineOptionsForDebug += " \n";
  950. }
  951. }
  952. std::vector<std::string>::const_iterator i;
  953. const std::vector<std::string>& libdirs = target.GetLinkDirectories();
  954. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  955. {
  956. std::string path = *i;
  957. if(path[path.size()-1] != '/')
  958. {
  959. path += "/";
  960. }
  961. std::string lpath =
  962. this->ConvertToOutputFormat(path.c_str(), SHELL);
  963. if(lpath.empty())
  964. {
  965. lpath = ".";
  966. }
  967. std::string lpathIntDir = path + "$(INTDIR)";
  968. lpathIntDir =
  969. this->ConvertToOutputFormat(lpathIntDir.c_str(), SHELL);
  970. if(pathEmitted.insert(lpath).second)
  971. {
  972. libOptions += " /LIBPATH:";
  973. libOptions += lpathIntDir;
  974. libOptions += " ";
  975. libOptions += " /LIBPATH:";
  976. libOptions += lpath;
  977. libOptions += " ";
  978. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  979. libMultiLineOptions += lpathIntDir;
  980. libMultiLineOptions += " ";
  981. libMultiLineOptions += " /LIBPATH:";
  982. libMultiLineOptions += lpath;
  983. libMultiLineOptions += " \n";
  984. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  985. libMultiLineOptionsForDebug += lpathIntDir;
  986. libMultiLineOptionsForDebug += " ";
  987. libMultiLineOptionsForDebug += " /LIBPATH:";
  988. libMultiLineOptionsForDebug += lpath;
  989. libMultiLineOptionsForDebug += " \n";
  990. }
  991. }
  992. // find link libraries
  993. const cmTarget::LinkLibraryVectorType& libs =
  994. target.GetLinkLibrariesForVS6();
  995. cmTarget::LinkLibraryVectorType::const_iterator j;
  996. for(j = libs.begin(); j != libs.end(); ++j)
  997. {
  998. // add libraries to executables and dlls (but never include
  999. // a library in a library, bad recursion)
  1000. // NEVER LINK STATIC LIBRARIES TO OTHER STATIC LIBRARIES
  1001. if ((target.GetType() != cmTarget::SHARED_LIBRARY
  1002. && target.GetType() != cmTarget::STATIC_LIBRARY
  1003. && target.GetType() != cmTarget::MODULE_LIBRARY) ||
  1004. (target.GetType()==cmTarget::SHARED_LIBRARY
  1005. && libName != GetVS6TargetName(j->first)) ||
  1006. (target.GetType()==cmTarget::MODULE_LIBRARY
  1007. && libName != GetVS6TargetName(j->first)))
  1008. {
  1009. // Compute the proper name to use to link this library.
  1010. std::string lib;
  1011. std::string libDebug;
  1012. cmTarget* tgt = this->GlobalGenerator->FindTarget(j->first.c_str());
  1013. if(tgt)
  1014. {
  1015. cmGeneratorTarget* gt =
  1016. this->GlobalGenerator->GetGeneratorTarget(tgt);
  1017. lib = cmSystemTools::GetFilenameWithoutExtension
  1018. (gt->GetFullName().c_str());
  1019. libDebug = cmSystemTools::GetFilenameWithoutExtension
  1020. (gt->GetFullName("Debug").c_str());
  1021. lib += ".lib";
  1022. libDebug += ".lib";
  1023. }
  1024. else
  1025. {
  1026. lib = j->first.c_str();
  1027. libDebug = j->first.c_str();
  1028. if(j->first.find(".lib") == std::string::npos)
  1029. {
  1030. lib += ".lib";
  1031. libDebug += ".lib";
  1032. }
  1033. }
  1034. lib = this->ConvertToOutputFormat(lib.c_str(), SHELL);
  1035. libDebug =
  1036. this->ConvertToOutputFormat(libDebug.c_str(), SHELL);
  1037. if (j->second == cmTarget::GENERAL)
  1038. {
  1039. libOptions += " ";
  1040. libOptions += lib;
  1041. libMultiLineOptions += "# ADD LINK32 ";
  1042. libMultiLineOptions += lib;
  1043. libMultiLineOptions += "\n";
  1044. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  1045. libMultiLineOptionsForDebug += libDebug;
  1046. libMultiLineOptionsForDebug += "\n";
  1047. }
  1048. if (j->second == cmTarget::DEBUG)
  1049. {
  1050. libDebugOptions += " ";
  1051. libDebugOptions += lib;
  1052. libMultiLineDebugOptions += "# ADD LINK32 ";
  1053. libMultiLineDebugOptions += libDebug;
  1054. libMultiLineDebugOptions += "\n";
  1055. }
  1056. if (j->second == cmTarget::OPTIMIZED)
  1057. {
  1058. libOptimizedOptions += " ";
  1059. libOptimizedOptions += lib;
  1060. libMultiLineOptimizedOptions += "# ADD LINK32 ";
  1061. libMultiLineOptimizedOptions += lib;
  1062. libMultiLineOptimizedOptions += "\n";
  1063. }
  1064. }
  1065. }
  1066. #endif
  1067. // Get include options for this target.
  1068. std::string includeOptionsDebug = this->GetTargetIncludeOptions(target,
  1069. "DEBUG");
  1070. std::string includeOptionsRelease = this->GetTargetIncludeOptions(target,
  1071. "RELEASE");
  1072. std::string includeOptionsRelWithDebInfo = this->GetTargetIncludeOptions(
  1073. target,
  1074. "RELWITHDEBINFO");
  1075. std::string includeOptionsMinSizeRel = this->GetTargetIncludeOptions(target,
  1076. "MINSIZEREL");
  1077. // Get extra linker options for this target type.
  1078. std::string extraLinkOptions;
  1079. std::string extraLinkOptionsDebug;
  1080. std::string extraLinkOptionsRelease;
  1081. std::string extraLinkOptionsMinSizeRel;
  1082. std::string extraLinkOptionsRelWithDebInfo;
  1083. if(target.GetType() == cmTarget::EXECUTABLE)
  1084. {
  1085. extraLinkOptions = this->Makefile->
  1086. GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS");
  1087. extraLinkOptionsDebug = this->Makefile->
  1088. GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS_DEBUG");
  1089. extraLinkOptionsRelease = this->Makefile->
  1090. GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS_RELEASE");
  1091. extraLinkOptionsMinSizeRel = this->Makefile->
  1092. GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS_MINSIZEREL");
  1093. extraLinkOptionsRelWithDebInfo = this->Makefile->
  1094. GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO");
  1095. }
  1096. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  1097. {
  1098. extraLinkOptions = this->Makefile->
  1099. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS");
  1100. extraLinkOptionsDebug = this->Makefile->
  1101. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS_DEBUG");
  1102. extraLinkOptionsRelease = this->Makefile->
  1103. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS_RELEASE");
  1104. extraLinkOptionsMinSizeRel = this->Makefile->
  1105. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL");
  1106. extraLinkOptionsRelWithDebInfo = this->Makefile->
  1107. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO");
  1108. }
  1109. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  1110. {
  1111. extraLinkOptions = this->Makefile->
  1112. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS");
  1113. extraLinkOptionsDebug = this->Makefile->
  1114. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS_DEBUG");
  1115. extraLinkOptionsRelease = this->Makefile->
  1116. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS_RELEASE");
  1117. extraLinkOptionsMinSizeRel = this->Makefile->
  1118. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL");
  1119. extraLinkOptionsRelWithDebInfo = this->Makefile->
  1120. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO");
  1121. }
  1122. // Get extra linker options for this target.
  1123. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS"))
  1124. {
  1125. extraLinkOptions += " ";
  1126. extraLinkOptions += targetLinkFlags;
  1127. }
  1128. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_DEBUG"))
  1129. {
  1130. extraLinkOptionsDebug += " ";
  1131. extraLinkOptionsDebug += targetLinkFlags;
  1132. }
  1133. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_RELEASE"))
  1134. {
  1135. extraLinkOptionsRelease += " ";
  1136. extraLinkOptionsRelease += targetLinkFlags;
  1137. }
  1138. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_MINSIZEREL"))
  1139. {
  1140. extraLinkOptionsMinSizeRel += " ";
  1141. extraLinkOptionsMinSizeRel += targetLinkFlags;
  1142. }
  1143. if(const char* targetLinkFlags =
  1144. target.GetProperty("LINK_FLAGS_RELWITHDEBINFO"))
  1145. {
  1146. extraLinkOptionsRelWithDebInfo += " ";
  1147. extraLinkOptionsRelWithDebInfo += targetLinkFlags;
  1148. }
  1149. cmGeneratorTarget* gt =
  1150. this->GlobalGenerator->GetGeneratorTarget(&target);
  1151. // Get standard libraries for this language.
  1152. if(targetBuilds)
  1153. {
  1154. // Get the language to use for linking.
  1155. std::vector<std::string> configs;
  1156. target.GetMakefile()->GetConfigurations(configs);
  1157. std::vector<std::string>::const_iterator it = configs.begin();
  1158. const std::string& linkLanguage = gt->GetLinkerLanguage(*it);
  1159. for ( ; it != configs.end(); ++it)
  1160. {
  1161. const std::string& configLinkLanguage = gt->GetLinkerLanguage(*it);
  1162. if (configLinkLanguage != linkLanguage)
  1163. {
  1164. cmSystemTools::Error
  1165. ("Linker language must not vary by configuration for target: ",
  1166. target.GetName().c_str());
  1167. }
  1168. }
  1169. if(linkLanguage.empty())
  1170. {
  1171. cmSystemTools::Error
  1172. ("CMake can not determine linker language for target: ",
  1173. target.GetName().c_str());
  1174. return;
  1175. }
  1176. // Compute the variable name to lookup standard libraries for this
  1177. // language.
  1178. std::string standardLibsVar = "CMAKE_";
  1179. standardLibsVar += linkLanguage;
  1180. standardLibsVar += "_STANDARD_LIBRARIES";
  1181. // Add standard libraries.
  1182. if(const char* stdLibs =
  1183. this->Makefile->GetDefinition(standardLibsVar.c_str()))
  1184. {
  1185. extraLinkOptions += " ";
  1186. extraLinkOptions += stdLibs;
  1187. }
  1188. }
  1189. // Compute version number information.
  1190. std::string targetVersionFlag;
  1191. if(target.GetType() == cmTarget::EXECUTABLE ||
  1192. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1193. target.GetType() == cmTarget::MODULE_LIBRARY)
  1194. {
  1195. int major;
  1196. int minor;
  1197. target.GetTargetVersion(major, minor);
  1198. std::ostringstream targetVersionStream;
  1199. targetVersionStream << "/version:" << major << "." << minor;
  1200. targetVersionFlag = targetVersionStream.str();
  1201. }
  1202. // Compute the real name of the target.
  1203. std::string outputName =
  1204. "(OUTPUT_NAME is for libraries and executables only)";
  1205. std::string outputNameDebug = outputName;
  1206. std::string outputNameRelease = outputName;
  1207. std::string outputNameMinSizeRel = outputName;
  1208. std::string outputNameRelWithDebInfo = outputName;
  1209. if(target.GetType() == cmTarget::EXECUTABLE ||
  1210. target.GetType() == cmTarget::STATIC_LIBRARY ||
  1211. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1212. target.GetType() == cmTarget::MODULE_LIBRARY)
  1213. {
  1214. outputName = gt->GetFullName();
  1215. outputNameDebug = gt->GetFullName("Debug");
  1216. outputNameRelease = gt->GetFullName("Release");
  1217. outputNameMinSizeRel = gt->GetFullName("MinSizeRel");
  1218. outputNameRelWithDebInfo = gt->GetFullName("RelWithDebInfo");
  1219. }
  1220. else if(target.GetType() == cmTarget::OBJECT_LIBRARY)
  1221. {
  1222. outputName = target.GetName();
  1223. outputName += ".lib";
  1224. outputNameDebug = outputName;
  1225. outputNameRelease = outputName;
  1226. outputNameMinSizeRel = outputName;
  1227. outputNameRelWithDebInfo = outputName;
  1228. }
  1229. // Compute the output directory for the target.
  1230. std::string outputDirOld;
  1231. std::string outputDirDebug;
  1232. std::string outputDirRelease;
  1233. std::string outputDirMinSizeRel;
  1234. std::string outputDirRelWithDebInfo;
  1235. if(target.GetType() == cmTarget::EXECUTABLE ||
  1236. target.GetType() == cmTarget::STATIC_LIBRARY ||
  1237. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1238. target.GetType() == cmTarget::MODULE_LIBRARY)
  1239. {
  1240. #ifdef CM_USE_OLD_VS6
  1241. outputDirOld =
  1242. removeQuotes(this->ConvertToOutputFormat
  1243. (target.GetDirectory().c_str(), SHELL));
  1244. #endif
  1245. outputDirDebug =
  1246. removeQuotes(this->ConvertToOutputFormat(
  1247. target.GetDirectory("Debug").c_str(), SHELL));
  1248. outputDirRelease =
  1249. removeQuotes(this->ConvertToOutputFormat(
  1250. target.GetDirectory("Release").c_str(), SHELL));
  1251. outputDirMinSizeRel =
  1252. removeQuotes(this->ConvertToOutputFormat(
  1253. target.GetDirectory("MinSizeRel").c_str(), SHELL));
  1254. outputDirRelWithDebInfo =
  1255. removeQuotes(this->ConvertToOutputFormat(
  1256. target.GetDirectory("RelWithDebInfo").c_str(), SHELL));
  1257. }
  1258. else if(target.GetType() == cmTarget::OBJECT_LIBRARY)
  1259. {
  1260. std::string outputDir = cmake::GetCMakeFilesDirectoryPostSlash();
  1261. outputDirDebug = outputDir + "Debug";
  1262. outputDirRelease = outputDir + "Release";
  1263. outputDirMinSizeRel = outputDir + "MinSizeRel";
  1264. outputDirRelWithDebInfo = outputDir + "RelWithDebInfo";
  1265. }
  1266. // Compute the proper link information for the target.
  1267. std::string optionsDebug;
  1268. std::string optionsRelease;
  1269. std::string optionsMinSizeRel;
  1270. std::string optionsRelWithDebInfo;
  1271. if(target.GetType() == cmTarget::EXECUTABLE ||
  1272. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1273. target.GetType() == cmTarget::MODULE_LIBRARY)
  1274. {
  1275. extraLinkOptionsDebug =
  1276. extraLinkOptions + " " + extraLinkOptionsDebug;
  1277. extraLinkOptionsRelease =
  1278. extraLinkOptions + " " + extraLinkOptionsRelease;
  1279. extraLinkOptionsMinSizeRel =
  1280. extraLinkOptions + " " + extraLinkOptionsMinSizeRel;
  1281. extraLinkOptionsRelWithDebInfo =
  1282. extraLinkOptions + " " + extraLinkOptionsRelWithDebInfo;
  1283. this->ComputeLinkOptions(target, "Debug", extraLinkOptionsDebug,
  1284. optionsDebug);
  1285. this->ComputeLinkOptions(target, "Release", extraLinkOptionsRelease,
  1286. optionsRelease);
  1287. this->ComputeLinkOptions(target, "MinSizeRel", extraLinkOptionsMinSizeRel,
  1288. optionsMinSizeRel);
  1289. this->ComputeLinkOptions(target, "RelWithDebInfo",
  1290. extraLinkOptionsRelWithDebInfo,
  1291. optionsRelWithDebInfo);
  1292. }
  1293. // Compute the path of the import library.
  1294. std::string targetImplibFlagDebug;
  1295. std::string targetImplibFlagRelease;
  1296. std::string targetImplibFlagMinSizeRel;
  1297. std::string targetImplibFlagRelWithDebInfo;
  1298. if(target.GetType() == cmTarget::SHARED_LIBRARY ||
  1299. target.GetType() == cmTarget::MODULE_LIBRARY ||
  1300. target.GetType() == cmTarget::EXECUTABLE)
  1301. {
  1302. std::string fullPathImpDebug = target.GetDirectory("Debug", true);
  1303. std::string fullPathImpRelease = target.GetDirectory("Release", true);
  1304. std::string fullPathImpMinSizeRel =
  1305. target.GetDirectory("MinSizeRel", true);
  1306. std::string fullPathImpRelWithDebInfo =
  1307. target.GetDirectory("RelWithDebInfo", true);
  1308. fullPathImpDebug += "/";
  1309. fullPathImpRelease += "/";
  1310. fullPathImpMinSizeRel += "/";
  1311. fullPathImpRelWithDebInfo += "/";
  1312. fullPathImpDebug += gt->GetFullName("Debug", true);
  1313. fullPathImpRelease += gt->GetFullName("Release", true);
  1314. fullPathImpMinSizeRel += gt->GetFullName("MinSizeRel", true);
  1315. fullPathImpRelWithDebInfo += gt->GetFullName("RelWithDebInfo", true);
  1316. targetImplibFlagDebug = "/implib:";
  1317. targetImplibFlagRelease = "/implib:";
  1318. targetImplibFlagMinSizeRel = "/implib:";
  1319. targetImplibFlagRelWithDebInfo = "/implib:";
  1320. targetImplibFlagDebug +=
  1321. this->ConvertToOutputFormat(fullPathImpDebug.c_str(), SHELL);
  1322. targetImplibFlagRelease +=
  1323. this->ConvertToOutputFormat(fullPathImpRelease.c_str(), SHELL);
  1324. targetImplibFlagMinSizeRel +=
  1325. this->ConvertToOutputFormat(fullPathImpMinSizeRel.c_str(), SHELL);
  1326. targetImplibFlagRelWithDebInfo +=
  1327. this->ConvertToOutputFormat(fullPathImpRelWithDebInfo.c_str(), SHELL);
  1328. }
  1329. #ifdef CM_USE_OLD_VS6
  1330. // Compute link information for the target.
  1331. if(!extraLinkOptions.empty())
  1332. {
  1333. libOptions += " ";
  1334. libOptions += extraLinkOptions;
  1335. libOptions += " ";
  1336. libMultiLineOptions += "# ADD LINK32 ";
  1337. libMultiLineOptions += extraLinkOptions;
  1338. libMultiLineOptions += " \n";
  1339. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  1340. libMultiLineOptionsForDebug += extraLinkOptions;
  1341. libMultiLineOptionsForDebug += " \n";
  1342. }
  1343. #endif
  1344. // are there any custom rules on the target itself
  1345. // only if the target is a lib or exe
  1346. std::string customRuleCodeRelease
  1347. = this->CreateTargetRules(target, "RELEASE", libName);
  1348. std::string customRuleCodeDebug
  1349. = this->CreateTargetRules(target, "DEBUG", libName);
  1350. std::string customRuleCodeMinSizeRel
  1351. = this->CreateTargetRules(target, "MINSIZEREL", libName);
  1352. std::string customRuleCodeRelWithDebInfo
  1353. = this->CreateTargetRules(target, "RELWITHDEBINFO", libName);
  1354. cmsys::ifstream fin(this->DSPHeaderTemplate.c_str());
  1355. if(!fin)
  1356. {
  1357. cmSystemTools::Error("Error Reading ", this->DSPHeaderTemplate.c_str());
  1358. }
  1359. std::string staticLibOptions;
  1360. std::string staticLibOptionsDebug;
  1361. std::string staticLibOptionsRelease;
  1362. std::string staticLibOptionsMinSizeRel;
  1363. std::string staticLibOptionsRelWithDebInfo;
  1364. if(target.GetType() == cmTarget::STATIC_LIBRARY )
  1365. {
  1366. const char *libflagsGlobal =
  1367. this->Makefile->GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS");
  1368. this->AppendFlags(staticLibOptions, libflagsGlobal);
  1369. this->AppendFlags(staticLibOptionsDebug, libflagsGlobal);
  1370. this->AppendFlags(staticLibOptionsRelease, libflagsGlobal);
  1371. this->AppendFlags(staticLibOptionsMinSizeRel, libflagsGlobal);
  1372. this->AppendFlags(staticLibOptionsRelWithDebInfo, libflagsGlobal);
  1373. this->AppendFlags(staticLibOptionsDebug, this->Makefile->
  1374. GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS_DEBUG"));
  1375. this->AppendFlags(staticLibOptionsRelease, this->Makefile->
  1376. GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS_RELEASE"));
  1377. this->AppendFlags(staticLibOptionsMinSizeRel, this->Makefile->
  1378. GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL"));
  1379. this->AppendFlags(staticLibOptionsRelWithDebInfo, this->Makefile->
  1380. GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO"));
  1381. const char *libflags = target.GetProperty("STATIC_LIBRARY_FLAGS");
  1382. this->AppendFlags(staticLibOptions, libflags);
  1383. this->AppendFlags(staticLibOptionsDebug, libflags);
  1384. this->AppendFlags(staticLibOptionsRelease, libflags);
  1385. this->AppendFlags(staticLibOptionsMinSizeRel, libflags);
  1386. this->AppendFlags(staticLibOptionsRelWithDebInfo, libflags);
  1387. this->AppendFlags(staticLibOptionsDebug,
  1388. target.GetProperty("STATIC_LIBRARY_FLAGS_DEBUG"));
  1389. this->AppendFlags(staticLibOptionsRelease,
  1390. target.GetProperty("STATIC_LIBRARY_FLAGS_RELEASE"));
  1391. this->AppendFlags(staticLibOptionsMinSizeRel,
  1392. target.GetProperty("STATIC_LIBRARY_FLAGS_MINSIZEREL"));
  1393. this->AppendFlags(staticLibOptionsRelWithDebInfo,
  1394. target.GetProperty("STATIC_LIBRARY_FLAGS_RELWITHDEBINFO"));
  1395. std::string objects;
  1396. this->OutputObjects(target, "LIB", objects);
  1397. if(!objects.empty())
  1398. {
  1399. objects = "\n" + objects;
  1400. staticLibOptionsDebug += objects;
  1401. staticLibOptionsRelease += objects;
  1402. staticLibOptionsMinSizeRel += objects;
  1403. staticLibOptionsRelWithDebInfo += objects;
  1404. }
  1405. }
  1406. // Add the export symbol definition for shared library objects.
  1407. std::string exportSymbol;
  1408. if(const char* exportMacro = target.GetExportMacro())
  1409. {
  1410. exportSymbol = exportMacro;
  1411. }
  1412. std::string line;
  1413. std::string libnameExports;
  1414. if(!exportSymbol.empty())
  1415. {
  1416. libnameExports = "/D \"";
  1417. libnameExports += exportSymbol;
  1418. libnameExports += "\"";
  1419. }
  1420. while(cmSystemTools::GetLineFromStream(fin, line))
  1421. {
  1422. const char* mfcFlag = this->Makefile->GetDefinition("CMAKE_MFC_FLAG");
  1423. if(!mfcFlag)
  1424. {
  1425. mfcFlag = "0";
  1426. }
  1427. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME_EXPORTS",
  1428. libnameExports.c_str());
  1429. cmSystemTools::ReplaceString(line, "CMAKE_MFC_FLAG",
  1430. mfcFlag);
  1431. if(target.GetType() == cmTarget::STATIC_LIBRARY ||
  1432. target.GetType() == cmTarget::OBJECT_LIBRARY)
  1433. {
  1434. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS_DEBUG",
  1435. staticLibOptionsDebug.c_str());
  1436. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS_RELEASE",
  1437. staticLibOptionsRelease.c_str());
  1438. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS_MINSIZEREL",
  1439. staticLibOptionsMinSizeRel.c_str());
  1440. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS_RELWITHDEBINFO",
  1441. staticLibOptionsRelWithDebInfo.c_str());
  1442. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS",
  1443. staticLibOptions.c_str());
  1444. }
  1445. if(this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  1446. {
  1447. cmSystemTools::ReplaceString(line, "/nologo", "");
  1448. }
  1449. #ifdef CM_USE_OLD_VS6
  1450. cmSystemTools::ReplaceString(line, "CM_LIBRARIES",
  1451. libOptions.c_str());
  1452. cmSystemTools::ReplaceString(line, "CM_DEBUG_LIBRARIES",
  1453. libDebugOptions.c_str());
  1454. cmSystemTools::ReplaceString(line, "CM_OPTIMIZED_LIBRARIES",
  1455. libOptimizedOptions.c_str());
  1456. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES_FOR_DEBUG",
  1457. libMultiLineOptionsForDebug.c_str());
  1458. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES",
  1459. libMultiLineOptions.c_str());
  1460. cmSystemTools::ReplaceString(line, "CM_MULTILINE_DEBUG_LIBRARIES",
  1461. libMultiLineDebugOptions.c_str());
  1462. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIMIZED_LIBRARIES",
  1463. libMultiLineOptimizedOptions.c_str());
  1464. #endif
  1465. // Substitute the rules for custom command. When specifying just the
  1466. // target name for the command the command can be different for
  1467. // different configs
  1468. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_RELEASE",
  1469. customRuleCodeRelease.c_str());
  1470. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_DEBUG",
  1471. customRuleCodeDebug.c_str());
  1472. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_MINSIZEREL",
  1473. customRuleCodeMinSizeRel.c_str());
  1474. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_RELWITHDEBINFO",
  1475. customRuleCodeRelWithDebInfo.c_str());
  1476. // Substitute the real output name into the template.
  1477. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_DEBUG",
  1478. outputNameDebug.c_str());
  1479. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_RELEASE",
  1480. outputNameRelease.c_str());
  1481. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_MINSIZEREL",
  1482. outputNameMinSizeRel.c_str());
  1483. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_RELWITHDEBINFO",
  1484. outputNameRelWithDebInfo.c_str());
  1485. cmSystemTools::ReplaceString(line, "OUTPUT_NAME", outputName.c_str());
  1486. // Substitute the proper link information into the template.
  1487. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_DEBUG",
  1488. optionsDebug.c_str());
  1489. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_RELEASE",
  1490. optionsRelease.c_str());
  1491. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_MINSIZEREL",
  1492. optionsMinSizeRel.c_str());
  1493. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_RELWITHDEBINFO",
  1494. optionsRelWithDebInfo.c_str());
  1495. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES_DEBUG",
  1496. includeOptionsDebug.c_str());
  1497. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES_RELEASE",
  1498. includeOptionsRelease.c_str());
  1499. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES_MINSIZEREL",
  1500. includeOptionsMinSizeRel.c_str());
  1501. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES_RELWITHDEBINFO",
  1502. includeOptionsRelWithDebInfo.c_str());
  1503. cmSystemTools::ReplaceString(line, "TARGET_VERSION_FLAG",
  1504. targetVersionFlag.c_str());
  1505. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_DEBUG",
  1506. targetImplibFlagDebug.c_str());
  1507. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_RELEASE",
  1508. targetImplibFlagRelease.c_str());
  1509. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_MINSIZEREL",
  1510. targetImplibFlagMinSizeRel.c_str());
  1511. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_RELWITHDEBINFO",
  1512. targetImplibFlagRelWithDebInfo.c_str());
  1513. std::string vs6name = GetVS6TargetName(libName);
  1514. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME", vs6name.c_str());
  1515. #ifdef CM_USE_OLD_VS6
  1516. // because LIBRARY_OUTPUT_PATH and EXECUTABLE_OUTPUT_PATH
  1517. // are already quoted in the template file,
  1518. // we need to remove the quotes here, we still need
  1519. // to convert to output path for unix to win32 conversion
  1520. cmSystemTools::ReplaceString
  1521. (line, "LIBRARY_OUTPUT_PATH",
  1522. removeQuotes(this->ConvertToOutputFormat
  1523. (libPath.c_str(), SHELL)).c_str());
  1524. cmSystemTools::ReplaceString
  1525. (line, "EXECUTABLE_OUTPUT_PATH",
  1526. removeQuotes(this->ConvertToOutputFormat
  1527. (exePath.c_str(), SHELL)).c_str());
  1528. #endif
  1529. if(targetBuilds || target.GetType() == cmTarget::OBJECT_LIBRARY)
  1530. {
  1531. cmSystemTools::ReplaceString(line, "OUTPUT_DIRECTORY_DEBUG",
  1532. outputDirDebug.c_str());
  1533. cmSystemTools::ReplaceString(line, "OUTPUT_DIRECTORY_RELEASE",
  1534. outputDirRelease.c_str());
  1535. cmSystemTools::ReplaceString(line, "OUTPUT_DIRECTORY_MINSIZEREL",
  1536. outputDirMinSizeRel.c_str());
  1537. cmSystemTools::ReplaceString(line, "OUTPUT_DIRECTORY_RELWITHDEBINFO",
  1538. outputDirRelWithDebInfo.c_str());
  1539. if(!outputDirOld.empty())
  1540. {
  1541. cmSystemTools::ReplaceString(line, "OUTPUT_DIRECTORY",
  1542. outputDirOld.c_str());
  1543. }
  1544. }
  1545. cmSystemTools::ReplaceString(line,
  1546. "EXTRA_DEFINES",
  1547. this->Makefile->GetDefineFlags());
  1548. const char* debugPostfix
  1549. = this->Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  1550. cmSystemTools::ReplaceString(line, "DEBUG_POSTFIX",
  1551. debugPostfix?debugPostfix:"");
  1552. if(target.GetType() >= cmTarget::EXECUTABLE &&
  1553. target.GetType() <= cmTarget::OBJECT_LIBRARY)
  1554. {
  1555. // store flags for each configuration
  1556. std::string flags = " ";
  1557. std::string flagsRelease = " ";
  1558. std::string flagsMinSizeRel = " ";
  1559. std::string flagsDebug = " ";
  1560. std::string flagsRelWithDebInfo = " ";
  1561. std::vector<std::string> configs;
  1562. target.GetMakefile()->GetConfigurations(configs);
  1563. std::vector<std::string>::const_iterator it = configs.begin();
  1564. const std::string& linkLanguage = gt->GetLinkerLanguage(*it);
  1565. for ( ; it != configs.end(); ++it)
  1566. {
  1567. const std::string& configLinkLanguage = gt->GetLinkerLanguage(*it);
  1568. if (configLinkLanguage != linkLanguage)
  1569. {
  1570. cmSystemTools::Error
  1571. ("Linker language must not vary by configuration for target: ",
  1572. target.GetName().c_str());
  1573. }
  1574. }
  1575. if(linkLanguage.empty())
  1576. {
  1577. cmSystemTools::Error
  1578. ("CMake can not determine linker language for target: ",
  1579. target.GetName().c_str());
  1580. return;
  1581. }
  1582. // if CXX is on and the target contains cxx code then add the cxx flags
  1583. std::string baseFlagVar = "CMAKE_";
  1584. baseFlagVar += linkLanguage;
  1585. baseFlagVar += "_FLAGS";
  1586. flags = this->Makefile->GetSafeDefinition(baseFlagVar.c_str());
  1587. std::string flagVar = baseFlagVar + "_RELEASE";
  1588. flagsRelease = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1589. flagsRelease += " -DCMAKE_INTDIR=\\\"Release\\\" ";
  1590. flagVar = baseFlagVar + "_MINSIZEREL";
  1591. flagsMinSizeRel = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1592. flagsMinSizeRel += " -DCMAKE_INTDIR=\\\"MinSizeRel\\\" ";
  1593. flagVar = baseFlagVar + "_DEBUG";
  1594. flagsDebug = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1595. flagsDebug += " -DCMAKE_INTDIR=\\\"Debug\\\" ";
  1596. flagVar = baseFlagVar + "_RELWITHDEBINFO";
  1597. flagsRelWithDebInfo = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1598. flagsRelWithDebInfo += " -DCMAKE_INTDIR=\\\"RelWithDebInfo\\\" ";
  1599. this->AddCompileOptions(flags, &target, linkLanguage, "");
  1600. this->AddCompileOptions(flagsDebug, &target, linkLanguage, "Debug");
  1601. this->AddCompileOptions(flagsRelease, &target, linkLanguage, "Release");
  1602. this->AddCompileOptions(flagsMinSizeRel, &target, linkLanguage,
  1603. "MinSizeRel");
  1604. this->AddCompileOptions(flagsRelWithDebInfo, &target, linkLanguage,
  1605. "RelWithDebInfo");
  1606. // if _UNICODE and _SBCS are not found, then add -D_MBCS
  1607. std::string defs = this->Makefile->GetDefineFlags();
  1608. if(flags.find("D_UNICODE") == flags.npos &&
  1609. defs.find("D_UNICODE") == flags.npos &&
  1610. flags.find("D_SBCS") == flags.npos &&
  1611. defs.find("D_SBCS") == flags.npos)
  1612. {
  1613. flags += " /D \"_MBCS\"";
  1614. }
  1615. // Add per-target and per-configuration preprocessor definitions.
  1616. std::set<std::string> definesSet;
  1617. std::set<std::string> debugDefinesSet;
  1618. std::set<std::string> releaseDefinesSet;
  1619. std::set<std::string> minsizeDefinesSet;
  1620. std::set<std::string> debugrelDefinesSet;
  1621. this->AddCompileDefinitions(definesSet, &target, "", linkLanguage);
  1622. this->AddCompileDefinitions(debugDefinesSet, &target,
  1623. "DEBUG", linkLanguage);
  1624. this->AddCompileDefinitions(releaseDefinesSet, &target,
  1625. "RELEASE", linkLanguage);
  1626. this->AddCompileDefinitions(minsizeDefinesSet, &target,
  1627. "MINSIZEREL", linkLanguage);
  1628. this->AddCompileDefinitions(debugrelDefinesSet, &target,
  1629. "RELWITHDEBINFO", linkLanguage);
  1630. std::string defines = " ";
  1631. std::string debugDefines = " ";
  1632. std::string releaseDefines = " ";
  1633. std::string minsizeDefines = " ";
  1634. std::string debugrelDefines = " ";
  1635. this->JoinDefines(definesSet, defines, "");
  1636. this->JoinDefines(debugDefinesSet, debugDefines, "");
  1637. this->JoinDefines(releaseDefinesSet, releaseDefines, "");
  1638. this->JoinDefines(minsizeDefinesSet, minsizeDefines, "");
  1639. this->JoinDefines(debugrelDefinesSet, debugrelDefines, "");
  1640. flags += defines;
  1641. flagsDebug += debugDefines;
  1642. flagsRelease += releaseDefines;
  1643. flagsMinSizeRel += minsizeDefines;
  1644. flagsRelWithDebInfo += debugrelDefines;
  1645. // The template files have CXX FLAGS in them, that need to be replaced.
  1646. // There are not separate CXX and C template files, so we use the same
  1647. // variable names. The previous code sets up flags* variables to
  1648. // contain the correct C or CXX flags
  1649. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_MINSIZEREL",
  1650. flagsMinSizeRel.c_str());
  1651. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_DEBUG",
  1652. flagsDebug.c_str());
  1653. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELWITHDEBINFO",
  1654. flagsRelWithDebInfo.c_str());
  1655. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELEASE",
  1656. flagsRelease.c_str());
  1657. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS", flags.c_str());
  1658. cmSystemTools::ReplaceString(line, "COMPILE_DEFINITIONS_MINSIZEREL",
  1659. minsizeDefines.c_str());
  1660. cmSystemTools::ReplaceString(line, "COMPILE_DEFINITIONS_DEBUG",
  1661. debugDefines.c_str());
  1662. cmSystemTools::ReplaceString(line, "COMPILE_DEFINITIONS_RELWITHDEBINFO",
  1663. debugrelDefines.c_str());
  1664. cmSystemTools::ReplaceString(line, "COMPILE_DEFINITIONS_RELEASE",
  1665. releaseDefines.c_str());
  1666. cmSystemTools::ReplaceString(line, "COMPILE_DEFINITIONS",
  1667. defines.c_str());
  1668. }
  1669. fout << line.c_str() << std::endl;
  1670. }
  1671. }
  1672. void cmLocalVisualStudio6Generator::WriteDSPFooter(std::ostream& fout)
  1673. {
  1674. cmsys::ifstream fin(this->DSPFooterTemplate.c_str());
  1675. if(!fin)
  1676. {
  1677. cmSystemTools::Error("Error Reading ",
  1678. this->DSPFooterTemplate.c_str());
  1679. }
  1680. std::string line;
  1681. while(cmSystemTools::GetLineFromStream(fin, line))
  1682. {
  1683. fout << line << std::endl;
  1684. }
  1685. }
  1686. //----------------------------------------------------------------------------
  1687. void cmLocalVisualStudio6Generator
  1688. ::ComputeLinkOptions(cmTarget& target,
  1689. const std::string& configName,
  1690. const std::string extraOptions,
  1691. std::string& options)
  1692. {
  1693. cmGeneratorTarget* gt =
  1694. this->GlobalGenerator->GetGeneratorTarget(&target);
  1695. // Compute the link information for this configuration.
  1696. cmComputeLinkInformation* pcli = gt->GetLinkInformation(configName);
  1697. if(!pcli)
  1698. {
  1699. return;
  1700. }
  1701. cmComputeLinkInformation& cli = *pcli;
  1702. typedef cmComputeLinkInformation::ItemVector ItemVector;
  1703. ItemVector const& linkLibs = cli.GetItems();
  1704. std::vector<std::string> const& linkDirs = cli.GetDirectories();
  1705. this->OutputObjects(target, "LINK", options);
  1706. // Build the link options code.
  1707. for(std::vector<std::string>::const_iterator d = linkDirs.begin();
  1708. d != linkDirs.end(); ++d)
  1709. {
  1710. std::string dir = *d;
  1711. if(!dir.empty())
  1712. {
  1713. if(dir[dir.size()-1] != '/')
  1714. {
  1715. dir += "/";
  1716. }
  1717. dir += "$(IntDir)";
  1718. options += "# ADD LINK32 /LIBPATH:";
  1719. options += this->ConvertToOutputFormat(dir.c_str(), SHELL);
  1720. options += " /LIBPATH:";
  1721. options += this->ConvertToOutputFormat(d->c_str(), SHELL);
  1722. options += "\n";
  1723. }
  1724. }
  1725. for(ItemVector::const_iterator l = linkLibs.begin();
  1726. l != linkLibs.end(); ++l)
  1727. {
  1728. options += "# ADD LINK32 ";
  1729. if(l->IsPath)
  1730. {
  1731. options +=
  1732. this->ConvertToOutputFormat(l->Value.c_str(), SHELL);
  1733. }
  1734. else if (!l->Target
  1735. || l->Target->GetType() != cmTarget::INTERFACE_LIBRARY)
  1736. {
  1737. options += l->Value;
  1738. }
  1739. options += "\n";
  1740. }
  1741. // Add extra options if any.
  1742. if(!extraOptions.empty())
  1743. {
  1744. options += "# ADD LINK32 ";
  1745. options += extraOptions;
  1746. options += "\n";
  1747. }
  1748. }
  1749. //----------------------------------------------------------------------------
  1750. void cmLocalVisualStudio6Generator
  1751. ::OutputObjects(cmTarget& target, const char* tool,
  1752. std::string& options)
  1753. {
  1754. // VS 6 does not support per-config source locations so we
  1755. // list object library content on the link line instead.
  1756. cmGeneratorTarget* gt =
  1757. this->GlobalGenerator->GetGeneratorTarget(&target);
  1758. std::vector<std::string> objs;
  1759. gt->UseObjectLibraries(objs, "");
  1760. for(std::vector<std::string>::const_iterator
  1761. oi = objs.begin(); oi != objs.end(); ++oi)
  1762. {
  1763. options += "# ADD ";
  1764. options += tool;
  1765. options += "32 ";
  1766. options += this->ConvertToOutputFormat(oi->c_str(), SHELL);
  1767. options += "\n";
  1768. }
  1769. }
  1770. std::string
  1771. cmLocalVisualStudio6Generator
  1772. ::GetTargetDirectory(cmTarget const&) const
  1773. {
  1774. // No per-target directory for this generator (yet).
  1775. return "";
  1776. }
  1777. //----------------------------------------------------------------------------
  1778. std::string
  1779. cmLocalVisualStudio6Generator
  1780. ::ComputeLongestObjectDirectory(cmTarget&) const
  1781. {
  1782. // Compute the maximum length configuration name.
  1783. std::string config_max;
  1784. for(std::vector<std::string>::const_iterator
  1785. i = this->Configurations.begin();
  1786. i != this->Configurations.end(); ++i)
  1787. {
  1788. // Strip the subdirectory name out of the configuration name.
  1789. std::string config = this->GetConfigName(*i);
  1790. if(config.size() > config_max.size())
  1791. {
  1792. config_max = config;
  1793. }
  1794. }
  1795. // Compute the maximum length full path to the intermediate
  1796. // files directory for any configuration. This is used to construct
  1797. // object file names that do not produce paths that are too long.
  1798. std::string dir_max;
  1799. dir_max += this->Makefile->GetCurrentBinaryDirectory();
  1800. dir_max += "/";
  1801. dir_max += config_max;
  1802. dir_max += "/";
  1803. return dir_max;
  1804. }
  1805. std::string
  1806. cmLocalVisualStudio6Generator
  1807. ::GetConfigName(std::string const& configuration) const
  1808. {
  1809. // Strip the subdirectory name out of the configuration name.
  1810. std::string config = configuration;
  1811. std::string::size_type pos = config.find_last_of(" ");
  1812. config = config.substr(pos+1, std::string::npos);
  1813. config = config.substr(0, config.size()-1);
  1814. return config;
  1815. }
  1816. //----------------------------------------------------------------------------
  1817. bool
  1818. cmLocalVisualStudio6Generator
  1819. ::CheckDefinition(std::string const& define) const
  1820. {
  1821. // Perform the standard check first.
  1822. if(!this->cmLocalGenerator::CheckDefinition(define))
  1823. {
  1824. return false;
  1825. }
  1826. // Now do the VS6-specific check.
  1827. if(define.find_first_of(" ") != define.npos &&
  1828. define.find_first_of("\"$;") != define.npos)
  1829. {
  1830. std::ostringstream e;
  1831. e << "WARNING: The VS6 IDE does not support preprocessor definition "
  1832. << "values with spaces and '\"', '$', or ';'.\n"
  1833. << "CMake is dropping a preprocessor definition: " << define << "\n"
  1834. << "Consider defining the macro in a (configured) header file.\n";
  1835. cmSystemTools::Message(e.str().c_str());
  1836. return false;
  1837. }
  1838. // Assume it is supported.
  1839. return true;
  1840. }