cmLocalVisualStudio6Generator.cxx 36 KB

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