cmDSPWriter.cxx 26 KB

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