cmLocalVisualStudio6Generator.cxx 48 KB

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