cmLocalVisualStudio6Generator.cxx 43 KB

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