cmLocalVisualStudio7Generator.cxx 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  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 "cmGlobalVisualStudio7Generator.h"
  14. #include "cmLocalVisualStudio7Generator.h"
  15. #include "cmMakefile.h"
  16. #include "cmSystemTools.h"
  17. #include "cmSourceFile.h"
  18. #include "cmCacheManager.h"
  19. #include "cmake.h"
  20. cmLocalVisualStudio7Generator::cmLocalVisualStudio7Generator()
  21. {
  22. m_Version = 7;
  23. }
  24. cmLocalVisualStudio7Generator::~cmLocalVisualStudio7Generator()
  25. {
  26. }
  27. void cmLocalVisualStudio7Generator::Generate()
  28. {
  29. std::set<cmStdString> lang;
  30. lang.insert("C");
  31. lang.insert("CXX");
  32. lang.insert("RC");
  33. lang.insert("IDL");
  34. lang.insert("DEF");
  35. this->CreateCustomTargetsAndCommands(lang);
  36. this->OutputVCProjFile();
  37. }
  38. // TODO
  39. // for CommandLine= need to repleace quotes with &quot
  40. // write out configurations
  41. void cmLocalVisualStudio7Generator::OutputVCProjFile()
  42. {
  43. // If not an in source build, then create the output directory
  44. if(strcmp(m_Makefile->GetStartOutputDirectory(),
  45. m_Makefile->GetHomeDirectory()) != 0)
  46. {
  47. if(!cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory()))
  48. {
  49. cmSystemTools::Error("Error creating directory ",
  50. m_Makefile->GetStartOutputDirectory());
  51. }
  52. }
  53. m_LibraryOutputPath = "";
  54. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  55. {
  56. m_LibraryOutputPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  57. }
  58. if(m_LibraryOutputPath.size())
  59. {
  60. // make sure there is a trailing slash
  61. if(m_LibraryOutputPath[m_LibraryOutputPath.size()-1] != '/')
  62. {
  63. m_LibraryOutputPath += "/";
  64. }
  65. }
  66. m_ExecutableOutputPath = "";
  67. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  68. {
  69. m_ExecutableOutputPath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  70. }
  71. if(m_ExecutableOutputPath.size())
  72. {
  73. // make sure there is a trailing slash
  74. if(m_ExecutableOutputPath[m_ExecutableOutputPath.size()-1] != '/')
  75. {
  76. m_ExecutableOutputPath += "/";
  77. }
  78. }
  79. // Create the VCProj or set of VCProj's for libraries and executables
  80. // clear project names
  81. m_CreatedProjectNames.clear();
  82. // build any targets
  83. cmTargets &tgts = m_Makefile->GetTargets();
  84. for(cmTargets::iterator l = tgts.begin();
  85. l != tgts.end(); l++)
  86. {
  87. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  88. // so don't build a projectfile for it
  89. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  90. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  91. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  92. {
  93. this->CreateSingleVCProj(l->first.c_str(),l->second);
  94. }
  95. }
  96. }
  97. void cmLocalVisualStudio7Generator::CreateSingleVCProj(const char *lname, cmTarget &target)
  98. {
  99. // add to the list of projects
  100. std::string pname = lname;
  101. m_CreatedProjectNames.push_back(pname);
  102. // create the dsp.cmake file
  103. std::string fname;
  104. fname = m_Makefile->GetStartOutputDirectory();
  105. fname += "/";
  106. fname += lname;
  107. fname += ".vcproj";
  108. // save the name of the real dsp file
  109. std::string realVCProj = fname;
  110. fname += ".cmake";
  111. std::ofstream fout(fname.c_str());
  112. if(!fout)
  113. {
  114. cmSystemTools::Error("Error Writing ", fname.c_str());
  115. }
  116. this->WriteVCProjFile(fout,lname,target);
  117. fout.close();
  118. // if the dsp file has changed, then write it.
  119. cmSystemTools::CopyFileIfDifferent(fname.c_str(), realVCProj.c_str());
  120. }
  121. void cmLocalVisualStudio7Generator::AddVCProjBuildRule()
  122. {
  123. std::string dspname = *(m_CreatedProjectNames.end()-1);
  124. dspname += ".vcproj.cmake";
  125. const char* dsprule = m_Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  126. cmCustomCommandLine commandLine;
  127. commandLine.push_back(dsprule);
  128. std::string makefileIn = m_Makefile->GetStartDirectory();
  129. makefileIn += "/";
  130. makefileIn += "CMakeLists.txt";
  131. std::string args;
  132. args = "-H";
  133. args +=
  134. this->Convert(m_Makefile->GetHomeDirectory(), START_OUTPUT, SHELL, true);
  135. commandLine.push_back(args);
  136. args = "-B";
  137. args +=
  138. this->Convert(m_Makefile->GetHomeOutputDirectory(),
  139. START_OUTPUT, SHELL, true);
  140. commandLine.push_back(args);
  141. std::string configFile =
  142. m_Makefile->GetRequiredDefinition("CMAKE_ROOT");
  143. configFile += "/Templates/CMakeWindowsSystemConfig.cmake";
  144. std::vector<std::string> listFiles = m_Makefile->GetListFiles();
  145. bool found = false;
  146. for(std::vector<std::string>::iterator i = listFiles.begin();
  147. i != listFiles.end(); ++i)
  148. {
  149. if(*i == configFile)
  150. {
  151. found = true;
  152. }
  153. }
  154. if(!found)
  155. {
  156. listFiles.push_back(configFile);
  157. }
  158. cmCustomCommandLines commandLines;
  159. commandLines.push_back(commandLine);
  160. const char* no_comment = 0;
  161. m_Makefile->AddCustomCommandToOutput(dspname.c_str(), listFiles, makefileIn.c_str(),
  162. commandLines, no_comment, true);
  163. }
  164. void cmLocalVisualStudio7Generator::WriteConfigurations(std::ostream& fout,
  165. const char *libName,
  166. cmTarget &target)
  167. {
  168. std::vector<std::string> *configs =
  169. static_cast<cmGlobalVisualStudio7Generator *>(m_GlobalGenerator)->GetConfigurations();
  170. fout << "\t<Configurations>\n";
  171. for( std::vector<std::string>::iterator i = configs->begin();
  172. i != configs->end(); ++i)
  173. {
  174. this->WriteConfiguration(fout, i->c_str(), libName, target);
  175. }
  176. fout << "\t</Configurations>\n";
  177. }
  178. // This is a table mapping XML tag IDE names to command line options
  179. struct cmVS7FlagTable
  180. {
  181. const char* IDEName; // name used in the IDE xml file
  182. const char* commandFlag; // command line flag
  183. const char* comment; // comment
  184. const char* value; // string value
  185. };
  186. // fill the table here
  187. // currently the comment field is not used for anything other than documentation
  188. // NOTE: Make sure the longer commandFlag comes FIRST!
  189. cmVS7FlagTable cmLocalVisualStudio7GeneratorFlagTable[] =
  190. {
  191. // option flags (some flags map to the same option)
  192. {"BasicRuntimeChecks", "GZ", "Stack frame checks", "1"},
  193. {"BasicRuntimeChecks", "RTCsu", "Both stack and uninitialized checks ", "3"},
  194. {"BasicRuntimeChecks", "RTCs", "Stack frame checks", "1"},
  195. {"BasicRuntimeChecks", "RTCu", "Uninitialized Variables ", "2"},
  196. {"BasicRuntimeChecks", "RTC1", "Both stack and uninitialized checks ", "3"},
  197. {"DebugInformationFormat", "Z7", "debug format", "1"},
  198. {"DebugInformationFormat", "Zd", "debug format", "2"},
  199. {"DebugInformationFormat", "Zi", "debug format", "3"},
  200. {"DebugInformationFormat", "ZI", "debug format", "4"},
  201. {"EnableEnhancedInstructionSet", "arch:SSE2", "Use sse2 instructions", "2"},
  202. {"EnableEnhancedInstructionSet", "arch:SSE", "Use sse instructions", "1"},
  203. {"FavorSizeOrSpeed", "Ot", "Favor fast code", "1"},
  204. {"FavorSizeOrSpeed", "Os", "Favor small code", "2"},
  205. {"Optimization", "Od", "Non Debug", "0"},
  206. {"Optimization", "O1", "Min Size", "1"},
  207. {"Optimization", "O2", "Max Speed", "2"},
  208. {"Optimization", "Ox", "Max Optimization", "3"},
  209. {"OptimizeForProcessor", "GB", "Blended processor mode", "0"},
  210. {"OptimizeForProcessor", "G5", "Pentium", "1"},
  211. {"OptimizeForProcessor", "G6", "PPro PII PIII", "2"},
  212. {"OptimizeForProcessor", "G7", "Pentium 4 or Athlon", "3"},
  213. {"InlineFunctionExpansion", "Ob0", "no inlines", "0"},
  214. {"InlineFunctionExpansion", "Ob1", "when inline keyword", "1"},
  215. {"InlineFunctionExpansion", "Ob2", "any time you can inline", "2"},
  216. {"RuntimeLibrary", "MTd", "Multithreded debug", "1"},
  217. {"RuntimeLibrary", "MT", "Multithreded", "0"},
  218. {"RuntimeLibrary", "MDd", "Multithreded dll debug", "3"},
  219. {"RuntimeLibrary", "MD", "Multithreded dll", "2"},
  220. {"RuntimeLibrary", "MLd", "Sinble Thread debug", "5"},
  221. {"RuntimeLibrary", "ML", "Sinble Thread", "4"},
  222. {"StructMemberAlignment", "Zp16", "struct align 16 byte ", "5"},
  223. {"StructMemberAlignment", "Zp1", "struct align 1 byte ", "1"},
  224. {"StructMemberAlignment", "Zp2", "struct align 2 byte ", "2"},
  225. {"StructMemberAlignment", "Zp4", "struct align 4 byte ", "3"},
  226. {"StructMemberAlignment", "Zp8", "struct align 8 byte ", "4"},
  227. {"WarningLevel", "W1", "Warning level", "1"},
  228. {"WarningLevel", "W2", "Warning level", "2"},
  229. {"WarningLevel", "W3", "Warning level", "3"},
  230. {"WarningLevel", "W4", "Warning level", "4"},
  231. // boolean flags
  232. {"BufferSecurityCheck", "GS", "Buffer security check", "TRUE"},
  233. {"EnableFibreSafeOptimization", "GT", "OmitFramePointers", "TRUE"},
  234. {"EnableIntrinsicFunctions", "Oi", "EnableIntrinsicFunctions", "TRUE"},
  235. {"ExceptionHandling", "EHsc", "enable c++ exceptions", "TRUE"},
  236. {"ExceptionHandling", "GX", "enable c++ exceptions", "TRUE"},
  237. {"GlobalOptimizations", "Og", "Global Optimize", "TRUE"},
  238. {"ImproveFloatingPointConsistency", "Op", "ImproveFloatingPointConsistency", "TRUE"},
  239. {"MinimalRebuild", "Gm", "minimal rebuild", "TRUE"},
  240. {"OmitFramePointers", "Oy", "OmitFramePointers", "TRUE"},
  241. {"OptimizeForWindowsApplication", "GA", "Optimize for windows", "TRUE"},
  242. {"RuntimeTypeInfo", "GR", "Turn on Run time type information for c++", "TRUE"},
  243. {"SmallerTypeCheck", "RTCc", "smaller type check", "TRUE"},
  244. {"WarnAsError", "WX", "Treat warnings as errors", "TRUE"},
  245. {0,0,0,0 }
  246. };
  247. cmVS7FlagTable cmLocalVisualStudio7GeneratorLinkFlagTable[] =
  248. {
  249. // option flags (some flags map to the same option)
  250. {"LinkIncremental", "INCREMENTAL:NO", "link incremental", "1"},
  251. {"LinkIncremental", "INCREMENTAL:YES", "link incremental", "2"},
  252. {0,0,0,0 }
  253. };
  254. void cmLocalVisualStudio7Generator::WriteConfiguration(std::ostream& fout,
  255. const char* configName,
  256. const char *libName,
  257. cmTarget &target)
  258. {
  259. // create a map of xml tags to the values they should have in the output
  260. // for example, "BufferSecurityCheck" = "TRUE"
  261. // first fill this table with the values for the configuration
  262. // Debug, Release, etc,
  263. // Then parse the command line flags specified in CMAKE_CXX_FLAGS
  264. // and CMAKE_C_FLAGS
  265. // and overwrite or add new values to this map
  266. std::map<cmStdString, cmStdString> flagMap;
  267. // since the default is on for this, but if /EHsc is found
  268. // in the flags it will be turned on and we have /EHSC on by
  269. // default in the CXX flags, then this is the only way to turn this off
  270. flagMap["ExceptionHandling"] = "FALSE";
  271. const char* mfcFlag = m_Makefile->GetDefinition("CMAKE_MFC_FLAG");
  272. if(!mfcFlag)
  273. {
  274. mfcFlag = "0";
  275. }
  276. fout << "\t\t<Configuration\n"
  277. << "\t\t\tName=\"" << configName << "|Win32\"\n"
  278. << "\t\t\tOutputDirectory=\"" << configName << "\"\n";
  279. // This is an internal type to Visual Studio, it seems that:
  280. // 4 == static library
  281. // 2 == dll
  282. // 1 == executable
  283. // 10 == utility
  284. const char* configType = "10";
  285. switch(target.GetType())
  286. {
  287. case cmTarget::STATIC_LIBRARY:
  288. configType = "4";
  289. break;
  290. case cmTarget::SHARED_LIBRARY:
  291. case cmTarget::MODULE_LIBRARY:
  292. configType = "2";
  293. break;
  294. case cmTarget::EXECUTABLE:
  295. configType = "1";
  296. break;
  297. case cmTarget::UTILITY:
  298. configType = "10";
  299. default:
  300. break;
  301. }
  302. std::string flags;
  303. std::string flagsRelease = " ";
  304. std::string flagsMinSize = " ";
  305. std::string flagsDebug = " ";
  306. std::string flagsDebugRel = " ";
  307. if(strcmp(configType, "10") != 0)
  308. {
  309. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  310. if(!linkLanguage)
  311. {
  312. cmSystemTools::Error("CMake can not determine linker language for target:",
  313. target.GetName());
  314. return;
  315. }
  316. if(!(strcmp(linkLanguage, "RC") == 0 || strcmp(linkLanguage, "DEF") == 0))
  317. {
  318. std::string baseFlagVar = "CMAKE_";
  319. baseFlagVar += linkLanguage;
  320. baseFlagVar += "_FLAGS";
  321. flags = m_Makefile->GetRequiredDefinition(baseFlagVar.c_str());
  322. std::string flagVar = baseFlagVar + "_RELEASE";
  323. flagsRelease += m_Makefile->GetRequiredDefinition(flagVar.c_str());
  324. flagVar = baseFlagVar + "_MINSIZEREL";
  325. flagsMinSize += m_Makefile->GetRequiredDefinition(flagVar.c_str());
  326. flagVar = baseFlagVar + "_DEBUG";
  327. flagsDebug += m_Makefile->GetRequiredDefinition(flagVar.c_str());
  328. flagVar = baseFlagVar + "_RELWITHDEBINFO";
  329. flagsDebugRel += m_Makefile->GetRequiredDefinition(flagVar.c_str());
  330. }
  331. }
  332. std::string programDatabase;
  333. const char* pre = "WIN32,_DEBUG,_WINDOWS";
  334. std::string debugPostfix = "";
  335. bool debug = !strcmp(configName,"Debug");
  336. if (debug && m_Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX"))
  337. {
  338. debugPostfix = m_Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  339. }
  340. // fill the flagMap for Debug, Release, MinSizeRel, and RelWithDebInfo
  341. // also set the flags, and pre-defined macros
  342. if(strcmp(configName, "Debug") == 0)
  343. {
  344. flagMap["InlineFunctionExpansion"] = "0";
  345. flagMap["Optimization"] = "0";
  346. flagMap["RuntimeLibrary"] = "3";
  347. flags += flagsDebug;
  348. pre = "WIN32,_DEBUG,_WINDOWS";
  349. std::string libpath = m_LibraryOutputPath +
  350. "$(OutDir)/" + libName + debugPostfix + ".pdb";
  351. programDatabase = "\t\t\t\tProgramDatabaseFileName=\"";
  352. programDatabase += libpath;
  353. programDatabase += "\"";
  354. }
  355. else if (strcmp(configName, "Release") == 0)
  356. {
  357. flagMap["InlineFunctionExpansion"] = "1";
  358. flagMap["Optimization"] = "2";
  359. flagMap["RuntimeLibrary"] = "2";
  360. flags += flagsRelease;
  361. pre = "WIN32,_WINDOWS";
  362. flags += flagsRelease;
  363. }
  364. else if(strcmp(configName, "MinSizeRel") == 0)
  365. {
  366. flagMap["InlineFunctionExpansion"] = "1";
  367. flagMap["Optimization"] = "1";
  368. flagMap["RuntimeLibrary"] = "2";
  369. pre = "WIN32,_WINDOWS";
  370. flags += flagsMinSize;
  371. }
  372. else if(strcmp(configName, "RelWithDebInfo") == 0)
  373. {
  374. flagMap["InlineFunctionExpansion"] = "1";
  375. flagMap["Optimization"] = "2";
  376. flagMap["RuntimeLibrary"] = "2";
  377. pre = "WIN32,_WINDOWS";
  378. flags += flagsDebugRel;
  379. std::string libpath = m_LibraryOutputPath +
  380. "$(OutDir)/" + libName + debugPostfix + ".pdb";
  381. programDatabase = "\t\t\t\tProgramDatabaseFileName=\"";
  382. programDatabase += libpath;
  383. programDatabase += "\"";
  384. }
  385. // The intermediate directory name consists of a directory for the
  386. // target and a subdirectory for the configuration name.
  387. std::string intermediateDir = this->GetTargetDirectory(target);
  388. intermediateDir += "/";
  389. intermediateDir += configName;
  390. fout << "\t\t\tIntermediateDirectory=\""
  391. << this->ConvertToXMLOutputPath(intermediateDir.c_str())
  392. << "\"\n"
  393. << "\t\t\tConfigurationType=\"" << configType << "\"\n"
  394. << "\t\t\tUseOfMFC=\"" << mfcFlag << "\"\n"
  395. << "\t\t\tATLMinimizesCRunTimeLibraryUsage=\"FALSE\"\n";
  396. // if -D_UNICODE or /D_UNICODE is found in the flags
  397. // change the character set to unicode, if not then
  398. // default to MBCS
  399. std::string defs = m_Makefile->GetDefineFlags();
  400. if(flags.find("D_UNICODE") != flags.npos ||
  401. defs.find("D_UNICODE") != flags.npos)
  402. {
  403. fout << "\t\t\tCharacterSet=\"1\">\n";
  404. }
  405. else
  406. {
  407. fout << "\t\t\tCharacterSet=\"2\">\n";
  408. }
  409. fout << "\t\t\t<Tool\n"
  410. << "\t\t\t\tName=\"VCCLCompilerTool\"\n"
  411. << "\t\t\t\tAdditionalOptions=\"";
  412. // now fill the flagMap from the command line flags, and
  413. // if a flag is used, it will be removed from the flags string by
  414. // this function call
  415. this->FillFlagMapFromCommandFlags(flagMap,
  416. &cmLocalVisualStudio7GeneratorFlagTable[0],
  417. flags);
  418. std::string defineFlags = m_Makefile->GetDefineFlags();
  419. // now check the define flags for flags other than -D and
  420. // put them in the map, the -D flags will be left in the defineFlags
  421. // variable as -D is not in the flagMap
  422. this->FillFlagMapFromCommandFlags(flagMap,
  423. &cmLocalVisualStudio7GeneratorFlagTable[0],
  424. defineFlags);
  425. // output remaining flags that were not mapped to anything
  426. fout << this->EscapeForXML(flags.c_str()).c_str();
  427. fout << " -DCMAKE_INTDIR=\\&quot;" << configName << "\\&quot;"
  428. << "\"\n";
  429. fout << "\t\t\t\tAdditionalIncludeDirectories=\"";
  430. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  431. std::vector<std::string>::iterator i = includes.begin();
  432. for(;i != includes.end(); ++i)
  433. {
  434. std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
  435. fout << ipath << ";";
  436. }
  437. fout << "\"\n";
  438. // set a few cmake specific flags
  439. if(m_Makefile->IsOn("CMAKE_CXX_USE_RTTI"))
  440. {
  441. flagMap["RuntimeTypeInfo"] = "TRUE";
  442. }
  443. if ( m_Makefile->GetDefinition("CMAKE_CXX_WARNING_LEVEL") )
  444. {
  445. flagMap["WarningLevel"] = m_Makefile->GetDefinition("CMAKE_CXX_WARNING_LEVEL");
  446. }
  447. // Now copy the flag map into the xml for the file
  448. for(std::map<cmStdString, cmStdString>::iterator m = flagMap.begin();
  449. m != flagMap.end(); ++m)
  450. {
  451. fout << "\t\t\t\t" << m->first << "=\"" << m->second << "\"\n";
  452. }
  453. fout << "\t\t\t\tPreprocessorDefinitions=\"" << pre;
  454. if(target.GetType() == cmTarget::SHARED_LIBRARY
  455. || target.GetType() == cmTarget::MODULE_LIBRARY)
  456. {
  457. std::string exportSymbol;
  458. if (const char* custom_export_name = target.GetProperty("DEFINE_SYMBOL"))
  459. {
  460. exportSymbol = custom_export_name;
  461. }
  462. else
  463. {
  464. std::string id = libName;
  465. id += "_EXPORTS";
  466. exportSymbol = cmSystemTools::MakeCindentifier(id.c_str());
  467. }
  468. fout << "," << exportSymbol;
  469. }
  470. this->OutputDefineFlags(defineFlags.c_str(), fout);
  471. fout << "\"\n";
  472. fout << "\t\t\t\tAssemblerListingLocation=\"" << configName << "\"\n";
  473. fout << "\t\t\t\tObjectFile=\"$(IntDir)\\\"\n";
  474. if(m_Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  475. {
  476. fout << "\t\t\t\tSuppressStartupBanner=\"FALSE\"\n";
  477. }
  478. if(programDatabase.size())
  479. {
  480. fout << programDatabase << "\n";
  481. }
  482. fout << "/>\n"; // end of <Tool Name=VCCLCompilerTool
  483. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCCustomBuildTool\"/>\n";
  484. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCResourceCompilerTool\"\n"
  485. << "\t\t\t\tAdditionalIncludeDirectories=\"";
  486. for(i = includes.begin();i != includes.end(); ++i)
  487. {
  488. std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
  489. fout << ipath << ";";
  490. }
  491. // add the -D flags to the RC tool
  492. fout << "\"\n"
  493. << "\t\t\t\tPreprocessorDefinitions=\"" << pre;
  494. this->OutputDefineFlags(defineFlags.c_str(), fout);
  495. fout << "\" />\n";
  496. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCMIDLTool\"/>\n";
  497. this->OutputTargetRules(fout, target, libName);
  498. this->OutputBuildTool(fout, configName, libName, target);
  499. fout << "\t\t</Configuration>\n";
  500. }
  501. void cmLocalVisualStudio7Generator::FillFlagMapFromCommandFlags(
  502. std::map<cmStdString, cmStdString>& flagMap,
  503. cmVS7FlagTable* flagTable,
  504. std::string& flags)
  505. {
  506. std::string replace;
  507. std::string option;
  508. while(flagTable->IDEName)
  509. {
  510. option.reserve(strlen(flagTable->commandFlag+2));
  511. // first do the - version
  512. option.insert(static_cast<std::string::size_type>(0),
  513. static_cast<std::string::size_type>(1),
  514. '-');
  515. option.append(flagTable->commandFlag);
  516. while(flags.find(option) != flags.npos)
  517. {
  518. // replace the flag
  519. cmSystemTools::ReplaceString(flags, option.c_str(), "");
  520. // now put value into flag map
  521. flagMap[flagTable->IDEName] = flagTable->value;
  522. }
  523. // now do the / version
  524. option[0] = '/';
  525. while(flags.find(option) != flags.npos)
  526. {
  527. // replace the flag
  528. cmSystemTools::ReplaceString(flags, option.c_str(), "");
  529. // now put value into flag map
  530. flagMap[flagTable->IDEName] = flagTable->value;
  531. }
  532. // move to next flag
  533. flagTable++;
  534. }
  535. }
  536. void cmLocalVisualStudio7Generator::OutputBuildTool(std::ostream& fout,
  537. const char* configName,
  538. const char *libName,
  539. cmTarget &target)
  540. {
  541. std::string temp;
  542. std::string debugPostfix = "";
  543. bool debug = !strcmp(configName,"Debug");
  544. if (debug && m_Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX"))
  545. {
  546. debugPostfix = m_Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  547. }
  548. std::string extraLinkOptions;
  549. if(target.GetType() == cmTarget::EXECUTABLE)
  550. {
  551. extraLinkOptions = m_Makefile->GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS");
  552. }
  553. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  554. {
  555. extraLinkOptions = m_Makefile->GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS");
  556. }
  557. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  558. {
  559. extraLinkOptions = m_Makefile->GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS");
  560. }
  561. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  562. if(targetLinkFlags)
  563. {
  564. extraLinkOptions += " ";
  565. extraLinkOptions += targetLinkFlags;
  566. }
  567. std::map<cmStdString, cmStdString> flagMap;
  568. this->
  569. FillFlagMapFromCommandFlags(flagMap,
  570. &cmLocalVisualStudio7GeneratorLinkFlagTable[0],
  571. extraLinkOptions);
  572. switch(target.GetType())
  573. {
  574. case cmTarget::STATIC_LIBRARY:
  575. {
  576. std::string libpath = m_LibraryOutputPath +
  577. "$(OutDir)/" + libName + debugPostfix + ".lib";
  578. fout << "\t\t\t<Tool\n"
  579. << "\t\t\t\tName=\"VCLibrarianTool\"\n";
  580. if(const char* libflags = target.GetProperty("STATIC_LIBRARY_FLAGS"))
  581. {
  582. fout << "\t\t\t\tAdditionalOptions=\"" << libflags << "\"\n";
  583. }
  584. fout << "\t\t\t\tOutputFile=\""
  585. << this->ConvertToXMLOutputPathSingle(libpath.c_str()) << ".\"/>\n";
  586. break;
  587. }
  588. case cmTarget::SHARED_LIBRARY:
  589. case cmTarget::MODULE_LIBRARY:
  590. {
  591. fout << "\t\t\t<Tool\n"
  592. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  593. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386";
  594. if(extraLinkOptions.size())
  595. {
  596. fout << " " << cmLocalVisualStudio7Generator::EscapeForXML(
  597. extraLinkOptions.c_str()).c_str();
  598. }
  599. fout << "\"\n"
  600. << "\t\t\t\tAdditionalDependencies=\" odbc32.lib odbccp32.lib ";
  601. this->OutputLibraries(fout, configName, libName, target);
  602. fout << "\"\n";
  603. temp = m_LibraryOutputPath;
  604. temp += configName;
  605. temp += "/";
  606. temp += libName;
  607. temp += debugPostfix;
  608. temp += ".dll";
  609. fout << "\t\t\t\tOutputFile=\""
  610. << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  611. for(std::map<cmStdString, cmStdString>::iterator i = flagMap.begin();
  612. i != flagMap.end(); ++i)
  613. {
  614. fout << "\t\t\t\t" << i->first << "=\"" << i->second << "\"\n";
  615. }
  616. if(m_Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  617. {
  618. fout << "\t\t\t\tSuppressStartupBanner=\"FALSE\"\n";
  619. }
  620. else
  621. {
  622. fout << "\t\t\t\tSuppressStartupBanner=\"TRUE\"\n";
  623. }
  624. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  625. this->OutputLibraryDirectories(fout, configName, libName, target);
  626. fout << "\"\n";
  627. this->OutputModuleDefinitionFile(fout, target);
  628. temp = m_LibraryOutputPath;
  629. temp += "$(OutDir)/";
  630. temp += libName;
  631. temp += debugPostfix;
  632. temp += ".pdb";
  633. fout << "\t\t\t\tProgramDatabaseFile=\"" <<
  634. this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  635. if(strcmp(configName, "Debug") == 0
  636. || strcmp(configName, "RelWithDebInfo") == 0)
  637. {
  638. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  639. }
  640. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  641. if(!linkLanguage)
  642. {
  643. cmSystemTools::Error("CMake can not determine linker language for target:",
  644. target.GetName());
  645. return;
  646. }
  647. std::string stackVar = "CMAKE_";
  648. stackVar += linkLanguage;
  649. stackVar += "_STACK_SIZE";
  650. const char* stackVal = m_Makefile->GetDefinition(stackVar.c_str());
  651. if(stackVal)
  652. {
  653. fout << "\t\t\t\tStackReserveSize=\"" << stackVal << "\"\n";
  654. }
  655. temp = m_LibraryOutputPath;
  656. temp += configName;
  657. temp += "/";
  658. temp += libName;
  659. temp += debugPostfix;
  660. temp += ".lib";
  661. fout << "\t\t\t\tImportLibrary=\"" << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"/>\n";
  662. }
  663. break;
  664. case cmTarget::EXECUTABLE:
  665. {
  666. fout << "\t\t\t<Tool\n"
  667. << "\t\t\t\tName=\"VCLinkerTool\"\n"
  668. << "\t\t\t\tAdditionalOptions=\"/MACHINE:I386";
  669. if(extraLinkOptions.size())
  670. {
  671. fout << " " << cmLocalVisualStudio7Generator::EscapeForXML(
  672. extraLinkOptions.c_str()).c_str();
  673. }
  674. fout << "\"\n"
  675. << "\t\t\t\tAdditionalDependencies=\""
  676. << m_Makefile->GetRequiredDefinition("CMAKE_STANDARD_LIBRARIES")
  677. << " ";
  678. this->OutputLibraries(fout, configName, libName, target);
  679. fout << "\"\n";
  680. temp = m_ExecutableOutputPath;
  681. temp += configName;
  682. temp += "/";
  683. // do we have a different executable name?
  684. if (target.GetProperty("OUTPUT_NAME"))
  685. {
  686. temp += target.GetProperty("OUTPUT_NAME");
  687. }
  688. else
  689. {
  690. temp += libName;
  691. }
  692. temp += ".exe";
  693. fout << "\t\t\t\tOutputFile=\"" << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  694. for(std::map<cmStdString, cmStdString>::iterator i = flagMap.begin();
  695. i != flagMap.end(); ++i)
  696. {
  697. fout << "\t\t\t\t" << i->first << "=\"" << i->second << "\"\n";
  698. }
  699. if(m_Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  700. {
  701. fout << "\t\t\t\tSuppressStartupBanner=\"FALSE\"\n";
  702. }
  703. else
  704. {
  705. fout << "\t\t\t\tSuppressStartupBanner=\"TRUE\"\n";
  706. }
  707. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  708. this->OutputLibraryDirectories(fout, configName, libName, target);
  709. fout << "\"\n";
  710. fout << "\t\t\t\tProgramDatabaseFile=\"" << m_LibraryOutputPath
  711. << "$(OutDir)\\" << libName << ".pdb\"\n";
  712. if(strcmp(configName, "Debug") == 0
  713. || strcmp(configName, "RelWithDebInfo") == 0)
  714. {
  715. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  716. }
  717. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  718. {
  719. fout << "\t\t\t\tSubSystem=\"2\"\n";
  720. }
  721. else
  722. {
  723. fout << "\t\t\t\tSubSystem=\"1\"\n";
  724. }
  725. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  726. if(!linkLanguage)
  727. {
  728. cmSystemTools::Error("CMake can not determine linker language for target:",
  729. target.GetName());
  730. return;
  731. }
  732. std::string stackVar = "CMAKE_";
  733. stackVar += linkLanguage;
  734. stackVar += "_STACK_SIZE";
  735. const char* stackVal = m_Makefile->GetDefinition(stackVar.c_str());
  736. if(stackVal)
  737. {
  738. fout << "\t\t\t\tStackReserveSize=\"" << stackVal << "\"";
  739. }
  740. fout << "/>\n";
  741. break;
  742. }
  743. case cmTarget::UTILITY:
  744. break;
  745. }
  746. }
  747. void cmLocalVisualStudio7Generator::OutputModuleDefinitionFile(std::ostream& fout,
  748. cmTarget &target)
  749. {
  750. std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
  751. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  752. i != classes.end(); i++)
  753. {
  754. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  755. {
  756. fout << "\t\t\t\tModuleDefinitionFile=\""
  757. << this->ConvertToXMLOutputPath((*i)->GetFullPath().c_str())
  758. << "\"\n";
  759. return;
  760. }
  761. }
  762. }
  763. void cmLocalVisualStudio7Generator::OutputLibraryDirectories(std::ostream& fout,
  764. const char*,
  765. const char*,
  766. cmTarget &tgt)
  767. {
  768. bool hasone = false;
  769. if(m_LibraryOutputPath.size())
  770. {
  771. hasone = true;
  772. std::string temp = m_LibraryOutputPath;
  773. temp += "$(OutDir)";
  774. fout << this->ConvertToXMLOutputPath(temp.c_str()) << "," <<
  775. this->ConvertToXMLOutputPath(m_LibraryOutputPath.c_str());
  776. }
  777. if(m_ExecutableOutputPath.size() &&
  778. (m_LibraryOutputPath != m_ExecutableOutputPath))
  779. {
  780. if (hasone)
  781. {
  782. fout << ",";
  783. }
  784. hasone = true;
  785. std::string temp = m_ExecutableOutputPath;
  786. temp += "$(OutDir)";
  787. fout << this->ConvertToXMLOutputPath(temp.c_str()) << "," <<
  788. this->ConvertToXMLOutputPath(m_ExecutableOutputPath.c_str());
  789. }
  790. std::set<std::string> pathEmitted;
  791. std::vector<std::string>::const_iterator i;
  792. const std::vector<std::string>& libdirs = tgt.GetLinkDirectories();
  793. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  794. {
  795. std::string lpath = *i;
  796. if(lpath[lpath.size()-1] != '/')
  797. {
  798. lpath += "/";
  799. }
  800. if(pathEmitted.insert(lpath).second)
  801. {
  802. if(hasone)
  803. {
  804. fout << ",";
  805. }
  806. std::string lpathi = lpath + "$(OutDir)";
  807. fout << this->ConvertToXMLOutputPath(lpathi.c_str()) << "," <<
  808. this->ConvertToXMLOutputPath(lpath.c_str());
  809. hasone = true;
  810. }
  811. }
  812. }
  813. void cmLocalVisualStudio7Generator::OutputLibraries(std::ostream& fout,
  814. const char* configName,
  815. const char* libName,
  816. cmTarget &target)
  817. {
  818. const cmTarget::LinkLibraries& libs = target.GetLinkLibraries();
  819. cmTarget::LinkLibraries::const_iterator j;
  820. for(j = libs.begin(); j != libs.end(); ++j)
  821. {
  822. if(j->first != libName)
  823. {
  824. std::string lib = j->first;
  825. std::string debugPostfix = "";
  826. // if this is a library we are building then watch for a debugPostfix
  827. if (!strcmp(configName,"Debug"))
  828. {
  829. std::string libPath = j->first + "_CMAKE_PATH";
  830. const char* cacheValue
  831. = m_GlobalGenerator->GetCMakeInstance()->GetCacheDefinition(libPath.c_str());
  832. if(cacheValue && *cacheValue && m_Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX"))
  833. {
  834. debugPostfix = m_Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  835. }
  836. }
  837. // chop off the last 4 chars of the library string
  838. std::string lowerCaseLibExt = j->first;
  839. if(lowerCaseLibExt.size() > 4)
  840. {
  841. lowerCaseLibExt = j->first.substr(j->first.size()-4, 4);
  842. }
  843. // lower case the extension
  844. lowerCaseLibExt = cmSystemTools::LowerCase(lowerCaseLibExt);
  845. // now check to see if it was a .lib, if not then add a .lib
  846. if(lowerCaseLibExt != ".lib")
  847. {
  848. lib += debugPostfix + ".lib";
  849. }
  850. lib = this->ConvertToXMLOutputPath(lib.c_str());
  851. if (j->second == cmTarget::GENERAL
  852. || (j->second == cmTarget::DEBUG && strcmp(configName, "Debug") == 0)
  853. || (j->second == cmTarget::OPTIMIZED && strcmp(configName, "Debug") != 0))
  854. {
  855. fout << lib << " ";
  856. }
  857. }
  858. }
  859. }
  860. void cmLocalVisualStudio7Generator::OutputDefineFlags(const char* flags,
  861. std::ostream& fout)
  862. {
  863. std::string defs = flags;
  864. cmSystemTools::ReplaceString(defs, "/D","-D");
  865. std::string::size_type pos = defs.find("-D");
  866. bool done = pos == std::string::npos;
  867. if(!done)
  868. {
  869. fout << ",";
  870. }
  871. while(!done)
  872. {
  873. std::string::size_type nextpos = defs.find("-D", pos+2);
  874. std::string define;
  875. if(nextpos != std::string::npos)
  876. {
  877. define = defs.substr(pos+2, nextpos - pos -3);
  878. }
  879. else
  880. {
  881. define = defs.substr(pos+2);
  882. done = true;
  883. }
  884. cmSystemTools::ReplaceString(define, "\"", "&quot;");
  885. fout << define << ",";
  886. if(!done)
  887. {
  888. pos = defs.find("-D", nextpos);
  889. }
  890. }
  891. }
  892. void cmLocalVisualStudio7Generator::WriteVCProjFile(std::ostream& fout,
  893. const char *libName,
  894. cmTarget &target)
  895. {
  896. // get the configurations
  897. std::vector<std::string> *configs =
  898. static_cast<cmGlobalVisualStudio7Generator *>
  899. (m_GlobalGenerator)->GetConfigurations();
  900. // if we should add regen rule then...
  901. const char *suppRegenRule =
  902. m_Makefile->GetDefinition("CMAKE_SUPPRESS_REGENERATION");
  903. if (!cmSystemTools::IsOn(suppRegenRule))
  904. {
  905. this->AddVCProjBuildRule();
  906. }
  907. // trace the visual studio dependencies
  908. std::string name = libName;
  909. name += ".vcproj.cmake";
  910. target.TraceVSDependencies(name, m_Makefile);
  911. // We may be modifying the source groups temporarily, so make a copy.
  912. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  913. // get the classes from the source lists then add them to the groups
  914. std::vector<cmSourceFile*> & classes = target.GetSourceFiles();
  915. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  916. i != classes.end(); i++)
  917. {
  918. // Add the file to the list of sources.
  919. std::string source = (*i)->GetFullPath();
  920. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  921. {
  922. m_ModuleDefinitionFile = (*i)->GetFullPath();
  923. }
  924. cmSourceGroup& sourceGroup =
  925. m_Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  926. sourceGroup.AssignSource(*i);
  927. }
  928. // open the project
  929. this->WriteProjectStart(fout, libName, target, sourceGroups);
  930. // write the configuration information
  931. this->WriteConfigurations(fout, libName, target);
  932. fout << "\t<Files>\n";
  933. // Loop through every source group.
  934. for(unsigned int i = 0; i < sourceGroups.size(); ++i)
  935. {
  936. cmSourceGroup sg = sourceGroups[i];
  937. this->WriteGroup(&sg, target, fout, libName, configs);
  938. }
  939. //}
  940. fout << "\t</Files>\n";
  941. // Write the VCProj file's footer.
  942. this->WriteVCProjFooter(fout);
  943. }
  944. void cmLocalVisualStudio7Generator::WriteGroup(const cmSourceGroup *sg, cmTarget target, std::ostream &fout, const char *libName, std::vector<std::string> *configs)
  945. {
  946. const std::vector<const cmSourceFile *> &sourceFiles =
  947. sg->GetSourceFiles();
  948. // If the group is empty, don't write it at all.
  949. if(sourceFiles.empty())
  950. {
  951. return;
  952. }
  953. // If the group has a name, write the header.
  954. std::string name = sg->GetName();
  955. if(name != "")
  956. {
  957. this->WriteVCProjBeginGroup(fout, name.c_str(), "");
  958. }
  959. // Loop through each source in the source group.
  960. for(std::vector<const cmSourceFile *>::const_iterator sf =
  961. sourceFiles.begin(); sf != sourceFiles.end(); ++sf)
  962. {
  963. std::string source = (*sf)->GetFullPath();
  964. const cmCustomCommand *command = (*sf)->GetCustomCommand();
  965. std::string compileFlags;
  966. std::string additionalDeps;
  967. // Check for extra compiler flags.
  968. const char* cflags = (*sf)->GetProperty("COMPILE_FLAGS");
  969. if(cflags)
  970. {
  971. compileFlags = cflags;
  972. }
  973. const char* lang =
  974. m_GlobalGenerator->GetLanguageFromExtension((*sf)->GetSourceExtension().c_str());
  975. if(lang && strcmp(lang, "CXX") == 0)
  976. {
  977. // force a C++ file type
  978. compileFlags += " /TP ";
  979. }
  980. // Check for extra object-file dependencies.
  981. const char* deps = (*sf)->GetProperty("OBJECT_DEPENDS");
  982. if(deps)
  983. {
  984. std::vector<std::string> depends;
  985. cmSystemTools::ExpandListArgument(deps, depends);
  986. if(!depends.empty())
  987. {
  988. std::vector<std::string>::iterator i = depends.begin();
  989. additionalDeps = this->ConvertToXMLOutputPath(i->c_str());
  990. for(++i;i != depends.end(); ++i)
  991. {
  992. additionalDeps += ";";
  993. additionalDeps += this->ConvertToXMLOutputPath(i->c_str());
  994. }
  995. }
  996. }
  997. if (source != libName || target.GetType() == cmTarget::UTILITY)
  998. {
  999. fout << "\t\t\t<File\n";
  1000. std::string d = this->ConvertToXMLOutputPathSingle(source.c_str());
  1001. // Tell MS-Dev what the source is. If the compiler knows how to
  1002. // build it, then it will.
  1003. fout << "\t\t\t\tRelativePath=\"" << d << "\">\n";
  1004. if (command)
  1005. {
  1006. // Construct the entire set of commands in one string.
  1007. std::string script = this->ConstructScript(command->GetCommandLines());
  1008. const char* comment = command->GetComment();
  1009. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  1010. this->WriteCustomRule(fout, source.c_str(), script.c_str(),
  1011. (*comment?comment:"Custom Rule"),
  1012. command->GetDepends(),
  1013. command->GetOutput(), flags);
  1014. }
  1015. else if(compileFlags.size() || additionalDeps.length())
  1016. {
  1017. const char* aCompilerTool = "VCCLCompilerTool";
  1018. std::string ext = (*sf)->GetSourceExtension();
  1019. ext = cmSystemTools::LowerCase(ext);
  1020. if(ext == "idl")
  1021. {
  1022. aCompilerTool = "VCMIDLTool";
  1023. }
  1024. if(ext == "rc")
  1025. {
  1026. aCompilerTool = "VCResourceCompilerTool";
  1027. }
  1028. if(ext == "def")
  1029. {
  1030. aCompilerTool = "VCCustomBuildTool";
  1031. }
  1032. for(std::vector<std::string>::iterator i = configs->begin();
  1033. i != configs->end(); ++i)
  1034. {
  1035. fout << "\t\t\t\t<FileConfiguration\n"
  1036. << "\t\t\t\t\tName=\"" << *i << "|Win32\">\n"
  1037. << "\t\t\t\t\t<Tool\n"
  1038. << "\t\t\t\t\tName=\"" << aCompilerTool << "\"\n";
  1039. if(compileFlags.size())
  1040. {
  1041. fout << "\t\t\t\t\tAdditionalOptions=\""
  1042. << this->EscapeForXML(compileFlags.c_str()) << "\"\n";
  1043. }
  1044. if(additionalDeps.length())
  1045. {
  1046. fout << "\t\t\t\t\tAdditionalDependencies=\""
  1047. << additionalDeps.c_str() << "\"\n";
  1048. }
  1049. fout << "\t\t\t\t\t/>\n"
  1050. << "\t\t\t\t</FileConfiguration>\n";
  1051. }
  1052. }
  1053. fout << "\t\t\t</File>\n";
  1054. }
  1055. }
  1056. std::vector<cmSourceGroup> children = sg->GetGroupChildren();
  1057. for(unsigned int i=0;i<children.size();++i)
  1058. {
  1059. this->WriteGroup(&children[i], target, fout, libName, configs);
  1060. }
  1061. // If the group has a name, write the footer.
  1062. if(name != "")
  1063. {
  1064. this->WriteVCProjEndGroup(fout);
  1065. }
  1066. }
  1067. void cmLocalVisualStudio7Generator::
  1068. WriteCustomRule(std::ostream& fout,
  1069. const char* source,
  1070. const char* command,
  1071. const char* comment,
  1072. const std::vector<std::string>& depends,
  1073. const char *output,
  1074. const char* compileFlags)
  1075. {
  1076. std::string cmd = command;
  1077. cmSystemTools::ReplaceString(cmd, "\"", "&quot;");
  1078. std::vector<std::string>::iterator i;
  1079. std::vector<std::string> *configs =
  1080. static_cast<cmGlobalVisualStudio7Generator *>(m_GlobalGenerator)->GetConfigurations();
  1081. for(i = configs->begin(); i != configs->end(); ++i)
  1082. {
  1083. fout << "\t\t\t\t<FileConfiguration\n";
  1084. fout << "\t\t\t\t\tName=\"" << *i << "|Win32\">\n";
  1085. if(compileFlags)
  1086. {
  1087. fout << "\t\t\t\t\t<Tool\n"
  1088. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n"
  1089. << "\t\t\t\t\tAdditionalOptions=\""
  1090. << this->EscapeForXML(compileFlags) << "\"/>\n";
  1091. }
  1092. fout << "\t\t\t\t\t<Tool\n"
  1093. << "\t\t\t\t\tName=\"VCCustomBuildTool\"\n"
  1094. << "\t\t\t\t\tDescription=\"Building " << comment;
  1095. fout << " " << output;
  1096. fout << "\"\n"
  1097. << "\t\t\t\t\tCommandLine=\"" << cmd << "\n\"\n"
  1098. << "\t\t\t\t\tAdditionalDependencies=\"";
  1099. // Write out the dependencies for the rule.
  1100. std::string temp;
  1101. for(std::vector<std::string>::const_iterator d = depends.begin();
  1102. d != depends.end(); ++d)
  1103. {
  1104. std::string dep = cmSystemTools::GetFilenameName(*d);
  1105. if (cmSystemTools::GetFilenameLastExtension(dep) == ".exe")
  1106. {
  1107. dep = cmSystemTools::GetFilenameWithoutLastExtension(dep);
  1108. }
  1109. // check to see if the dependency is another target built by cmake
  1110. std::string libPath = dep + "_CMAKE_PATH";
  1111. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  1112. if (cacheValue && *cacheValue)
  1113. {
  1114. std::string exePath = "";
  1115. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  1116. {
  1117. exePath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  1118. }
  1119. if(exePath.size())
  1120. {
  1121. libPath = exePath;
  1122. }
  1123. else
  1124. {
  1125. libPath = cacheValue;
  1126. }
  1127. libPath += "/";
  1128. libPath += "$(OutDir)/";
  1129. libPath += dep;
  1130. libPath += ".exe";
  1131. fout << this->ConvertToXMLOutputPath(libPath.c_str())
  1132. << ";";
  1133. }
  1134. else
  1135. {
  1136. fout << this->ConvertToXMLOutputPath(d->c_str())
  1137. << ";";
  1138. }
  1139. }
  1140. fout << "\"\n";
  1141. fout << "\t\t\t\t\tOutputs=\"";
  1142. if(output == 0)
  1143. {
  1144. fout << source << "_force";
  1145. }
  1146. // Write a rule for the output generated by this command.
  1147. fout << this->ConvertToXMLOutputPathSingle(output);
  1148. fout << "\"/>\n";
  1149. fout << "\t\t\t\t</FileConfiguration>\n";
  1150. }
  1151. }
  1152. void cmLocalVisualStudio7Generator::WriteVCProjBeginGroup(std::ostream& fout,
  1153. const char* group,
  1154. const char* )
  1155. {
  1156. fout << "\t\t<Filter\n"
  1157. << "\t\t\tName=\"" << group << "\"\n"
  1158. << "\t\t\tFilter=\"\">\n";
  1159. }
  1160. void cmLocalVisualStudio7Generator::WriteVCProjEndGroup(std::ostream& fout)
  1161. {
  1162. fout << "\t\t</Filter>\n";
  1163. }
  1164. // look for custom rules on a target and collect them together
  1165. void cmLocalVisualStudio7Generator::OutputTargetRules(std::ostream& fout,
  1166. cmTarget &target,
  1167. const char * /*libName*/)
  1168. {
  1169. if (target.GetType() > cmTarget::UTILITY)
  1170. {
  1171. return;
  1172. }
  1173. // add the pre build rules
  1174. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreBuildEventTool\"";
  1175. bool init = false;
  1176. for (std::vector<cmCustomCommand>::const_iterator cr =
  1177. target.GetPreBuildCommands().begin();
  1178. cr != target.GetPreBuildCommands().end(); ++cr)
  1179. {
  1180. if(!init)
  1181. {
  1182. fout << "\nCommandLine=\"";
  1183. init = true;
  1184. }
  1185. std::string script = this->ConstructScript(cr->GetCommandLines());
  1186. fout << this->EscapeForXML(script.c_str()).c_str();
  1187. }
  1188. if (init)
  1189. {
  1190. fout << "\"";
  1191. }
  1192. fout << "/>\n";
  1193. // add the pre Link rules
  1194. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreLinkEventTool\"";
  1195. init = false;
  1196. for (std::vector<cmCustomCommand>::const_iterator cr =
  1197. target.GetPreLinkCommands().begin();
  1198. cr != target.GetPreLinkCommands().end(); ++cr)
  1199. {
  1200. if(!init)
  1201. {
  1202. fout << "\nCommandLine=\"";
  1203. init = true;
  1204. }
  1205. std::string script = this->ConstructScript(cr->GetCommandLines());
  1206. fout << this->EscapeForXML(script.c_str()).c_str();
  1207. }
  1208. if (init)
  1209. {
  1210. fout << "\"";
  1211. }
  1212. fout << "/>\n";
  1213. // add the PostBuild rules
  1214. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPostBuildEventTool\"";
  1215. init = false;
  1216. for (std::vector<cmCustomCommand>::const_iterator cr =
  1217. target.GetPostBuildCommands().begin();
  1218. cr != target.GetPostBuildCommands().end(); ++cr)
  1219. {
  1220. if(!init)
  1221. {
  1222. fout << "\nCommandLine=\"";
  1223. init = true;
  1224. }
  1225. std::string script = this->ConstructScript(cr->GetCommandLines());
  1226. fout << this->EscapeForXML(script.c_str()).c_str();
  1227. }
  1228. if (init)
  1229. {
  1230. fout << "\"";
  1231. }
  1232. fout << "/>\n";
  1233. }
  1234. void
  1235. cmLocalVisualStudio7Generator::WriteProjectStart(std::ostream& fout,
  1236. const char *libName,
  1237. cmTarget &,
  1238. std::vector<cmSourceGroup> &)
  1239. {
  1240. fout << "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\n"
  1241. << "<VisualStudioProject\n"
  1242. << "\tProjectType=\"Visual C++\"\n";
  1243. if(m_Version == 71)
  1244. {
  1245. fout << "\tVersion=\"7.10\"\n";
  1246. }
  1247. else
  1248. {
  1249. if (m_Version == 8)
  1250. {
  1251. fout << "\tVersion=\"8.00\"\n";
  1252. }
  1253. else
  1254. {
  1255. fout << "\tVersion=\"7.00\"\n";
  1256. }
  1257. }
  1258. fout << "\tName=\"" << libName << "\"\n"
  1259. << "\tSccProjectName=\"\"\n"
  1260. << "\tSccLocalPath=\"\"\n"
  1261. << "\tKeyword=\"Win32Proj\">\n"
  1262. << "\t<Platforms>\n"
  1263. << "\t\t<Platform\n\t\t\tName=\"Win32\"/>\n"
  1264. << "\t</Platforms>\n";
  1265. }
  1266. void cmLocalVisualStudio7Generator::WriteVCProjFooter(std::ostream& fout)
  1267. {
  1268. fout << "\t<Globals>\n"
  1269. << "\t</Globals>\n"
  1270. << "</VisualStudioProject>\n";
  1271. }
  1272. std::string cmLocalVisualStudio7Generator::EscapeForXML(const char* s)
  1273. {
  1274. std::string ret = s;
  1275. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1276. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1277. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1278. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1279. return ret;
  1280. }
  1281. std::string cmLocalVisualStudio7Generator::ConvertToXMLOutputPath(const char* path)
  1282. {
  1283. std::string ret = this->ConvertToOptionallyRelativeOutputPath(path);
  1284. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1285. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1286. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1287. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1288. return ret;
  1289. }
  1290. std::string cmLocalVisualStudio7Generator::ConvertToXMLOutputPathSingle(const char* path)
  1291. {
  1292. std::string ret = this->ConvertToOptionallyRelativeOutputPath(path);
  1293. cmSystemTools::ReplaceString(ret, "\"", "");
  1294. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1295. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1296. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1297. return ret;
  1298. }
  1299. void cmLocalVisualStudio7Generator::ConfigureFinalPass()
  1300. {
  1301. cmLocalGenerator::ConfigureFinalPass();
  1302. cmTargets &tgts = m_Makefile->GetTargets();
  1303. cmGlobalVisualStudio7Generator* gg =
  1304. static_cast<cmGlobalVisualStudio7Generator *>(m_GlobalGenerator);
  1305. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  1306. {
  1307. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  1308. {
  1309. cmCustomCommand cc = l->second.GetPostBuildCommands()[0];
  1310. const cmCustomCommandLines& cmds = cc.GetCommandLines();
  1311. std::string project_name = cmds[0][0];
  1312. gg->CreateGUID(project_name.c_str());
  1313. }
  1314. else
  1315. {
  1316. gg->CreateGUID(l->first.c_str());
  1317. }
  1318. }
  1319. }
  1320. //----------------------------------------------------------------------------
  1321. std::string cmLocalVisualStudio7Generator::GetTargetDirectory(cmTarget& target)
  1322. {
  1323. std::string dir;
  1324. // Put a prefix on the name if one is given by the CMake code.
  1325. if(const char* prefix = m_Makefile->GetDefinition("CMAKE_TARGET_DIR_PREFIX"))
  1326. {
  1327. dir = prefix;
  1328. }
  1329. dir += target.GetName();
  1330. dir += ".dir";
  1331. return dir;
  1332. }