cmLocalVisualStudio7Generator.cxx 49 KB

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