cmLocalVisualStudio6Generator.cxx 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  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. void cmLocalVisualStudio6Generator
  724. ::WriteDSPHeader(std::ostream& fout,
  725. const char *libName, cmTarget &target,
  726. std::vector<cmSourceGroup> &)
  727. {
  728. std::set<std::string> pathEmitted;
  729. // determine the link directories
  730. std::string libOptions;
  731. std::string libDebugOptions;
  732. std::string libOptimizedOptions;
  733. std::string libMultiLineOptions;
  734. std::string libMultiLineOptionsForDebug;
  735. std::string libMultiLineDebugOptions;
  736. std::string libMultiLineOptimizedOptions;
  737. // suppoirt override in output directory
  738. std::string libPath = "";
  739. if (this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  740. {
  741. libPath = this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  742. }
  743. std::string exePath = "";
  744. if (this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  745. {
  746. exePath = this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  747. }
  748. if(libPath.size())
  749. {
  750. // make sure there is a trailing slash
  751. if(libPath[libPath.size()-1] != '/')
  752. {
  753. libPath += "/";
  754. }
  755. std::string lpath =
  756. this->ConvertToOptionallyRelativeOutputPath(libPath.c_str());
  757. if(lpath.size() == 0)
  758. {
  759. lpath = ".";
  760. }
  761. std::string lpathIntDir = libPath + "$(INTDIR)";
  762. lpathIntDir = this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  763. if(pathEmitted.insert(lpath).second)
  764. {
  765. libOptions += " /LIBPATH:";
  766. libOptions += lpathIntDir;
  767. libOptions += " ";
  768. libOptions += " /LIBPATH:";
  769. libOptions += lpath;
  770. libOptions += " ";
  771. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  772. libMultiLineOptions += lpathIntDir;
  773. libMultiLineOptions += " ";
  774. libMultiLineOptions += " /LIBPATH:";
  775. libMultiLineOptions += lpath;
  776. libMultiLineOptions += " \n";
  777. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  778. libMultiLineOptionsForDebug += lpathIntDir;
  779. libMultiLineOptionsForDebug += " ";
  780. libMultiLineOptionsForDebug += " /LIBPATH:";
  781. libMultiLineOptionsForDebug += lpath;
  782. libMultiLineOptionsForDebug += " \n";
  783. }
  784. }
  785. if(exePath.size())
  786. {
  787. // make sure there is a trailing slash
  788. if(exePath[exePath.size()-1] != '/')
  789. {
  790. exePath += "/";
  791. }
  792. std::string lpath =
  793. this->ConvertToOptionallyRelativeOutputPath(exePath.c_str());
  794. if(lpath.size() == 0)
  795. {
  796. lpath = ".";
  797. }
  798. std::string lpathIntDir = exePath + "$(INTDIR)";
  799. lpathIntDir = this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  800. if(pathEmitted.insert(lpath).second)
  801. {
  802. libOptions += " /LIBPATH:";
  803. libOptions += lpathIntDir;
  804. libOptions += " ";
  805. libOptions += " /LIBPATH:";
  806. libOptions += lpath;
  807. libOptions += " ";
  808. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  809. libMultiLineOptions += lpathIntDir;
  810. libMultiLineOptions += " ";
  811. libMultiLineOptions += " /LIBPATH:";
  812. libMultiLineOptions += lpath;
  813. libMultiLineOptions += " \n";
  814. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  815. libMultiLineOptionsForDebug += lpathIntDir;
  816. libMultiLineOptionsForDebug += " ";
  817. libMultiLineOptionsForDebug += " /LIBPATH:";
  818. libMultiLineOptionsForDebug += lpath;
  819. libMultiLineOptionsForDebug += " \n";
  820. }
  821. }
  822. std::vector<std::string>::const_iterator i;
  823. const std::vector<std::string>& libdirs = target.GetLinkDirectories();
  824. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  825. {
  826. std::string path = *i;
  827. if(path[path.size()-1] != '/')
  828. {
  829. path += "/";
  830. }
  831. std::string lpath =
  832. this->ConvertToOptionallyRelativeOutputPath(path.c_str());
  833. if(lpath.size() == 0)
  834. {
  835. lpath = ".";
  836. }
  837. std::string lpathIntDir = path + "$(INTDIR)";
  838. lpathIntDir = this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  839. if(pathEmitted.insert(lpath).second)
  840. {
  841. libOptions += " /LIBPATH:";
  842. libOptions += lpathIntDir;
  843. libOptions += " ";
  844. libOptions += " /LIBPATH:";
  845. libOptions += lpath;
  846. libOptions += " ";
  847. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  848. libMultiLineOptions += lpathIntDir;
  849. libMultiLineOptions += " ";
  850. libMultiLineOptions += " /LIBPATH:";
  851. libMultiLineOptions += lpath;
  852. libMultiLineOptions += " \n";
  853. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  854. libMultiLineOptionsForDebug += lpathIntDir;
  855. libMultiLineOptionsForDebug += " ";
  856. libMultiLineOptionsForDebug += " /LIBPATH:";
  857. libMultiLineOptionsForDebug += lpath;
  858. libMultiLineOptionsForDebug += " \n";
  859. }
  860. }
  861. // find link libraries
  862. const cmTarget::LinkLibraryVectorType& libs = target.GetLinkLibraries();
  863. cmTarget::LinkLibraryVectorType::const_iterator j;
  864. for(j = libs.begin(); j != libs.end(); ++j)
  865. {
  866. // add libraries to executables and dlls (but never include
  867. // a library in a library, bad recursion)
  868. // NEVER LINK STATIC LIBRARIES TO OTHER STATIC LIBRARIES
  869. if ((target.GetType() != cmTarget::SHARED_LIBRARY
  870. && target.GetType() != cmTarget::STATIC_LIBRARY
  871. && target.GetType() != cmTarget::MODULE_LIBRARY) ||
  872. (target.GetType()==cmTarget::SHARED_LIBRARY && libName != j->first) ||
  873. (target.GetType()==cmTarget::MODULE_LIBRARY && libName != j->first))
  874. {
  875. // Compute the proper name to use to link this library.
  876. std::string lib;
  877. std::string libDebug;
  878. cmTarget* tgt = this->GlobalGenerator->FindTarget(0, j->first.c_str());
  879. if(tgt)
  880. {
  881. lib = cmSystemTools::GetFilenameWithoutExtension(tgt->GetFullName().c_str());
  882. libDebug = cmSystemTools::GetFilenameWithoutExtension(tgt->GetFullName("Debug").c_str());
  883. lib += ".lib";
  884. libDebug += ".lib";
  885. }
  886. else
  887. {
  888. lib = j->first.c_str();
  889. libDebug = j->first.c_str();
  890. if(j->first.find(".lib") == std::string::npos)
  891. {
  892. lib += ".lib";
  893. libDebug += ".lib";
  894. }
  895. }
  896. lib = this->ConvertToOptionallyRelativeOutputPath(lib.c_str());
  897. libDebug = this->ConvertToOptionallyRelativeOutputPath(libDebug.c_str());
  898. if (j->second == cmTarget::GENERAL)
  899. {
  900. libOptions += " ";
  901. libOptions += lib;
  902. libMultiLineOptions += "# ADD LINK32 ";
  903. libMultiLineOptions += lib;
  904. libMultiLineOptions += "\n";
  905. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  906. libMultiLineOptionsForDebug += libDebug;
  907. libMultiLineOptionsForDebug += "\n";
  908. }
  909. if (j->second == cmTarget::DEBUG)
  910. {
  911. libDebugOptions += " ";
  912. libDebugOptions += lib;
  913. libMultiLineDebugOptions += "# ADD LINK32 ";
  914. libMultiLineDebugOptions += libDebug;
  915. libMultiLineDebugOptions += "\n";
  916. }
  917. if (j->second == cmTarget::OPTIMIZED)
  918. {
  919. libOptimizedOptions += " ";
  920. libOptimizedOptions += lib;
  921. libMultiLineOptimizedOptions += "# ADD LINK32 ";
  922. libMultiLineOptimizedOptions += lib;
  923. libMultiLineOptimizedOptions += "\n";
  924. }
  925. }
  926. }
  927. std::string outputName = "(OUTPUT_NAME is for executables only)";
  928. std::string extraLinkOptions;
  929. // TODO: Fix construction of library/executable name through
  930. // cmTarget. OUTPUT_LIBNAMEDEBUG_POSTFIX should be replaced by the
  931. // library's debug configuration name. OUTPUT_LIBNAME should be
  932. // replaced by the non-debug configuration name. This generator
  933. // should just be re-written to not use template files and just
  934. // generate the code. Setting up these substitutions is a pain.
  935. if(target.GetType() == cmTarget::EXECUTABLE)
  936. {
  937. extraLinkOptions =
  938. this->Makefile->GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS");
  939. // Use the OUTPUT_NAME property if it was set. This is supported
  940. // only for executables.
  941. if(const char* outName = target.GetProperty("OUTPUT_NAME"))
  942. {
  943. outputName = outName;
  944. }
  945. else
  946. {
  947. outputName = target.GetName();
  948. }
  949. outputName += ".exe";
  950. }
  951. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  952. {
  953. extraLinkOptions = this->Makefile->GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS");
  954. }
  955. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  956. {
  957. extraLinkOptions = this->Makefile->GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS");
  958. }
  959. if(extraLinkOptions.size())
  960. {
  961. libOptions += " ";
  962. libOptions += extraLinkOptions;
  963. libOptions += " ";
  964. libMultiLineOptions += "# ADD LINK32 ";
  965. libMultiLineOptions += extraLinkOptions;
  966. libMultiLineOptions += " \n";
  967. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  968. libMultiLineOptionsForDebug += extraLinkOptions;
  969. libMultiLineOptionsForDebug += " \n";
  970. }
  971. if(const char* stdLibs = this->Makefile->GetDefinition("CMAKE_STANDARD_LIBRARIES"))
  972. {
  973. libOptions += " ";
  974. libOptions += stdLibs;
  975. libOptions += " ";
  976. libMultiLineOptions += "# ADD LINK32 ";
  977. libMultiLineOptions += stdLibs;
  978. libMultiLineOptions += " \n";
  979. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  980. libMultiLineOptionsForDebug += stdLibs;
  981. libMultiLineOptionsForDebug += " \n";
  982. }
  983. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS"))
  984. {
  985. libOptions += " ";
  986. libOptions += targetLinkFlags;
  987. libOptions += " ";
  988. libMultiLineOptions += "# ADD LINK32 ";
  989. libMultiLineOptions += targetLinkFlags;
  990. libMultiLineOptions += " \n";
  991. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  992. libMultiLineOptionsForDebug += targetLinkFlags;
  993. libMultiLineOptionsForDebug += " \n";
  994. }
  995. // are there any custom rules on the target itself
  996. // only if the target is a lib or exe
  997. std::string customRuleCode = this->CreateTargetRules(target, libName);
  998. std::ifstream fin(this->DSPHeaderTemplate.c_str());
  999. if(!fin)
  1000. {
  1001. cmSystemTools::Error("Error Reading ", this->DSPHeaderTemplate.c_str());
  1002. }
  1003. std::string staticLibOptions;
  1004. if(target.GetType() == cmTarget::STATIC_LIBRARY )
  1005. {
  1006. if(const char* libflags = target.GetProperty("STATIC_LIBRARY_FLAGS"))
  1007. {
  1008. staticLibOptions = libflags;
  1009. }
  1010. }
  1011. std::string exportSymbol;
  1012. if (const char* custom_export_name = target.GetProperty("DEFINE_SYMBOL"))
  1013. {
  1014. exportSymbol = custom_export_name;
  1015. }
  1016. else
  1017. {
  1018. std::string in = libName;
  1019. in += "_EXPORTS";
  1020. exportSymbol = cmSystemTools::MakeCindentifier(in.c_str());
  1021. }
  1022. std::string line;
  1023. while(cmSystemTools::GetLineFromStream(fin, line))
  1024. {
  1025. const char* mfcFlag = this->Makefile->GetDefinition("CMAKE_MFC_FLAG");
  1026. if(!mfcFlag)
  1027. {
  1028. mfcFlag = "0";
  1029. }
  1030. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME_EXPORTS",
  1031. exportSymbol.c_str());
  1032. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE",
  1033. customRuleCode.c_str());
  1034. cmSystemTools::ReplaceString(line, "CMAKE_MFC_FLAG",
  1035. mfcFlag);
  1036. if(target.GetType() == cmTarget::STATIC_LIBRARY )
  1037. {
  1038. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS",
  1039. staticLibOptions.c_str());
  1040. }
  1041. if(this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  1042. {
  1043. cmSystemTools::ReplaceString(line, "/nologo", "");
  1044. }
  1045. cmSystemTools::ReplaceString(line, "CM_LIBRARIES",
  1046. libOptions.c_str());
  1047. cmSystemTools::ReplaceString(line, "CM_DEBUG_LIBRARIES",
  1048. libDebugOptions.c_str());
  1049. cmSystemTools::ReplaceString(line, "CM_OPTIMIZED_LIBRARIES",
  1050. libOptimizedOptions.c_str());
  1051. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES_FOR_DEBUG",
  1052. libMultiLineOptionsForDebug.c_str());
  1053. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES",
  1054. libMultiLineOptions.c_str());
  1055. cmSystemTools::ReplaceString(line, "CM_MULTILINE_DEBUG_LIBRARIES",
  1056. libMultiLineDebugOptions.c_str());
  1057. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIMIZED_LIBRARIES",
  1058. libMultiLineOptimizedOptions.c_str());
  1059. // Replace the template file text OUTPUT_NAME with the real output
  1060. // name that will be used. Only the executable template should
  1061. // have this text.
  1062. cmSystemTools::ReplaceString(line, "OUTPUT_NAME", outputName.c_str());
  1063. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES",
  1064. this->IncludeOptions.c_str());
  1065. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  1066. // because LIBRARY_OUTPUT_PATH and EXECUTABLE_OUTPUT_PATH
  1067. // are already quoted in the template file,
  1068. // we need to remove the quotes here, we still need
  1069. // to convert to output path for unix to win32 conversion
  1070. cmSystemTools::ReplaceString(line, "LIBRARY_OUTPUT_PATH",
  1071. removeQuotes(
  1072. this->ConvertToOptionallyRelativeOutputPath(libPath.c_str())).c_str());
  1073. cmSystemTools::ReplaceString(line, "EXECUTABLE_OUTPUT_PATH",
  1074. removeQuotes(
  1075. this->ConvertToOptionallyRelativeOutputPath(exePath.c_str())).c_str());
  1076. cmSystemTools::ReplaceString(line,
  1077. "EXTRA_DEFINES",
  1078. this->Makefile->GetDefineFlags());
  1079. const char* debugPostfix
  1080. = this->Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  1081. cmSystemTools::ReplaceString(line, "DEBUG_POSTFIX",
  1082. debugPostfix?debugPostfix:"");
  1083. // store flags for each configuration
  1084. std::string flags = " ";
  1085. std::string flagsRelease = " ";
  1086. std::string flagsMinSize = " ";
  1087. std::string flagsDebug = " ";
  1088. std::string flagsDebugRel = " ";
  1089. if(target.GetType() >= cmTarget::EXECUTABLE &&
  1090. target.GetType() <= cmTarget::MODULE_LIBRARY)
  1091. {
  1092. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  1093. if(!linkLanguage)
  1094. {
  1095. cmSystemTools::Error("CMake can not determine linker language for target:",
  1096. target.GetName());
  1097. return;
  1098. }
  1099. // if CXX is on and the target contains cxx code then add the cxx flags
  1100. std::string baseFlagVar = "CMAKE_";
  1101. baseFlagVar += linkLanguage;
  1102. baseFlagVar += "_FLAGS";
  1103. flags = this->Makefile->GetRequiredDefinition(baseFlagVar.c_str());
  1104. std::string flagVar = baseFlagVar + "_RELEASE";
  1105. flagsRelease = this->Makefile->GetRequiredDefinition(flagVar.c_str());
  1106. flagsRelease += " -DCMAKE_INTDIR=\\\"Release\\\" ";
  1107. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_RELEASE"))
  1108. {
  1109. flagsRelease += targetLinkFlags;
  1110. flagsRelease += " ";
  1111. }
  1112. flagVar = baseFlagVar + "_MINSIZEREL";
  1113. flagsMinSize = this->Makefile->GetRequiredDefinition(flagVar.c_str());
  1114. flagsMinSize += " -DCMAKE_INTDIR=\\\"MinSizeRel\\\" ";
  1115. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_MINSIZEREL"))
  1116. {
  1117. flagsMinSize += targetLinkFlags;
  1118. flagsMinSize += " ";
  1119. }
  1120. flagVar = baseFlagVar + "_DEBUG";
  1121. flagsDebug = this->Makefile->GetRequiredDefinition(flagVar.c_str());
  1122. flagsDebug += " -DCMAKE_INTDIR=\\\"Debug\\\" ";
  1123. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_DEBUG"))
  1124. {
  1125. flagsDebug += targetLinkFlags;
  1126. flagsDebug += " ";
  1127. }
  1128. flagVar = baseFlagVar + "_RELWITHDEBINFO";
  1129. flagsDebugRel = this->Makefile->GetRequiredDefinition(flagVar.c_str());
  1130. flagsDebugRel += " -DCMAKE_INTDIR=\\\"RelWithDebInfo\\\" ";
  1131. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_RELWITHDEBINFO"))
  1132. {
  1133. flagsDebugRel += targetLinkFlags;
  1134. flagsDebugRel += " ";
  1135. }
  1136. }
  1137. // if unicode is not found, then add -D_MBCS
  1138. std::string defs = this->Makefile->GetDefineFlags();
  1139. if(flags.find("D_UNICODE") == flags.npos &&
  1140. defs.find("D_UNICODE") == flags.npos)
  1141. {
  1142. flags += " /D \"_MBCS\"";
  1143. }
  1144. // Add per-target flags.
  1145. if(const char* targetFlags = target.GetProperty("COMPILE_FLAGS"))
  1146. {
  1147. flags += " ";
  1148. flags += targetFlags;
  1149. }
  1150. // The template files have CXX FLAGS in them, that need to be replaced.
  1151. // There are not separate CXX and C template files, so we use the same
  1152. // variable names. The previous code sets up flags* variables to contain
  1153. // the correct C or CXX flags
  1154. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_MINSIZEREL", flagsMinSize.c_str());
  1155. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_DEBUG", flagsDebug.c_str());
  1156. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELWITHDEBINFO", flagsDebugRel.c_str());
  1157. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELEASE", flagsRelease.c_str());
  1158. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS", flags.c_str());
  1159. fout << line.c_str() << std::endl;
  1160. }
  1161. }
  1162. void cmLocalVisualStudio6Generator::WriteDSPFooter(std::ostream& fout)
  1163. {
  1164. std::ifstream fin(this->DSPFooterTemplate.c_str());
  1165. if(!fin)
  1166. {
  1167. cmSystemTools::Error("Error Reading ",
  1168. this->DSPFooterTemplate.c_str());
  1169. }
  1170. std::string line;
  1171. while(cmSystemTools::GetLineFromStream(fin, line))
  1172. {
  1173. fout << line << std::endl;
  1174. }
  1175. }