cmLocalVisualStudio6Generator.cxx 69 KB

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