cmLocalVisualStudio6Generator.cxx 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmGlobalGenerator.h"
  14. #include "cmLocalVisualStudio6Generator.h"
  15. #include "cmMakefile.h"
  16. #include "cmSystemTools.h"
  17. #include "cmSourceFile.h"
  18. #include "cmCacheManager.h"
  19. #include "cmake.h"
  20. #include <cmsys/RegularExpression.hxx>
  21. cmLocalVisualStudio6Generator::cmLocalVisualStudio6Generator()
  22. {
  23. }
  24. cmLocalVisualStudio6Generator::~cmLocalVisualStudio6Generator()
  25. {
  26. }
  27. void cmLocalVisualStudio6Generator::Generate()
  28. {
  29. std::set<cmStdString> lang;
  30. lang.insert("C");
  31. lang.insert("CXX");
  32. this->CreateCustomTargetsAndCommands(lang);
  33. this->OutputDSPFile();
  34. }
  35. void cmLocalVisualStudio6Generator::OutputDSPFile()
  36. {
  37. // If not an in source build, then create the output directory
  38. if(strcmp(this->Makefile->GetStartOutputDirectory(),
  39. this->Makefile->GetHomeDirectory()) != 0)
  40. {
  41. if(!cmSystemTools::MakeDirectory(this->Makefile->GetStartOutputDirectory()))
  42. {
  43. cmSystemTools::Error("Error creating directory ",
  44. this->Makefile->GetStartOutputDirectory());
  45. }
  46. }
  47. // Setup /I and /LIBPATH options for the resulting DSP file. VS 6
  48. // truncates long include paths so make it as short as possible if
  49. // the length threatents this problem.
  50. unsigned int maxIncludeLength = 3000;
  51. bool useShortPath = false;
  52. for(int j=0; j < 2; ++j)
  53. {
  54. std::vector<std::string> includes;
  55. this->GetIncludeDirectories(includes);
  56. std::vector<std::string>::iterator i;
  57. for(i = includes.begin(); i != includes.end(); ++i)
  58. {
  59. std::string tmp =
  60. this->ConvertToOptionallyRelativeOutputPath(i->c_str());
  61. if(useShortPath)
  62. {
  63. cmSystemTools::GetShortPath(tmp.c_str(), tmp);
  64. }
  65. this->IncludeOptions += " /I ";
  66. // quote if not already quoted
  67. if (tmp[0] != '"')
  68. {
  69. this->IncludeOptions += "\"";
  70. this->IncludeOptions += tmp;
  71. this->IncludeOptions += "\"";
  72. }
  73. else
  74. {
  75. this->IncludeOptions += tmp;
  76. }
  77. }
  78. if(j == 0 && this->IncludeOptions.size() > maxIncludeLength)
  79. {
  80. this->IncludeOptions = "";
  81. useShortPath = true;
  82. }
  83. else
  84. {
  85. break;
  86. }
  87. }
  88. // Create the DSP or set of DSP's for libraries and executables
  89. // clear project names
  90. this->CreatedProjectNames.clear();
  91. // Call TraceVSDependencies on all targets
  92. cmTargets &tgts = this->Makefile->GetTargets();
  93. for(cmTargets::iterator l = tgts.begin();
  94. l != tgts.end(); l++)
  95. {
  96. // Add a rule to regenerate the build system when the target
  97. // specification source changes.
  98. const char* suppRegenRule =
  99. this->Makefile->GetDefinition("CMAKE_SUPPRESS_REGENERATION");
  100. if (!cmSystemTools::IsOn(suppRegenRule))
  101. {
  102. this->AddDSPBuildRule(l->second);
  103. }
  104. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  105. // so don't build a projectfile for it
  106. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  107. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  108. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  109. {
  110. cmTarget& target = l->second;
  111. target.TraceVSDependencies(target.GetName(), this->Makefile);
  112. }
  113. }
  114. // build any targets
  115. for(cmTargets::iterator l = tgts.begin();
  116. l != tgts.end(); l++)
  117. {
  118. switch(l->second.GetType())
  119. {
  120. case cmTarget::STATIC_LIBRARY:
  121. this->SetBuildType(STATIC_LIBRARY, l->first.c_str(), l->second);
  122. break;
  123. case cmTarget::SHARED_LIBRARY:
  124. case cmTarget::MODULE_LIBRARY:
  125. this->SetBuildType(DLL, l->first.c_str(), l->second);
  126. break;
  127. case cmTarget::EXECUTABLE:
  128. this->SetBuildType(EXECUTABLE,l->first.c_str(), l->second);
  129. break;
  130. case cmTarget::UTILITY:
  131. case cmTarget::GLOBAL_TARGET:
  132. this->SetBuildType(UTILITY, l->first.c_str(), l->second);
  133. break;
  134. case cmTarget::INSTALL_FILES:
  135. break;
  136. case cmTarget::INSTALL_PROGRAMS:
  137. break;
  138. default:
  139. cmSystemTools::Error("Bad target type", l->first.c_str());
  140. break;
  141. }
  142. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  143. // so don't build a projectfile for it
  144. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  145. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  146. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  147. {
  148. // check to see if the dsp is going into a sub-directory
  149. std::string::size_type pos = l->first.rfind('/');
  150. if(pos != std::string::npos)
  151. {
  152. std::string dir = this->Makefile->GetStartOutputDirectory();
  153. dir += "/";
  154. dir += l->first.substr(0, pos);
  155. if(!cmSystemTools::MakeDirectory(dir.c_str()))
  156. {
  157. cmSystemTools::Error("Error creating directory ", dir.c_str());
  158. }
  159. }
  160. this->CreateSingleDSP(l->first.c_str(),l->second);
  161. }
  162. }
  163. }
  164. void cmLocalVisualStudio6Generator::CreateSingleDSP(const char *lname, cmTarget &target)
  165. {
  166. // add to the list of projects
  167. std::string pname = lname;
  168. this->CreatedProjectNames.push_back(pname);
  169. // create the dsp.cmake file
  170. std::string fname;
  171. fname = this->Makefile->GetStartOutputDirectory();
  172. fname += "/";
  173. fname += lname;
  174. fname += ".dsp";
  175. // save the name of the real dsp file
  176. std::string realDSP = fname;
  177. fname += ".cmake";
  178. std::ofstream fout(fname.c_str());
  179. if(!fout)
  180. {
  181. cmSystemTools::Error("Error Writing ", fname.c_str());
  182. cmSystemTools::ReportLastSystemError("");
  183. }
  184. this->WriteDSPFile(fout,lname,target);
  185. fout.close();
  186. // if the dsp file has changed, then write it.
  187. cmSystemTools::CopyFileIfDifferent(fname.c_str(), realDSP.c_str());
  188. }
  189. void cmLocalVisualStudio6Generator::AddDSPBuildRule(cmTarget& tgt)
  190. {
  191. std::string dspname = tgt.GetName();
  192. dspname += ".dsp.cmake";
  193. const char* dsprule = this->Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  194. cmCustomCommandLine commandLine;
  195. commandLine.push_back(dsprule);
  196. std::string makefileIn = this->Makefile->GetStartDirectory();
  197. makefileIn += "/";
  198. makefileIn += "CMakeLists.txt";
  199. std::string comment = "Building Custom Rule ";
  200. comment += makefileIn;
  201. std::string args;
  202. args = "-H";
  203. args +=
  204. this->Convert(this->Makefile->GetHomeDirectory(),START_OUTPUT, SHELL, true);
  205. commandLine.push_back(args);
  206. args = "-B";
  207. args +=
  208. this->Convert(this->Makefile->GetHomeOutputDirectory(),
  209. START_OUTPUT, SHELL, true);
  210. commandLine.push_back(args);
  211. std::string configFile =
  212. this->Makefile->GetRequiredDefinition("CMAKE_ROOT");
  213. configFile += "/Templates/CMakeWindowsSystemConfig.cmake";
  214. std::vector<std::string> listFiles = this->Makefile->GetListFiles();
  215. bool found = false;
  216. for(std::vector<std::string>::iterator i = listFiles.begin();
  217. i != listFiles.end(); ++i)
  218. {
  219. if(*i == configFile)
  220. {
  221. found = true;
  222. }
  223. }
  224. if(!found)
  225. {
  226. listFiles.push_back(configFile);
  227. }
  228. cmCustomCommandLines commandLines;
  229. commandLines.push_back(commandLine);
  230. const char* no_working_directory = 0;
  231. this->Makefile->AddCustomCommandToOutput(dspname.c_str(), listFiles,
  232. makefileIn.c_str(), commandLines,
  233. comment.c_str(),
  234. no_working_directory, true);
  235. if(cmSourceFile* file = this->Makefile->GetSource(makefileIn.c_str()))
  236. {
  237. tgt.GetSourceFiles().push_back(file);
  238. }
  239. else
  240. {
  241. cmSystemTools::Error("Error adding rule for ", makefileIn.c_str());
  242. }
  243. }
  244. void cmLocalVisualStudio6Generator::WriteDSPFile(std::ostream& fout,
  245. const char *libName,
  246. cmTarget &target)
  247. {
  248. // For utility targets need custom command since pre- and post-
  249. // build does not do anything in Visual Studio 6. In order for the
  250. // rules to run in the correct order as custom commands, we need
  251. // special care for dependencies. The first rule must depend on all
  252. // the dependencies of all the rules. The later rules must each
  253. // depend only on the previous rule.
  254. if ((target.GetType() == cmTarget::UTILITY ||
  255. target.GetType() == cmTarget::GLOBAL_TARGET) &&
  256. (!target.GetPreBuildCommands().empty() ||
  257. !target.GetPostBuildCommands().empty()))
  258. {
  259. // Accumulate the dependencies of all the commands.
  260. std::vector<std::string> depends;
  261. for (std::vector<cmCustomCommand>::const_iterator cr =
  262. target.GetPreBuildCommands().begin();
  263. cr != target.GetPreBuildCommands().end(); ++cr)
  264. {
  265. depends.insert(depends.end(),
  266. cr->GetDepends().begin(), cr->GetDepends().end());
  267. }
  268. for (std::vector<cmCustomCommand>::const_iterator cr =
  269. target.GetPostBuildCommands().begin();
  270. cr != target.GetPostBuildCommands().end(); ++cr)
  271. {
  272. depends.insert(depends.end(),
  273. cr->GetDepends().begin(), cr->GetDepends().end());
  274. }
  275. // Add the pre- and post-build commands in order.
  276. int count = 1;
  277. for (std::vector<cmCustomCommand>::const_iterator cr =
  278. target.GetPreBuildCommands().begin();
  279. cr != target.GetPreBuildCommands().end(); ++cr)
  280. {
  281. this->AddUtilityCommandHack(target, count++, depends, *cr);
  282. }
  283. for (std::vector<cmCustomCommand>::const_iterator cr =
  284. target.GetPostBuildCommands().begin();
  285. cr != target.GetPostBuildCommands().end(); ++cr)
  286. {
  287. this->AddUtilityCommandHack(target, count++, depends, *cr);
  288. }
  289. }
  290. // trace the visual studio dependencies
  291. std::string name = libName;
  292. name += ".dsp.cmake";
  293. // We may be modifying the source groups temporarily, so make a copy.
  294. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  295. // get the classes from the source lists then add them to the groups
  296. std::vector<cmSourceFile*> & classes = target.GetSourceFiles();
  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*>::iterator i = classes.begin();
  300. i != classes.end(); i++)
  301. {
  302. // Add the file to the list of sources.
  303. std::string source = (*i)->GetFullPath();
  304. cmSourceGroup& sourceGroup = this->Makefile->FindSourceGroup(source.c_str(),
  305. sourceGroups);
  306. sourceGroup.AssignSource(*i);
  307. // while we are at it, if it is a .rule file then for visual studio 6 we
  308. // must generate it
  309. if ((*i)->GetSourceExtension() == "rule")
  310. {
  311. if(!cmSystemTools::FileExists(source.c_str()))
  312. {
  313. cmSystemTools::ReplaceString(source, "$(IntDir)/", "");
  314. #if defined(_WIN32) || defined(__CYGWIN__)
  315. std::ofstream fout(source.c_str(),
  316. std::ios::binary | std::ios::out | std::ios::trunc);
  317. #else
  318. std::ofstream fout(source.c_str(),
  319. std::ios::out | std::ios::trunc);
  320. #endif
  321. if(fout)
  322. {
  323. fout.write("# generated from CMake",22);
  324. fout.flush();
  325. fout.close();
  326. }
  327. }
  328. }
  329. }
  330. // Write the DSP file's header.
  331. this->WriteDSPHeader(fout, libName, target, sourceGroups);
  332. // Loop through every source group.
  333. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  334. sg != sourceGroups.end(); ++sg)
  335. {
  336. this->WriteGroup(&(*sg), target, fout, libName);
  337. }
  338. // Write the DSP file's footer.
  339. this->WriteDSPFooter(fout);
  340. }
  341. void cmLocalVisualStudio6Generator::WriteGroup(const cmSourceGroup *sg, cmTarget target, std::ostream &fout, const char *libName)
  342. {
  343. const std::vector<const cmSourceFile *> &sourceFiles =
  344. sg->GetSourceFiles();
  345. // If the group is empty, don't write it at all.
  346. if(sourceFiles.empty())
  347. {
  348. return;
  349. }
  350. // If the group has a name, write the header.
  351. std::string name = sg->GetName();
  352. if(name != "")
  353. {
  354. this->WriteDSPBeginGroup(fout, name.c_str(), "");
  355. }
  356. // Loop through each source in the source group.
  357. for(std::vector<const cmSourceFile *>::const_iterator sf =
  358. sourceFiles.begin(); sf != sourceFiles.end(); ++sf)
  359. {
  360. std::string source = (*sf)->GetFullPath();
  361. const cmCustomCommand *command =
  362. (*sf)->GetCustomCommand();
  363. std::string compileFlags;
  364. std::vector<std::string> depends;
  365. // Add per-source file flags.
  366. if(const char* cflags = (*sf)->GetProperty("COMPILE_FLAGS"))
  367. {
  368. compileFlags += cflags;
  369. }
  370. const char* lang =
  371. this->GlobalGenerator->GetLanguageFromExtension((*sf)->GetSourceExtension().c_str());
  372. if(lang && strcmp(lang, "CXX") == 0)
  373. {
  374. // force a C++ file type
  375. compileFlags += " /TP ";
  376. }
  377. // Check for extra object-file dependencies.
  378. const char* dependsValue = (*sf)->GetProperty("OBJECT_DEPENDS");
  379. if(dependsValue)
  380. {
  381. cmSystemTools::ExpandListArgument(dependsValue, depends);
  382. }
  383. if (source != libName || target.GetType() == cmTarget::UTILITY ||
  384. target.GetType() == cmTarget::GLOBAL_TARGET)
  385. {
  386. fout << "# Begin Source File\n\n";
  387. // Tell MS-Dev what the source is. If the compiler knows how to
  388. // build it, then it will.
  389. fout << "SOURCE=" <<
  390. this->ConvertToOptionallyRelativeOutputPath(source.c_str()) << "\n\n";
  391. if(!depends.empty())
  392. {
  393. // Write out the dependencies for the rule.
  394. fout << "USERDEP__HACK=";
  395. for(std::vector<std::string>::const_iterator d = depends.begin();
  396. d != depends.end(); ++d)
  397. {
  398. fout << "\\\n\t" <<
  399. this->ConvertToOptionallyRelativeOutputPath(d->c_str());
  400. }
  401. fout << "\n";
  402. }
  403. if (command)
  404. {
  405. std::string script =
  406. this->ConstructScript(command->GetCommandLines(),
  407. command->GetWorkingDirectory(),
  408. "\\\n\t");
  409. std::string comment =
  410. this->ConstructComment(*command,
  411. "Building Custom Rule $(InputPath)");
  412. if(comment == "<hack>")
  413. {
  414. comment = "";
  415. }
  416. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  417. this->WriteCustomRule(fout, source.c_str(), script.c_str(),
  418. comment.c_str(), command->GetDepends(),
  419. command->GetOutputs(), flags);
  420. }
  421. else if(compileFlags.size())
  422. {
  423. for(std::vector<std::string>::iterator i
  424. = this->Configurations.begin(); i != this->Configurations.end(); ++i)
  425. {
  426. if (i == this->Configurations.begin())
  427. {
  428. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  429. }
  430. else
  431. {
  432. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  433. }
  434. fout << "\n# ADD CPP " << compileFlags << "\n\n";
  435. }
  436. fout << "!ENDIF\n\n";
  437. }
  438. fout << "# End Source File\n";
  439. }
  440. }
  441. std::vector<cmSourceGroup> children = sg->GetGroupChildren();
  442. for(unsigned int i=0;i<children.size();++i)
  443. {
  444. this->WriteGroup(&children[i], target, fout, libName);
  445. }
  446. // If the group has a name, write the footer.
  447. if(name != "")
  448. {
  449. this->WriteDSPEndGroup(fout);
  450. }
  451. }
  452. void
  453. cmLocalVisualStudio6Generator
  454. ::AddUtilityCommandHack(cmTarget& target, int count,
  455. std::vector<std::string>& depends,
  456. const cmCustomCommand& origCommand)
  457. {
  458. // Create a fake output that forces the rule to run.
  459. char* output = new char[(strlen(this->Makefile->GetStartOutputDirectory()) +
  460. strlen(target.GetName()) + 30)];
  461. sprintf(output,"%s/%s_force_%i", this->Makefile->GetStartOutputDirectory(),
  462. target.GetName(), count);
  463. std::string comment = this->ConstructComment(origCommand, "<hack>");
  464. // Add the rule with the given dependencies and commands.
  465. const char* no_main_dependency = 0;
  466. this->Makefile->AddCustomCommandToOutput(output,
  467. depends,
  468. no_main_dependency,
  469. origCommand.GetCommandLines(),
  470. comment.c_str(),
  471. origCommand.GetWorkingDirectory());
  472. // Replace the dependencies with the output of this rule so that the
  473. // next rule added will run after this one.
  474. depends.clear();
  475. depends.push_back(output);
  476. // Add a source file representing this output to the project.
  477. cmSourceFile* outsf = this->Makefile->GetSourceFileWithOutput(output);
  478. target.GetSourceFiles().push_back(outsf);
  479. // Free the fake output name.
  480. delete [] output;
  481. }
  482. void
  483. cmLocalVisualStudio6Generator
  484. ::WriteCustomRule(std::ostream& fout,
  485. const char* source,
  486. const char* command,
  487. const char* comment,
  488. const std::vector<std::string>& depends,
  489. const std::vector<std::string>& outputs,
  490. const char* flags)
  491. {
  492. // Write the rule for each configuration.
  493. std::vector<std::string>::iterator i;
  494. for(i = this->Configurations.begin(); i != this->Configurations.end(); ++i)
  495. {
  496. if (i == this->Configurations.begin())
  497. {
  498. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  499. }
  500. else
  501. {
  502. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  503. }
  504. if(flags)
  505. {
  506. fout << "\n# ADD CPP " << flags << "\n\n";
  507. }
  508. // Write out the dependencies for the rule.
  509. fout << "USERDEP__HACK=";
  510. for(std::vector<std::string>::const_iterator d = depends.begin();
  511. d != depends.end(); ++d)
  512. {
  513. // Lookup the real name of the dependency in case it is a CMake target.
  514. std::string dep = this->GetRealDependency(d->c_str(), i->c_str());
  515. fout << "\\\n\t" <<
  516. this->ConvertToOptionallyRelativeOutputPath(dep.c_str());
  517. }
  518. fout << "\n";
  519. fout << "# PROP Ignore_Default_Tool 1\n";
  520. fout << "# Begin Custom Build -";
  521. if(comment && *comment)
  522. {
  523. fout << " " << comment;
  524. }
  525. fout << "\n\n";
  526. if(outputs.empty())
  527. {
  528. fout << source << "_force : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  529. fout << command << "\n\n";
  530. }
  531. else
  532. {
  533. for(std::vector<std::string>::const_iterator o = outputs.begin();
  534. o != outputs.end(); ++o)
  535. {
  536. // Write a rule for every output generated by this command.
  537. fout << this->ConvertToOptionallyRelativeOutputPath(o->c_str())
  538. << " : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  539. fout << command << "\n\n";
  540. }
  541. }
  542. fout << "# End Custom Build\n\n";
  543. }
  544. fout << "!ENDIF\n\n";
  545. }
  546. void cmLocalVisualStudio6Generator::WriteDSPBeginGroup(std::ostream& fout,
  547. const char* group,
  548. const char* filter)
  549. {
  550. fout << "# Begin Group \"" << group << "\"\n"
  551. "# PROP Default_Filter \"" << filter << "\"\n";
  552. }
  553. void cmLocalVisualStudio6Generator::WriteDSPEndGroup(std::ostream& fout)
  554. {
  555. fout << "# End Group\n";
  556. }
  557. void cmLocalVisualStudio6Generator::SetBuildType(BuildType b,
  558. const char* libName,
  559. cmTarget& target)
  560. {
  561. std::string root= this->Makefile->GetRequiredDefinition("CMAKE_ROOT");
  562. const char *def= this->Makefile->GetDefinition( "MSPROJECT_TEMPLATE_DIRECTORY");
  563. if( def)
  564. {
  565. root = def;
  566. }
  567. else
  568. {
  569. root += "/Templates";
  570. }
  571. switch(b)
  572. {
  573. case STATIC_LIBRARY:
  574. this->DSPHeaderTemplate = root;
  575. this->DSPHeaderTemplate += "/staticLibHeader.dsptemplate";
  576. this->DSPFooterTemplate = root;
  577. this->DSPFooterTemplate += "/staticLibFooter.dsptemplate";
  578. break;
  579. case DLL:
  580. this->DSPHeaderTemplate = root;
  581. this->DSPHeaderTemplate += "/DLLHeader.dsptemplate";
  582. this->DSPFooterTemplate = root;
  583. this->DSPFooterTemplate += "/DLLFooter.dsptemplate";
  584. break;
  585. case EXECUTABLE:
  586. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  587. {
  588. this->DSPHeaderTemplate = root;
  589. this->DSPHeaderTemplate += "/EXEWinHeader.dsptemplate";
  590. this->DSPFooterTemplate = root;
  591. this->DSPFooterTemplate += "/EXEFooter.dsptemplate";
  592. }
  593. else
  594. {
  595. this->DSPHeaderTemplate = root;
  596. this->DSPHeaderTemplate += "/EXEHeader.dsptemplate";
  597. this->DSPFooterTemplate = root;
  598. this->DSPFooterTemplate += "/EXEFooter.dsptemplate";
  599. }
  600. break;
  601. case UTILITY:
  602. this->DSPHeaderTemplate = root;
  603. this->DSPHeaderTemplate += "/UtilityHeader.dsptemplate";
  604. this->DSPFooterTemplate = root;
  605. this->DSPFooterTemplate += "/UtilityFooter.dsptemplate";
  606. break;
  607. }
  608. // once the build type is set, determine what configurations are
  609. // possible
  610. std::ifstream fin(this->DSPHeaderTemplate.c_str());
  611. cmsys::RegularExpression reg("# Name ");
  612. if(!fin)
  613. {
  614. cmSystemTools::Error("Error Reading ", this->DSPHeaderTemplate.c_str());
  615. }
  616. // reset this->Configurations
  617. this->Configurations.erase(this->Configurations.begin(), this->Configurations.end());
  618. // now add all the configurations possible
  619. std::string line;
  620. while(cmSystemTools::GetLineFromStream(fin, line))
  621. {
  622. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  623. if (reg.find(line))
  624. {
  625. this->Configurations.push_back(line.substr(reg.end()));
  626. }
  627. }
  628. }
  629. // look for custom rules on a target and collect them together
  630. std::string
  631. cmLocalVisualStudio6Generator::CreateTargetRules(cmTarget &target,
  632. const char * /* libName */)
  633. {
  634. std::string customRuleCode = "";
  635. if (target.GetType() >= cmTarget::UTILITY )
  636. {
  637. return customRuleCode;
  638. }
  639. // are there any rules?
  640. if (target.GetPreBuildCommands().size() +
  641. target.GetPreLinkCommands().size() +
  642. target.GetPostBuildCommands().size() == 0)
  643. {
  644. return customRuleCode;
  645. }
  646. customRuleCode = "# Begin Special Build Tool\n";
  647. // Write the pre-build and pre-link together (VS6 does not support
  648. // both). Make sure no continuation character is put on the last
  649. // line.
  650. int prelink_total = (static_cast<int>(target.GetPreBuildCommands().size())+
  651. static_cast<int>(target.GetPreLinkCommands().size()));
  652. int prelink_count = 0;
  653. if(prelink_total > 0)
  654. {
  655. // header stuff
  656. customRuleCode += "PreLink_Cmds=";
  657. }
  658. const char* prelink_newline = "\\\n\t";
  659. for (std::vector<cmCustomCommand>::const_iterator cr =
  660. target.GetPreBuildCommands().begin();
  661. cr != target.GetPreBuildCommands().end(); ++cr)
  662. {
  663. if(++prelink_count == prelink_total)
  664. {
  665. prelink_newline = "";
  666. }
  667. customRuleCode += this->ConstructScript(cr->GetCommandLines(),
  668. cr->GetWorkingDirectory(),
  669. prelink_newline);
  670. }
  671. for (std::vector<cmCustomCommand>::const_iterator cr =
  672. target.GetPreLinkCommands().begin();
  673. cr != target.GetPreLinkCommands().end(); ++cr)
  674. {
  675. if(++prelink_count == prelink_total)
  676. {
  677. prelink_newline = "";
  678. }
  679. customRuleCode += this->ConstructScript(cr->GetCommandLines(),
  680. cr->GetWorkingDirectory(),
  681. prelink_newline);
  682. }
  683. if(prelink_total > 0)
  684. {
  685. customRuleCode += "\n";
  686. }
  687. // Write the post-build rules. Make sure no continuation character
  688. // is put on the last line.
  689. int postbuild_total = static_cast<int>(target.GetPostBuildCommands().size());
  690. int postbuild_count = 0;
  691. const char* postbuild_newline = "\\\n\t";
  692. if(postbuild_total > 0)
  693. {
  694. customRuleCode += "PostBuild_Cmds=";
  695. }
  696. for (std::vector<cmCustomCommand>::const_iterator cr =
  697. target.GetPostBuildCommands().begin();
  698. cr != target.GetPostBuildCommands().end(); ++cr)
  699. {
  700. if(++postbuild_count == postbuild_total)
  701. {
  702. postbuild_newline = "";
  703. }
  704. customRuleCode += this->ConstructScript(cr->GetCommandLines(),
  705. cr->GetWorkingDirectory(),
  706. postbuild_newline);
  707. }
  708. if(postbuild_total > 0)
  709. {
  710. customRuleCode += "\n";
  711. }
  712. customRuleCode += "# End Special Build Tool\n";
  713. return customRuleCode;
  714. }
  715. inline std::string removeQuotes(const std::string& s)
  716. {
  717. if(s[0] == '\"' && s[s.size()-1] == '\"')
  718. {
  719. return s.substr(1, s.size()-2);
  720. }
  721. return s;
  722. }
  723. // Code in blocks surrounded by a test for this definition is needed
  724. // only for compatibility with user project's replacement DSP
  725. // templates. The CMake templates no longer use them.
  726. #define CM_USE_OLD_VS6
  727. void cmLocalVisualStudio6Generator
  728. ::WriteDSPHeader(std::ostream& fout,
  729. const char *libName, cmTarget &target,
  730. std::vector<cmSourceGroup> &)
  731. {
  732. // Lookup the library and executable output directories.
  733. std::string libPath;
  734. if(this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  735. {
  736. libPath = this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  737. }
  738. std::string exePath;
  739. if(this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  740. {
  741. exePath = this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  742. }
  743. // Make sure there are trailing slashes.
  744. if(!libPath.empty())
  745. {
  746. if(libPath[libPath.size()-1] != '/')
  747. {
  748. libPath += "/";
  749. }
  750. }
  751. if(!exePath.empty())
  752. {
  753. if(exePath[exePath.size()-1] != '/')
  754. {
  755. exePath += "/";
  756. }
  757. }
  758. #ifdef CM_USE_OLD_VS6
  759. std::set<std::string> pathEmitted;
  760. // determine the link directories
  761. std::string libOptions;
  762. std::string libDebugOptions;
  763. std::string libOptimizedOptions;
  764. std::string libMultiLineOptions;
  765. std::string libMultiLineOptionsForDebug;
  766. std::string libMultiLineDebugOptions;
  767. std::string libMultiLineOptimizedOptions;
  768. if(libPath.size())
  769. {
  770. std::string lpath =
  771. this->ConvertToOptionallyRelativeOutputPath(libPath.c_str());
  772. if(lpath.size() == 0)
  773. {
  774. lpath = ".";
  775. }
  776. std::string lpathIntDir = libPath + "$(INTDIR)";
  777. lpathIntDir = this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  778. if(pathEmitted.insert(lpath).second)
  779. {
  780. libOptions += " /LIBPATH:";
  781. libOptions += lpathIntDir;
  782. libOptions += " ";
  783. libOptions += " /LIBPATH:";
  784. libOptions += lpath;
  785. libOptions += " ";
  786. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  787. libMultiLineOptions += lpathIntDir;
  788. libMultiLineOptions += " ";
  789. libMultiLineOptions += " /LIBPATH:";
  790. libMultiLineOptions += lpath;
  791. libMultiLineOptions += " \n";
  792. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  793. libMultiLineOptionsForDebug += lpathIntDir;
  794. libMultiLineOptionsForDebug += " ";
  795. libMultiLineOptionsForDebug += " /LIBPATH:";
  796. libMultiLineOptionsForDebug += lpath;
  797. libMultiLineOptionsForDebug += " \n";
  798. }
  799. }
  800. if(exePath.size())
  801. {
  802. std::string lpath =
  803. this->ConvertToOptionallyRelativeOutputPath(exePath.c_str());
  804. if(lpath.size() == 0)
  805. {
  806. lpath = ".";
  807. }
  808. std::string lpathIntDir = exePath + "$(INTDIR)";
  809. lpathIntDir = this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  810. if(pathEmitted.insert(lpath).second)
  811. {
  812. libOptions += " /LIBPATH:";
  813. libOptions += lpathIntDir;
  814. libOptions += " ";
  815. libOptions += " /LIBPATH:";
  816. libOptions += lpath;
  817. libOptions += " ";
  818. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  819. libMultiLineOptions += lpathIntDir;
  820. libMultiLineOptions += " ";
  821. libMultiLineOptions += " /LIBPATH:";
  822. libMultiLineOptions += lpath;
  823. libMultiLineOptions += " \n";
  824. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  825. libMultiLineOptionsForDebug += lpathIntDir;
  826. libMultiLineOptionsForDebug += " ";
  827. libMultiLineOptionsForDebug += " /LIBPATH:";
  828. libMultiLineOptionsForDebug += lpath;
  829. libMultiLineOptionsForDebug += " \n";
  830. }
  831. }
  832. std::vector<std::string>::const_iterator i;
  833. const std::vector<std::string>& libdirs = target.GetLinkDirectories();
  834. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  835. {
  836. std::string path = *i;
  837. if(path[path.size()-1] != '/')
  838. {
  839. path += "/";
  840. }
  841. std::string lpath =
  842. this->ConvertToOptionallyRelativeOutputPath(path.c_str());
  843. if(lpath.size() == 0)
  844. {
  845. lpath = ".";
  846. }
  847. std::string lpathIntDir = path + "$(INTDIR)";
  848. lpathIntDir = this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  849. if(pathEmitted.insert(lpath).second)
  850. {
  851. libOptions += " /LIBPATH:";
  852. libOptions += lpathIntDir;
  853. libOptions += " ";
  854. libOptions += " /LIBPATH:";
  855. libOptions += lpath;
  856. libOptions += " ";
  857. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  858. libMultiLineOptions += lpathIntDir;
  859. libMultiLineOptions += " ";
  860. libMultiLineOptions += " /LIBPATH:";
  861. libMultiLineOptions += lpath;
  862. libMultiLineOptions += " \n";
  863. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  864. libMultiLineOptionsForDebug += lpathIntDir;
  865. libMultiLineOptionsForDebug += " ";
  866. libMultiLineOptionsForDebug += " /LIBPATH:";
  867. libMultiLineOptionsForDebug += lpath;
  868. libMultiLineOptionsForDebug += " \n";
  869. }
  870. }
  871. // find link libraries
  872. const cmTarget::LinkLibraryVectorType& libs = target.GetLinkLibraries();
  873. cmTarget::LinkLibraryVectorType::const_iterator j;
  874. for(j = libs.begin(); j != libs.end(); ++j)
  875. {
  876. // add libraries to executables and dlls (but never include
  877. // a library in a library, bad recursion)
  878. // NEVER LINK STATIC LIBRARIES TO OTHER STATIC LIBRARIES
  879. if ((target.GetType() != cmTarget::SHARED_LIBRARY
  880. && target.GetType() != cmTarget::STATIC_LIBRARY
  881. && target.GetType() != cmTarget::MODULE_LIBRARY) ||
  882. (target.GetType()==cmTarget::SHARED_LIBRARY && libName != j->first) ||
  883. (target.GetType()==cmTarget::MODULE_LIBRARY && libName != j->first))
  884. {
  885. // Compute the proper name to use to link this library.
  886. std::string lib;
  887. std::string libDebug;
  888. cmTarget* tgt = this->GlobalGenerator->FindTarget(0, j->first.c_str());
  889. if(tgt)
  890. {
  891. lib = cmSystemTools::GetFilenameWithoutExtension(tgt->GetFullName().c_str());
  892. libDebug = cmSystemTools::GetFilenameWithoutExtension(tgt->GetFullName("Debug").c_str());
  893. lib += ".lib";
  894. libDebug += ".lib";
  895. }
  896. else
  897. {
  898. lib = j->first.c_str();
  899. libDebug = j->first.c_str();
  900. if(j->first.find(".lib") == std::string::npos)
  901. {
  902. lib += ".lib";
  903. libDebug += ".lib";
  904. }
  905. }
  906. lib = this->ConvertToOptionallyRelativeOutputPath(lib.c_str());
  907. libDebug = this->ConvertToOptionallyRelativeOutputPath(libDebug.c_str());
  908. if (j->second == cmTarget::GENERAL)
  909. {
  910. libOptions += " ";
  911. libOptions += lib;
  912. libMultiLineOptions += "# ADD LINK32 ";
  913. libMultiLineOptions += lib;
  914. libMultiLineOptions += "\n";
  915. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  916. libMultiLineOptionsForDebug += libDebug;
  917. libMultiLineOptionsForDebug += "\n";
  918. }
  919. if (j->second == cmTarget::DEBUG)
  920. {
  921. libDebugOptions += " ";
  922. libDebugOptions += lib;
  923. libMultiLineDebugOptions += "# ADD LINK32 ";
  924. libMultiLineDebugOptions += libDebug;
  925. libMultiLineDebugOptions += "\n";
  926. }
  927. if (j->second == cmTarget::OPTIMIZED)
  928. {
  929. libOptimizedOptions += " ";
  930. libOptimizedOptions += lib;
  931. libMultiLineOptimizedOptions += "# ADD LINK32 ";
  932. libMultiLineOptimizedOptions += lib;
  933. libMultiLineOptimizedOptions += "\n";
  934. }
  935. }
  936. }
  937. #endif
  938. // Get extra linker options for this target type.
  939. std::string extraLinkOptions;
  940. if(target.GetType() == cmTarget::EXECUTABLE)
  941. {
  942. extraLinkOptions = this->Makefile->GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS");
  943. }
  944. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  945. {
  946. extraLinkOptions = this->Makefile->GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS");
  947. }
  948. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  949. {
  950. extraLinkOptions = this->Makefile->GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS");
  951. }
  952. // Get extra linker options for this target.
  953. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS"))
  954. {
  955. extraLinkOptions += " ";
  956. extraLinkOptions += targetLinkFlags;
  957. }
  958. // Get standard libraries for this language.
  959. if(target.GetType() >= cmTarget::EXECUTABLE &&
  960. target.GetType() <= cmTarget::MODULE_LIBRARY)
  961. {
  962. // Get the language to use for linking.
  963. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  964. if(!linkLanguage)
  965. {
  966. cmSystemTools::Error("CMake can not determine linker language for target:",
  967. target.GetName());
  968. return;
  969. }
  970. // Compute the variable name to lookup standard libraries for this
  971. // language.
  972. std::string standardLibsVar = "CMAKE_";
  973. standardLibsVar += linkLanguage;
  974. standardLibsVar += "_STANDARD_LIBRARIES";
  975. // Add standard libraries.
  976. if(const char* stdLibs =
  977. this->Makefile->GetDefinition(standardLibsVar.c_str()))
  978. {
  979. extraLinkOptions += " ";
  980. extraLinkOptions += stdLibs;
  981. }
  982. }
  983. // Compute the real name of the target.
  984. std::string outputName = "(OUTPUT_NAME is for libraries and executables only)";
  985. std::string outputNameDebug = outputName;
  986. std::string outputNameRelease = outputName;
  987. std::string outputNameMinSizeRel = outputName;
  988. std::string outputNameRelWithDebInfo = outputName;
  989. if(target.GetType() == cmTarget::EXECUTABLE ||
  990. target.GetType() == cmTarget::STATIC_LIBRARY ||
  991. target.GetType() == cmTarget::SHARED_LIBRARY ||
  992. target.GetType() == cmTarget::MODULE_LIBRARY)
  993. {
  994. outputName = target.GetFullName();
  995. outputNameDebug = target.GetFullName("Debug");
  996. outputNameRelease = target.GetFullName("Release");
  997. outputNameMinSizeRel = target.GetFullName("MinSizeRel");
  998. outputNameRelWithDebInfo = target.GetFullName("RelWithDebInfo");
  999. }
  1000. // Compute the proper link information for the target.
  1001. std::string optionsDebug;
  1002. std::string optionsRelease;
  1003. std::string optionsMinSizeRel;
  1004. std::string optionsRelWithDebInfo;
  1005. if(target.GetType() == cmTarget::EXECUTABLE ||
  1006. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1007. target.GetType() == cmTarget::MODULE_LIBRARY)
  1008. {
  1009. this->ComputeLinkOptions(target, "Debug", extraLinkOptions,
  1010. optionsDebug);
  1011. this->ComputeLinkOptions(target, "Release", extraLinkOptions,
  1012. optionsRelease);
  1013. this->ComputeLinkOptions(target, "MinSizeRel", extraLinkOptions,
  1014. optionsMinSizeRel);
  1015. this->ComputeLinkOptions(target, "RelWithDebInfo", extraLinkOptions,
  1016. optionsRelWithDebInfo);
  1017. }
  1018. #ifdef CM_USE_OLD_VS6
  1019. // Compute link information for the target.
  1020. if(extraLinkOptions.size())
  1021. {
  1022. libOptions += " ";
  1023. libOptions += extraLinkOptions;
  1024. libOptions += " ";
  1025. libMultiLineOptions += "# ADD LINK32 ";
  1026. libMultiLineOptions += extraLinkOptions;
  1027. libMultiLineOptions += " \n";
  1028. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  1029. libMultiLineOptionsForDebug += extraLinkOptions;
  1030. libMultiLineOptionsForDebug += " \n";
  1031. }
  1032. #endif
  1033. // are there any custom rules on the target itself
  1034. // only if the target is a lib or exe
  1035. std::string customRuleCode = this->CreateTargetRules(target, libName);
  1036. std::ifstream fin(this->DSPHeaderTemplate.c_str());
  1037. if(!fin)
  1038. {
  1039. cmSystemTools::Error("Error Reading ", this->DSPHeaderTemplate.c_str());
  1040. }
  1041. std::string staticLibOptions;
  1042. if(target.GetType() == cmTarget::STATIC_LIBRARY )
  1043. {
  1044. if(const char* libflags = target.GetProperty("STATIC_LIBRARY_FLAGS"))
  1045. {
  1046. staticLibOptions = libflags;
  1047. }
  1048. }
  1049. std::string exportSymbol;
  1050. if (const char* custom_export_name = target.GetProperty("DEFINE_SYMBOL"))
  1051. {
  1052. exportSymbol = custom_export_name;
  1053. }
  1054. else
  1055. {
  1056. std::string in = libName;
  1057. in += "_EXPORTS";
  1058. exportSymbol = cmSystemTools::MakeCindentifier(in.c_str());
  1059. }
  1060. std::string line;
  1061. while(cmSystemTools::GetLineFromStream(fin, line))
  1062. {
  1063. const char* mfcFlag = this->Makefile->GetDefinition("CMAKE_MFC_FLAG");
  1064. if(!mfcFlag)
  1065. {
  1066. mfcFlag = "0";
  1067. }
  1068. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME_EXPORTS",
  1069. exportSymbol.c_str());
  1070. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE",
  1071. customRuleCode.c_str());
  1072. cmSystemTools::ReplaceString(line, "CMAKE_MFC_FLAG",
  1073. mfcFlag);
  1074. if(target.GetType() == cmTarget::STATIC_LIBRARY )
  1075. {
  1076. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS",
  1077. staticLibOptions.c_str());
  1078. }
  1079. if(this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  1080. {
  1081. cmSystemTools::ReplaceString(line, "/nologo", "");
  1082. }
  1083. #ifdef CM_USE_OLD_VS6
  1084. cmSystemTools::ReplaceString(line, "CM_LIBRARIES",
  1085. libOptions.c_str());
  1086. cmSystemTools::ReplaceString(line, "CM_DEBUG_LIBRARIES",
  1087. libDebugOptions.c_str());
  1088. cmSystemTools::ReplaceString(line, "CM_OPTIMIZED_LIBRARIES",
  1089. libOptimizedOptions.c_str());
  1090. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES_FOR_DEBUG",
  1091. libMultiLineOptionsForDebug.c_str());
  1092. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES",
  1093. libMultiLineOptions.c_str());
  1094. cmSystemTools::ReplaceString(line, "CM_MULTILINE_DEBUG_LIBRARIES",
  1095. libMultiLineDebugOptions.c_str());
  1096. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIMIZED_LIBRARIES",
  1097. libMultiLineOptimizedOptions.c_str());
  1098. #endif
  1099. // Substitute the real output name into the template.
  1100. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_DEBUG", outputNameDebug.c_str());
  1101. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_RELEASE", outputNameRelease.c_str());
  1102. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_MINSIZEREL", outputNameMinSizeRel.c_str());
  1103. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_RELWITHDEBINFO", outputNameRelWithDebInfo.c_str());
  1104. cmSystemTools::ReplaceString(line, "OUTPUT_NAME", outputName.c_str());
  1105. // Substitute the proper link information into the template.
  1106. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_DEBUG", optionsDebug.c_str());
  1107. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_RELEASE", optionsRelease.c_str());
  1108. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_MINSIZEREL", optionsMinSizeRel.c_str());
  1109. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_RELWITHDEBINFO", optionsRelWithDebInfo.c_str());
  1110. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES",
  1111. this->IncludeOptions.c_str());
  1112. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  1113. // because LIBRARY_OUTPUT_PATH and EXECUTABLE_OUTPUT_PATH
  1114. // are already quoted in the template file,
  1115. // we need to remove the quotes here, we still need
  1116. // to convert to output path for unix to win32 conversion
  1117. cmSystemTools::ReplaceString(line, "LIBRARY_OUTPUT_PATH",
  1118. removeQuotes(
  1119. this->ConvertToOptionallyRelativeOutputPath(libPath.c_str())).c_str());
  1120. cmSystemTools::ReplaceString(line, "EXECUTABLE_OUTPUT_PATH",
  1121. removeQuotes(
  1122. this->ConvertToOptionallyRelativeOutputPath(exePath.c_str())).c_str());
  1123. cmSystemTools::ReplaceString(line,
  1124. "EXTRA_DEFINES",
  1125. this->Makefile->GetDefineFlags());
  1126. const char* debugPostfix
  1127. = this->Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  1128. cmSystemTools::ReplaceString(line, "DEBUG_POSTFIX",
  1129. debugPostfix?debugPostfix:"");
  1130. // store flags for each configuration
  1131. std::string flags = " ";
  1132. std::string flagsRelease = " ";
  1133. std::string flagsMinSize = " ";
  1134. std::string flagsDebug = " ";
  1135. std::string flagsDebugRel = " ";
  1136. if(target.GetType() >= cmTarget::EXECUTABLE &&
  1137. target.GetType() <= cmTarget::MODULE_LIBRARY)
  1138. {
  1139. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  1140. if(!linkLanguage)
  1141. {
  1142. cmSystemTools::Error("CMake can not determine linker language for target:",
  1143. target.GetName());
  1144. return;
  1145. }
  1146. // if CXX is on and the target contains cxx code then add the cxx flags
  1147. std::string baseFlagVar = "CMAKE_";
  1148. baseFlagVar += linkLanguage;
  1149. baseFlagVar += "_FLAGS";
  1150. flags = this->Makefile->GetSafeDefinition(baseFlagVar.c_str());
  1151. std::string flagVar = baseFlagVar + "_RELEASE";
  1152. flagsRelease = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1153. flagsRelease += " -DCMAKE_INTDIR=\\\"Release\\\" ";
  1154. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_RELEASE"))
  1155. {
  1156. flagsRelease += targetLinkFlags;
  1157. flagsRelease += " ";
  1158. }
  1159. flagVar = baseFlagVar + "_MINSIZEREL";
  1160. flagsMinSize = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1161. flagsMinSize += " -DCMAKE_INTDIR=\\\"MinSizeRel\\\" ";
  1162. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_MINSIZEREL"))
  1163. {
  1164. flagsMinSize += targetLinkFlags;
  1165. flagsMinSize += " ";
  1166. }
  1167. flagVar = baseFlagVar + "_DEBUG";
  1168. flagsDebug = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1169. flagsDebug += " -DCMAKE_INTDIR=\\\"Debug\\\" ";
  1170. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_DEBUG"))
  1171. {
  1172. flagsDebug += targetLinkFlags;
  1173. flagsDebug += " ";
  1174. }
  1175. flagVar = baseFlagVar + "_RELWITHDEBINFO";
  1176. flagsDebugRel = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1177. flagsDebugRel += " -DCMAKE_INTDIR=\\\"RelWithDebInfo\\\" ";
  1178. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_RELWITHDEBINFO"))
  1179. {
  1180. flagsDebugRel += targetLinkFlags;
  1181. flagsDebugRel += " ";
  1182. }
  1183. }
  1184. // if unicode is not found, then add -D_MBCS
  1185. std::string defs = this->Makefile->GetDefineFlags();
  1186. if(flags.find("D_UNICODE") == flags.npos &&
  1187. defs.find("D_UNICODE") == flags.npos)
  1188. {
  1189. flags += " /D \"_MBCS\"";
  1190. }
  1191. // Add per-target flags.
  1192. if(const char* targetFlags = target.GetProperty("COMPILE_FLAGS"))
  1193. {
  1194. flags += " ";
  1195. flags += targetFlags;
  1196. }
  1197. // The template files have CXX FLAGS in them, that need to be replaced.
  1198. // There are not separate CXX and C template files, so we use the same
  1199. // variable names. The previous code sets up flags* variables to contain
  1200. // the correct C or CXX flags
  1201. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_MINSIZEREL", flagsMinSize.c_str());
  1202. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_DEBUG", flagsDebug.c_str());
  1203. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELWITHDEBINFO", flagsDebugRel.c_str());
  1204. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELEASE", flagsRelease.c_str());
  1205. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS", flags.c_str());
  1206. fout << line.c_str() << std::endl;
  1207. }
  1208. }
  1209. void cmLocalVisualStudio6Generator::WriteDSPFooter(std::ostream& fout)
  1210. {
  1211. std::ifstream fin(this->DSPFooterTemplate.c_str());
  1212. if(!fin)
  1213. {
  1214. cmSystemTools::Error("Error Reading ",
  1215. this->DSPFooterTemplate.c_str());
  1216. }
  1217. std::string line;
  1218. while(cmSystemTools::GetLineFromStream(fin, line))
  1219. {
  1220. fout << line << std::endl;
  1221. }
  1222. }
  1223. //-----------------------------------------------------------------------------
  1224. void cmLocalVisualStudio6Generator::ComputeLinkOptions(cmTarget& target,
  1225. const char* configName,
  1226. const std::string extraOptions,
  1227. std::string& options)
  1228. {
  1229. // Compute the link information for this configuration.
  1230. std::vector<cmStdString> linkLibs;
  1231. std::vector<cmStdString> linkDirs;
  1232. this->ComputeLinkInformation(target, configName, linkLibs, linkDirs);
  1233. // Build the link options code.
  1234. for(std::vector<cmStdString>::const_iterator d = linkDirs.begin();
  1235. d != linkDirs.end(); ++d)
  1236. {
  1237. std::string dir = *d;
  1238. if(!dir.empty())
  1239. {
  1240. if(dir[dir.size()-1] != '/')
  1241. {
  1242. dir += "/";
  1243. }
  1244. dir += "$(IntDir)";
  1245. options += "# ADD LINK32 /LIBPATH:";
  1246. options += this->ConvertToOptionallyRelativeOutputPath(dir.c_str());
  1247. options += " /LIBPATH:";
  1248. options += this->ConvertToOptionallyRelativeOutputPath(d->c_str());
  1249. options += "\n";
  1250. }
  1251. }
  1252. for(std::vector<cmStdString>::const_iterator l = linkLibs.begin();
  1253. l != linkLibs.end(); ++l)
  1254. {
  1255. options += "# ADD LINK32 ";
  1256. options += this->ConvertToOptionallyRelativeOutputPath(l->c_str());
  1257. options += "\n";
  1258. }
  1259. // Add extra options if any.
  1260. if(!extraOptions.empty())
  1261. {
  1262. options += "# ADD LINK32 ";
  1263. options += extraOptions;
  1264. options += "\n";
  1265. }
  1266. }