cmLocalVisualStudio6Generator.cxx 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmGlobalGenerator.h"
  11. #include "cmLocalVisualStudio6Generator.h"
  12. #include "cmMakefile.h"
  13. #include "cmSystemTools.h"
  14. #include "cmSourceFile.h"
  15. #include "cmCacheManager.h"
  16. #include "cmGeneratorTarget.h"
  17. #include "cmake.h"
  18. #include "cmComputeLinkInformation.h"
  19. #include <cmsys/RegularExpression.hxx>
  20. #include <cmsys/FStream.hxx>
  21. cmLocalVisualStudio6Generator::cmLocalVisualStudio6Generator():
  22. cmLocalVisualStudioGenerator(VS6)
  23. {
  24. }
  25. cmLocalVisualStudio6Generator::~cmLocalVisualStudio6Generator()
  26. {
  27. }
  28. //----------------------------------------------------------------------------
  29. // Helper class to write build events.
  30. class cmLocalVisualStudio6Generator::EventWriter
  31. {
  32. public:
  33. EventWriter(cmLocalVisualStudio6Generator* lg,
  34. const char* config, std::string& code):
  35. LG(lg), Config(config), Code(code), First(true) {}
  36. void Start(const char* event)
  37. {
  38. this->First = true;
  39. this->Event = event;
  40. }
  41. void Finish()
  42. {
  43. this->Code += (this->First? "" : "\n");
  44. }
  45. void Write(std::vector<cmCustomCommand> const& ccs)
  46. {
  47. for(std::vector<cmCustomCommand>::const_iterator ci = ccs.begin();
  48. ci != ccs.end(); ++ci)
  49. {
  50. this->Write(*ci);
  51. }
  52. }
  53. void Write(cmCustomCommand const& cc)
  54. {
  55. if(this->First)
  56. {
  57. this->Code += this->Event + "_Cmds=";
  58. this->First = false;
  59. }
  60. else
  61. {
  62. this->Code += "\\\n\t";
  63. }
  64. this->Code += this->LG->ConstructScript(cc, this->Config, "\\\n\t");
  65. }
  66. private:
  67. cmLocalVisualStudio6Generator* LG;
  68. const char* Config;
  69. std::string& Code;
  70. bool First;
  71. std::string Event;
  72. };
  73. void cmLocalVisualStudio6Generator::AddHelperCommands()
  74. {
  75. std::set<cmStdString> lang;
  76. lang.insert("C");
  77. lang.insert("CXX");
  78. this->CreateCustomTargetsAndCommands(lang);
  79. }
  80. void cmLocalVisualStudio6Generator::AddCMakeListsRules()
  81. {
  82. cmTargets &tgts = this->Makefile->GetTargets();
  83. for(cmTargets::iterator l = tgts.begin();
  84. l != tgts.end(); l++)
  85. {
  86. if (l->second.GetType() == cmTarget::INTERFACE_LIBRARY)
  87. {
  88. continue;
  89. }
  90. // Add a rule to regenerate the build system when the target
  91. // specification source changes.
  92. const char* suppRegenRule =
  93. this->Makefile->GetDefinition("CMAKE_SUPPRESS_REGENERATION");
  94. if (!cmSystemTools::IsOn(suppRegenRule))
  95. {
  96. this->AddDSPBuildRule(l->second);
  97. }
  98. }
  99. }
  100. void cmLocalVisualStudio6Generator::Generate()
  101. {
  102. this->OutputDSPFile();
  103. }
  104. void cmLocalVisualStudio6Generator::OutputDSPFile()
  105. {
  106. // If not an in source build, then create the output directory
  107. if(strcmp(this->Makefile->GetStartOutputDirectory(),
  108. this->Makefile->GetHomeDirectory()) != 0)
  109. {
  110. if(!cmSystemTools::MakeDirectory
  111. (this->Makefile->GetStartOutputDirectory()))
  112. {
  113. cmSystemTools::Error("Error creating directory ",
  114. this->Makefile->GetStartOutputDirectory());
  115. }
  116. }
  117. // Create the DSP or set of DSP's for libraries and executables
  118. cmTargets &tgts = this->Makefile->GetTargets();
  119. // build any targets
  120. for(cmTargets::iterator l = tgts.begin();
  121. l != tgts.end(); l++)
  122. {
  123. switch(l->second.GetType())
  124. {
  125. case cmTarget::STATIC_LIBRARY:
  126. case cmTarget::OBJECT_LIBRARY:
  127. this->SetBuildType(STATIC_LIBRARY, l->first.c_str(), l->second);
  128. break;
  129. case cmTarget::SHARED_LIBRARY:
  130. case cmTarget::MODULE_LIBRARY:
  131. this->SetBuildType(DLL, l->first.c_str(), l->second);
  132. break;
  133. case cmTarget::EXECUTABLE:
  134. this->SetBuildType(EXECUTABLE,l->first.c_str(), l->second);
  135. break;
  136. case cmTarget::UTILITY:
  137. case cmTarget::GLOBAL_TARGET:
  138. this->SetBuildType(UTILITY, l->first.c_str(), l->second);
  139. break;
  140. case cmTarget::INTERFACE_LIBRARY:
  141. continue;
  142. default:
  143. cmSystemTools::Error("Bad target type: ", l->first.c_str());
  144. break;
  145. }
  146. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  147. // so don't build a projectfile for it
  148. const char* path =
  149. l->second.GetProperty("EXTERNAL_MSPROJECT");
  150. if(!path)
  151. {
  152. // check to see if the dsp is going into a sub-directory
  153. std::string::size_type pos = l->first.rfind('/');
  154. if(pos != std::string::npos)
  155. {
  156. std::string dir = this->Makefile->GetStartOutputDirectory();
  157. dir += "/";
  158. dir += l->first.substr(0, pos);
  159. if(!cmSystemTools::MakeDirectory(dir.c_str()))
  160. {
  161. cmSystemTools::Error("Error creating directory: ", dir.c_str());
  162. }
  163. }
  164. this->CreateSingleDSP(l->first.c_str(),l->second);
  165. }
  166. }
  167. }
  168. // Utility function to make a valid VS6 *.dsp filename out
  169. // of a CMake target name:
  170. //
  171. extern std::string GetVS6TargetName(const std::string& targetName);
  172. void cmLocalVisualStudio6Generator::CreateSingleDSP(const char *lname,
  173. cmTarget &target)
  174. {
  175. // add to the list of projects
  176. std::string pname = GetVS6TargetName(lname);
  177. // create the dsp.cmake file
  178. std::string fname;
  179. fname = this->Makefile->GetStartOutputDirectory();
  180. fname += "/";
  181. fname += pname;
  182. fname += ".dsp";
  183. // save the name of the real dsp file
  184. std::string realDSP = fname;
  185. fname += ".cmake";
  186. cmsys::ofstream fout(fname.c_str());
  187. if(!fout)
  188. {
  189. cmSystemTools::Error("Error Writing ", fname.c_str());
  190. cmSystemTools::ReportLastSystemError("");
  191. }
  192. this->WriteDSPFile(fout,pname.c_str(),target);
  193. fout.close();
  194. // if the dsp file has changed, then write it.
  195. cmSystemTools::CopyFileIfDifferent(fname.c_str(), realDSP.c_str());
  196. }
  197. void cmLocalVisualStudio6Generator::AddDSPBuildRule(cmTarget& tgt)
  198. {
  199. std::string dspname = GetVS6TargetName(tgt.GetName());
  200. dspname += ".dsp.cmake";
  201. const char* dsprule =
  202. this->Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  203. cmCustomCommandLine commandLine;
  204. commandLine.push_back(dsprule);
  205. std::string makefileIn = this->Makefile->GetStartDirectory();
  206. makefileIn += "/";
  207. makefileIn += "CMakeLists.txt";
  208. if(!cmSystemTools::FileExists(makefileIn.c_str()))
  209. {
  210. return;
  211. }
  212. std::string comment = "Building Custom Rule ";
  213. comment += makefileIn;
  214. std::string args;
  215. args = "-H";
  216. args += this->Convert(this->Makefile->GetHomeDirectory(),
  217. START_OUTPUT, UNCHANGED, true);
  218. commandLine.push_back(args);
  219. args = "-B";
  220. args +=
  221. this->Convert(this->Makefile->GetHomeOutputDirectory(),
  222. START_OUTPUT, UNCHANGED, true);
  223. commandLine.push_back(args);
  224. std::vector<std::string> const& listFiles = this->Makefile->GetListFiles();
  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.AddSourceFile(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. // We may be modifying the source groups temporarily, so make a copy.
  288. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  289. // get the classes from the source lists then add them to the groups
  290. std::vector<cmSourceFile*> const & classes = target.GetSourceFiles();
  291. // now all of the source files have been properly assigned to the target
  292. // now stick them into source groups using the reg expressions
  293. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  294. i != classes.end(); i++)
  295. {
  296. // Add the file to the list of sources.
  297. std::string source = (*i)->GetFullPath();
  298. cmSourceGroup& sourceGroup =
  299. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  300. sourceGroup.AssignSource(*i);
  301. // while we are at it, if it is a .rule file then for visual studio 6 we
  302. // must generate it
  303. if ((*i)->GetPropertyAsBool("__CMAKE_RULE"))
  304. {
  305. if(!cmSystemTools::FileExists(source.c_str()))
  306. {
  307. cmSystemTools::ReplaceString(source, "$(IntDir)/", "");
  308. // Make sure the path exists for the file
  309. std::string path = cmSystemTools::GetFilenamePath(source);
  310. cmSystemTools::MakeDirectory(path.c_str());
  311. #if defined(_WIN32) || defined(__CYGWIN__)
  312. cmsys::ofstream sourceFout(source.c_str(),
  313. std::ios::binary | std::ios::out
  314. | std::ios::trunc);
  315. #else
  316. cmsys::ofstream sourceFout(source.c_str(),
  317. std::ios::out | std::ios::trunc);
  318. #endif
  319. if(sourceFout)
  320. {
  321. sourceFout.write("# generated from CMake",22);
  322. sourceFout.flush();
  323. sourceFout.close();
  324. }
  325. }
  326. }
  327. }
  328. // Write the DSP file's header.
  329. this->WriteDSPHeader(fout, libName, target, sourceGroups);
  330. // Loop through every source group.
  331. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  332. sg != sourceGroups.end(); ++sg)
  333. {
  334. this->WriteGroup(&(*sg), target, fout, libName);
  335. }
  336. // Write the DSP file's footer.
  337. this->WriteDSPFooter(fout);
  338. }
  339. void cmLocalVisualStudio6Generator
  340. ::WriteGroup(const cmSourceGroup *sg, cmTarget& target,
  341. std::ostream &fout, const char *libName)
  342. {
  343. cmGeneratorTarget* gt =
  344. this->GlobalGenerator->GetGeneratorTarget(&target);
  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() && sg->GetGroupChildren().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. // Loop through each source in the source group.
  359. for(std::vector<const cmSourceFile *>::const_iterator sf =
  360. sourceFiles.begin(); sf != sourceFiles.end(); ++sf)
  361. {
  362. std::string source = (*sf)->GetFullPath();
  363. const cmCustomCommand *command =
  364. (*sf)->GetCustomCommand();
  365. std::string compileFlags;
  366. std::vector<std::string> depends;
  367. std::string objectNameDir;
  368. if(gt->HasExplicitObjectName(*sf))
  369. {
  370. objectNameDir = cmSystemTools::GetFilenamePath(gt->GetObjectName(*sf));
  371. }
  372. // Add per-source file flags.
  373. if(const char* cflags = (*sf)->GetProperty("COMPILE_FLAGS"))
  374. {
  375. compileFlags += cflags;
  376. }
  377. const char* lang = this->GetSourceFileLanguage(*(*sf));
  378. if(lang)
  379. {
  380. if(strcmp(lang, "CXX") == 0)
  381. {
  382. // force a C++ file type
  383. compileFlags += " /TP ";
  384. }
  385. else if(strcmp(lang, "C") == 0)
  386. {
  387. // force to c file type
  388. compileFlags += " /TC ";
  389. }
  390. }
  391. // Add per-source and per-configuration preprocessor definitions.
  392. std::map<cmStdString, cmStdString> cdmap;
  393. {
  394. std::set<std::string> targetCompileDefinitions;
  395. this->AppendDefines(targetCompileDefinitions,
  396. (*sf)->GetProperty("COMPILE_DEFINITIONS"));
  397. this->JoinDefines(targetCompileDefinitions, compileFlags, lang);
  398. }
  399. if(const char* cdefs = (*sf)->GetProperty("COMPILE_DEFINITIONS_DEBUG"))
  400. {
  401. std::set<std::string> debugCompileDefinitions;
  402. this->AppendDefines(debugCompileDefinitions, cdefs);
  403. this->JoinDefines(debugCompileDefinitions, cdmap["DEBUG"], lang);
  404. }
  405. if(const char* cdefs = (*sf)->GetProperty("COMPILE_DEFINITIONS_RELEASE"))
  406. {
  407. std::set<std::string> releaseCompileDefinitions;
  408. this->AppendDefines(releaseCompileDefinitions, cdefs);
  409. this->JoinDefines(releaseCompileDefinitions, cdmap["RELEASE"], lang);
  410. }
  411. if(const char* cdefs =
  412. (*sf)->GetProperty("COMPILE_DEFINITIONS_MINSIZEREL"))
  413. {
  414. std::set<std::string> minsizerelCompileDefinitions;
  415. this->AppendDefines(minsizerelCompileDefinitions, cdefs);
  416. this->JoinDefines(minsizerelCompileDefinitions, cdmap["MINSIZEREL"],
  417. lang);
  418. }
  419. if(const char* cdefs =
  420. (*sf)->GetProperty("COMPILE_DEFINITIONS_RELWITHDEBINFO"))
  421. {
  422. std::set<std::string> relwithdebinfoCompileDefinitions;
  423. this->AppendDefines(relwithdebinfoCompileDefinitions, cdefs);
  424. this->JoinDefines(relwithdebinfoCompileDefinitions,
  425. cdmap["RELWITHDEBINFO"], lang);
  426. }
  427. bool excludedFromBuild =
  428. (lang && (*sf)->GetPropertyAsBool("HEADER_FILE_ONLY"));
  429. // Check for extra object-file dependencies.
  430. const char* dependsValue = (*sf)->GetProperty("OBJECT_DEPENDS");
  431. if(dependsValue)
  432. {
  433. cmSystemTools::ExpandListArgument(dependsValue, depends);
  434. }
  435. if (GetVS6TargetName(source) != libName ||
  436. target.GetType() == cmTarget::UTILITY ||
  437. target.GetType() == cmTarget::GLOBAL_TARGET)
  438. {
  439. fout << "# Begin Source File\n\n";
  440. // Tell MS-Dev what the source is. If the compiler knows how to
  441. // build it, then it will.
  442. fout << "SOURCE=" <<
  443. this->ConvertToOptionallyRelativeOutputPath(source.c_str()) << "\n\n";
  444. if(!depends.empty())
  445. {
  446. // Write out the dependencies for the rule.
  447. fout << "USERDEP__HACK=";
  448. for(std::vector<std::string>::const_iterator d = depends.begin();
  449. d != depends.end(); ++d)
  450. {
  451. fout << "\\\n\t" <<
  452. this->ConvertToOptionallyRelativeOutputPath(d->c_str());
  453. }
  454. fout << "\n";
  455. }
  456. if (command)
  457. {
  458. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  459. this->WriteCustomRule(fout, source.c_str(), *command, flags);
  460. }
  461. else if(!compileFlags.empty() || !objectNameDir.empty() ||
  462. excludedFromBuild || !cdmap.empty())
  463. {
  464. for(std::vector<std::string>::iterator i
  465. = this->Configurations.begin();
  466. i != this->Configurations.end(); ++i)
  467. {
  468. // Strip the subdirectory name out of the configuration name.
  469. std::string config = this->GetConfigName(*i);
  470. if (i == this->Configurations.begin())
  471. {
  472. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  473. }
  474. else
  475. {
  476. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  477. }
  478. if(excludedFromBuild)
  479. {
  480. fout << "# PROP Exclude_From_Build 1\n";
  481. }
  482. if(!compileFlags.empty())
  483. {
  484. fout << "\n# ADD CPP " << compileFlags << "\n\n";
  485. }
  486. std::map<cmStdString, cmStdString>::iterator cdi =
  487. cdmap.find(cmSystemTools::UpperCase(config));
  488. if(cdi != cdmap.end() && !cdi->second.empty())
  489. {
  490. fout << "\n# ADD CPP " << cdi->second << "\n\n";
  491. }
  492. if(!objectNameDir.empty())
  493. {
  494. // Setup an alternate object file directory.
  495. fout << "\n# PROP Intermediate_Dir \""
  496. << config << "/" << objectNameDir << "\"\n\n";
  497. }
  498. }
  499. fout << "!ENDIF\n\n";
  500. }
  501. fout << "# End Source File\n";
  502. }
  503. }
  504. std::vector<cmSourceGroup> const& children = sg->GetGroupChildren();
  505. for(unsigned int i=0;i<children.size();++i)
  506. {
  507. this->WriteGroup(&children[i], target, fout, libName);
  508. }
  509. // If the group has a name, write the footer.
  510. if(name != "")
  511. {
  512. this->WriteDSPEndGroup(fout);
  513. }
  514. }
  515. void
  516. cmLocalVisualStudio6Generator
  517. ::AddUtilityCommandHack(cmTarget& target, int count,
  518. std::vector<std::string>& depends,
  519. const cmCustomCommand& origCommand)
  520. {
  521. // Create a fake output that forces the rule to run.
  522. char* output = new char[(strlen(this->Makefile->GetStartOutputDirectory()) +
  523. strlen(target.GetName()) + 30)];
  524. sprintf(output,"%s/%s_force_%i", this->Makefile->GetStartOutputDirectory(),
  525. target.GetName(), count);
  526. std::string comment = this->ConstructComment(origCommand, "<hack>");
  527. // Add the rule with the given dependencies and commands.
  528. const char* no_main_dependency = 0;
  529. if(cmSourceFile* outsf =
  530. this->Makefile->AddCustomCommandToOutput(
  531. output, depends, no_main_dependency,
  532. origCommand.GetCommandLines(), comment.c_str(),
  533. origCommand.GetWorkingDirectory()))
  534. {
  535. target.AddSourceFile(outsf);
  536. }
  537. // Replace the dependencies with the output of this rule so that the
  538. // next rule added will run after this one.
  539. depends.clear();
  540. depends.push_back(output);
  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 cmCustomCommand& command,
  549. const char* flags)
  550. {
  551. std::string comment =
  552. this->ConstructComment(command, "Building Custom Rule $(InputPath)");
  553. if(comment == "<hack>")
  554. {
  555. comment = "";
  556. }
  557. // Write the rule for each configuration.
  558. std::vector<std::string>::iterator i;
  559. for(i = this->Configurations.begin(); i != this->Configurations.end(); ++i)
  560. {
  561. std::string config = this->GetConfigName(*i);
  562. std::string script =
  563. this->ConstructScript(command, config.c_str(), "\\\n\t");
  564. if (i == this->Configurations.begin())
  565. {
  566. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  567. }
  568. else
  569. {
  570. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  571. }
  572. if(flags)
  573. {
  574. fout << "\n# ADD CPP " << flags << "\n\n";
  575. }
  576. // Write out the dependencies for the rule.
  577. fout << "USERDEP__HACK=";
  578. for(std::vector<std::string>::const_iterator d =
  579. command.GetDepends().begin();
  580. d != command.GetDepends().end();
  581. ++d)
  582. {
  583. // Lookup the real name of the dependency in case it is a CMake target.
  584. std::string dep;
  585. if(this->GetRealDependency(d->c_str(), config.c_str(), dep))
  586. {
  587. fout << "\\\n\t" <<
  588. this->ConvertToOptionallyRelativeOutputPath(dep.c_str());
  589. }
  590. }
  591. fout << "\n";
  592. fout << "# PROP Ignore_Default_Tool 1\n";
  593. fout << "# Begin Custom Build -";
  594. if(!comment.empty())
  595. {
  596. fout << " " << comment.c_str();
  597. }
  598. fout << "\n\n";
  599. if(command.GetOutputs().empty())
  600. {
  601. fout << source
  602. << "_force : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  603. fout << script.c_str() << "\n\n";
  604. }
  605. else
  606. {
  607. for(std::vector<std::string>::const_iterator o =
  608. command.GetOutputs().begin();
  609. o != command.GetOutputs().end();
  610. ++o)
  611. {
  612. // Write a rule for every output generated by this command.
  613. fout << this->ConvertToOptionallyRelativeOutputPath(o->c_str())
  614. << " : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"\n\t";
  615. fout << script.c_str() << "\n\n";
  616. }
  617. }
  618. fout << "# End Custom Build\n\n";
  619. }
  620. fout << "!ENDIF\n\n";
  621. }
  622. void cmLocalVisualStudio6Generator::WriteDSPBeginGroup(std::ostream& fout,
  623. const char* group,
  624. const char* filter)
  625. {
  626. fout << "# Begin Group \"" << group << "\"\n"
  627. "# PROP Default_Filter \"" << filter << "\"\n";
  628. }
  629. void cmLocalVisualStudio6Generator::WriteDSPEndGroup(std::ostream& fout)
  630. {
  631. fout << "# End Group\n";
  632. }
  633. void cmLocalVisualStudio6Generator::SetBuildType(BuildType b,
  634. const char* libName,
  635. cmTarget& target)
  636. {
  637. std::string root= this->Makefile->GetRequiredDefinition("CMAKE_ROOT");
  638. const char *def=
  639. this->Makefile->GetDefinition( "MSPROJECT_TEMPLATE_DIRECTORY");
  640. if( def)
  641. {
  642. root = def;
  643. }
  644. else
  645. {
  646. root += "/Templates";
  647. }
  648. switch(b)
  649. {
  650. case WIN32_EXECUTABLE:
  651. break;
  652. case STATIC_LIBRARY:
  653. this->DSPHeaderTemplate = root;
  654. this->DSPHeaderTemplate += "/staticLibHeader.dsptemplate";
  655. this->DSPFooterTemplate = root;
  656. this->DSPFooterTemplate += "/staticLibFooter.dsptemplate";
  657. break;
  658. case DLL:
  659. this->DSPHeaderTemplate = root;
  660. this->DSPHeaderTemplate += "/DLLHeader.dsptemplate";
  661. this->DSPFooterTemplate = root;
  662. this->DSPFooterTemplate += "/DLLFooter.dsptemplate";
  663. break;
  664. case EXECUTABLE:
  665. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  666. {
  667. this->DSPHeaderTemplate = root;
  668. this->DSPHeaderTemplate += "/EXEWinHeader.dsptemplate";
  669. this->DSPFooterTemplate = root;
  670. this->DSPFooterTemplate += "/EXEFooter.dsptemplate";
  671. }
  672. else
  673. {
  674. this->DSPHeaderTemplate = root;
  675. this->DSPHeaderTemplate += "/EXEHeader.dsptemplate";
  676. this->DSPFooterTemplate = root;
  677. this->DSPFooterTemplate += "/EXEFooter.dsptemplate";
  678. }
  679. break;
  680. case UTILITY:
  681. this->DSPHeaderTemplate = root;
  682. this->DSPHeaderTemplate += "/UtilityHeader.dsptemplate";
  683. this->DSPFooterTemplate = root;
  684. this->DSPFooterTemplate += "/UtilityFooter.dsptemplate";
  685. break;
  686. }
  687. // once the build type is set, determine what configurations are
  688. // possible
  689. cmsys::ifstream fin(this->DSPHeaderTemplate.c_str());
  690. cmsys::RegularExpression reg("# Name ");
  691. if(!fin)
  692. {
  693. cmSystemTools::Error("Error Reading ", this->DSPHeaderTemplate.c_str());
  694. }
  695. // reset this->Configurations
  696. this->Configurations.erase(this->Configurations.begin(),
  697. this->Configurations.end());
  698. // now add all the configurations possible
  699. std::string vs6name = GetVS6TargetName(libName);
  700. std::string line;
  701. while(cmSystemTools::GetLineFromStream(fin, line))
  702. {
  703. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME", vs6name.c_str());
  704. if (reg.find(line))
  705. {
  706. this->Configurations.push_back(line.substr(reg.end()));
  707. }
  708. }
  709. }
  710. //----------------------------------------------------------------------------
  711. cmsys::auto_ptr<cmCustomCommand>
  712. cmLocalVisualStudio6Generator::MaybeCreateOutputDir(cmTarget& target,
  713. const char* config)
  714. {
  715. cmsys::auto_ptr<cmCustomCommand> pcc;
  716. // VS6 forgets to create the output directory for archives if it
  717. // differs from the intermediate directory.
  718. if(target.GetType() != cmTarget::STATIC_LIBRARY) { return pcc; }
  719. std::string outDir = target.GetDirectory(config, false);
  720. // Add a pre-link event to create the directory.
  721. cmCustomCommandLine command;
  722. command.push_back(this->Makefile->GetRequiredDefinition("CMAKE_COMMAND"));
  723. command.push_back("-E");
  724. command.push_back("make_directory");
  725. command.push_back(outDir);
  726. std::vector<std::string> no_output;
  727. std::vector<std::string> no_depends;
  728. cmCustomCommandLines commands;
  729. commands.push_back(command);
  730. pcc.reset(new cmCustomCommand(0, no_output, no_depends, commands, 0, 0));
  731. pcc->SetEscapeOldStyle(false);
  732. pcc->SetEscapeAllowMakeVars(true);
  733. return pcc;
  734. }
  735. // look for custom rules on a target and collect them together
  736. std::string
  737. cmLocalVisualStudio6Generator::CreateTargetRules(cmTarget &target,
  738. const char* configName,
  739. const char * /* libName */)
  740. {
  741. if (target.GetType() >= cmTarget::UTILITY )
  742. {
  743. return "";
  744. }
  745. std::string customRuleCode = "# Begin Special Build Tool\n";
  746. EventWriter event(this, configName, customRuleCode);
  747. // Write the pre-build and pre-link together (VS6 does not support both).
  748. event.Start("PreLink");
  749. event.Write(target.GetPreBuildCommands());
  750. event.Write(target.GetPreLinkCommands());
  751. cmsys::auto_ptr<cmCustomCommand> pcc(
  752. this->MaybeCreateImplibDir(target, configName, false));
  753. if(pcc.get())
  754. {
  755. event.Write(*pcc);
  756. }
  757. pcc = this->MaybeCreateOutputDir(target, configName);
  758. if(pcc.get())
  759. {
  760. event.Write(*pcc);
  761. }
  762. event.Finish();
  763. // Write the post-build rules.
  764. event.Start("PostBuild");
  765. event.Write(target.GetPostBuildCommands());
  766. event.Finish();
  767. customRuleCode += "# End Special Build Tool\n";
  768. return customRuleCode;
  769. }
  770. inline std::string removeQuotes(const std::string& s)
  771. {
  772. if(s[0] == '\"' && s[s.size()-1] == '\"')
  773. {
  774. return s.substr(1, s.size()-2);
  775. }
  776. return s;
  777. }
  778. std::string
  779. cmLocalVisualStudio6Generator::GetTargetIncludeOptions(cmTarget &target,
  780. const char *config)
  781. {
  782. std::string includeOptions;
  783. // Setup /I and /LIBPATH options for the resulting DSP file. VS 6
  784. // truncates long include paths so make it as short as possible if
  785. // the length threatens this problem.
  786. unsigned int maxIncludeLength = 3000;
  787. bool useShortPath = false;
  788. cmGeneratorTarget* gt =
  789. this->GlobalGenerator->GetGeneratorTarget(&target);
  790. for(int j=0; j < 2; ++j)
  791. {
  792. std::vector<std::string> includes;
  793. this->GetIncludeDirectories(includes, gt, "C", config);
  794. std::vector<std::string>::iterator i;
  795. for(i = includes.begin(); i != includes.end(); ++i)
  796. {
  797. std::string tmp =
  798. this->ConvertToOptionallyRelativeOutputPath(i->c_str());
  799. if(useShortPath)
  800. {
  801. cmSystemTools::GetShortPath(tmp.c_str(), tmp);
  802. }
  803. includeOptions += " /I ";
  804. // quote if not already quoted
  805. if (tmp[0] != '"')
  806. {
  807. includeOptions += "\"";
  808. includeOptions += tmp;
  809. includeOptions += "\"";
  810. }
  811. else
  812. {
  813. includeOptions += tmp;
  814. }
  815. }
  816. if(j == 0 && includeOptions.size() > maxIncludeLength)
  817. {
  818. includeOptions = "";
  819. useShortPath = true;
  820. }
  821. else
  822. {
  823. break;
  824. }
  825. }
  826. return includeOptions;
  827. }
  828. // Code in blocks surrounded by a test for this definition is needed
  829. // only for compatibility with user project's replacement DSP
  830. // templates. The CMake templates no longer use them.
  831. #define CM_USE_OLD_VS6
  832. void cmLocalVisualStudio6Generator
  833. ::WriteDSPHeader(std::ostream& fout,
  834. const char *libName, cmTarget &target,
  835. std::vector<cmSourceGroup> &)
  836. {
  837. bool targetBuilds = (target.GetType() >= cmTarget::EXECUTABLE &&
  838. target.GetType() <= cmTarget::MODULE_LIBRARY);
  839. #ifdef CM_USE_OLD_VS6
  840. // Lookup the library and executable output directories.
  841. std::string libPath;
  842. if(this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  843. {
  844. libPath = this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  845. }
  846. std::string exePath;
  847. if(this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  848. {
  849. exePath = this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  850. }
  851. // Make sure there are trailing slashes.
  852. if(!libPath.empty())
  853. {
  854. if(libPath[libPath.size()-1] != '/')
  855. {
  856. libPath += "/";
  857. }
  858. }
  859. if(!exePath.empty())
  860. {
  861. if(exePath[exePath.size()-1] != '/')
  862. {
  863. exePath += "/";
  864. }
  865. }
  866. std::set<std::string> pathEmitted;
  867. // determine the link directories
  868. std::string libOptions;
  869. std::string libDebugOptions;
  870. std::string libOptimizedOptions;
  871. std::string libMultiLineOptions;
  872. std::string libMultiLineOptionsForDebug;
  873. std::string libMultiLineDebugOptions;
  874. std::string libMultiLineOptimizedOptions;
  875. if(libPath.size())
  876. {
  877. std::string lpath =
  878. this->ConvertToOptionallyRelativeOutputPath(libPath.c_str());
  879. if(lpath.size() == 0)
  880. {
  881. lpath = ".";
  882. }
  883. std::string lpathIntDir = libPath + "$(INTDIR)";
  884. lpathIntDir =
  885. this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  886. if(pathEmitted.insert(lpath).second)
  887. {
  888. libOptions += " /LIBPATH:";
  889. libOptions += lpathIntDir;
  890. libOptions += " ";
  891. libOptions += " /LIBPATH:";
  892. libOptions += lpath;
  893. libOptions += " ";
  894. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  895. libMultiLineOptions += lpathIntDir;
  896. libMultiLineOptions += " ";
  897. libMultiLineOptions += " /LIBPATH:";
  898. libMultiLineOptions += lpath;
  899. libMultiLineOptions += " \n";
  900. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  901. libMultiLineOptionsForDebug += lpathIntDir;
  902. libMultiLineOptionsForDebug += " ";
  903. libMultiLineOptionsForDebug += " /LIBPATH:";
  904. libMultiLineOptionsForDebug += lpath;
  905. libMultiLineOptionsForDebug += " \n";
  906. }
  907. }
  908. if(exePath.size())
  909. {
  910. std::string lpath =
  911. this->ConvertToOptionallyRelativeOutputPath(exePath.c_str());
  912. if(lpath.size() == 0)
  913. {
  914. lpath = ".";
  915. }
  916. std::string lpathIntDir = exePath + "$(INTDIR)";
  917. lpathIntDir =
  918. this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  919. if(pathEmitted.insert(lpath).second)
  920. {
  921. libOptions += " /LIBPATH:";
  922. libOptions += lpathIntDir;
  923. libOptions += " ";
  924. libOptions += " /LIBPATH:";
  925. libOptions += lpath;
  926. libOptions += " ";
  927. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  928. libMultiLineOptions += lpathIntDir;
  929. libMultiLineOptions += " ";
  930. libMultiLineOptions += " /LIBPATH:";
  931. libMultiLineOptions += lpath;
  932. libMultiLineOptions += " \n";
  933. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  934. libMultiLineOptionsForDebug += lpathIntDir;
  935. libMultiLineOptionsForDebug += " ";
  936. libMultiLineOptionsForDebug += " /LIBPATH:";
  937. libMultiLineOptionsForDebug += lpath;
  938. libMultiLineOptionsForDebug += " \n";
  939. }
  940. }
  941. std::vector<std::string>::const_iterator i;
  942. const std::vector<std::string>& libdirs = target.GetLinkDirectories();
  943. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  944. {
  945. std::string path = *i;
  946. if(path[path.size()-1] != '/')
  947. {
  948. path += "/";
  949. }
  950. std::string lpath =
  951. this->ConvertToOptionallyRelativeOutputPath(path.c_str());
  952. if(lpath.size() == 0)
  953. {
  954. lpath = ".";
  955. }
  956. std::string lpathIntDir = path + "$(INTDIR)";
  957. lpathIntDir =
  958. this->ConvertToOptionallyRelativeOutputPath(lpathIntDir.c_str());
  959. if(pathEmitted.insert(lpath).second)
  960. {
  961. libOptions += " /LIBPATH:";
  962. libOptions += lpathIntDir;
  963. libOptions += " ";
  964. libOptions += " /LIBPATH:";
  965. libOptions += lpath;
  966. libOptions += " ";
  967. libMultiLineOptions += "# ADD LINK32 /LIBPATH:";
  968. libMultiLineOptions += lpathIntDir;
  969. libMultiLineOptions += " ";
  970. libMultiLineOptions += " /LIBPATH:";
  971. libMultiLineOptions += lpath;
  972. libMultiLineOptions += " \n";
  973. libMultiLineOptionsForDebug += "# ADD LINK32 /LIBPATH:";
  974. libMultiLineOptionsForDebug += lpathIntDir;
  975. libMultiLineOptionsForDebug += " ";
  976. libMultiLineOptionsForDebug += " /LIBPATH:";
  977. libMultiLineOptionsForDebug += lpath;
  978. libMultiLineOptionsForDebug += " \n";
  979. }
  980. }
  981. // find link libraries
  982. const cmTarget::LinkLibraryVectorType& libs = target.GetLinkLibraries();
  983. cmTarget::LinkLibraryVectorType::const_iterator j;
  984. for(j = libs.begin(); j != libs.end(); ++j)
  985. {
  986. // add libraries to executables and dlls (but never include
  987. // a library in a library, bad recursion)
  988. // NEVER LINK STATIC LIBRARIES TO OTHER STATIC LIBRARIES
  989. if ((target.GetType() != cmTarget::SHARED_LIBRARY
  990. && target.GetType() != cmTarget::STATIC_LIBRARY
  991. && target.GetType() != cmTarget::MODULE_LIBRARY) ||
  992. (target.GetType()==cmTarget::SHARED_LIBRARY
  993. && libName != GetVS6TargetName(j->first)) ||
  994. (target.GetType()==cmTarget::MODULE_LIBRARY
  995. && libName != GetVS6TargetName(j->first)))
  996. {
  997. // Compute the proper name to use to link this library.
  998. std::string lib;
  999. std::string libDebug;
  1000. cmTarget* tgt = this->GlobalGenerator->FindTarget(0, j->first.c_str());
  1001. if(tgt)
  1002. {
  1003. lib = cmSystemTools::GetFilenameWithoutExtension
  1004. (tgt->GetFullName().c_str());
  1005. libDebug = cmSystemTools::GetFilenameWithoutExtension
  1006. (tgt->GetFullName("Debug").c_str());
  1007. lib += ".lib";
  1008. libDebug += ".lib";
  1009. }
  1010. else
  1011. {
  1012. lib = j->first.c_str();
  1013. libDebug = j->first.c_str();
  1014. if(j->first.find(".lib") == std::string::npos)
  1015. {
  1016. lib += ".lib";
  1017. libDebug += ".lib";
  1018. }
  1019. }
  1020. lib = this->ConvertToOptionallyRelativeOutputPath(lib.c_str());
  1021. libDebug =
  1022. this->ConvertToOptionallyRelativeOutputPath(libDebug.c_str());
  1023. if (j->second == cmTarget::GENERAL)
  1024. {
  1025. libOptions += " ";
  1026. libOptions += lib;
  1027. libMultiLineOptions += "# ADD LINK32 ";
  1028. libMultiLineOptions += lib;
  1029. libMultiLineOptions += "\n";
  1030. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  1031. libMultiLineOptionsForDebug += libDebug;
  1032. libMultiLineOptionsForDebug += "\n";
  1033. }
  1034. if (j->second == cmTarget::DEBUG)
  1035. {
  1036. libDebugOptions += " ";
  1037. libDebugOptions += lib;
  1038. libMultiLineDebugOptions += "# ADD LINK32 ";
  1039. libMultiLineDebugOptions += libDebug;
  1040. libMultiLineDebugOptions += "\n";
  1041. }
  1042. if (j->second == cmTarget::OPTIMIZED)
  1043. {
  1044. libOptimizedOptions += " ";
  1045. libOptimizedOptions += lib;
  1046. libMultiLineOptimizedOptions += "# ADD LINK32 ";
  1047. libMultiLineOptimizedOptions += lib;
  1048. libMultiLineOptimizedOptions += "\n";
  1049. }
  1050. }
  1051. }
  1052. #endif
  1053. // Get include options for this target.
  1054. std::string includeOptionsDebug = this->GetTargetIncludeOptions(target,
  1055. "DEBUG");
  1056. std::string includeOptionsRelease = this->GetTargetIncludeOptions(target,
  1057. "RELEASE");
  1058. std::string includeOptionsRelWithDebInfo = this->GetTargetIncludeOptions(
  1059. target,
  1060. "RELWITHDEBINFO");
  1061. std::string includeOptionsMinSizeRel = this->GetTargetIncludeOptions(target,
  1062. "MINSIZEREL");
  1063. // Get extra linker options for this target type.
  1064. std::string extraLinkOptions;
  1065. std::string extraLinkOptionsDebug;
  1066. std::string extraLinkOptionsRelease;
  1067. std::string extraLinkOptionsMinSizeRel;
  1068. std::string extraLinkOptionsRelWithDebInfo;
  1069. if(target.GetType() == cmTarget::EXECUTABLE)
  1070. {
  1071. extraLinkOptions = this->Makefile->
  1072. GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS");
  1073. extraLinkOptionsDebug = this->Makefile->
  1074. GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS_DEBUG");
  1075. extraLinkOptionsRelease = this->Makefile->
  1076. GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS_RELEASE");
  1077. extraLinkOptionsMinSizeRel = this->Makefile->
  1078. GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS_MINSIZEREL");
  1079. extraLinkOptionsRelWithDebInfo = this->Makefile->
  1080. GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO");
  1081. }
  1082. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  1083. {
  1084. extraLinkOptions = this->Makefile->
  1085. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS");
  1086. extraLinkOptionsDebug = this->Makefile->
  1087. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS_DEBUG");
  1088. extraLinkOptionsRelease = this->Makefile->
  1089. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS_RELEASE");
  1090. extraLinkOptionsMinSizeRel = this->Makefile->
  1091. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL");
  1092. extraLinkOptionsRelWithDebInfo = this->Makefile->
  1093. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO");
  1094. }
  1095. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  1096. {
  1097. extraLinkOptions = this->Makefile->
  1098. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS");
  1099. extraLinkOptionsDebug = this->Makefile->
  1100. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS_DEBUG");
  1101. extraLinkOptionsRelease = this->Makefile->
  1102. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS_RELEASE");
  1103. extraLinkOptionsMinSizeRel = this->Makefile->
  1104. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL");
  1105. extraLinkOptionsRelWithDebInfo = this->Makefile->
  1106. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO");
  1107. }
  1108. // Get extra linker options for this target.
  1109. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS"))
  1110. {
  1111. extraLinkOptions += " ";
  1112. extraLinkOptions += targetLinkFlags;
  1113. }
  1114. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_DEBUG"))
  1115. {
  1116. extraLinkOptionsDebug += " ";
  1117. extraLinkOptionsDebug += targetLinkFlags;
  1118. }
  1119. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_RELEASE"))
  1120. {
  1121. extraLinkOptionsRelease += " ";
  1122. extraLinkOptionsRelease += targetLinkFlags;
  1123. }
  1124. if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS_MINSIZEREL"))
  1125. {
  1126. extraLinkOptionsMinSizeRel += " ";
  1127. extraLinkOptionsMinSizeRel += targetLinkFlags;
  1128. }
  1129. if(const char* targetLinkFlags =
  1130. target.GetProperty("LINK_FLAGS_RELWITHDEBINFO"))
  1131. {
  1132. extraLinkOptionsRelWithDebInfo += " ";
  1133. extraLinkOptionsRelWithDebInfo += targetLinkFlags;
  1134. }
  1135. // Get standard libraries for this language.
  1136. if(targetBuilds)
  1137. {
  1138. // Get the language to use for linking.
  1139. const char* linkLanguage = target.GetLinkerLanguage();
  1140. if(!linkLanguage)
  1141. {
  1142. cmSystemTools::Error
  1143. ("CMake can not determine linker language for target: ",
  1144. target.GetName());
  1145. return;
  1146. }
  1147. // Compute the variable name to lookup standard libraries for this
  1148. // language.
  1149. std::string standardLibsVar = "CMAKE_";
  1150. standardLibsVar += linkLanguage;
  1151. standardLibsVar += "_STANDARD_LIBRARIES";
  1152. // Add standard libraries.
  1153. if(const char* stdLibs =
  1154. this->Makefile->GetDefinition(standardLibsVar.c_str()))
  1155. {
  1156. extraLinkOptions += " ";
  1157. extraLinkOptions += stdLibs;
  1158. }
  1159. }
  1160. // Compute version number information.
  1161. std::string targetVersionFlag;
  1162. if(target.GetType() == cmTarget::EXECUTABLE ||
  1163. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1164. target.GetType() == cmTarget::MODULE_LIBRARY)
  1165. {
  1166. int major;
  1167. int minor;
  1168. target.GetTargetVersion(major, minor);
  1169. cmOStringStream targetVersionStream;
  1170. targetVersionStream << "/version:" << major << "." << minor;
  1171. targetVersionFlag = targetVersionStream.str();
  1172. }
  1173. // Compute the real name of the target.
  1174. std::string outputName =
  1175. "(OUTPUT_NAME is for libraries and executables only)";
  1176. std::string outputNameDebug = outputName;
  1177. std::string outputNameRelease = outputName;
  1178. std::string outputNameMinSizeRel = outputName;
  1179. std::string outputNameRelWithDebInfo = outputName;
  1180. if(target.GetType() == cmTarget::EXECUTABLE ||
  1181. target.GetType() == cmTarget::STATIC_LIBRARY ||
  1182. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1183. target.GetType() == cmTarget::MODULE_LIBRARY)
  1184. {
  1185. outputName = target.GetFullName();
  1186. outputNameDebug = target.GetFullName("Debug");
  1187. outputNameRelease = target.GetFullName("Release");
  1188. outputNameMinSizeRel = target.GetFullName("MinSizeRel");
  1189. outputNameRelWithDebInfo = target.GetFullName("RelWithDebInfo");
  1190. }
  1191. else if(target.GetType() == cmTarget::OBJECT_LIBRARY)
  1192. {
  1193. outputName = target.GetName();
  1194. outputName += ".lib";
  1195. outputNameDebug = outputName;
  1196. outputNameRelease = outputName;
  1197. outputNameMinSizeRel = outputName;
  1198. outputNameRelWithDebInfo = outputName;
  1199. }
  1200. // Compute the output directory for the target.
  1201. std::string outputDirOld;
  1202. std::string outputDirDebug;
  1203. std::string outputDirRelease;
  1204. std::string outputDirMinSizeRel;
  1205. std::string outputDirRelWithDebInfo;
  1206. if(target.GetType() == cmTarget::EXECUTABLE ||
  1207. target.GetType() == cmTarget::STATIC_LIBRARY ||
  1208. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1209. target.GetType() == cmTarget::MODULE_LIBRARY)
  1210. {
  1211. #ifdef CM_USE_OLD_VS6
  1212. outputDirOld =
  1213. removeQuotes(this->ConvertToOptionallyRelativeOutputPath
  1214. (target.GetDirectory().c_str()));
  1215. #endif
  1216. outputDirDebug =
  1217. removeQuotes(this->ConvertToOptionallyRelativeOutputPath(
  1218. target.GetDirectory("Debug").c_str()));
  1219. outputDirRelease =
  1220. removeQuotes(this->ConvertToOptionallyRelativeOutputPath(
  1221. target.GetDirectory("Release").c_str()));
  1222. outputDirMinSizeRel =
  1223. removeQuotes(this->ConvertToOptionallyRelativeOutputPath(
  1224. target.GetDirectory("MinSizeRel").c_str()));
  1225. outputDirRelWithDebInfo =
  1226. removeQuotes(this->ConvertToOptionallyRelativeOutputPath(
  1227. target.GetDirectory("RelWithDebInfo").c_str()));
  1228. }
  1229. else if(target.GetType() == cmTarget::OBJECT_LIBRARY)
  1230. {
  1231. std::string outputDir = cmake::GetCMakeFilesDirectoryPostSlash();
  1232. outputDirDebug = outputDir + "Debug";
  1233. outputDirRelease = outputDir + "Release";
  1234. outputDirMinSizeRel = outputDir + "MinSizeRel";
  1235. outputDirRelWithDebInfo = outputDir + "RelWithDebInfo";
  1236. }
  1237. // Compute the proper link information for the target.
  1238. std::string optionsDebug;
  1239. std::string optionsRelease;
  1240. std::string optionsMinSizeRel;
  1241. std::string optionsRelWithDebInfo;
  1242. if(target.GetType() == cmTarget::EXECUTABLE ||
  1243. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1244. target.GetType() == cmTarget::MODULE_LIBRARY)
  1245. {
  1246. extraLinkOptionsDebug =
  1247. extraLinkOptions + " " + extraLinkOptionsDebug;
  1248. extraLinkOptionsRelease =
  1249. extraLinkOptions + " " + extraLinkOptionsRelease;
  1250. extraLinkOptionsMinSizeRel =
  1251. extraLinkOptions + " " + extraLinkOptionsMinSizeRel;
  1252. extraLinkOptionsRelWithDebInfo =
  1253. extraLinkOptions + " " + extraLinkOptionsRelWithDebInfo;
  1254. this->ComputeLinkOptions(target, "Debug", extraLinkOptionsDebug,
  1255. optionsDebug);
  1256. this->ComputeLinkOptions(target, "Release", extraLinkOptionsRelease,
  1257. optionsRelease);
  1258. this->ComputeLinkOptions(target, "MinSizeRel", extraLinkOptionsMinSizeRel,
  1259. optionsMinSizeRel);
  1260. this->ComputeLinkOptions(target, "RelWithDebInfo",
  1261. extraLinkOptionsRelWithDebInfo,
  1262. optionsRelWithDebInfo);
  1263. }
  1264. // Compute the path of the import library.
  1265. std::string targetImplibFlagDebug;
  1266. std::string targetImplibFlagRelease;
  1267. std::string targetImplibFlagMinSizeRel;
  1268. std::string targetImplibFlagRelWithDebInfo;
  1269. if(target.GetType() == cmTarget::SHARED_LIBRARY ||
  1270. target.GetType() == cmTarget::MODULE_LIBRARY ||
  1271. target.GetType() == cmTarget::EXECUTABLE)
  1272. {
  1273. std::string fullPathImpDebug = target.GetDirectory("Debug", true);
  1274. std::string fullPathImpRelease = target.GetDirectory("Release", true);
  1275. std::string fullPathImpMinSizeRel =
  1276. target.GetDirectory("MinSizeRel", true);
  1277. std::string fullPathImpRelWithDebInfo =
  1278. target.GetDirectory("RelWithDebInfo", true);
  1279. fullPathImpDebug += "/";
  1280. fullPathImpRelease += "/";
  1281. fullPathImpMinSizeRel += "/";
  1282. fullPathImpRelWithDebInfo += "/";
  1283. fullPathImpDebug += target.GetFullName("Debug", true);
  1284. fullPathImpRelease += target.GetFullName("Release", true);
  1285. fullPathImpMinSizeRel += target.GetFullName("MinSizeRel", true);
  1286. fullPathImpRelWithDebInfo += target.GetFullName("RelWithDebInfo", true);
  1287. targetImplibFlagDebug = "/implib:";
  1288. targetImplibFlagRelease = "/implib:";
  1289. targetImplibFlagMinSizeRel = "/implib:";
  1290. targetImplibFlagRelWithDebInfo = "/implib:";
  1291. targetImplibFlagDebug +=
  1292. this->ConvertToOptionallyRelativeOutputPath(fullPathImpDebug.c_str());
  1293. targetImplibFlagRelease +=
  1294. this->ConvertToOptionallyRelativeOutputPath(fullPathImpRelease.c_str());
  1295. targetImplibFlagMinSizeRel +=
  1296. this->ConvertToOptionallyRelativeOutputPath(
  1297. fullPathImpMinSizeRel.c_str());
  1298. targetImplibFlagRelWithDebInfo +=
  1299. this->ConvertToOptionallyRelativeOutputPath(
  1300. fullPathImpRelWithDebInfo.c_str());
  1301. }
  1302. #ifdef CM_USE_OLD_VS6
  1303. // Compute link information for the target.
  1304. if(extraLinkOptions.size())
  1305. {
  1306. libOptions += " ";
  1307. libOptions += extraLinkOptions;
  1308. libOptions += " ";
  1309. libMultiLineOptions += "# ADD LINK32 ";
  1310. libMultiLineOptions += extraLinkOptions;
  1311. libMultiLineOptions += " \n";
  1312. libMultiLineOptionsForDebug += "# ADD LINK32 ";
  1313. libMultiLineOptionsForDebug += extraLinkOptions;
  1314. libMultiLineOptionsForDebug += " \n";
  1315. }
  1316. #endif
  1317. // are there any custom rules on the target itself
  1318. // only if the target is a lib or exe
  1319. std::string customRuleCodeRelease
  1320. = this->CreateTargetRules(target, "RELEASE", libName);
  1321. std::string customRuleCodeDebug
  1322. = this->CreateTargetRules(target, "DEBUG", libName);
  1323. std::string customRuleCodeMinSizeRel
  1324. = this->CreateTargetRules(target, "MINSIZEREL", libName);
  1325. std::string customRuleCodeRelWithDebInfo
  1326. = this->CreateTargetRules(target, "RELWITHDEBINFO", libName);
  1327. cmsys::ifstream fin(this->DSPHeaderTemplate.c_str());
  1328. if(!fin)
  1329. {
  1330. cmSystemTools::Error("Error Reading ", this->DSPHeaderTemplate.c_str());
  1331. }
  1332. std::string staticLibOptions;
  1333. std::string staticLibOptionsDebug;
  1334. std::string staticLibOptionsRelease;
  1335. std::string staticLibOptionsMinSizeRel;
  1336. std::string staticLibOptionsRelWithDebInfo;
  1337. if(target.GetType() == cmTarget::STATIC_LIBRARY )
  1338. {
  1339. const char *libflagsGlobal =
  1340. this->Makefile->GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS");
  1341. this->AppendFlags(staticLibOptions, libflagsGlobal);
  1342. this->AppendFlags(staticLibOptionsDebug, libflagsGlobal);
  1343. this->AppendFlags(staticLibOptionsRelease, libflagsGlobal);
  1344. this->AppendFlags(staticLibOptionsMinSizeRel, libflagsGlobal);
  1345. this->AppendFlags(staticLibOptionsRelWithDebInfo, libflagsGlobal);
  1346. this->AppendFlags(staticLibOptionsDebug, this->Makefile->
  1347. GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS_DEBUG"));
  1348. this->AppendFlags(staticLibOptionsRelease, this->Makefile->
  1349. GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS_RELEASE"));
  1350. this->AppendFlags(staticLibOptionsMinSizeRel, this->Makefile->
  1351. GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL"));
  1352. this->AppendFlags(staticLibOptionsRelWithDebInfo, this->Makefile->
  1353. GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO"));
  1354. const char *libflags = target.GetProperty("STATIC_LIBRARY_FLAGS");
  1355. this->AppendFlags(staticLibOptions, libflags);
  1356. this->AppendFlags(staticLibOptionsDebug, libflags);
  1357. this->AppendFlags(staticLibOptionsRelease, libflags);
  1358. this->AppendFlags(staticLibOptionsMinSizeRel, libflags);
  1359. this->AppendFlags(staticLibOptionsRelWithDebInfo, libflags);
  1360. this->AppendFlags(staticLibOptionsDebug,
  1361. target.GetProperty("STATIC_LIBRARY_FLAGS_DEBUG"));
  1362. this->AppendFlags(staticLibOptionsRelease,
  1363. target.GetProperty("STATIC_LIBRARY_FLAGS_RELEASE"));
  1364. this->AppendFlags(staticLibOptionsMinSizeRel,
  1365. target.GetProperty("STATIC_LIBRARY_FLAGS_MINSIZEREL"));
  1366. this->AppendFlags(staticLibOptionsRelWithDebInfo,
  1367. target.GetProperty("STATIC_LIBRARY_FLAGS_RELWITHDEBINFO"));
  1368. std::string objects;
  1369. this->OutputObjects(target, "LIB", objects);
  1370. if(!objects.empty())
  1371. {
  1372. objects = "\n" + objects;
  1373. staticLibOptionsDebug += objects;
  1374. staticLibOptionsRelease += objects;
  1375. staticLibOptionsMinSizeRel += objects;
  1376. staticLibOptionsRelWithDebInfo += objects;
  1377. }
  1378. }
  1379. // Add the export symbol definition for shared library objects.
  1380. std::string exportSymbol;
  1381. if(const char* exportMacro = target.GetExportMacro())
  1382. {
  1383. exportSymbol = exportMacro;
  1384. }
  1385. std::string line;
  1386. std::string libnameExports;
  1387. if(exportSymbol.size())
  1388. {
  1389. libnameExports = "/D \"";
  1390. libnameExports += exportSymbol;
  1391. libnameExports += "\"";
  1392. }
  1393. while(cmSystemTools::GetLineFromStream(fin, line))
  1394. {
  1395. const char* mfcFlag = this->Makefile->GetDefinition("CMAKE_MFC_FLAG");
  1396. if(!mfcFlag)
  1397. {
  1398. mfcFlag = "0";
  1399. }
  1400. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME_EXPORTS",
  1401. libnameExports.c_str());
  1402. cmSystemTools::ReplaceString(line, "CMAKE_MFC_FLAG",
  1403. mfcFlag);
  1404. if(target.GetType() == cmTarget::STATIC_LIBRARY ||
  1405. target.GetType() == cmTarget::OBJECT_LIBRARY)
  1406. {
  1407. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS_DEBUG",
  1408. staticLibOptionsDebug.c_str());
  1409. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS_RELEASE",
  1410. staticLibOptionsRelease.c_str());
  1411. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS_MINSIZEREL",
  1412. staticLibOptionsMinSizeRel.c_str());
  1413. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS_RELWITHDEBINFO",
  1414. staticLibOptionsRelWithDebInfo.c_str());
  1415. cmSystemTools::ReplaceString(line, "CM_STATIC_LIB_ARGS",
  1416. staticLibOptions.c_str());
  1417. }
  1418. if(this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  1419. {
  1420. cmSystemTools::ReplaceString(line, "/nologo", "");
  1421. }
  1422. #ifdef CM_USE_OLD_VS6
  1423. cmSystemTools::ReplaceString(line, "CM_LIBRARIES",
  1424. libOptions.c_str());
  1425. cmSystemTools::ReplaceString(line, "CM_DEBUG_LIBRARIES",
  1426. libDebugOptions.c_str());
  1427. cmSystemTools::ReplaceString(line, "CM_OPTIMIZED_LIBRARIES",
  1428. libOptimizedOptions.c_str());
  1429. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES_FOR_DEBUG",
  1430. libMultiLineOptionsForDebug.c_str());
  1431. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES",
  1432. libMultiLineOptions.c_str());
  1433. cmSystemTools::ReplaceString(line, "CM_MULTILINE_DEBUG_LIBRARIES",
  1434. libMultiLineDebugOptions.c_str());
  1435. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIMIZED_LIBRARIES",
  1436. libMultiLineOptimizedOptions.c_str());
  1437. #endif
  1438. // Substitute the rules for custom command. When specifying just the
  1439. // target name for the command the command can be different for
  1440. // different configs
  1441. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_RELEASE",
  1442. customRuleCodeRelease.c_str());
  1443. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_DEBUG",
  1444. customRuleCodeDebug.c_str());
  1445. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_MINSIZEREL",
  1446. customRuleCodeMinSizeRel.c_str());
  1447. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE_RELWITHDEBINFO",
  1448. customRuleCodeRelWithDebInfo.c_str());
  1449. // Substitute the real output name into the template.
  1450. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_DEBUG",
  1451. outputNameDebug.c_str());
  1452. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_RELEASE",
  1453. outputNameRelease.c_str());
  1454. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_MINSIZEREL",
  1455. outputNameMinSizeRel.c_str());
  1456. cmSystemTools::ReplaceString(line, "OUTPUT_NAME_RELWITHDEBINFO",
  1457. outputNameRelWithDebInfo.c_str());
  1458. cmSystemTools::ReplaceString(line, "OUTPUT_NAME", outputName.c_str());
  1459. // Substitute the proper link information into the template.
  1460. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_DEBUG",
  1461. optionsDebug.c_str());
  1462. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_RELEASE",
  1463. optionsRelease.c_str());
  1464. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_MINSIZEREL",
  1465. optionsMinSizeRel.c_str());
  1466. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIONS_RELWITHDEBINFO",
  1467. optionsRelWithDebInfo.c_str());
  1468. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES_DEBUG",
  1469. includeOptionsDebug.c_str());
  1470. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES_RELEASE",
  1471. includeOptionsRelease.c_str());
  1472. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES_MINSIZEREL",
  1473. includeOptionsMinSizeRel.c_str());
  1474. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES_RELWITHDEBINFO",
  1475. includeOptionsRelWithDebInfo.c_str());
  1476. cmSystemTools::ReplaceString(line, "TARGET_VERSION_FLAG",
  1477. targetVersionFlag.c_str());
  1478. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_DEBUG",
  1479. targetImplibFlagDebug.c_str());
  1480. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_RELEASE",
  1481. targetImplibFlagRelease.c_str());
  1482. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_MINSIZEREL",
  1483. targetImplibFlagMinSizeRel.c_str());
  1484. cmSystemTools::ReplaceString(line, "TARGET_IMPLIB_FLAG_RELWITHDEBINFO",
  1485. targetImplibFlagRelWithDebInfo.c_str());
  1486. std::string vs6name = GetVS6TargetName(libName);
  1487. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME", vs6name.c_str());
  1488. #ifdef CM_USE_OLD_VS6
  1489. // because LIBRARY_OUTPUT_PATH and EXECUTABLE_OUTPUT_PATH
  1490. // are already quoted in the template file,
  1491. // we need to remove the quotes here, we still need
  1492. // to convert to output path for unix to win32 conversion
  1493. cmSystemTools::ReplaceString
  1494. (line, "LIBRARY_OUTPUT_PATH",
  1495. removeQuotes(this->ConvertToOptionallyRelativeOutputPath
  1496. (libPath.c_str())).c_str());
  1497. cmSystemTools::ReplaceString
  1498. (line, "EXECUTABLE_OUTPUT_PATH",
  1499. removeQuotes(this->ConvertToOptionallyRelativeOutputPath
  1500. (exePath.c_str())).c_str());
  1501. #endif
  1502. if(targetBuilds || target.GetType() == cmTarget::OBJECT_LIBRARY)
  1503. {
  1504. cmSystemTools::ReplaceString(line, "OUTPUT_DIRECTORY_DEBUG",
  1505. outputDirDebug.c_str());
  1506. cmSystemTools::ReplaceString(line, "OUTPUT_DIRECTORY_RELEASE",
  1507. outputDirRelease.c_str());
  1508. cmSystemTools::ReplaceString(line, "OUTPUT_DIRECTORY_MINSIZEREL",
  1509. outputDirMinSizeRel.c_str());
  1510. cmSystemTools::ReplaceString(line, "OUTPUT_DIRECTORY_RELWITHDEBINFO",
  1511. outputDirRelWithDebInfo.c_str());
  1512. if(!outputDirOld.empty())
  1513. {
  1514. cmSystemTools::ReplaceString(line, "OUTPUT_DIRECTORY",
  1515. outputDirOld.c_str());
  1516. }
  1517. }
  1518. cmSystemTools::ReplaceString(line,
  1519. "EXTRA_DEFINES",
  1520. this->Makefile->GetDefineFlags());
  1521. const char* debugPostfix
  1522. = this->Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  1523. cmSystemTools::ReplaceString(line, "DEBUG_POSTFIX",
  1524. debugPostfix?debugPostfix:"");
  1525. // store flags for each configuration
  1526. std::string flags = " ";
  1527. std::string flagsRelease = " ";
  1528. std::string flagsMinSizeRel = " ";
  1529. std::string flagsDebug = " ";
  1530. std::string flagsRelWithDebInfo = " ";
  1531. if(target.GetType() >= cmTarget::EXECUTABLE &&
  1532. target.GetType() <= cmTarget::OBJECT_LIBRARY)
  1533. {
  1534. const char* linkLanguage = target.GetLinkerLanguage();
  1535. if(!linkLanguage)
  1536. {
  1537. cmSystemTools::Error
  1538. ("CMake can not determine linker language for target: ",
  1539. target.GetName());
  1540. return;
  1541. }
  1542. // if CXX is on and the target contains cxx code then add the cxx flags
  1543. std::string baseFlagVar = "CMAKE_";
  1544. baseFlagVar += linkLanguage;
  1545. baseFlagVar += "_FLAGS";
  1546. flags = this->Makefile->GetSafeDefinition(baseFlagVar.c_str());
  1547. std::string flagVar = baseFlagVar + "_RELEASE";
  1548. flagsRelease = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1549. flagsRelease += " -DCMAKE_INTDIR=\\\"Release\\\" ";
  1550. flagVar = baseFlagVar + "_MINSIZEREL";
  1551. flagsMinSizeRel = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1552. flagsMinSizeRel += " -DCMAKE_INTDIR=\\\"MinSizeRel\\\" ";
  1553. flagVar = baseFlagVar + "_DEBUG";
  1554. flagsDebug = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1555. flagsDebug += " -DCMAKE_INTDIR=\\\"Debug\\\" ";
  1556. flagVar = baseFlagVar + "_RELWITHDEBINFO";
  1557. flagsRelWithDebInfo = this->Makefile->GetSafeDefinition(flagVar.c_str());
  1558. flagsRelWithDebInfo += " -DCMAKE_INTDIR=\\\"RelWithDebInfo\\\" ";
  1559. this->AddCompileOptions(flags, &target, linkLanguage, 0);
  1560. this->AddCompileOptions(flagsDebug, &target, linkLanguage, "Debug");
  1561. this->AddCompileOptions(flagsRelease, &target, linkLanguage, "Release");
  1562. this->AddCompileOptions(flagsMinSizeRel, &target, linkLanguage,
  1563. "MinSizeRel");
  1564. this->AddCompileOptions(flagsRelWithDebInfo, &target, linkLanguage,
  1565. "RelWithDebInfo");
  1566. }
  1567. // if _UNICODE and _SBCS are not found, then add -D_MBCS
  1568. std::string defs = this->Makefile->GetDefineFlags();
  1569. if(flags.find("D_UNICODE") == flags.npos &&
  1570. defs.find("D_UNICODE") == flags.npos &&
  1571. flags.find("D_SBCS") == flags.npos &&
  1572. defs.find("D_SBCS") == flags.npos)
  1573. {
  1574. flags += " /D \"_MBCS\"";
  1575. }
  1576. // Add per-target and per-configuration preprocessor definitions.
  1577. std::set<std::string> definesSet;
  1578. std::set<std::string> debugDefinesSet;
  1579. std::set<std::string> releaseDefinesSet;
  1580. std::set<std::string> minsizeDefinesSet;
  1581. std::set<std::string> debugrelDefinesSet;
  1582. this->AddCompileDefinitions(definesSet, &target, 0);
  1583. this->AddCompileDefinitions(debugDefinesSet, &target, "DEBUG");
  1584. this->AddCompileDefinitions(releaseDefinesSet, &target, "RELEASE");
  1585. this->AddCompileDefinitions(minsizeDefinesSet, &target, "MINSIZEREL");
  1586. this->AddCompileDefinitions(debugrelDefinesSet, &target, "RELWITHDEBINFO");
  1587. std::string defines = " ";
  1588. std::string debugDefines = " ";
  1589. std::string releaseDefines = " ";
  1590. std::string minsizeDefines = " ";
  1591. std::string debugrelDefines = " ";
  1592. this->JoinDefines(definesSet, defines, 0);
  1593. this->JoinDefines(debugDefinesSet, debugDefines, 0);
  1594. this->JoinDefines(releaseDefinesSet, releaseDefines, 0);
  1595. this->JoinDefines(minsizeDefinesSet, minsizeDefines, 0);
  1596. this->JoinDefines(debugrelDefinesSet, debugrelDefines, 0);
  1597. flags += defines;
  1598. flagsDebug += debugDefines;
  1599. flagsRelease += releaseDefines;
  1600. flagsMinSizeRel += minsizeDefines;
  1601. flagsRelWithDebInfo += debugrelDefines;
  1602. // The template files have CXX FLAGS in them, that need to be replaced.
  1603. // There are not separate CXX and C template files, so we use the same
  1604. // variable names. The previous code sets up flags* variables to contain
  1605. // the correct C or CXX flags
  1606. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_MINSIZEREL",
  1607. flagsMinSizeRel.c_str());
  1608. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_DEBUG",
  1609. flagsDebug.c_str());
  1610. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELWITHDEBINFO",
  1611. flagsRelWithDebInfo.c_str());
  1612. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS_RELEASE",
  1613. flagsRelease.c_str());
  1614. cmSystemTools::ReplaceString(line, "CMAKE_CXX_FLAGS", flags.c_str());
  1615. cmSystemTools::ReplaceString(line, "COMPILE_DEFINITIONS_MINSIZEREL",
  1616. minsizeDefines.c_str());
  1617. cmSystemTools::ReplaceString(line, "COMPILE_DEFINITIONS_DEBUG",
  1618. debugDefines.c_str());
  1619. cmSystemTools::ReplaceString(line, "COMPILE_DEFINITIONS_RELWITHDEBINFO",
  1620. debugrelDefines.c_str());
  1621. cmSystemTools::ReplaceString(line, "COMPILE_DEFINITIONS_RELEASE",
  1622. releaseDefines.c_str());
  1623. cmSystemTools::ReplaceString(line, "COMPILE_DEFINITIONS", defines.c_str());
  1624. fout << line.c_str() << std::endl;
  1625. }
  1626. }
  1627. void cmLocalVisualStudio6Generator::WriteDSPFooter(std::ostream& fout)
  1628. {
  1629. cmsys::ifstream fin(this->DSPFooterTemplate.c_str());
  1630. if(!fin)
  1631. {
  1632. cmSystemTools::Error("Error Reading ",
  1633. this->DSPFooterTemplate.c_str());
  1634. }
  1635. std::string line;
  1636. while(cmSystemTools::GetLineFromStream(fin, line))
  1637. {
  1638. fout << line << std::endl;
  1639. }
  1640. }
  1641. //----------------------------------------------------------------------------
  1642. void cmLocalVisualStudio6Generator
  1643. ::ComputeLinkOptions(cmTarget& target,
  1644. const char* configName,
  1645. const std::string extraOptions,
  1646. std::string& options)
  1647. {
  1648. // Compute the link information for this configuration.
  1649. cmComputeLinkInformation* pcli = target.GetLinkInformation(configName);
  1650. if(!pcli)
  1651. {
  1652. return;
  1653. }
  1654. cmComputeLinkInformation& cli = *pcli;
  1655. typedef cmComputeLinkInformation::ItemVector ItemVector;
  1656. ItemVector const& linkLibs = cli.GetItems();
  1657. std::vector<std::string> const& linkDirs = cli.GetDirectories();
  1658. this->OutputObjects(target, "LINK", options);
  1659. // Build the link options code.
  1660. for(std::vector<std::string>::const_iterator d = linkDirs.begin();
  1661. d != linkDirs.end(); ++d)
  1662. {
  1663. std::string dir = *d;
  1664. if(!dir.empty())
  1665. {
  1666. if(dir[dir.size()-1] != '/')
  1667. {
  1668. dir += "/";
  1669. }
  1670. dir += "$(IntDir)";
  1671. options += "# ADD LINK32 /LIBPATH:";
  1672. options += this->ConvertToOptionallyRelativeOutputPath(dir.c_str());
  1673. options += " /LIBPATH:";
  1674. options += this->ConvertToOptionallyRelativeOutputPath(d->c_str());
  1675. options += "\n";
  1676. }
  1677. }
  1678. for(ItemVector::const_iterator l = linkLibs.begin();
  1679. l != linkLibs.end(); ++l)
  1680. {
  1681. options += "# ADD LINK32 ";
  1682. if(l->IsPath)
  1683. {
  1684. options +=
  1685. this->ConvertToOptionallyRelativeOutputPath(l->Value.c_str());
  1686. }
  1687. else if (!l->Target
  1688. || l->Target->GetType() != cmTarget::INTERFACE_LIBRARY)
  1689. {
  1690. options += l->Value;
  1691. }
  1692. options += "\n";
  1693. }
  1694. // Add extra options if any.
  1695. if(!extraOptions.empty())
  1696. {
  1697. options += "# ADD LINK32 ";
  1698. options += extraOptions;
  1699. options += "\n";
  1700. }
  1701. }
  1702. //----------------------------------------------------------------------------
  1703. void cmLocalVisualStudio6Generator
  1704. ::OutputObjects(cmTarget& target, const char* tool,
  1705. std::string& options)
  1706. {
  1707. // VS 6 does not support per-config source locations so we
  1708. // list object library content on the link line instead.
  1709. cmGeneratorTarget* gt =
  1710. this->GlobalGenerator->GetGeneratorTarget(&target);
  1711. std::vector<std::string> objs;
  1712. gt->UseObjectLibraries(objs);
  1713. for(std::vector<std::string>::const_iterator
  1714. oi = objs.begin(); oi != objs.end(); ++oi)
  1715. {
  1716. options += "# ADD ";
  1717. options += tool;
  1718. options += "32 ";
  1719. options += this->ConvertToOptionallyRelativeOutputPath(oi->c_str());
  1720. options += "\n";
  1721. }
  1722. }
  1723. std::string
  1724. cmLocalVisualStudio6Generator
  1725. ::GetTargetDirectory(cmTarget const&) const
  1726. {
  1727. // No per-target directory for this generator (yet).
  1728. return "";
  1729. }
  1730. //----------------------------------------------------------------------------
  1731. std::string
  1732. cmLocalVisualStudio6Generator
  1733. ::ComputeLongestObjectDirectory(cmTarget&) const
  1734. {
  1735. // Compute the maximum length configuration name.
  1736. std::string config_max;
  1737. for(std::vector<std::string>::const_iterator
  1738. i = this->Configurations.begin();
  1739. i != this->Configurations.end(); ++i)
  1740. {
  1741. // Strip the subdirectory name out of the configuration name.
  1742. std::string config = this->GetConfigName(*i);
  1743. if(config.size() > config_max.size())
  1744. {
  1745. config_max = config;
  1746. }
  1747. }
  1748. // Compute the maximum length full path to the intermediate
  1749. // files directory for any configuration. This is used to construct
  1750. // object file names that do not produce paths that are too long.
  1751. std::string dir_max;
  1752. dir_max += this->Makefile->GetCurrentOutputDirectory();
  1753. dir_max += "/";
  1754. dir_max += config_max;
  1755. dir_max += "/";
  1756. return dir_max;
  1757. }
  1758. std::string
  1759. cmLocalVisualStudio6Generator
  1760. ::GetConfigName(std::string const& configuration) const
  1761. {
  1762. // Strip the subdirectory name out of the configuration name.
  1763. std::string config = configuration;
  1764. std::string::size_type pos = config.find_last_of(" ");
  1765. config = config.substr(pos+1, std::string::npos);
  1766. config = config.substr(0, config.size()-1);
  1767. return config;
  1768. }
  1769. //----------------------------------------------------------------------------
  1770. bool
  1771. cmLocalVisualStudio6Generator
  1772. ::CheckDefinition(std::string const& define) const
  1773. {
  1774. // Perform the standard check first.
  1775. if(!this->cmLocalGenerator::CheckDefinition(define))
  1776. {
  1777. return false;
  1778. }
  1779. // Now do the VS6-specific check.
  1780. if(define.find_first_of(" ") != define.npos &&
  1781. define.find_first_of("\"$;") != define.npos)
  1782. {
  1783. cmOStringStream e;
  1784. e << "WARNING: The VS6 IDE does not support preprocessor definition "
  1785. << "values with spaces and '\"', '$', or ';'.\n"
  1786. << "CMake is dropping a preprocessor definition: " << define << "\n"
  1787. << "Consider defining the macro in a (configured) header file.\n";
  1788. cmSystemTools::Message(e.str().c_str());
  1789. return false;
  1790. }
  1791. // Assume it is supported.
  1792. return true;
  1793. }