cmDSPWriter.cxx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 "cmDSPWriter.h"
  14. #include "cmStandardIncludes.h"
  15. #include "cmSystemTools.h"
  16. #include "cmRegularExpression.h"
  17. cmDSPWriter::~cmDSPWriter()
  18. {
  19. }
  20. cmDSPWriter::cmDSPWriter(cmMakefile*mf)
  21. {
  22. m_Makefile = mf;
  23. }
  24. void cmDSPWriter::OutputDSPFile()
  25. {
  26. // If not an in source build, then create the output directory
  27. if(strcmp(m_Makefile->GetStartOutputDirectory(),
  28. m_Makefile->GetHomeDirectory()) != 0)
  29. {
  30. if(!cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory()))
  31. {
  32. cmSystemTools::Error("Error creating directory ",
  33. m_Makefile->GetStartOutputDirectory());
  34. }
  35. }
  36. // Setup /I and /LIBPATH options for the resulting DSP file
  37. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  38. std::vector<std::string>::iterator i;
  39. for(i = includes.begin(); i != includes.end(); ++i)
  40. {
  41. m_IncludeOptions += " /I ";
  42. std::string tmp = cmSystemTools::EscapeSpaces(i->c_str());
  43. cmSystemTools::ConvertToWindowsSlashesAndCleanUp(tmp);
  44. // quote if not already quoted
  45. if (tmp[0] != '"')
  46. {
  47. m_IncludeOptions += "\"";
  48. m_IncludeOptions += tmp;
  49. m_IncludeOptions += "\"";
  50. }
  51. else
  52. {
  53. m_IncludeOptions += tmp;
  54. }
  55. }
  56. // Create the DSP or set of DSP's for libraries and executables
  57. // clear project names
  58. m_CreatedProjectNames.clear();
  59. // build any targets
  60. cmTargets &tgts = m_Makefile->GetTargets();
  61. for(cmTargets::iterator l = tgts.begin();
  62. l != tgts.end(); l++)
  63. {
  64. switch(l->second.GetType())
  65. {
  66. case cmTarget::STATIC_LIBRARY:
  67. this->SetBuildType(STATIC_LIBRARY, l->first.c_str());
  68. break;
  69. case cmTarget::SHARED_LIBRARY:
  70. this->SetBuildType(DLL, l->first.c_str());
  71. break;
  72. case cmTarget::EXECUTABLE:
  73. this->SetBuildType(EXECUTABLE,l->first.c_str());
  74. break;
  75. case cmTarget::WIN32_EXECUTABLE:
  76. this->SetBuildType(WIN32_EXECUTABLE,l->first.c_str());
  77. break;
  78. case cmTarget::UTILITY:
  79. this->SetBuildType(UTILITY, l->first.c_str());
  80. break;
  81. case cmTarget::INSTALL_FILES:
  82. break;
  83. case cmTarget::INSTALL_PROGRAMS:
  84. break;
  85. default:
  86. cmSystemTools::Error("Bad target type", l->first.c_str());
  87. break;
  88. }
  89. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  90. // so don't build a projectfile for it
  91. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  92. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  93. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  94. {
  95. this->CreateSingleDSP(l->first.c_str(),l->second);
  96. }
  97. }
  98. }
  99. void cmDSPWriter::CreateSingleDSP(const char *lname, cmTarget &target)
  100. {
  101. // add to the list of projects
  102. std::string pname = lname;
  103. m_CreatedProjectNames.push_back(pname);
  104. // create the dsp.cmake file
  105. std::string fname;
  106. fname = m_Makefile->GetStartOutputDirectory();
  107. fname += "/";
  108. fname += lname;
  109. fname += ".dsp";
  110. // save the name of the real dsp file
  111. std::string realDSP = fname;
  112. fname += ".cmake";
  113. std::ofstream fout(fname.c_str());
  114. if(!fout)
  115. {
  116. cmSystemTools::Error("Error Writing ", fname.c_str());
  117. }
  118. this->WriteDSPFile(fout,lname,target);
  119. fout.close();
  120. // if the dsp file has changed, then write it.
  121. cmSystemTools::CopyFileIfDifferent(fname.c_str(), realDSP.c_str());
  122. }
  123. void cmDSPWriter::AddDSPBuildRule(cmSourceGroup& sourceGroup)
  124. {
  125. std::string dspname = *(m_CreatedProjectNames.end()-1);
  126. if(dspname == "ALL_BUILD")
  127. {
  128. return;
  129. }
  130. dspname += ".dsp.cmake";
  131. std::string makefileIn = m_Makefile->GetStartDirectory();
  132. makefileIn += "/";
  133. makefileIn += "CMakeLists.txt";
  134. makefileIn = cmSystemTools::HandleNetworkPaths(makefileIn.c_str());
  135. makefileIn = cmSystemTools::EscapeSpaces(makefileIn.c_str());
  136. std::string dsprule = "${CMAKE_COMMAND}";
  137. m_Makefile->ExpandVariablesInString(dsprule);
  138. dsprule = cmSystemTools::HandleNetworkPaths(dsprule.c_str());
  139. std::string args = makefileIn;
  140. args += " -H\"";
  141. args += cmSystemTools::HandleNetworkPaths(m_Makefile->GetHomeDirectory());
  142. args += "\" -S\"";
  143. args += cmSystemTools::HandleNetworkPaths(m_Makefile->GetStartDirectory());
  144. args += "\" -O\"";
  145. args += cmSystemTools::HandleNetworkPaths(m_Makefile->GetStartOutputDirectory());
  146. args += "\" -B\"";
  147. args += cmSystemTools::HandleNetworkPaths(m_Makefile->GetHomeOutputDirectory());
  148. args += "\"";
  149. m_Makefile->ExpandVariablesInString(args);
  150. std::string configFile =
  151. m_Makefile->GetDefinition("CMAKE_ROOT");
  152. configFile += "/Templates/CMakeWindowsSystemConfig.cmake";
  153. std::vector<std::string> listFiles = m_Makefile->GetListFiles();
  154. bool found = false;
  155. for(std::vector<std::string>::iterator i = listFiles.begin();
  156. i != listFiles.end(); ++i)
  157. {
  158. if(*i == configFile)
  159. {
  160. found = true;
  161. }
  162. }
  163. if(!found)
  164. {
  165. listFiles.push_back(configFile);
  166. }
  167. std::vector<std::string> outputs;
  168. outputs.push_back(dspname);
  169. cmCustomCommand cc(makefileIn.c_str(), dsprule.c_str(),
  170. args.c_str(),
  171. listFiles,
  172. outputs);
  173. sourceGroup.AddCustomCommand(cc);
  174. }
  175. void cmDSPWriter::WriteDSPFile(std::ostream& fout,
  176. const char *libName,
  177. cmTarget &target)
  178. {
  179. // We may be modifying the source groups temporarily, so make a copy.
  180. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  181. // get the classes from the source lists then add them to the groups
  182. std::vector<cmSourceFile> classes = target.GetSourceFiles();
  183. for(std::vector<cmSourceFile>::iterator i = classes.begin();
  184. i != classes.end(); i++)
  185. {
  186. // Add the file to the list of sources.
  187. std::string source = i->GetFullPath();
  188. cmSourceGroup& sourceGroup = m_Makefile->FindSourceGroup(source.c_str(),
  189. sourceGroups);
  190. sourceGroup.AddSource(source.c_str());
  191. }
  192. // add any custom rules to the source groups
  193. for (std::vector<cmCustomCommand>::const_iterator cr =
  194. target.GetCustomCommands().begin();
  195. cr != target.GetCustomCommands().end(); ++cr)
  196. {
  197. cmSourceGroup& sourceGroup =
  198. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  199. sourceGroups);
  200. cmCustomCommand cc(*cr);
  201. cc.ExpandVariables(*m_Makefile);
  202. sourceGroup.AddCustomCommand(cc);
  203. }
  204. // Write the DSP file's header.
  205. this->WriteDSPHeader(fout, libName, target, sourceGroups);
  206. // Find the group in which the CMakeLists.txt source belongs, and add
  207. // the rule to generate this DSP file.
  208. for(std::vector<cmSourceGroup>::reverse_iterator sg = sourceGroups.rbegin();
  209. sg != sourceGroups.rend(); ++sg)
  210. {
  211. if(sg->Matches("CMakeLists.txt"))
  212. {
  213. this->AddDSPBuildRule(*sg);
  214. break;
  215. }
  216. }
  217. // Loop through every source group.
  218. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  219. sg != sourceGroups.end(); ++sg)
  220. {
  221. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  222. // If the group is empty, don't write it at all.
  223. if(buildRules.empty())
  224. { continue; }
  225. // If the group has a name, write the header.
  226. std::string name = sg->GetName();
  227. if(name != "")
  228. {
  229. this->WriteDSPBeginGroup(fout, name.c_str(), "");
  230. }
  231. // Loop through each build rule in the source group.
  232. for(cmSourceGroup::BuildRules::const_iterator cc =
  233. buildRules.begin(); cc != buildRules.end(); ++ cc)
  234. {
  235. std::string source = cc->first;
  236. const cmSourceGroup::Commands& commands = cc->second;
  237. if (source != libName || target.GetType() == cmTarget::UTILITY)
  238. {
  239. fout << "# Begin Source File\n\n";
  240. // Tell MS-Dev what the source is. If the compiler knows how to
  241. // build it, then it will.
  242. fout << "SOURCE=" << cmSystemTools::EscapeSpaces(source.c_str()) << "\n\n";
  243. if (!commands.empty())
  244. {
  245. cmSourceGroup::CommandFiles totalCommand;
  246. std::string totalCommandStr;
  247. totalCommandStr = this->CombineCommands(commands, totalCommand,
  248. source.c_str());
  249. this->WriteCustomRule(fout, source.c_str(), totalCommandStr.c_str(),
  250. totalCommand.m_Depends,
  251. totalCommand.m_Outputs);
  252. }
  253. fout << "# End Source File\n";
  254. }
  255. }
  256. // If the group has a name, write the footer.
  257. if(name != "")
  258. {
  259. this->WriteDSPEndGroup(fout);
  260. }
  261. }
  262. // Write the DSP file's footer.
  263. this->WriteDSPFooter(fout);
  264. }
  265. void cmDSPWriter::WriteCustomRule(std::ostream& fout,
  266. const char* source,
  267. const char* command,
  268. const std::set<std::string>& depends,
  269. const std::set<std::string>& outputs)
  270. {
  271. std::vector<std::string>::iterator i;
  272. for(i = m_Configurations.begin(); i != m_Configurations.end(); ++i)
  273. {
  274. if (i == m_Configurations.begin())
  275. {
  276. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  277. }
  278. else
  279. {
  280. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  281. }
  282. // Write out the dependencies for the rule.
  283. fout << "USERDEP__HACK=";
  284. std::string temp;
  285. for(std::set<std::string>::const_iterator d = depends.begin();
  286. d != depends.end(); ++d)
  287. {
  288. temp = *d;
  289. fout << "\\\n\t" << cmSystemTools::EscapeSpaces(cmSystemTools::ConvertToWindowsSlashes(temp));
  290. }
  291. fout << "\n";
  292. fout << "# PROP Ignore_Default_Tool 1\n";
  293. fout << "# Begin Custom Build\n\n";
  294. if(outputs.size() == 0)
  295. {
  296. fout << source << "_force : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"";
  297. fout << command << "\n\n";
  298. }
  299. // Write a rule for every output generated by this command.
  300. for(std::set<std::string>::const_iterator output = outputs.begin();
  301. output != outputs.end(); ++output)
  302. {
  303. fout << "\"" << output->c_str()
  304. << "\" : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"";
  305. fout << command << "\n\n";
  306. }
  307. fout << "# End Custom Build\n\n";
  308. }
  309. fout << "!ENDIF\n\n";
  310. }
  311. void cmDSPWriter::WriteDSPBeginGroup(std::ostream& fout,
  312. const char* group,
  313. const char* filter)
  314. {
  315. fout << "# Begin Group \"" << group << "\"\n"
  316. "# PROP Default_Filter \"" << filter << "\"\n";
  317. }
  318. void cmDSPWriter::WriteDSPEndGroup(std::ostream& fout)
  319. {
  320. fout << "# End Group\n";
  321. }
  322. void cmDSPWriter::SetBuildType(BuildType b, const char *libName)
  323. {
  324. std::string root= m_Makefile->GetDefinition("CMAKE_ROOT");
  325. const char *def= m_Makefile->GetDefinition( "MSPROJECT_TEMPLATE_DIRECTORY");
  326. if( def)
  327. {
  328. root = def;
  329. }
  330. else
  331. {
  332. root += "/Templates";
  333. }
  334. switch(b)
  335. {
  336. case STATIC_LIBRARY:
  337. m_DSPHeaderTemplate = root;
  338. m_DSPHeaderTemplate += "/staticLibHeader.dsptemplate";
  339. m_DSPFooterTemplate = root;
  340. m_DSPFooterTemplate += "/staticLibFooter.dsptemplate";
  341. break;
  342. case DLL:
  343. m_DSPHeaderTemplate = root;
  344. m_DSPHeaderTemplate += "/DLLHeader.dsptemplate";
  345. m_DSPFooterTemplate = root;
  346. m_DSPFooterTemplate += "/DLLFooter.dsptemplate";
  347. break;
  348. case EXECUTABLE:
  349. m_DSPHeaderTemplate = root;
  350. m_DSPHeaderTemplate += "/EXEHeader.dsptemplate";
  351. m_DSPFooterTemplate = root;
  352. m_DSPFooterTemplate += "/EXEFooter.dsptemplate";
  353. break;
  354. case WIN32_EXECUTABLE:
  355. m_DSPHeaderTemplate = root;
  356. m_DSPHeaderTemplate += "/EXEWinHeader.dsptemplate";
  357. m_DSPFooterTemplate = root;
  358. m_DSPFooterTemplate += "/EXEFooter.dsptemplate";
  359. break;
  360. case UTILITY:
  361. m_DSPHeaderTemplate = root;
  362. m_DSPHeaderTemplate += "/UtilityHeader.dsptemplate";
  363. m_DSPFooterTemplate = root;
  364. m_DSPFooterTemplate += "/UtilityFooter.dsptemplate";
  365. break;
  366. }
  367. // once the build type is set, determine what configurations are
  368. // possible
  369. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  370. cmRegularExpression reg("# Name ");
  371. if(!fin)
  372. {
  373. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  374. }
  375. // reset m_Configurations
  376. m_Configurations.erase(m_Configurations.begin(), m_Configurations.end());
  377. // now add all the configurations possible
  378. char buffer[2048];
  379. while(fin)
  380. {
  381. fin.getline(buffer, 2048);
  382. std::string line = buffer;
  383. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  384. if (reg.find(line))
  385. {
  386. m_Configurations.push_back(line.substr(reg.end()));
  387. }
  388. }
  389. }
  390. std::string
  391. cmDSPWriter::CombineCommands(const cmSourceGroup::Commands &commands,
  392. cmSourceGroup::CommandFiles &totalCommand,
  393. const char *source)
  394. {
  395. // Loop through every custom command generating code from the
  396. // current source.
  397. // build up the depends and outputs and commands
  398. std::string totalCommandStr = "";
  399. std::string temp;
  400. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  401. c != commands.end(); ++c)
  402. {
  403. totalCommandStr += "\n\t";
  404. temp= c->second.m_Command;
  405. cmSystemTools::ConvertToWindowsSlashes(temp);
  406. temp = cmSystemTools::EscapeSpaces(temp.c_str());
  407. totalCommandStr += temp;
  408. totalCommandStr += " ";
  409. totalCommandStr += c->second.m_Arguments;
  410. totalCommand.Merge(c->second);
  411. }
  412. // Create a dummy file with the name of the source if it does
  413. // not exist
  414. if(totalCommand.m_Outputs.empty())
  415. {
  416. std::string dummyFile = m_Makefile->GetStartOutputDirectory();
  417. dummyFile += "/";
  418. dummyFile += source;
  419. if(!cmSystemTools::FileExists(dummyFile.c_str()))
  420. {
  421. std::ofstream fout(dummyFile.c_str());
  422. fout << "Dummy file created by cmake as unused source for utility command.\n";
  423. }
  424. }
  425. return totalCommandStr;
  426. }
  427. // look for custom rules on a target and collect them together
  428. std::string
  429. cmDSPWriter::CreateTargetRules(const cmTarget &target,
  430. const char *libName)
  431. {
  432. std::string customRuleCode = "";
  433. if (target.GetType() >= cmTarget::UTILITY)
  434. {
  435. return customRuleCode;
  436. }
  437. // Find the group in which the lix exe custom rules belong
  438. bool init = false;
  439. for (std::vector<cmCustomCommand>::const_iterator cr =
  440. target.GetCustomCommands().begin();
  441. cr != target.GetCustomCommands().end(); ++cr)
  442. {
  443. cmCustomCommand cc(*cr);
  444. cc.ExpandVariables(*m_Makefile);
  445. if (cc.GetSourceName() == libName)
  446. {
  447. if (!init)
  448. {
  449. // header stuff
  450. customRuleCode = "# Begin Special Build Tool\nPostBuild_Cmds=";
  451. init = true;
  452. }
  453. else
  454. {
  455. customRuleCode += "\t";
  456. }
  457. customRuleCode += cc.GetCommand() + " " + cc.GetArguments();
  458. }
  459. }
  460. if (init)
  461. {
  462. customRuleCode += "\n# End Special Build Tool\n";
  463. }
  464. return customRuleCode;
  465. }
  466. void cmDSPWriter::WriteDSPHeader(std::ostream& fout, const char *libName,
  467. const cmTarget &target,
  468. std::vector<cmSourceGroup> &)
  469. {
  470. std::set<std::string> pathEmitted;
  471. // determine the link directories
  472. std::string libOptions;
  473. std::string libDebugOptions;
  474. std::string libOptimizedOptions;
  475. std::string libMultiLineOptions;
  476. std::string libMultiLineDebugOptions;
  477. std::string libMultiLineOptimizedOptions;
  478. // suppoirt override in output directory
  479. std::string libPath = "";
  480. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  481. {
  482. libPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  483. }
  484. std::string exePath = "";
  485. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  486. {
  487. exePath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  488. }
  489. if(libPath.size())
  490. {
  491. // make sure there is a trailing slash
  492. if(libPath[libPath.size()-1] != '/')
  493. {
  494. libPath += "/";
  495. }
  496. std::string lpath = cmSystemTools::HandleNetworkPaths(libPath.c_str());
  497. if(pathEmitted.insert(lpath).second)
  498. {
  499. libOptions += " /LIBPATH:\"";
  500. libOptions += lpath;
  501. libOptions += "$(INTDIR)\" ";
  502. libOptions += " /LIBPATH:\"";
  503. libOptions += lpath;
  504. libOptions += "\" ";
  505. libMultiLineOptions += "# ADD LINK32 /LIBPATH:\"";
  506. libMultiLineOptions += lpath;
  507. libMultiLineOptions += "$(INTDIR)\" ";
  508. libMultiLineOptions += " /LIBPATH:\"";
  509. libMultiLineOptions += lpath;
  510. libMultiLineOptions += "\" \n";
  511. }
  512. }
  513. if(exePath.size())
  514. {
  515. // make sure there is a trailing slash
  516. if(exePath[exePath.size()-1] != '/')
  517. {
  518. exePath += "/";
  519. }
  520. std::string lpath = cmSystemTools::HandleNetworkPaths(exePath.c_str());
  521. if(pathEmitted.insert(lpath).second)
  522. {
  523. libOptions += " /LIBPATH:\"";
  524. libOptions += lpath;
  525. libOptions += "$(INTDIR)\" ";
  526. libOptions += " /LIBPATH:\"";
  527. libOptions += lpath;
  528. libOptions += "\" ";
  529. libMultiLineOptions += "# ADD LINK32 /LIBPATH:\"";
  530. libMultiLineOptions += lpath;
  531. libMultiLineOptions += "$(INTDIR)\" ";
  532. libMultiLineOptions += " /LIBPATH:\"";
  533. libMultiLineOptions += lpath;
  534. libMultiLineOptions += "\" \n";
  535. }
  536. }
  537. std::vector<std::string>::iterator i;
  538. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  539. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  540. {
  541. std::string lpath = cmSystemTools::HandleNetworkPaths(i->c_str());
  542. if(lpath[lpath.size()-1] != '/')
  543. {
  544. lpath += "/";
  545. }
  546. if(pathEmitted.insert(lpath).second)
  547. {
  548. libOptions += " /LIBPATH:\"";
  549. libOptions += lpath;
  550. libOptions += "/$(INTDIR)\" ";
  551. libOptions += " /LIBPATH:\"";
  552. libOptions += lpath;
  553. libOptions += "\" ";
  554. libMultiLineOptions += "# ADD LINK32 /LIBPATH:\"";
  555. libMultiLineOptions += lpath;
  556. libMultiLineOptions += "/$(INTDIR)\" ";
  557. libMultiLineOptions += " /LIBPATH:\"";
  558. libMultiLineOptions += lpath;
  559. libMultiLineOptions += "\" \n";
  560. }
  561. }
  562. // find link libraries
  563. const cmTarget::LinkLibraries& libs = target.GetLinkLibraries();
  564. cmTarget::LinkLibraries::const_iterator j;
  565. for(j = libs.begin(); j != libs.end(); ++j)
  566. {
  567. // add libraries to executables and dlls (but never include
  568. // a library in a library, bad recursion)
  569. if ((target.GetType() != cmTarget::SHARED_LIBRARY
  570. && target.GetType() != cmTarget::STATIC_LIBRARY) ||
  571. (target.GetType() == cmTarget::SHARED_LIBRARY && libName != j->first))
  572. {
  573. std::string lib = j->first;
  574. if(j->first.find(".lib") == std::string::npos)
  575. {
  576. lib += ".lib";
  577. }
  578. lib = cmSystemTools::EscapeSpaces(lib.c_str());
  579. if (j->second == cmTarget::GENERAL)
  580. {
  581. libOptions += " ";
  582. libOptions += lib;
  583. libMultiLineOptions += "# ADD LINK32 ";
  584. libMultiLineOptions += lib;
  585. libMultiLineOptions += "\n";
  586. }
  587. if (j->second == cmTarget::DEBUG)
  588. {
  589. libDebugOptions += " ";
  590. libDebugOptions += lib;
  591. libMultiLineDebugOptions += "# ADD LINK32 ";
  592. libMultiLineDebugOptions += lib;
  593. libMultiLineDebugOptions += "\n";
  594. }
  595. if (j->second == cmTarget::OPTIMIZED)
  596. {
  597. libOptimizedOptions += " ";
  598. libOptimizedOptions += lib;
  599. libMultiLineOptimizedOptions += "# ADD LINK32 ";
  600. libMultiLineOptimizedOptions += lib;
  601. libMultiLineOptimizedOptions += "\n";
  602. }
  603. }
  604. }
  605. std::string extraLinkOptions =
  606. m_Makefile->GetDefinition("CMAKE_EXTRA_LINK_FLAGS");
  607. if(extraLinkOptions.size())
  608. {
  609. libOptions += " ";
  610. libOptions += extraLinkOptions;
  611. libOptions += " ";
  612. libMultiLineOptions += "# ADD LINK32 ";
  613. libMultiLineOptions += extraLinkOptions;
  614. libMultiLineOptions += " \n";
  615. }
  616. // are there any custom rules on the target itself
  617. // only if the target is a lib or exe
  618. std::string customRuleCode = this->CreateTargetRules(target, libName);
  619. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  620. if(!fin)
  621. {
  622. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  623. }
  624. char buffer[2048];
  625. while(fin)
  626. {
  627. fin.getline(buffer, 2048);
  628. std::string line = buffer;
  629. const char* mfcFlag = m_Makefile->GetDefinition("CMAKE_MFC_FLAG");
  630. if(!mfcFlag)
  631. {
  632. mfcFlag = "0";
  633. }
  634. cmSystemTools::ReplaceString(line, "CMAKE_CUSTOM_RULE_CODE",
  635. customRuleCode.c_str());
  636. cmSystemTools::ReplaceString(line, "CMAKE_MFC_FLAG",
  637. mfcFlag);
  638. cmSystemTools::ReplaceString(line, "CM_LIBRARIES",
  639. libOptions.c_str());
  640. cmSystemTools::ReplaceString(line, "CM_DEBUG_LIBRARIES",
  641. libDebugOptions.c_str());
  642. cmSystemTools::ReplaceString(line, "CM_OPTIMIZED_LIBRARIES",
  643. libOptimizedOptions.c_str());
  644. cmSystemTools::ReplaceString(line, "CM_MULTILINE_LIBRARIES",
  645. libMultiLineOptions.c_str());
  646. cmSystemTools::ReplaceString(line, "CM_MULTILINE_DEBUG_LIBRARIES",
  647. libMultiLineDebugOptions.c_str());
  648. cmSystemTools::ReplaceString(line, "CM_MULTILINE_OPTIMIZED_LIBRARIES",
  649. libMultiLineOptimizedOptions.c_str());
  650. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES",
  651. m_IncludeOptions.c_str());
  652. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  653. cmSystemTools::ReplaceString(line, "LIBRARY_OUTPUT_PATH",
  654. cmSystemTools::HandleNetworkPaths(libPath.c_str()).c_str());
  655. cmSystemTools::ReplaceString(line, "EXECUTABLE_OUTPUT_PATH",
  656. cmSystemTools::HandleNetworkPaths(exePath.c_str()).c_str());
  657. cmSystemTools::ReplaceString(line,
  658. "EXTRA_DEFINES",
  659. m_Makefile->GetDefineFlags());
  660. cmSystemTools::ReplaceString(line,
  661. "CMAKE_CXX_FLAGS_RELEASE",
  662. m_Makefile->
  663. GetDefinition("CMAKE_CXX_FLAGS_RELEASE"));
  664. cmSystemTools::ReplaceString(line,
  665. "CMAKE_CXX_FLAGS_MINSIZEREL",
  666. m_Makefile->
  667. GetDefinition("CMAKE_CXX_FLAGS_MINSIZEREL")
  668. );
  669. cmSystemTools::ReplaceString(line,
  670. "CMAKE_CXX_FLAGS_DEBUG",
  671. m_Makefile->
  672. GetDefinition("CMAKE_CXX_FLAGS_DEBUG"));
  673. cmSystemTools::ReplaceString(line,
  674. "CMAKE_CXX_FLAGS_RELWITHDEBINFO",
  675. m_Makefile->
  676. GetDefinition("CMAKE_CXX_FLAGS_RELWITHDEBINFO"));
  677. cmSystemTools::ReplaceString(line,
  678. "CMAKE_CXX_FLAGS",
  679. m_Makefile->
  680. GetDefinition("CMAKE_CXX_FLAGS"));
  681. fout << line.c_str() << std::endl;
  682. }
  683. }
  684. void cmDSPWriter::WriteDSPFooter(std::ostream& fout)
  685. {
  686. std::ifstream fin(m_DSPFooterTemplate.c_str());
  687. if(!fin)
  688. {
  689. cmSystemTools::Error("Error Reading ",
  690. m_DSPFooterTemplate.c_str());
  691. }
  692. char buffer[2048];
  693. while(fin)
  694. {
  695. fin.getline(buffer, 2048);
  696. fout << buffer << std::endl;
  697. }
  698. }