cmLocalVisualStudio6Generator.cxx 53 KB

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