1
0

cmNMakeMakefileGenerator.cxx 24 KB

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