cmLocalVisualStudio6Generator.cxx 37 KB

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