cmLocalVisualStudio7Generator.cxx 48 KB

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