cmLocalVisualStudio7Generator.cxx 48 KB

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