cmNMakeMakefileGenerator.cxx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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 "cmNMakeMakefileGenerator.h"
  14. #include "cmMakefile.h"
  15. #include "cmStandardIncludes.h"
  16. #include "cmSystemTools.h"
  17. #include "cmSourceFile.h"
  18. #include "cmMakeDepend.h"
  19. #include "cmCacheManager.h"
  20. #include "cmGeneratedFileStream.h"
  21. #include "windows.h"
  22. cmNMakeMakefileGenerator::cmNMakeMakefileGenerator()
  23. {
  24. this->SetLibraryPathOption("@CMAKE_C_LIBPATH_FLAG@"); // Use @ here
  25. this->SetObjectFileExtension("$(CMAKE_OBJECT_FILE_SUFFIX)");
  26. this->SetExecutableExtension("$(CMAKE_EXECUTABLE_SUFFIX)");
  27. this->SetLibraryPrefix("");
  28. this->SetStaticLibraryExtension("$(CMAKE_STATICLIB_SUFFIX)");
  29. this->SetSharedLibraryExtension("$(CMAKE_SHLIB_SUFFIX)");
  30. }
  31. cmNMakeMakefileGenerator::~cmNMakeMakefileGenerator()
  32. {
  33. }
  34. // convert to windows short paths if there are spaces
  35. // in path
  36. std::string cmNMakeMakefileGenerator::ShortPath(const char* path)
  37. {
  38. std::string ret = path;
  39. // if there are no spaces in path, then just
  40. // call ConvertToOutputPath
  41. if(ret.find(' ') == std::string::npos)
  42. {
  43. return this->ConvertToOutputPath(path);
  44. }
  45. // if there are spaces then call GetShortPathName to get rid of them
  46. char *buffer = new char[strlen(path)+1];
  47. if(GetShortPathName(path, buffer,
  48. strlen(path)+1) != 0)
  49. {
  50. ret = buffer;
  51. }
  52. else
  53. {
  54. // if GetShortPathName failed for some reason use
  55. // ConvertToOutputPath instead which will at least escape the spaces
  56. ret = this->ConvertToOutputPath(path);
  57. }
  58. delete [] buffer;
  59. return ret;
  60. }
  61. // convert a command to a short path if it has spaces
  62. // this separates the arguments from the command and puts
  63. // them back together
  64. std::string cmNMakeMakefileGenerator::ShortPathCommand(const char* command)
  65. {
  66. if(!strchr(command, ' '))
  67. {
  68. return command;
  69. }
  70. cmRegularExpression reg("^\"([^\"]*)\"(.*)");
  71. if(reg.find(command))
  72. {
  73. std::string c = reg.match(1);
  74. cmRegularExpression removeIntDir("(.*)/\\$\\(IntDir\\)(.*)");
  75. if(removeIntDir.find(c))
  76. {
  77. c = removeIntDir.match(1) + removeIntDir.match(2);
  78. }
  79. c = this->ShortPath(c.c_str());
  80. std::string ret = c;
  81. std::string args = reg.match(2);
  82. ret += args;
  83. return ret;
  84. }
  85. return command;
  86. }
  87. void cmNMakeMakefileGenerator::ComputeSystemInfo()
  88. {
  89. // now load the settings
  90. if(!m_Makefile->GetDefinition("CMAKE_ROOT"))
  91. {
  92. cmSystemTools::Error(
  93. "CMAKE_ROOT has not been defined, bad GUI or driver program");
  94. return;
  95. }
  96. std::string fpath =
  97. m_Makefile->GetDefinition("CMAKE_ROOT");
  98. fpath += "/Templates/CMakeNMakeWindowsSystemConfig.cmake";
  99. m_Makefile->ReadListFile(NULL,fpath.c_str());
  100. }
  101. void cmNMakeMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  102. {
  103. fout << "# NMake Makefile generated by cmake\n";
  104. const char* variables =
  105. "# general variables used in the makefile\n"
  106. "\n"
  107. "# Path to cmake\n"
  108. "MAKESILENT = /nologo\n"
  109. "CMAKE_STANDARD_WINDOWS_LIBRARIES = @CMAKE_STANDARD_WINDOWS_LIBRARIES@\n"
  110. "CMAKE_C_FLAGS = @CMAKE_C_FLAGS@ @BUILD_FLAGS@\n"
  111. "CMAKE_C_LINK_EXECUTABLE_FLAG = @CMAKE_C_LINK_EXECUTABLE_FLAG@\n"
  112. "CMAKE_CXX_FLAGS = @CMAKE_CXX_FLAGS@ @BUILD_FLAGS@\n"
  113. "CMAKE_LINKER_FLAGS = @CMAKE_LINKER_FLAGS@ @LINKER_BUILD_FLAGS@\n"
  114. "CMAKE_LINKER_SHARED_LIBRARY_FLAG = @CMAKE_LINKER_SHARED_LIBRARY_FLAG@\n"
  115. "CMAKE_LIBRARY_MANAGER_FLAGS = @CMAKE_LIBRARY_MANAGER_FLAGS@\n"
  116. "CMAKE_OBJECT_FILE_SUFFIX = @CMAKE_OBJECT_FILE_SUFFIX@\n"
  117. "CMAKE_EXECUTABLE_SUFFIX = @CMAKE_EXECUTABLE_SUFFIX@\n"
  118. "CMAKE_STATICLIB_SUFFIX = @CMAKE_STATICLIB_SUFFIX@\n"
  119. "CMAKE_SHLIB_SUFFIX = @CMAKE_SHLIB_SUFFIX@\n"
  120. "!IF \"$(OS)\" == \"Windows_NT\"\n"
  121. "NULL=\n"
  122. "!ELSE \n"
  123. "NULL=nul\n"
  124. "!ENDIF \n"
  125. "RM = del\n";
  126. std::string buildType = "CMAKE_CXX_FLAGS_";
  127. buildType += m_Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  128. buildType = cmSystemTools::UpperCase(buildType);
  129. m_Makefile->AddDefinition("BUILD_FLAGS",
  130. m_Makefile->GetDefinition(
  131. buildType.c_str()));
  132. buildType = "CMAKE_LINKER_FLAGS_";
  133. buildType += m_Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  134. buildType = cmSystemTools::UpperCase(buildType);
  135. m_Makefile->AddDefinition("LINKER_BUILD_FLAGS",
  136. m_Makefile->GetDefinition(
  137. buildType.c_str()));
  138. std::string replaceVars = variables;
  139. m_Makefile->ExpandVariablesInString(replaceVars);
  140. fout << replaceVars.c_str();
  141. std::string ccompiler = m_Makefile->GetDefinition("CMAKE_C_COMPILER");
  142. fout << "CMAKE_C_COMPILER = "
  143. << this->ConvertToOutputPath(ccompiler.c_str()) << "\n";
  144. std::string cxxcompiler = m_Makefile->GetDefinition("CMAKE_CXX_COMPILER");
  145. fout << "CMAKE_CXX_COMPILER = "
  146. << this->ConvertToOutputPath(cxxcompiler.c_str()) << "\n";
  147. std::string linker = m_Makefile->GetDefinition("CMAKE_LINKER");
  148. fout << "CMAKE_LINKER = " <<
  149. this->ConvertToOutputPath(linker.c_str()) << "\n";
  150. std::string lib_manager = m_Makefile->GetDefinition("CMAKE_LIBRARY_MANAGER");
  151. fout << "CMAKE_LIBRARY_MANAGER = "
  152. << this->ConvertToOutputPath(lib_manager.c_str()) << "\n";
  153. std::string cmakecommand = m_Makefile->GetDefinition("CMAKE_COMMAND");
  154. fout << "CMAKE_COMMAND = "
  155. << this->ConvertToOutputPath(cmakecommand.c_str()) << "\n";
  156. fout << "CMAKE_CURRENT_SOURCE = "
  157. << ShortPath(m_Makefile->GetStartDirectory() )
  158. << "\n";
  159. fout << "CMAKE_CURRENT_BINARY = "
  160. << ShortPath(m_Makefile->GetStartOutputDirectory())
  161. << "\n";
  162. fout << "CMAKE_SOURCE_DIR = "
  163. << ShortPath(m_Makefile->GetHomeDirectory()) << "\n";
  164. fout << "CMAKE_BINARY_DIR = "
  165. << ShortPath(m_Makefile->GetHomeOutputDirectory() )
  166. << "\n";
  167. // Output Include paths
  168. fout << "INCLUDE_FLAGS = ";
  169. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  170. std::vector<std::string>::iterator i;
  171. fout << "-I" <<
  172. this->ConvertToOutputPath(m_Makefile->GetStartDirectory()) << " ";
  173. for(i = includes.begin(); i != includes.end(); ++i)
  174. {
  175. std::string include = *i;
  176. // Don't output a -I for the standard include path "/usr/include".
  177. // This can cause problems with certain standard library
  178. // implementations because the wrong headers may be found first.
  179. fout << "-I" << this->ConvertToOutputPath(i->c_str()).c_str() << " ";
  180. }
  181. fout << m_Makefile->GetDefineFlags();
  182. fout << "\n\n";
  183. }
  184. void cmNMakeMakefileGenerator::BuildInSubDirectory(std::ostream& fout,
  185. const char* directory,
  186. const char* target1,
  187. const char* target2)
  188. {
  189. if(target1)
  190. {
  191. std::string dir = this->ConvertToOutputPath(directory);
  192. fout << "\tif not exist \"" << dir << "\\$(NULL)\""
  193. << " "
  194. << "$(MAKE) $(MAKESILENT) rebuild_cache\n"
  195. << "\techo Building " << target1 << " in directory " << directory << "\n"
  196. << "\tcd " << dir << "\n"
  197. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target1 << "\n";
  198. }
  199. if(target2)
  200. {
  201. fout << "\techo Building " << target2 << " in directory " << directory << "\n";
  202. fout << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target2 << "\n";
  203. }
  204. std::string currentDir = m_Makefile->GetCurrentOutputDirectory();
  205. fout << "\tcd " << this->ConvertToOutputPath(currentDir.c_str()) << "\n\n";
  206. }
  207. // This needs to be overriden because nmake requires commands to be quoted
  208. // if the are full paths to the executable????
  209. void cmNMakeMakefileGenerator::OutputMakeRule(std::ostream& fout,
  210. const char* comment,
  211. const char* target,
  212. const char* depends,
  213. const char* command,
  214. const char* command2,
  215. const char* command3,
  216. const char* command4)
  217. {
  218. if(!target)
  219. {
  220. cmSystemTools::Error("no target for OutputMakeRule");
  221. return;
  222. }
  223. std::string replace;
  224. if(comment)
  225. {
  226. replace = comment;
  227. m_Makefile->ExpandVariablesInString(replace);
  228. fout << "#---------------------------------------------------------\n";
  229. fout << "# " << comment;
  230. fout << "\n#\n";
  231. }
  232. fout << "\n";
  233. replace = target;
  234. m_Makefile->ExpandVariablesInString(replace);
  235. replace = this->ConvertToOutputPath(replace.c_str());
  236. fout << replace.c_str() << ": ";
  237. if(depends)
  238. {
  239. replace = depends;
  240. m_Makefile->ExpandVariablesInString(replace);
  241. fout << replace.c_str();
  242. }
  243. fout << "\n";
  244. if(command)
  245. {
  246. replace = ShortPathCommand(command);
  247. m_Makefile->ExpandVariablesInString(replace);
  248. if(replace[0] != '-' && replace.find("echo") != 0
  249. && replace.find("$(MAKE)") != 0)
  250. {
  251. fout << "\t" << "echo " << replace.c_str() << "\n";
  252. }
  253. fout << "\t" << replace.c_str() << "\n";
  254. }
  255. if(command2)
  256. {
  257. replace = ShortPathCommand(command2);
  258. m_Makefile->ExpandVariablesInString(replace);
  259. if(replace[0] != '-' && replace.find("echo") != 0
  260. && replace.find("$(MAKE)") != 0)
  261. {
  262. fout << "\t" << "echo " << replace.c_str() << "\n";
  263. }
  264. fout << "\t" << replace.c_str() << "\n";
  265. }
  266. if(command3)
  267. {
  268. replace = ShortPathCommand(command3);
  269. m_Makefile->ExpandVariablesInString(replace);
  270. if(replace[0] != '-' && replace.find("echo") != 0
  271. && replace.find("$(MAKE)") != 0)
  272. {
  273. fout << "\t" << "echo " << replace.c_str() << "\n";
  274. }
  275. fout << "\t" << replace.c_str() << "\n";
  276. }
  277. if(command4)
  278. {
  279. replace = ShortPathCommand(command4);
  280. m_Makefile->ExpandVariablesInString(replace);
  281. if(replace[0] != '-' && replace.find("echo") != 0
  282. && replace.find("$(MAKE)") != 0)
  283. {
  284. fout << "\t" << "echo " << replace.c_str() << "\n";
  285. }
  286. fout << "\t" << replace.c_str() << "\n";
  287. }
  288. fout << "\n";
  289. }
  290. void
  291. cmNMakeMakefileGenerator::
  292. OutputBuildObjectFromSource(std::ostream& fout,
  293. const char* shortName,
  294. const cmSourceFile& source,
  295. const char* extraCompileFlags,
  296. bool shared)
  297. {
  298. // Header files shouldn't have build rules.
  299. if(source.IsAHeaderFileOnly())
  300. return;
  301. std::string comment = "Build ";
  302. std::string objectFile = std::string(shortName) +
  303. this->GetOutputExtension(source.GetSourceExtension().c_str());
  304. comment += objectFile + " From ";
  305. comment += source.GetFullPath();
  306. std::string compileCommand;
  307. std::string ext = source.GetSourceExtension();
  308. if(ext == "c" )
  309. {
  310. compileCommand = "$(CMAKE_C_COMPILER) $(CMAKE_C_FLAGS) ";
  311. compileCommand += extraCompileFlags;
  312. if(shared)
  313. {
  314. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  315. }
  316. compileCommand += "$(INCLUDE_FLAGS) -c ";
  317. compileCommand +=
  318. this->ConvertToOutputPath(source.GetFullPath().c_str());
  319. // Need to get the definition here because this value might have
  320. // trailing space (since it is directly prepended to the filename)
  321. std::string output_object_file_flag =
  322. m_Makefile->GetDefinition("CMAKE_C_OUTPUT_OBJECT_FILE_FLAG");
  323. m_Makefile->ExpandVariablesInString(output_object_file_flag);
  324. compileCommand += " " + output_object_file_flag;
  325. compileCommand += objectFile;
  326. }
  327. else if (ext == "rc")
  328. {
  329. compileCommand = "$(RC) /fo\"";
  330. compileCommand += objectFile;
  331. compileCommand += "\" ";
  332. compileCommand +=
  333. this->ConvertToOutputPath(source.GetFullPath().c_str());
  334. }
  335. else if (ext == "def")
  336. {
  337. // no rule to output for this one
  338. return;
  339. }
  340. // assume c++ if not c rc or def
  341. else
  342. {
  343. compileCommand = "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_FLAGS) ";
  344. compileCommand += extraCompileFlags;
  345. if(shared)
  346. {
  347. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  348. }
  349. compileCommand += "$(INCLUDE_FLAGS) -c ";
  350. compileCommand +=
  351. this->ConvertToOutputPath(source.GetFullPath().c_str());
  352. // Need to get the definition here because this value might have
  353. // trailing space (since it is directly prepended to the filename)
  354. std::string output_object_file_flag =
  355. m_Makefile->GetDefinition("CMAKE_C_OUTPUT_OBJECT_FILE_FLAG");
  356. m_Makefile->ExpandVariablesInString(output_object_file_flag);
  357. compileCommand += " " + output_object_file_flag;
  358. compileCommand += objectFile;
  359. }
  360. this->OutputMakeRule(fout,
  361. comment.c_str(),
  362. objectFile.c_str(),
  363. this->ConvertToOutputPath(
  364. source.GetFullPath().c_str()).c_str(),
  365. compileCommand.c_str());
  366. }
  367. void cmNMakeMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  368. const char* name,
  369. const cmTarget &t)
  370. {
  371. std::string target = m_LibraryOutputPath + name + m_SharedLibraryExtension;
  372. std::string depend = "$(";
  373. depend += this->CreateMakeVariable(name, "_SRC_OBJS");
  374. depend += ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  375. // Need to get the definition here because this value might have
  376. // trailing space (since it is directly prepended to the filename)
  377. std::string linker_output_file_flag =
  378. m_Makefile->GetDefinition("CMAKE_LINKER_OUTPUT_FILE_FLAG");
  379. m_Makefile->ExpandVariablesInString(linker_output_file_flag);
  380. std::string command = "$(CMAKE_LINKER) $(CMAKE_LINKER_SHARED_LIBRARY_FLAG)";
  381. bool hide_param = m_Makefile->IsOn("CMAKE_LINKER_HIDE_PARAMETERS");
  382. if (hide_param)
  383. {
  384. command += " @<<\n\t";
  385. }
  386. command += " $(CMAKE_LINKER_FLAGS) " + linker_output_file_flag;
  387. std::string dllpath = m_LibraryOutputPath + std::string(name) + m_SharedLibraryExtension;
  388. command += this->ConvertToOutputPath(dllpath.c_str());
  389. command += " $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  390. std::strstream linklibs;
  391. this->OutputLinkLibraries(linklibs, name, t);
  392. linklibs << std::ends;
  393. command += linklibs.str();
  394. delete [] linklibs.str();
  395. const std::vector<cmSourceFile>& sources = t.GetSourceFiles();
  396. for(std::vector<cmSourceFile>::const_iterator i = sources.begin();
  397. i != sources.end(); ++i)
  398. {
  399. if(i->GetSourceExtension() == "def")
  400. {
  401. command += "/DEF:";
  402. command += i->GetFullPath();
  403. }
  404. }
  405. command += "\n";
  406. if (hide_param)
  407. {
  408. command += "<<\n";
  409. }
  410. std::string customCommands = this->CreateTargetRules(t, name);
  411. const char* cc = 0;
  412. if(customCommands.size() > 0)
  413. {
  414. cc = customCommands.c_str();
  415. }
  416. this->OutputMakeRule(fout, "rules for a shared library",
  417. target.c_str(),
  418. depend.c_str(),
  419. command.c_str(), cc);
  420. }
  421. void cmNMakeMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  422. const char* name,
  423. const cmTarget &target)
  424. {
  425. this->OutputSharedLibraryRule(fout, name, target);
  426. }
  427. void cmNMakeMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  428. const char* name,
  429. const cmTarget &t)
  430. {
  431. std::string target = m_LibraryOutputPath + std::string(name) + m_StaticLibraryExtension;
  432. std::string depend = "$(";
  433. depend += this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  434. // Need to get the definition here because this value might have
  435. // trailing space (since it is directly prepended to the filename)
  436. std::string library_manager_output_file_flag =
  437. m_Makefile->GetDefinition("CMAKE_LIBRARY_MANAGER_OUTPUT_FILE_FLAG");
  438. m_Makefile->ExpandVariablesInString(library_manager_output_file_flag);
  439. std::string command = "$(CMAKE_LIBRARY_MANAGER) $(CMAKE_LIBRARY_MANAGER_FLAGS) @<<\n\t " + library_manager_output_file_flag;
  440. std::string libpath = m_LibraryOutputPath + std::string(name) + m_StaticLibraryExtension;
  441. command += this->ConvertToOutputPath(libpath.c_str());
  442. command += " $(";
  443. command += this->CreateMakeVariable(name, "_SRC_OBJS") + ")";
  444. command += "\n<<\n";
  445. std::string comment = "rule to build static library: ";
  446. comment += name;
  447. std::string customCommands = this->CreateTargetRules(t, name);
  448. const char* cc = 0;
  449. if(customCommands.size() > 0)
  450. {
  451. cc = customCommands.c_str();
  452. }
  453. this->OutputMakeRule(fout,
  454. comment.c_str(),
  455. target.c_str(),
  456. depend.c_str(),
  457. command.c_str(), cc);
  458. }
  459. void cmNMakeMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  460. const char* name,
  461. const cmTarget &t)
  462. {
  463. std::string target = m_ExecutableOutputPath + name;
  464. target += m_ExecutableExtension;
  465. std::string depend = "$(";
  466. depend += this->CreateMakeVariable(name, "_SRC_OBJS") + ") $(" +
  467. this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  468. std::string command =
  469. "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_FLAGS) ";
  470. command += "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  471. std::string path = m_ExecutableOutputPath + name + m_ExecutableExtension;
  472. // Need to get the definition here because this value might have
  473. // trailing space (since it is directly prepended to the filename)
  474. std::string output_executable_file_flag =
  475. m_Makefile->GetDefinition("CMAKE_C_OUTPUT_EXECUTABLE_FILE_FLAG");
  476. m_Makefile->ExpandVariablesInString(output_executable_file_flag);
  477. command += " " + output_executable_file_flag +
  478. this->ConvertToOutputPath(path.c_str());
  479. command += " $(CMAKE_C_LINK_EXECUTABLE_FLAG) ";
  480. if(t.GetType() == cmTarget::WIN32_EXECUTABLE)
  481. {
  482. command += " /subsystem:windows ";
  483. }
  484. std::strstream linklibs;
  485. this->OutputLinkLibraries(linklibs, 0, t);
  486. linklibs << std::ends;
  487. command += linklibs.str();
  488. std::string comment = "rule to build executable: ";
  489. comment += name;
  490. std::string customCommands = this->CreateTargetRules(t, name);
  491. const char* cc = 0;
  492. if(customCommands.size() > 0)
  493. {
  494. cc = customCommands.c_str();
  495. }
  496. this->OutputMakeRule(fout,
  497. comment.c_str(),
  498. target.c_str(),
  499. depend.c_str(),
  500. command.c_str(), cc);
  501. }
  502. void cmNMakeMakefileGenerator::OutputLinkLibraries(std::ostream& fout,
  503. const char* targetLibrary,
  504. const cmTarget &tgt)
  505. {
  506. // Try to emit each search path once
  507. std::set<std::string> emitted;
  508. // Embed runtime search paths if possible and if required.
  509. // collect all the flags needed for linking libraries
  510. // Do not try if there is no library path option (it is set to -L or
  511. // -LIBPATH for some linker, but some others do not even support link
  512. // search path).
  513. std::string linkLibs;
  514. // Expand content because this value might have
  515. // trailing space (since it is directly prepended to the filename)
  516. std::string lib_path_opt = m_LibraryPathOption;
  517. m_Makefile->ExpandVariablesInString(lib_path_opt);
  518. if (lib_path_opt.size())
  519. {
  520. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  521. for(std::vector<std::string>::iterator libDir = libdirs.begin();
  522. libDir != libdirs.end(); ++libDir)
  523. {
  524. std::string libpath = ShortPath(libDir->c_str());
  525. if(emitted.insert(libpath).second)
  526. {
  527. linkLibs += lib_path_opt;
  528. this->ConvertToOutputPath(libpath.c_str());
  529. linkLibs += libpath;
  530. linkLibs += " ";
  531. }
  532. }
  533. }
  534. std::string librariesLinked;
  535. const cmTarget::LinkLibraries& libs = tgt.GetLinkLibraries();
  536. for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
  537. lib != libs.end(); ++lib)
  538. {
  539. // Don't link the library against itself!
  540. if(targetLibrary && (lib->first == targetLibrary)) continue;
  541. // ** should fix this later, it should check to see if this is
  542. // a debug build and add the library
  543. // don't look at debug libraries
  544. // if (lib->second == cmTarget::DEBUG) continue;
  545. // skip zero size library entries, this may happen
  546. // if a variable expands to nothing.
  547. if (lib->first.size() == 0) continue;
  548. if(emitted.insert(lib->first).second)
  549. {
  550. std::string regexp = ".*\\";
  551. regexp += m_Makefile->GetDefinition("CMAKE_STATICLIB_SUFFIX");
  552. regexp += "$";
  553. cmRegularExpression reg(regexp.c_str());
  554. // if it ends in .lib, then it is a full path and should
  555. // be escaped, and does not need .lib added
  556. if(reg.find(lib->first))
  557. {
  558. librariesLinked += ShortPath(lib->first.c_str());
  559. librariesLinked += " ";
  560. }
  561. else
  562. {
  563. librariesLinked += m_LibraryLinkOption;
  564. librariesLinked += lib->first;
  565. librariesLinked += m_StaticLibraryExtension + " ";
  566. }
  567. }
  568. }
  569. linkLibs += librariesLinked;
  570. fout << linkLibs;
  571. fout << "$(CMAKE_STANDARD_WINDOWS_LIBRARIES) ";
  572. }
  573. std::string cmNMakeMakefileGenerator::GetOutputExtension(const char* s)
  574. {
  575. std::string sourceExtension = s;
  576. if(sourceExtension == "def")
  577. {
  578. return "";
  579. }
  580. if(sourceExtension == "ico" || sourceExtension == "rc2")
  581. {
  582. return "";
  583. }
  584. if(sourceExtension == "rc")
  585. {
  586. return ".res";
  587. }
  588. return m_ObjectFileExtension;
  589. }
  590. void cmNMakeMakefileGenerator::OutputIncludeMakefile(std::ostream& fout,
  591. const char* file)
  592. {
  593. fout << "!include " << file << "\n";
  594. }
  595. bool cmNMakeMakefileGenerator::SamePath(const char* path1, const char* path2)
  596. {
  597. // first check to see if they are the same anyway
  598. if (strcmp(path1, path2) == 0)
  599. {
  600. return true;
  601. }
  602. // next short path and lower case both of them for the compare
  603. return
  604. cmSystemTools::LowerCase(ShortPath(path1)) ==
  605. cmSystemTools::LowerCase(ShortPath(path2));
  606. }
  607. void cmNMakeMakefileGenerator::OutputBuildLibraryInDir(std::ostream& fout,
  608. const char* path,
  609. const char* ,
  610. const char* fullpath)
  611. {
  612. std::string currentDir =
  613. this->ConvertToOutputPath(m_Makefile->GetCurrentOutputDirectory());
  614. std::string wpath = this->ConvertToOutputPath(path);
  615. std::string wfullpath = this->ConvertToOutputPath(fullpath);
  616. fout << wfullpath
  617. << ":\n\tcd " << wpath << "\n"
  618. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.depends\n"
  619. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.check_depends\n"
  620. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends\n"
  621. << "\t$(MAKE) $(MAKESILENT) " << wfullpath
  622. << "\n\tcd " << currentDir << "\n";
  623. }
  624. std::string cmNMakeMakefileGenerator::ConvertToOutputPath(const char* s)
  625. {
  626. return cmSystemTools::ConvertToOutputPath(s);
  627. }