cmLocalVisualStudio6Generator.cxx 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmGlobalGenerator.h"
  14. #include "cmLocalVisualStudio6Generator.h"
  15. #include "cmMakefile.h"
  16. #include "cmSystemTools.h"
  17. #include "cmSourceFile.h"
  18. #include "cmCacheManager.h"
  19. #include "cmake.h"
  20. #include <cmsys/RegularExpression.hxx>
  21. cmLocalVisualStudio6Generator::cmLocalVisualStudio6Generator()
  22. {
  23. }
  24. cmLocalVisualStudio6Generator::~cmLocalVisualStudio6Generator()
  25. {
  26. }
  27. void cmLocalVisualStudio6Generator::Generate()
  28. {
  29. std::set<cmStdString> lang;
  30. lang.insert("C");
  31. lang.insert("CXX");
  32. this->CreateCustomTargetsAndCommands(lang);
  33. this->OutputDSPFile();
  34. }
  35. void cmLocalVisualStudio6Generator::OutputDSPFile()
  36. {
  37. // If not an in source build, then create the output directory
  38. if(strcmp(this->Makefile->GetStartOutputDirectory(),
  39. this->Makefile->GetHomeDirectory()) != 0)
  40. {
  41. if(!cmSystemTools::MakeDirectory
  42. (this->Makefile->GetStartOutputDirectory()))
  43. {
  44. cmSystemTools::Error("Error creating directory ",
  45. this->Makefile->GetStartOutputDirectory());
  46. }
  47. }
  48. // Setup /I and /LIBPATH options for the resulting DSP file. VS 6
  49. // truncates long include paths so make it as short as possible if
  50. // the length threatents this problem.
  51. unsigned int maxIncludeLength = 3000;
  52. bool useShortPath = false;
  53. for(int j=0; j < 2; ++j)
  54. {
  55. std::vector<std::string> includes;
  56. this->GetIncludeDirectories(includes);
  57. std::vector<std::string>::iterator i;
  58. for(i = includes.begin(); i != includes.end(); ++i)
  59. {
  60. std::string tmp =
  61. this->ConvertToOptionallyRelativeOutputPath(i->c_str());
  62. if(useShortPath)
  63. {
  64. cmSystemTools::GetShortPath(tmp.c_str(), tmp);
  65. }
  66. this->IncludeOptions += " /I ";
  67. // quote if not already quoted
  68. if (tmp[0] != '"')
  69. {
  70. this->IncludeOptions += "\"";
  71. this->IncludeOptions += tmp;
  72. this->IncludeOptions += "\"";
  73. }
  74. else
  75. {
  76. this->IncludeOptions += tmp;
  77. }
  78. }
  79. if(j == 0 && this->IncludeOptions.size() > maxIncludeLength)
  80. {
  81. this->IncludeOptions = "";
  82. useShortPath = true;
  83. }
  84. else
  85. {
  86. break;
  87. }
  88. }
  89. // Create the DSP or set of DSP's for libraries and executables
  90. // clear project names
  91. this->CreatedProjectNames.clear();
  92. // Call TraceVSDependencies on all targets
  93. cmTargets &tgts = this->Makefile->GetTargets();
  94. for(cmTargets::iterator l = tgts.begin();
  95. l != tgts.end(); l++)
  96. {
  97. // Add a rule to regenerate the build system when the target
  98. // specification source changes.
  99. const char* suppRegenRule =
  100. this->Makefile->GetDefinition("CMAKE_SUPPRESS_REGENERATION");
  101. if (!cmSystemTools::IsOn(suppRegenRule))
  102. {
  103. this->AddDSPBuildRule(l->second);
  104. }
  105. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  106. // so don't build a projectfile for it
  107. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  108. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  109. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  110. {
  111. cmTarget& target = l->second;
  112. target.TraceVSDependencies(target.GetName(), this->Makefile);
  113. }
  114. }
  115. // build any targets
  116. for(cmTargets::iterator l = tgts.begin();
  117. l != tgts.end(); l++)
  118. {
  119. switch(l->second.GetType())
  120. {
  121. case cmTarget::STATIC_LIBRARY:
  122. this->SetBuildType(STATIC_LIBRARY, l->first.c_str(), l->second);
  123. break;
  124. case cmTarget::SHARED_LIBRARY:
  125. case cmTarget::MODULE_LIBRARY:
  126. this->SetBuildType(DLL, l->first.c_str(), l->second);
  127. break;
  128. case cmTarget::EXECUTABLE:
  129. this->SetBuildType(EXECUTABLE,l->first.c_str(), l->second);
  130. break;
  131. case cmTarget::UTILITY:
  132. case cmTarget::GLOBAL_TARGET:
  133. this->SetBuildType(UTILITY, l->first.c_str(), l->second);
  134. break;
  135. case cmTarget::INSTALL_FILES:
  136. break;
  137. case cmTarget::INSTALL_PROGRAMS:
  138. break;
  139. default:
  140. cmSystemTools::Error("Bad target type", l->first.c_str());
  141. break;
  142. }
  143. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  144. // so don't build a projectfile for it
  145. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  146. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  147. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  148. {
  149. // check to see if the dsp is going into a sub-directory
  150. std::string::size_type pos = l->first.rfind('/');
  151. if(pos != std::string::npos)
  152. {
  153. std::string dir = this->Makefile->GetStartOutputDirectory();
  154. dir += "/";
  155. dir += l->first.substr(0, pos);
  156. if(!cmSystemTools::MakeDirectory(dir.c_str()))
  157. {
  158. cmSystemTools::Error("Error creating directory ", dir.c_str());
  159. }
  160. }
  161. this->CreateSingleDSP(l->first.c_str(),l->second);
  162. }
  163. }
  164. }
  165. void cmLocalVisualStudio6Generator::CreateSingleDSP(const char *lname,
  166. cmTarget &target)
  167. {
  168. // add to the list of projects
  169. std::string pname = lname;
  170. this->CreatedProjectNames.push_back(pname);
  171. // create the dsp.cmake file
  172. std::string fname;
  173. fname = this->Makefile->GetStartOutputDirectory();
  174. fname += "/";
  175. fname += lname;
  176. fname += ".dsp";
  177. // save the name of the real dsp file
  178. std::string realDSP = fname;
  179. fname += ".cmake";
  180. std::ofstream fout(fname.c_str());
  181. if(!fout)
  182. {
  183. cmSystemTools::Error("Error Writing ", fname.c_str());
  184. cmSystemTools::ReportLastSystemError("");
  185. }
  186. this->WriteDSPFile(fout,lname,target);
  187. fout.close();
  188. // if the dsp file has changed, then write it.
  189. cmSystemTools::CopyFileIfDifferent(fname.c_str(), realDSP.c_str());
  190. }
  191. void cmLocalVisualStudio6Generator::AddDSPBuildRule(cmTarget& tgt)
  192. {
  193. std::string dspname = tgt.GetName();
  194. dspname += ".dsp.cmake";
  195. const char* dsprule =
  196. this->Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  197. cmCustomCommandLine commandLine;
  198. commandLine.push_back(dsprule);
  199. std::string makefileIn = this->Makefile->GetStartDirectory();
  200. makefileIn += "/";
  201. makefileIn += "CMakeLists.txt";
  202. std::string comment = "Building Custom Rule ";
  203. comment += makefileIn;
  204. std::string args;
  205. args = "-H";
  206. args += this->Convert(this->Makefile->GetHomeDirectory(),
  207. START_OUTPUT, UNCHANGED, true);
  208. commandLine.push_back(args);
  209. args = "-B";
  210. args +=
  211. this->Convert(this->Makefile->GetHomeOutputDirectory(),
  212. START_OUTPUT, UNCHANGED, true);
  213. commandLine.push_back(args);
  214. std::string configFile =
  215. this->Makefile->GetRequiredDefinition("CMAKE_ROOT");
  216. configFile += "/Templates/CMakeWindowsSystemConfig.cmake";
  217. std::vector<std::string> listFiles = this->Makefile->GetListFiles();
  218. bool found = false;
  219. for(std::vector<std::string>::iterator i = listFiles.begin();
  220. i != listFiles.end(); ++i)
  221. {
  222. if(*i == configFile)
  223. {
  224. found = true;
  225. }
  226. }
  227. if(!found)
  228. {
  229. listFiles.push_back(configFile);
  230. }
  231. cmCustomCommandLines commandLines;
  232. commandLines.push_back(commandLine);
  233. const char* no_working_directory = 0;
  234. this->Makefile->AddCustomCommandToOutput(dspname.c_str(), listFiles,
  235. makefileIn.c_str(), commandLines,
  236. comment.c_str(),
  237. no_working_directory, true);
  238. if(cmSourceFile* file = this->Makefile->GetSource(makefileIn.c_str()))
  239. {
  240. tgt.GetSourceFiles().push_back(file);
  241. }
  242. else
  243. {
  244. cmSystemTools::Error("Error adding rule for ", makefileIn.c_str());
  245. }
  246. }
  247. void cmLocalVisualStudio6Generator::WriteDSPFile(std::ostream& fout,
  248. const char *libName,
  249. cmTarget &target)
  250. {
  251. // For utility targets need custom command since pre- and post-
  252. // build does not do anything in Visual Studio 6. In order for the
  253. // rules to run in the correct order as custom commands, we need
  254. // special care for dependencies. The first rule must depend on all
  255. // the dependencies of all the rules. The later rules must each
  256. // depend only on the previous rule.
  257. if ((target.GetType() == cmTarget::UTILITY ||
  258. target.GetType() == cmTarget::GLOBAL_TARGET) &&
  259. (!target.GetPreBuildCommands().empty() ||
  260. !target.GetPostBuildCommands().empty()))
  261. {
  262. // Accumulate the dependencies of all the commands.
  263. std::vector<std::string> depends;
  264. for (std::vector<cmCustomCommand>::const_iterator cr =
  265. target.GetPreBuildCommands().begin();
  266. cr != target.GetPreBuildCommands().end(); ++cr)
  267. {
  268. depends.insert(depends.end(),
  269. cr->GetDepends().begin(), cr->GetDepends().end());
  270. }
  271. for (std::vector<cmCustomCommand>::const_iterator cr =
  272. target.GetPostBuildCommands().begin();
  273. cr != target.GetPostBuildCommands().end(); ++cr)
  274. {
  275. depends.insert(depends.end(),
  276. cr->GetDepends().begin(), cr->GetDepends().end());
  277. }
  278. // Add the pre- and post-build commands in order.
  279. int count = 1;
  280. for (std::vector<cmCustomCommand>::const_iterator cr =
  281. target.GetPreBuildCommands().begin();
  282. cr != target.GetPreBuildCommands().end(); ++cr)
  283. {
  284. this->AddUtilityCommandHack(target, count++, depends, *cr);
  285. }
  286. for (std::vector<cmCustomCommand>::const_iterator cr =
  287. target.GetPostBuildCommands().begin();
  288. cr != target.GetPostBuildCommands().end(); ++cr)
  289. {
  290. this->AddUtilityCommandHack(target, count++, depends, *cr);
  291. }
  292. }
  293. // trace the visual studio dependencies
  294. std::string name = libName;
  295. name += ".dsp.cmake";
  296. // We may be modifying the source groups temporarily, so make a copy.
  297. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  298. // get the classes from the source lists then add them to the groups
  299. std::vector<cmSourceFile*> & classes = target.GetSourceFiles();
  300. // now all of the source files have been properly assigned to the target
  301. // now stick them into source groups using the reg expressions
  302. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  303. i != classes.end(); i++)
  304. {
  305. // Add the file to the list of sources.
  306. std::string source = (*i)->GetFullPath();
  307. cmSourceGroup& sourceGroup =
  308. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  309. sourceGroup.AssignSource(*i);
  310. // while we are at it, if it is a .rule file then for visual studio 6 we
  311. // must generate it
  312. if ((*i)->GetSourceExtension() == "rule")
  313. {
  314. if(!cmSystemTools::FileExists(source.c_str()))
  315. {
  316. cmSystemTools::ReplaceString(source, "$(IntDir)/", "");
  317. #if defined(_WIN32) || defined(__CYGWIN__)
  318. std::ofstream fout(source.c_str(),
  319. std::ios::binary | std::ios::out
  320. | std::ios::trunc);
  321. #else
  322. std::ofstream fout(source.c_str(),
  323. std::ios::out | std::ios::trunc);
  324. #endif
  325. if(fout)
  326. {
  327. fout.write("# generated from CMake",22);
  328. fout.flush();
  329. fout.close();
  330. }
  331. }
  332. }
  333. }
  334. // Compute which sources need unique object computation.
  335. this->ComputeObjectNameRequirements(sourceGroups);
  336. // Write the DSP file's header.
  337. this->WriteDSPHeader(fout, libName, target, sourceGroups);
  338. // Loop through every source group.
  339. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  340. sg != sourceGroups.end(); ++sg)
  341. {
  342. this->WriteGroup(&(*sg), target, fout, libName);
  343. }
  344. // Write the DSP file's footer.
  345. this->WriteDSPFooter(fout);
  346. }
  347. void cmLocalVisualStudio6Generator
  348. ::WriteGroup(const cmSourceGroup *sg, cmTarget target,
  349. std::ostream &fout, const char *libName)
  350. {
  351. const std::vector<const cmSourceFile *> &sourceFiles =
  352. sg->GetSourceFiles();
  353. // If the group is empty, don't write it at all.
  354. if(sourceFiles.empty())
  355. {
  356. return;
  357. }
  358. // If the group has a name, write the header.
  359. std::string name = sg->GetName();
  360. if(name != "")
  361. {
  362. this->WriteDSPBeginGroup(fout, name.c_str(), "");
  363. }
  364. // Compute the maximum length of a configuration name.
  365. std::string::size_type config_len_max = 0;
  366. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  367. i != this->Configurations.end(); ++i)
  368. {
  369. // Strip the subdirectory name out of the configuration name.
  370. std::string config = *i;
  371. std::string::size_type pos = config.find_last_of(" ");
  372. config = config.substr(pos+1, std::string::npos);
  373. config = config.substr(0, config.size()-1);
  374. if(config.size() > config_len_max)
  375. {
  376. config_len_max = config.size();
  377. }
  378. }
  379. // Compute the maximum length of the full path to the intermediate
  380. // files directory for any configuration. This is used to construct
  381. // object file names that do not produce paths that are too long.
  382. std::string::size_type dir_len = 0;
  383. dir_len += strlen(this->Makefile->GetCurrentOutputDirectory());
  384. dir_len += 1;
  385. dir_len += config_len_max;
  386. dir_len += 1;
  387. // Loop through each source in the source group.
  388. for(std::vector<const cmSourceFile *>::const_iterator sf =
  389. sourceFiles.begin(); sf != sourceFiles.end(); ++sf)
  390. {
  391. std::string source = (*sf)->GetFullPath();
  392. const cmCustomCommand *command =
  393. (*sf)->GetCustomCommand();
  394. std::string compileFlags;
  395. std::vector<std::string> depends;
  396. std::string objectNameDir;
  397. if(this->NeedObjectName.find(*sf) != this->NeedObjectName.end())
  398. {
  399. objectNameDir =
  400. cmSystemTools::GetFilenamePath(
  401. this->GetObjectFileNameWithoutTarget(*(*sf), dir_len));
  402. }
  403. // Add per-source file flags.
  404. if(const char* cflags = (*sf)->GetProperty("COMPILE_FLAGS"))
  405. {
  406. compileFlags += cflags;
  407. }
  408. const char* lang = this->GetSourceFileLanguage(*(*sf));
  409. if(lang)
  410. {
  411. if(strcmp(lang, "CXX") == 0)
  412. {
  413. // force a C++ file type
  414. compileFlags += " /TP ";
  415. }
  416. else if(strcmp(lang, "C") == 0)
  417. {
  418. // force to c file type
  419. compileFlags += " /TC ";
  420. }
  421. }
  422. // Check for extra object-file dependencies.
  423. const char* dependsValue = (*sf)->GetProperty("OBJECT_DEPENDS");
  424. if(dependsValue)
  425. {
  426. cmSystemTools::ExpandListArgument(dependsValue, depends);
  427. }
  428. if (source != libName || target.GetType() == cmTarget::UTILITY ||
  429. target.GetType() == cmTarget::GLOBAL_TARGET)
  430. {
  431. fout << "# Begin Source File\n\n";
  432. // Tell MS-Dev what the source is. If the compiler knows how to
  433. // build it, then it will.
  434. fout << "SOURCE=" <<
  435. this->ConvertToOptionallyRelativeOutputPath(source.c_str()) << "\n\n";
  436. if(!depends.empty())
  437. {
  438. // Write out the dependencies for the rule.
  439. fout << "USERDEP__HACK=";
  440. for(std::vector<std::string>::const_iterator d = depends.begin();
  441. d != depends.end(); ++d)
  442. {
  443. fout << "\\\n\t" <<
  444. this->ConvertToOptionallyRelativeOutputPath(d->c_str());
  445. }
  446. fout << "\n";
  447. }
  448. if (command)
  449. {
  450. std::string script =
  451. this->ConstructScript(command->GetCommandLines(),
  452. command->GetWorkingDirectory(),
  453. command->GetEscapeOldStyle(),
  454. command->GetEscapeAllowMakeVars(),
  455. "\\\n\t");
  456. std::string comment =
  457. this->ConstructComment(*command,
  458. "Building Custom Rule $(InputPath)");
  459. if(comment == "<hack>")
  460. {
  461. comment = "";
  462. }
  463. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  464. this->WriteCustomRule(fout, source.c_str(), script.c_str(),
  465. comment.c_str(), command->GetDepends(),
  466. command->GetOutputs(), flags);
  467. }
  468. else if(!compileFlags.empty() || !objectNameDir.empty())
  469. {
  470. for(std::vector<std::string>::iterator i
  471. = this->Configurations.begin();
  472. i != this->Configurations.end(); ++i)
  473. {
  474. if (i == this->Configurations.begin())
  475. {
  476. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  477. }
  478. else
  479. {
  480. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  481. }
  482. if(!compileFlags.empty())
  483. {
  484. fout << "\n# ADD CPP " << compileFlags << "\n\n";
  485. }
  486. if(!objectNameDir.empty())
  487. {
  488. // Strip the subdirectory name out of the configuration name.
  489. std::string config = *i;
  490. std::string::size_type pos = config.find_last_of(" ");
  491. config = config.substr(pos+1, std::string::npos);
  492. config = config.substr(0, config.size()-1);
  493. // Setup an alternate object file directory.
  494. fout << "\n# PROP Intermediate_Dir \""
  495. << config << "/" << objectNameDir << "\"\n\n";
  496. }
  497. }
  498. fout << "!ENDIF\n\n";
  499. }
  500. fout << "# End Source File\n";
  501. }
  502. }
  503. std::vector<cmSourceGroup> children = sg->GetGroupChildren();
  504. for(unsigned int i=0;i<children.size();++i)
  505. {
  506. this->WriteGroup(&children[i], target, fout, libName);
  507. }
  508. // If the group has a name, write the footer.
  509. if(name != "")
  510. {
  511. this->WriteDSPEndGroup(fout);
  512. }
  513. }
  514. void
  515. cmLocalVisualStudio6Generator
  516. ::AddUtilityCommandHack(cmTarget& target, int count,
  517. std::vector<std::string>& depends,
  518. const cmCustomCommand& origCommand)
  519. {
  520. // Create a fake output that forces the rule to run.
  521. char* output = new char[(strlen(this->Makefile->GetStartOutputDirectory()) +
  522. strlen(target.GetName()) + 30)];
  523. sprintf(output,"%s/%s_force_%i", this->Makefile->GetStartOutputDirectory(),
  524. target.GetName(), count);
  525. std::string comment = this->ConstructComment(origCommand, "<hack>");
  526. // Add the rule with the given dependencies and commands.
  527. const char* no_main_dependency = 0;
  528. this->Makefile->AddCustomCommandToOutput(output,
  529. depends,
  530. no_main_dependency,
  531. origCommand.GetCommandLines(),
  532. comment.c_str(),
  533. origCommand.GetWorkingDirectory());
  534. // Replace the dependencies with the output of this rule so that the
  535. // next rule added will run after this one.
  536. depends.clear();
  537. depends.push_back(output);
  538. // Add a source file representing this output to the project.
  539. cmSourceFile* outsf = this->Makefile->GetSourceFileWithOutput(output);
  540. target.GetSourceFiles().push_back(outsf);
  541. // Free the fake output name.
  542. delete [] output;
  543. }
  544. void
  545. cmLocalVisualStudio6Generator
  546. ::WriteCustomRule(std::ostream& fout,
  547. const char* source,
  548. const char* command,
  549. const char* comment,
  550. const std::vector<std::string>& depends,
  551. const std::vector<std::string>& outputs,
  552. const char* flags)
  553. {
  554. // Write the rule for each configuration.
  555. std::vector<std::string>::iterator i;
  556. for(i = this->Configurations.begin(); i != this->Configurations.end(); ++i)
  557. {
  558. if (i == this->Configurations.begin())
  559. {
  560. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  561. }
  562. else
  563. {
  564. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  565. }
  566. if(flags)
  567. {
  568. fout << "\n# ADD CPP " << flags << "\n\n";
  569. }
  570. // Write out the dependencies for the rule.
  571. fout << "USERDEP__HACK=";
  572. for(std::vector<std::string>::const_iterator d = depends.begin();
  573. d != depends.end(); ++d)
  574. {
  575. // Lookup the real name of the dependency in case it is a CMake target.
  576. std::string dep = this->GetRealDependency(d->c_str(), i->c_str());
  577. fout << "\\\n\t" <<
  578. this->ConvertToOptionallyRelativeOutputPath(dep.c_str());
  579. }
  580. fout << "\n";
  581. fout << "# PROP Ignore_Default_Tool 1\n";
  582. fout << "# Begin Custom Build -";
  583. if(comment && *comment)
  584. {
  585. fout << " " << comment;
  586. }
  587. fout << "\n\n";
  588. if(outputs.empty())
  589. {
  590. fout << source
  591. << "_force : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  592. fout << command << "\n\n";
  593. }
  594. else
  595. {
  596. for(std::vector<std::string>::const_iterator o = outputs.begin();
  597. o != outputs.end(); ++o)
  598. {
  599. // Write a rule for every output generated by this command.
  600. fout << this->ConvertToOptionallyRelativeOutputPath(o->c_str())
  601. << " : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  602. fout << command << "\n\n";
  603. }
  604. }
  605. fout << "# End Custom Build\n\n";
  606. }
  607. fout << "!ENDIF\n\n";
  608. }
  609. void cmLocalVisualStudio6Generator::WriteDSPBeginGroup(std::ostream& fout,
  610. const char* group,
  611. const char* filter)
  612. {
  613. fout << "# Begin Group \"" << group << "\"\n"
  614. "# PROP Default_Filter \"" << filter << "\"\n";
  615. }
  616. void cmLocalVisualStudio6Generator::WriteDSPEndGroup(std::ostream& fout)
  617. {
  618. fout << "# End Group\n";
  619. }
  620. void cmLocalVisualStudio6Generator::SetBuildType(BuildType b,
  621. const char* libName,
  622. cmTarget& target)
  623. {
  624. std::string root= this->Makefile->GetRequiredDefinition("CMAKE_ROOT");
  625. const char *def=
  626. this->Makefile->GetDefinition( "MSPROJECT_TEMPLATE_DIRECTORY");
  627. if( def)
  628. {
  629. root = def;
  630. }
  631. else
  632. {
  633. root += "/Templates";
  634. }
  635. switch(b)
  636. {
  637. case STATIC_LIBRARY:
  638. this->DSPHeaderTemplate = root;
  639. this->DSPHeaderTemplate += "/staticLibHeader.dsptemplate";
  640. this->DSPFooterTemplate = root;
  641. this->DSPFooterTemplate += "/staticLibFooter.dsptemplate";
  642. break;
  643. case DLL:
  644. this->DSPHeaderTemplate = root;
  645. this->DSPHeaderTemplate += "/DLLHeader.dsptemplate";
  646. this->DSPFooterTemplate = root;
  647. this->DSPFooterTemplate += "/DLLFooter.dsptemplate";
  648. break;
  649. case EXECUTABLE:
  650. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  651. {
  652. this->DSPHeaderTemplate = root;
  653. this->DSPHeaderTemplate += "/EXEWinHeader.dsptemplate";
  654. this->DSPFooterTemplate = root;
  655. this->DSPFooterTemplate += "/EXEFooter.dsptemplate";
  656. }
  657. else
  658. {
  659. this->DSPHeaderTemplate = root;
  660. this->DSPHeaderTemplate += "/EXEHeader.dsptemplate";
  661. this->DSPFooterTemplate = root;
  662. this->DSPFooterTemplate += "/EXEFooter.dsptemplate";
  663. }
  664. break;
  665. case UTILITY:
  666. this->DSPHeaderTemplate = root;
  667. this->DSPHeaderTemplate += "/UtilityHeader.dsptemplate";
  668. this->DSPFooterTemplate = root;
  669. this->DSPFooterTemplate += "/UtilityFooter.dsptemplate";
  670. break;
  671. }
  672. // once the build type is set, determine what configurations are
  673. // possible
  674. std::ifstream fin(this->DSPHeaderTemplate.c_str());
  675. cmsys::RegularExpression reg("# Name ");
  676. if(!fin)
  677. {
  678. cmSystemTools::Error("Error Reading ", this->DSPHeaderTemplate.c_str());
  679. }
  680. // reset this->Configurations
  681. this->Configurations.erase(this->Configurations.begin(),
  682. this->Configurations.end());
  683. // now add all the configurations possible
  684. std::string line;
  685. while(cmSystemTools::GetLineFromStream(fin, line))
  686. {
  687. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  688. if (reg.find(line))
  689. {
  690. this->Configurations.push_back(line.substr(reg.end()));
  691. }
  692. }
  693. }
  694. // look for custom rules on a target and collect them together
  695. std::string
  696. cmLocalVisualStudio6Generator::CreateTargetRules(cmTarget &target,
  697. const char * /* libName */)
  698. {
  699. std::string customRuleCode = "";
  700. if (target.GetType() >= cmTarget::UTILITY )
  701. {
  702. return customRuleCode;
  703. }
  704. // are there any rules?
  705. if (target.GetPreBuildCommands().size() +
  706. target.GetPreLinkCommands().size() +
  707. target.GetPostBuildCommands().size() == 0)
  708. {
  709. return customRuleCode;
  710. }
  711. customRuleCode = "# Begin Special Build Tool\n";
  712. // Write the pre-build and pre-link together (VS6 does not support
  713. // both). Make sure no continuation character is put on the last
  714. // line.
  715. int prelink_total = (static_cast<int>(target.GetPreBuildCommands().size())+
  716. static_cast<int>(target.GetPreLinkCommands().size()));
  717. int prelink_count = 0;
  718. if(prelink_total > 0)
  719. {
  720. // header stuff
  721. customRuleCode += "PreLink_Cmds=";
  722. }
  723. for (std::vector<cmCustomCommand>::const_iterator cr =
  724. target.GetPreBuildCommands().begin();
  725. cr != target.GetPreBuildCommands().end(); ++cr)
  726. {
  727. if(prelink_count++ > 0)
  728. {
  729. customRuleCode += "\\\n\t";
  730. }
  731. customRuleCode += this->ConstructScript(cr->GetCommandLines(),
  732. cr->GetWorkingDirectory(),
  733. cr->GetEscapeOldStyle(),
  734. cr->GetEscapeAllowMakeVars(),
  735. "\\\n\t");
  736. }
  737. for (std::vector<cmCustomCommand>::const_iterator cr =
  738. target.GetPreLinkCommands().begin();
  739. cr != target.GetPreLinkCommands().end(); ++cr)
  740. {
  741. if(prelink_count++ > 0)
  742. {
  743. customRuleCode += "\\\n\t";
  744. }
  745. customRuleCode += this->ConstructScript(cr->GetCommandLines(),
  746. cr->GetWorkingDirectory(),
  747. cr->GetEscapeOldStyle(),
  748. cr->GetEscapeAllowMakeVars(),
  749. "\\\n\t");
  750. }
  751. if(prelink_total > 0)
  752. {
  753. customRuleCode += "\n";
  754. }
  755. // Write the post-build rules. Make sure no continuation character
  756. // is put on the last line.
  757. int postbuild_total =
  758. static_cast<int>(target.GetPostBuildCommands().size());
  759. int postbuild_count = 0;
  760. if(postbuild_total > 0)
  761. {
  762. customRuleCode += "PostBuild_Cmds=";
  763. }
  764. for (std::vector<cmCustomCommand>::const_iterator cr =
  765. target.GetPostBuildCommands().begin();
  766. cr != target.GetPostBuildCommands().end(); ++cr)
  767. {
  768. if(postbuild_count++ > 0)
  769. {
  770. customRuleCode += "\\\n\t";
  771. }
  772. customRuleCode += this->ConstructScript(cr->GetCommandLines(),
  773. cr->GetWorkingDirectory(),
  774. cr->GetEscapeOldStyle(),
  775. cr->GetEscapeAllowMakeVars(),
  776. "\\\n\t");
  777. }
  778. if(postbuild_total > 0)
  779. {
  780. customRuleCode += "\n";
  781. }
  782. customRuleCode += "# End Special Build Tool\n";
  783. return customRuleCode;
  784. }
  785. inline std::string removeQuotes(const std::string& s)
  786. {
  787. if(s[0] == '\"' && s[s.size()-1] == '\"')
  788. {
  789. return s.substr(1, s.size()-2);
  790. }
  791. return s;
  792. }
  793. // Code in blocks surrounded by a test for this definition is needed
  794. // only for compatibility with user project's replacement DSP
  795. // templates. The CMake templates no longer use them.
  796. #define CM_USE_OLD_VS6
  797. void cmLocalVisualStudio6Generator
  798. ::WriteDSPHeader(std::ostream& fout,
  799. const char *libName, cmTarget &target,
  800. std::vector<cmSourceGroup> &)
  801. {
  802. // Lookup the output directory for the target.
  803. std::string outPath = target.GetDirectory();
  804. #ifdef CM_USE_OLD_VS6
  805. // Lookup the library and executable output directories.
  806. std::string libPath;
  807. if(this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  808. {
  809. libPath = this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  810. }
  811. std::string exePath;
  812. if(this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  813. {
  814. exePath = this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  815. }
  816. // Make sure there are trailing slashes.
  817. if(!libPath.empty())
  818. {
  819. if(libPath[libPath.size()-1] != '/')
  820. {
  821. libPath += "/";
  822. }
  823. }
  824. if(!exePath.empty())
  825. {
  826. if(exePath[exePath.size()-1] != '/')
  827. {
  828. exePath += "/";
  829. }
  830. }
  831. std::set<std::string> pathEmitted;
  832. // determine the link directories
  833. std::string libOptions;
  834. std::string libDebugOptions;
  835. std::string libOptimizedOptions;
  836. std::string libMultiLineOptions;
  837. std::string libMultiLineOptionsForDebug;
  838. std::string libMultiLineDebugOptions;
  839. std::string libMultiLineOptimizedOptions;
  840. if(libPath.size())
  841. {
  842. std::string lpath =
  843. this->ConvertToOptionallyRelativeOutputPath(libPath.c_str());
  844. if(lpath.size() == 0)
  845. {
  846. lpath = ".";
  847. }
  848. std::string lpathIntDir = libPath + "$(INTDIR)";
  849. lpathIntDir =
  850. this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  851. if(pathEmitted.insert(lpath).second)
  852. {
  853. libOptions += " /LIBPATH:";
  854. libOptions += lpathIntDir;
  855. libOptions += " ";
  856. libOptions += " /LIBPATH:";
  857. libOptions += lpath;
  858. libOptions += " ";
  859. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  860. libMultiLineOptions += lpathIntDir;
  861. libMultiLineOptions += " ";
  862. libMultiLineOptions += " /LIBPATH:";
  863. libMultiLineOptions += lpath;
  864. libMultiLineOptions += " \n";
  865. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  866. libMultiLineOptionsForDebug += lpathIntDir;
  867. libMultiLineOptionsForDebug += " ";
  868. libMultiLineOptionsForDebug += " /LIBPATH:";
  869. libMultiLineOptionsForDebug += lpath;
  870. libMultiLineOptionsForDebug += " \n";
  871. }
  872. }
  873. if(exePath.size())
  874. {
  875. std::string lpath =
  876. this->ConvertToOptionallyRelativeOutputPath(exePath.c_str());
  877. if(lpath.size() == 0)
  878. {
  879. lpath = ".";
  880. }
  881. std::string lpathIntDir = exePath + "$(INTDIR)";
  882. lpathIntDir =
  883. this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  884. if(pathEmitted.insert(lpath).second)
  885. {
  886. libOptions += " /LIBPATH:";
  887. libOptions += lpathIntDir;
  888. libOptions += " ";
  889. libOptions += " /LIBPATH:";
  890. libOptions += lpath;
  891. libOptions += " ";
  892. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  893. libMultiLineOptions += lpathIntDir;
  894. libMultiLineOptions += " ";
  895. libMultiLineOptions += " /LIBPATH:";
  896. libMultiLineOptions += lpath;
  897. libMultiLineOptions += " \n";
  898. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  899. libMultiLineOptionsForDebug += lpathIntDir;
  900. libMultiLineOptionsForDebug += " ";
  901. libMultiLineOptionsForDebug += " /LIBPATH:";
  902. libMultiLineOptionsForDebug += lpath;
  903. libMultiLineOptionsForDebug += " \n";
  904. }
  905. }
  906. std::vector<std::string>::const_iterator i;
  907. const std::vector<std::string>& libdirs = target.GetLinkDirectories();
  908. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  909. {
  910. std::string path = *i;
  911. if(path[path.size()-1] != '/')
  912. {
  913. path += "/";
  914. }
  915. std::string lpath =
  916. this->ConvertToOptionallyRelativeOutputPath(path.c_str());
  917. if(lpath.size() == 0)
  918. {
  919. lpath = ".";
  920. }
  921. std::string lpathIntDir = path + "$(INTDIR)";
  922. lpathIntDir =
  923. this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  924. if(pathEmitted.insert(lpath).second)
  925. {
  926. libOptions += " /LIBPATH:";
  927. libOptions += lpathIntDir;
  928. libOptions += " ";
  929. libOptions += " /LIBPATH:";
  930. libOptions += lpath;
  931. libOptions += " ";
  932. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  933. libMultiLineOptions += lpathIntDir;
  934. libMultiLineOptions += " ";
  935. libMultiLineOptions += " /LIBPATH:";
  936. libMultiLineOptions += lpath;
  937. libMultiLineOptions += " \n";
  938. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  939. libMultiLineOptionsForDebug += lpathIntDir;
  940. libMultiLineOptionsForDebug += " ";
  941. libMultiLineOptionsForDebug += " /LIBPATH:";
  942. libMultiLineOptionsForDebug += lpath;
  943. libMultiLineOptionsForDebug += " \n";
  944. }
  945. }
  946. // find link libraries
  947. const cmTarget::LinkLibraryVectorType& libs = target.GetLinkLibraries();
  948. cmTarget::LinkLibraryVectorType::const_iterator j;
  949. for(j = libs.begin(); j != libs.end(); ++j)
  950. {
  951. // add libraries to executables and dlls (but never include
  952. // a library in a library, bad recursion)
  953. // NEVER LINK STATIC LIBRARIES TO OTHER STATIC LIBRARIES
  954. if ((target.GetType() != cmTarget::SHARED_LIBRARY
  955. && target.GetType() != cmTarget::STATIC_LIBRARY
  956. && target.GetType() != cmTarget::MODULE_LIBRARY) ||
  957. (target.GetType()==cmTarget::SHARED_LIBRARY && libName != j->first) ||
  958. (target.GetType()==cmTarget::MODULE_LIBRARY && libName != j->first))
  959. {
  960. // Compute the proper name to use to link this library.
  961. std::string lib;
  962. std::string libDebug;
  963. cmTarget* tgt = this->GlobalGenerator->FindTarget(0, j->first.c_str());
  964. if(tgt)
  965. {
  966. lib = cmSystemTools::GetFilenameWithoutExtension
  967. (tgt->GetFullName().c_str());
  968. libDebug = cmSystemTools::GetFilenameWithoutExtension
  969. (tgt->GetFullName("Debug").c_str());
  970. lib += ".lib";
  971. libDebug += ".lib";
  972. }
  973. else
  974. {
  975. lib = j->first.c_str();
  976. libDebug = j->first.c_str();
  977. if(j->first.find(".lib") == std::string::npos)
  978. {
  979. lib += ".lib";
  980. libDebug += ".lib";
  981. }
  982. }
  983. lib = this->ConvertToOptionallyRelativeOutputPath(lib.c_str());
  984. libDebug =
  985. this->ConvertToOptionallyRelativeOutputPath(libDebug.c_str());
  986. if (j->second == cmTarget::GENERAL)
  987. {
  988. libOptions += " ";
  989. libOptions += lib;
  990. libMultiLineOptions += "# ADD LINK32 ";
  991. libMultiLineOptions += lib;
  992. libMultiLineOptions += "\n";
  993. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  994. libMultiLineOptionsForDebug += libDebug;
  995. libMultiLineOptionsForDebug += "\n";
  996. }
  997. if (j->second == cmTarget::DEBUG)
  998. {
  999. libDebugOptions += " ";
  1000. libDebugOptions += lib;
  1001. libMultiLineDebugOptions += "# ADD LINK32 ";
  1002. libMultiLineDebugOptions += libDebug;
  1003. libMultiLineDebugOptions += "\n";
  1004. }
  1005. if (j->second == cmTarget::OPTIMIZED)
  1006. {
  1007. libOptimizedOptions += " ";
  1008. libOptimizedOptions += lib;
  1009. libMultiLineOptimizedOptions += "# ADD LINK32 ";
  1010. libMultiLineOptimizedOptions += lib;
  1011. libMultiLineOptimizedOptions += "\n";
  1012. }
  1013. }
  1014. }
  1015. #endif
  1016. // Get extra linker options for this target type.
  1017. std::string extraLinkOptions;
  1018. if(target.GetType() == cmTarget::EXECUTABLE)
  1019. {
  1020. extraLinkOptions =
  1021. this->Makefile->GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS");
  1022. }
  1023. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  1024. {
  1025. extraLinkOptions =
  1026. this->Makefile->GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS");
  1027. }
  1028. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  1029. {
  1030. extraLinkOptions =
  1031. this->Makefile->GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS");
  1032. }
  1033. // Get extra linker options for this target.
  1034. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS"))
  1035. {
  1036. extraLinkOptions += " ";
  1037. extraLinkOptions += targetLinkFlags;
  1038. }
  1039. // Get standard libraries for this language.
  1040. if(target.GetType() >= cmTarget::EXECUTABLE &&
  1041. target.GetType() <= cmTarget::MODULE_LIBRARY)
  1042. {
  1043. // Get the language to use for linking.
  1044. const char* linkLanguage =
  1045. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1046. if(!linkLanguage)
  1047. {
  1048. cmSystemTools::Error
  1049. ("CMake can not determine linker language for target:",
  1050. target.GetName());
  1051. return;
  1052. }
  1053. // Compute the variable name to lookup standard libraries for this
  1054. // language.
  1055. std::string standardLibsVar = "CMAKE_";
  1056. standardLibsVar += linkLanguage;
  1057. standardLibsVar += "_STANDARD_LIBRARIES";
  1058. // Add standard libraries.
  1059. if(const char* stdLibs =
  1060. this->Makefile->GetDefinition(standardLibsVar.c_str()))
  1061. {
  1062. extraLinkOptions += " ";
  1063. extraLinkOptions += stdLibs;
  1064. }
  1065. }
  1066. // Compute version number information.
  1067. std::string targetVersionFlag;
  1068. if(target.GetType() == cmTarget::EXECUTABLE ||
  1069. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1070. target.GetType() == cmTarget::MODULE_LIBRARY)
  1071. {
  1072. int major;
  1073. int minor;
  1074. target.GetTargetVersion(major, minor);
  1075. cmOStringStream targetVersionStream;
  1076. targetVersionStream << "/version:" << major << "." << minor;
  1077. targetVersionFlag = targetVersionStream.str();
  1078. }
  1079. // Compute the real name of the target.
  1080. std::string outputName =
  1081. "(OUTPUT_NAME is for libraries and executables only)";
  1082. std::string outputNameDebug = outputName;
  1083. std::string outputNameRelease = outputName;
  1084. std::string outputNameMinSizeRel = outputName;
  1085. std::string outputNameRelWithDebInfo = outputName;
  1086. if(target.GetType() == cmTarget::EXECUTABLE ||
  1087. target.GetType() == cmTarget::STATIC_LIBRARY ||
  1088. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1089. target.GetType() == cmTarget::MODULE_LIBRARY)
  1090. {
  1091. outputName = target.GetFullName();
  1092. outputNameDebug = target.GetFullName("Debug");
  1093. outputNameRelease = target.GetFullName("Release");
  1094. outputNameMinSizeRel = target.GetFullName("MinSizeRel");
  1095. outputNameRelWithDebInfo = target.GetFullName("RelWithDebInfo");
  1096. }
  1097. // Compute the proper link information for the target.
  1098. std::string optionsDebug;
  1099. std::string optionsRelease;
  1100. std::string optionsMinSizeRel;
  1101. std::string optionsRelWithDebInfo;
  1102. if(target.GetType() == cmTarget::EXECUTABLE ||
  1103. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1104. target.GetType() == cmTarget::MODULE_LIBRARY)
  1105. {
  1106. this->ComputeLinkOptions(target, "Debug", extraLinkOptions,
  1107. optionsDebug);
  1108. this->ComputeLinkOptions(target, "Release", extraLinkOptions,
  1109. optionsRelease);
  1110. this->ComputeLinkOptions(target, "MinSizeRel", extraLinkOptions,
  1111. optionsMinSizeRel);
  1112. this->ComputeLinkOptions(target, "RelWithDebInfo", extraLinkOptions,
  1113. optionsRelWithDebInfo);
  1114. }
  1115. // Compute the path of the import library.
  1116. std::string targetImplibFlagDebug;
  1117. std::string targetImplibFlagRelease;
  1118. std::string targetImplibFlagMinSizeRel;
  1119. std::string targetImplibFlagRelWithDebInfo;
  1120. if(target.GetType() == cmTarget::SHARED_LIBRARY ||
  1121. target.GetType() == cmTarget::MODULE_LIBRARY ||
  1122. target.GetType() == cmTarget::EXECUTABLE)
  1123. {
  1124. std::string fullPathImpDebug = target.GetDirectory("Debug", true);
  1125. std::string fullPathImpRelease = target.GetDirectory("Release", true);
  1126. std::string fullPathImpMinSizeRel =
  1127. target.GetDirectory("MinSizeRel", true);
  1128. std::string fullPathImpRelWithDebInfo =
  1129. target.GetDirectory("RelWithDebInfo", true);
  1130. fullPathImpDebug += "/";
  1131. fullPathImpRelease += "/";
  1132. fullPathImpMinSizeRel += "/";
  1133. fullPathImpRelWithDebInfo += "/";
  1134. fullPathImpDebug += target.GetFullName("Debug", true);
  1135. fullPathImpRelease += target.GetFullName("Release", true);
  1136. fullPathImpMinSizeRel += target.GetFullName("MinSizeRel", true);
  1137. fullPathImpRelWithDebInfo += target.GetFullName("RelWithDebInfo", true);
  1138. targetImplibFlagDebug = "/implib:";
  1139. targetImplibFlagRelease = "/implib:";
  1140. targetImplibFlagMinSizeRel = "/implib:";
  1141. targetImplibFlagRelWithDebInfo = "/implib:";
  1142. targetImplibFlagDebug +=
  1143. this->ConvertToOptionallyRelativeOutputPath(fullPathImpDebug.c_str());
  1144. targetImplibFlagRelease +=
  1145. this->ConvertToOptionallyRelativeOutputPath(fullPathImpRelease.c_str());
  1146. targetImplibFlagMinSizeRel +=
  1147. this->ConvertToOptionallyRelativeOutputPath(
  1148. fullPathImpMinSizeRel.c_str());
  1149. targetImplibFlagRelWithDebInfo +=
  1150. this->ConvertToOptionallyRelativeOutputPath(
  1151. fullPathImpRelWithDebInfo.c_str());
  1152. }
  1153. #ifdef CM_USE_OLD_VS6
  1154. // Compute link information for the target.
  1155. if(extraLinkOptions.size())
  1156. {
  1157. libOptions += " ";
  1158. libOptions += extraLinkOptions;
  1159. libOptions += " ";
  1160. libMultiLineOptions += "# ADD LINK32 ";
  1161. libMultiLineOptions += extraLinkOptions;
  1162. libMultiLineOptions += " \n";
  1163. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  1164. libMultiLineOptionsForDebug += extraLinkOptions;
  1165. libMultiLineOptionsForDebug += " \n";
  1166. }
  1167. #endif
  1168. // are there any custom rules on the target itself
  1169. // only if the target is a lib or exe
  1170. std::string customRuleCode = this->CreateTargetRules(target, libName);
  1171. std::ifstream fin(this->DSPHeaderTemplate.c_str());
  1172. if(!fin)
  1173. {
  1174. cmSystemTools::Error("Error Reading ", this->DSPHeaderTemplate.c_str());
  1175. }
  1176. std::string staticLibOptions;
  1177. if(target.GetType() == cmTarget::STATIC_LIBRARY )
  1178. {
  1179. if(const char* libflags = target.GetProperty("STATIC_LIBRARY_FLAGS"))
  1180. {
  1181. staticLibOptions = libflags;
  1182. }
  1183. }
  1184. // Add the export symbol definition for shared library objects.
  1185. std::string exportSymbol;
  1186. if(const char* exportMacro = target.GetExportMacro())
  1187. {
  1188. exportSymbol = exportMacro;
  1189. }
  1190. std::string line;
  1191. while(cmSystemTools::GetLineFromStream(fin, line))
  1192. {
  1193. const char* mfcFlag = this->Makefile->GetDefinition("CMAKE_MFC_FLAG");
  1194. if(!mfcFlag)
  1195. {
  1196. mfcFlag = "0";
  1197. }
  1198. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME_EXPORTS",
  1199. exportSymbol.c_str());
  1200. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE",
  1201. customRuleCode.c_str());
  1202. cmSystemTools::ReplaceString(line, "CMAKE_MFC_FLAG",
  1203. mfcFlag);
  1204. if(target.GetType() == cmTarget::STATIC_LIBRARY )
  1205. {
  1206. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS",
  1207. staticLibOptions.c_str());
  1208. }
  1209. if(this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  1210. {
  1211. cmSystemTools::ReplaceString(line, "/nologo", "");
  1212. }
  1213. #ifdef CM_USE_OLD_VS6
  1214. cmSystemTools::ReplaceString(line, "CM_LIBRARIES",
  1215. libOptions.c_str());
  1216. cmSystemTools::ReplaceString(line, "CM_DEBUG_LIBRARIES",
  1217. libDebugOptions.c_str());
  1218. cmSystemTools::ReplaceString(line, "CM_OPTIMIZED_LIBRARIES",
  1219. libOptimizedOptions.c_str());
  1220. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES_FOR_DEBUG",
  1221. libMultiLineOptionsForDebug.c_str());
  1222. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES",
  1223. libMultiLineOptions.c_str());
  1224. cmSystemTools::ReplaceString(line, "CM_MULTILINE_DEBUG_LIBRARIES",
  1225. libMultiLineDebugOptions.c_str());
  1226. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIMIZED_LIBRARIES",
  1227. libMultiLineOptimizedOptions.c_str());
  1228. #endif
  1229. // Substitute the real output name into the template.
  1230. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_DEBUG",
  1231. outputNameDebug.c_str());
  1232. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_RELEASE",
  1233. outputNameRelease.c_str());
  1234. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_MINSIZEREL",
  1235. outputNameMinSizeRel.c_str());
  1236. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_RELWITHDEBINFO",
  1237. outputNameRelWithDebInfo.c_str());
  1238. cmSystemTools::ReplaceString(line, "OUTPUT_NAME", outputName.c_str());
  1239. // Substitute the proper link information into the template.
  1240. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_DEBUG",
  1241. optionsDebug.c_str());
  1242. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_RELEASE",
  1243. optionsRelease.c_str());
  1244. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_MINSIZEREL",
  1245. optionsMinSizeRel.c_str());
  1246. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_RELWITHDEBINFO",
  1247. optionsRelWithDebInfo.c_str());
  1248. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES",
  1249. this->IncludeOptions.c_str());
  1250. cmSystemTools::ReplaceString(line, "TARGET_VERSION_FLAG",
  1251. targetVersionFlag.c_str());
  1252. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_DEBUG",
  1253. targetImplibFlagDebug.c_str());
  1254. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_RELEASE",
  1255. targetImplibFlagRelease.c_str());
  1256. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_MINSIZEREL",
  1257. targetImplibFlagMinSizeRel.c_str());
  1258. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_RELWITHDEBINFO",
  1259. targetImplibFlagRelWithDebInfo.c_str());
  1260. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  1261. #ifdef CM_USE_OLD_VS6
  1262. // because LIBRARY_OUTPUT_PATH and EXECUTABLE_OUTPUT_PATH
  1263. // are already quoted in the template file,
  1264. // we need to remove the quotes here, we still need
  1265. // to convert to output path for unix to win32 conversion
  1266. cmSystemTools::ReplaceString
  1267. (line, "LIBRARY_OUTPUT_PATH",
  1268. removeQuotes(this->ConvertToOptionallyRelativeOutputPath
  1269. (libPath.c_str())).c_str());
  1270. cmSystemTools::ReplaceString
  1271. (line, "EXECUTABLE_OUTPUT_PATH",
  1272. removeQuotes(this->ConvertToOptionallyRelativeOutputPath
  1273. (exePath.c_str())).c_str());
  1274. #endif
  1275. cmSystemTools::ReplaceString
  1276. (line, "OUTPUT_DIRECTORY",
  1277. removeQuotes(this->ConvertToOptionallyRelativeOutputPath
  1278. (outPath.c_str())).c_str());
  1279. cmSystemTools::ReplaceString(line,
  1280. "EXTRA_DEFINES",
  1281. this->Makefile->GetDefineFlags());
  1282. const char* debugPostfix
  1283. = this->Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  1284. cmSystemTools::ReplaceString(line, "DEBUG_POSTFIX",
  1285. debugPostfix?debugPostfix:"");
  1286. // store flags for each configuration
  1287. std::string flags = " ";
  1288. std::string flagsRelease = " ";
  1289. std::string flagsMinSize = " ";
  1290. std::string flagsDebug = " ";
  1291. std::string flagsDebugRel = " ";
  1292. if(target.GetType() >= cmTarget::EXECUTABLE &&
  1293. target.GetType() <= cmTarget::MODULE_LIBRARY)
  1294. {
  1295. const char* linkLanguage =
  1296. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1297. if(!linkLanguage)
  1298. {
  1299. cmSystemTools::Error
  1300. ("CMake can not determine linker language for target:",
  1301. target.GetName());
  1302. return;
  1303. }
  1304. // if CXX is on and the target contains cxx code then add the cxx flags
  1305. std::string baseFlagVar = "CMAKE_";
  1306. baseFlagVar += linkLanguage;
  1307. baseFlagVar += "_FLAGS";
  1308. flags = this->Makefile->GetSafeDefinition(baseFlagVar.c_str());
  1309. std::string flagVar = baseFlagVar + "_RELEASE";
  1310. flagsRelease = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1311. flagsRelease += " -DCMAKE_INTDIR=\\\"Release\\\" ";
  1312. if(const char* targetLinkFlags =
  1313. target.GetProperty("LINK_FLAGS_RELEASE"))
  1314. {
  1315. flagsRelease += targetLinkFlags;
  1316. flagsRelease += " ";
  1317. }
  1318. flagVar = baseFlagVar + "_MINSIZEREL";
  1319. flagsMinSize = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1320. flagsMinSize += " -DCMAKE_INTDIR=\\\"MinSizeRel\\\" ";
  1321. if(const char* targetLinkFlags =
  1322. target.GetProperty("LINK_FLAGS_MINSIZEREL"))
  1323. {
  1324. flagsMinSize += targetLinkFlags;
  1325. flagsMinSize += " ";
  1326. }
  1327. flagVar = baseFlagVar + "_DEBUG";
  1328. flagsDebug = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1329. flagsDebug += " -DCMAKE_INTDIR=\\\"Debug\\\" ";
  1330. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_DEBUG"))
  1331. {
  1332. flagsDebug += targetLinkFlags;
  1333. flagsDebug += " ";
  1334. }
  1335. flagVar = baseFlagVar + "_RELWITHDEBINFO";
  1336. flagsDebugRel = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1337. flagsDebugRel += " -DCMAKE_INTDIR=\\\"RelWithDebInfo\\\" ";
  1338. if(const char* targetLinkFlags =
  1339. target.GetProperty("LINK_FLAGS_RELWITHDEBINFO"))
  1340. {
  1341. flagsDebugRel += targetLinkFlags;
  1342. flagsDebugRel += " ";
  1343. }
  1344. }
  1345. // if unicode is not found, then add -D_MBCS
  1346. std::string defs = this->Makefile->GetDefineFlags();
  1347. if(flags.find("D_UNICODE") == flags.npos &&
  1348. defs.find("D_UNICODE") == flags.npos)
  1349. {
  1350. flags += " /D \"_MBCS\"";
  1351. }
  1352. // Add per-target flags.
  1353. if(const char* targetFlags = target.GetProperty("COMPILE_FLAGS"))
  1354. {
  1355. flags += " ";
  1356. flags += targetFlags;
  1357. }
  1358. // The template files have CXX FLAGS in them, that need to be replaced.
  1359. // There are not separate CXX and C template files, so we use the same
  1360. // variable names. The previous code sets up flags* variables to contain
  1361. // the correct C or CXX flags
  1362. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_MINSIZEREL",
  1363. flagsMinSize.c_str());
  1364. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_DEBUG",
  1365. flagsDebug.c_str());
  1366. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELWITHDEBINFO",
  1367. flagsDebugRel.c_str());
  1368. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELEASE",
  1369. flagsRelease.c_str());
  1370. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS", flags.c_str());
  1371. fout << line.c_str() << std::endl;
  1372. }
  1373. }
  1374. void cmLocalVisualStudio6Generator::WriteDSPFooter(std::ostream& fout)
  1375. {
  1376. std::ifstream fin(this->DSPFooterTemplate.c_str());
  1377. if(!fin)
  1378. {
  1379. cmSystemTools::Error("Error Reading ",
  1380. this->DSPFooterTemplate.c_str());
  1381. }
  1382. std::string line;
  1383. while(cmSystemTools::GetLineFromStream(fin, line))
  1384. {
  1385. fout << line << std::endl;
  1386. }
  1387. }
  1388. //----------------------------------------------------------------------------
  1389. void cmLocalVisualStudio6Generator
  1390. ::ComputeLinkOptions(cmTarget& target,
  1391. const char* configName,
  1392. const std::string extraOptions,
  1393. std::string& options)
  1394. {
  1395. // Compute the link information for this configuration.
  1396. std::vector<cmStdString> linkLibs;
  1397. std::vector<cmStdString> linkDirs;
  1398. this->ComputeLinkInformation(target, configName, linkLibs, linkDirs);
  1399. // Build the link options code.
  1400. for(std::vector<cmStdString>::const_iterator d = linkDirs.begin();
  1401. d != linkDirs.end(); ++d)
  1402. {
  1403. std::string dir = *d;
  1404. if(!dir.empty())
  1405. {
  1406. if(dir[dir.size()-1] != '/')
  1407. {
  1408. dir += "/";
  1409. }
  1410. dir += "$(IntDir)";
  1411. options += "# ADD LINK32 /LIBPATH:";
  1412. options += this->ConvertToOptionallyRelativeOutputPath(dir.c_str());
  1413. options += " /LIBPATH:";
  1414. options += this->ConvertToOptionallyRelativeOutputPath(d->c_str());
  1415. options += "\n";
  1416. }
  1417. }
  1418. for(std::vector<cmStdString>::const_iterator l = linkLibs.begin();
  1419. l != linkLibs.end(); ++l)
  1420. {
  1421. options += "# ADD LINK32 ";
  1422. options += this->ConvertToOptionallyRelativeOutputPath(l->c_str());
  1423. options += "\n";
  1424. }
  1425. // Add extra options if any.
  1426. if(!extraOptions.empty())
  1427. {
  1428. options += "# ADD LINK32 ";
  1429. options += extraOptions;
  1430. options += "\n";
  1431. }
  1432. }
  1433. void cmLocalVisualStudio6Generator
  1434. ::GetTargetObjectFileDirectories(cmTarget* ,
  1435. std::vector<std::string>&
  1436. dirs)
  1437. {
  1438. std::string dir = this->Makefile->GetCurrentOutputDirectory();
  1439. dir += "/";
  1440. dir += this->GetGlobalGenerator()->GetCMakeCFGInitDirectory();
  1441. dirs.push_back(dir);
  1442. }