cmNMakeMakefileGenerator.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #include "cmNMakeMakefileGenerator.h"
  33. #include "cmMakefile.h"
  34. #include "cmStandardIncludes.h"
  35. #include "cmSystemTools.h"
  36. #include "cmSourceFile.h"
  37. #include "cmMakeDepend.h"
  38. #include "cmCacheManager.h"
  39. #include "cmGeneratedFileStream.h"
  40. #include "windows.h"
  41. cmNMakeMakefileGenerator::cmNMakeMakefileGenerator()
  42. {
  43. m_LibraryPathOption = "-LIBPATH:";
  44. this->SetObjectFileExtension(".obj");
  45. this->SetExecutableExtension(".exe");
  46. this->SetLibraryPrefix("");
  47. this->SetSharedLibraryExtension(".dll");
  48. this->SetStaticLibraryExtension(".lib");
  49. m_QuoteNextCommand = true; // most of the time command should be quoted
  50. }
  51. cmNMakeMakefileGenerator::~cmNMakeMakefileGenerator()
  52. {
  53. }
  54. // convert to windows short paths if there are spaces
  55. // in path
  56. std::string cmNMakeMakefileGenerator::ShortPath(const char* path)
  57. {
  58. std::string ret = path;
  59. // if there are no spaces in path, then just return path
  60. if(ret.find(' ') == std::string::npos)
  61. {
  62. return ret;
  63. }
  64. // if there are spaces then call GetShortPathName to get rid of them
  65. char *buffer = new char[strlen(path)+1];
  66. if(GetShortPathName(path, buffer,
  67. strlen(path)+1) != 0)
  68. {
  69. ret = buffer;
  70. }
  71. else
  72. {
  73. // if GetShortPathName failed for some reason use
  74. // EscapeSpaces instead
  75. ret = cmSystemTools::EscapeSpaces(path);
  76. }
  77. delete [] buffer;
  78. return ret;
  79. }
  80. // convert a command to a short path if it has spaces
  81. // this separates the arguments from the command and puts
  82. // them back together
  83. std::string cmNMakeMakefileGenerator::ShortPathCommand(const char* command)
  84. {
  85. if(!strchr(command, ' '))
  86. {
  87. return command;
  88. }
  89. cmRegularExpression reg("^\"([^\"]*)\"(.*)");
  90. if(reg.find(command))
  91. {
  92. std::string c = reg.match(1);
  93. cmRegularExpression removeIntDir("(.*)/\\$\\(IntDir\\)(.*)");
  94. if(removeIntDir.find(c))
  95. {
  96. c = removeIntDir.match(1) + removeIntDir.match(2);
  97. }
  98. c = ShortPath(c.c_str());
  99. std::string ret = c;
  100. std::string args = reg.match(2);
  101. ret += args;
  102. return ret;
  103. }
  104. return command;
  105. }
  106. void cmNMakeMakefileGenerator::ComputeSystemInfo()
  107. {
  108. // now load the settings
  109. if(!m_Makefile->GetDefinition("CMAKE_ROOT"))
  110. {
  111. cmSystemTools::Error(
  112. "CMAKE_ROOT has not been defined, bad GUI or driver program");
  113. return;
  114. }
  115. std::string fpath =
  116. m_Makefile->GetDefinition("CMAKE_ROOT");
  117. fpath += "/Templates/CMakeNMakeWindowsSystemConfig.cmake";
  118. m_Makefile->ReadListFile(NULL,fpath.c_str());
  119. }
  120. void cmNMakeMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  121. {
  122. fout << "# NMake Makefile generated by cmake\n";
  123. const char* variables =
  124. "# general varibles used in the makefile\n"
  125. "\n"
  126. "# Path to cmake\n"
  127. "CMAKE_COMMAND = ${CMAKE_COMMAND}\n"
  128. "CMAKE_STANDARD_WINDOWS_LIBRARIES = @CMAKE_STANDARD_WINDOWS_LIBRARIES@\n"
  129. "CMAKE_C_COMPILER = @CMAKE_C_COMPILER@ \n"
  130. "CMAKE_CFLAGS = @CMAKE_CFLAGS@ @BUILD_FLAGS@\n"
  131. "CMAKE_CXX_COMPILER = @CMAKE_CXX_COMPILER@\n"
  132. "CMAKE_CXXFLAGS = @CMAKE_CXX_FLAGS@ @BUILD_FLAGS@\n";
  133. std::string buildType = "CMAKE_CXX_FLAGS_";
  134. buildType += m_Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  135. buildType = cmSystemTools::UpperCase(buildType);
  136. m_Makefile->AddDefinition("BUILD_FLAGS",
  137. m_Makefile->GetDefinition(
  138. buildType.c_str()));
  139. std::string replaceVars = variables;
  140. m_Makefile->ExpandVariablesInString(replaceVars);
  141. fout << replaceVars.c_str();
  142. fout << "CMAKE_CURRENT_SOURCE = "
  143. << ShortPath(m_Makefile->GetStartDirectory() )
  144. << "\n";
  145. fout << "CMAKE_CURRENT_BINARY = "
  146. << ShortPath(m_Makefile->GetStartOutputDirectory())
  147. << "\n";
  148. fout << "CMAKE_SOURCE_DIR = "
  149. << ShortPath(m_Makefile->GetHomeDirectory()) << "\n";
  150. fout << "CMAKE_BINARY_DIR = "
  151. << ShortPath(m_Makefile->GetHomeOutputDirectory() )
  152. << "\n";
  153. // Output Include paths
  154. fout << "INCLUDE_FLAGS = ";
  155. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  156. std::vector<std::string>::iterator i;
  157. fout << "-I" << cmSystemTools::EscapeSpaces(m_Makefile->GetStartDirectory()) << " ";
  158. for(i = includes.begin(); i != includes.end(); ++i)
  159. {
  160. std::string include = *i;
  161. // Don't output a -I for the standard include path "/usr/include".
  162. // This can cause problems with certain standard library
  163. // implementations because the wrong headers may be found first.
  164. if(include != "/usr/include")
  165. {
  166. fout << "-I" << cmSystemTools::EscapeSpaces(i->c_str()).c_str() << " ";
  167. }
  168. }
  169. fout << m_Makefile->GetDefineFlags();
  170. fout << "\n\n";
  171. }
  172. void cmNMakeMakefileGenerator::BuildInSubDirectory(std::ostream& fout,
  173. const char* directory,
  174. const char* target1,
  175. const char* target2)
  176. {
  177. if(target1)
  178. {
  179. std::string dir = directory;
  180. cmSystemTools::ConvertToWindowsSlashes(dir);
  181. fout << "\tif not exist " << cmSystemTools::EscapeSpaces(dir.c_str())
  182. << " "
  183. << "$(MAKE) rebuild_cache\n"
  184. << "\tcd .\\" << directory << "\n"
  185. << "\t$(MAKE) -$(MAKEFLAGS) " << target1 << "\n";
  186. }
  187. if(target2)
  188. {
  189. fout << "\t$(MAKE) -$(MAKEFLAGS) " << target2 << "\n";
  190. }
  191. std::string currentDir = m_Makefile->GetCurrentOutputDirectory();
  192. cmSystemTools::ConvertToWindowsSlashes(currentDir);
  193. fout << "\tcd " << cmSystemTools::EscapeSpaces(currentDir.c_str()) << "\n";
  194. }
  195. // This needs to be overriden because nmake requires commands to be quoted
  196. // if the are full paths to the executable????
  197. void cmNMakeMakefileGenerator::OutputMakeRule(std::ostream& fout,
  198. const char* comment,
  199. const char* target,
  200. const char* depends,
  201. const char* command,
  202. const char* command2,
  203. const char* command3,
  204. const char* command4)
  205. {
  206. if(!target)
  207. {
  208. cmSystemTools::Error("no target for OutputMakeRule");
  209. return;
  210. }
  211. std::string replace;
  212. if(comment)
  213. {
  214. replace = comment;
  215. m_Makefile->ExpandVariablesInString(replace);
  216. fout << "#---------------------------------------------------------\n";
  217. fout << "# " << comment;
  218. fout << "\n#\n";
  219. }
  220. fout << "\n";
  221. replace = target;
  222. m_Makefile->ExpandVariablesInString(replace);
  223. replace = cmSystemTools::EscapeSpaces(replace.c_str());
  224. fout << replace.c_str() << ": ";
  225. if(depends)
  226. {
  227. replace = depends;
  228. m_Makefile->ExpandVariablesInString(replace);
  229. fout << replace.c_str();
  230. }
  231. fout << "\n";
  232. const char* startCommand = "\t\"";
  233. const char* endCommand = "\"\n";
  234. if(!m_QuoteNextCommand)
  235. {
  236. startCommand = "\t";
  237. endCommand = "\n";
  238. }
  239. if(command)
  240. {
  241. replace = ShortPathCommand(command);
  242. m_Makefile->ExpandVariablesInString(replace);
  243. fout << startCommand << replace.c_str() << endCommand;
  244. }
  245. if(command2)
  246. {
  247. replace = ShortPathCommand(command2);
  248. m_Makefile->ExpandVariablesInString(replace);
  249. fout << startCommand << replace.c_str() << endCommand;
  250. }
  251. if(command3)
  252. {
  253. replace = ShortPathCommand(command3);
  254. m_Makefile->ExpandVariablesInString(replace);
  255. fout << startCommand << replace.c_str() << endCommand;
  256. }
  257. if(command4)
  258. {
  259. replace = ShortPathCommand(command4);
  260. m_Makefile->ExpandVariablesInString(replace);
  261. fout << startCommand << replace.c_str() << endCommand;
  262. }
  263. fout << "\n";
  264. // reset m_QuoteNextCommand, as the default should be to quote the
  265. // commands. We need the quotes when the command has a full path
  266. // to an executable. However, the quotes break things like the
  267. // linker command.
  268. m_QuoteNextCommand = true;
  269. }
  270. void
  271. cmNMakeMakefileGenerator::
  272. OutputBuildObjectFromSource(std::ostream& fout,
  273. const char* shortName,
  274. const cmSourceFile& source,
  275. const char* extraCompileFlags,
  276. bool shared)
  277. {
  278. std::string comment = "Build ";
  279. std::string objectFile = std::string(shortName) +
  280. this->GetOutputExtension(source.GetSourceExtension().c_str());
  281. comment += objectFile + " From ";
  282. comment += source.GetFullPath();
  283. std::string compileCommand;
  284. std::string ext = source.GetSourceExtension();
  285. if(ext == "c" )
  286. {
  287. compileCommand = "$(CMAKE_C_COMPILER) $(CMAKE_CFLAGS) ";
  288. compileCommand += extraCompileFlags;
  289. if(shared)
  290. {
  291. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  292. }
  293. compileCommand += "$(INCLUDE_FLAGS) -c ";
  294. compileCommand +=
  295. cmSystemTools::EscapeSpaces(source.GetFullPath().c_str());
  296. compileCommand += " /Fo";
  297. compileCommand += objectFile;
  298. }
  299. else if (ext == "rc")
  300. {
  301. compileCommand = "$(RC) /fo\"";
  302. compileCommand += objectFile;
  303. compileCommand += "\" ";
  304. compileCommand +=
  305. cmSystemTools::EscapeSpaces(source.GetFullPath().c_str());
  306. }
  307. else if (ext == "def")
  308. {
  309. // no rule to output for this one
  310. return;
  311. }
  312. // assume c++ if not c rc or def
  313. else
  314. {
  315. compileCommand = "$(CMAKE_CXX_COMPILER) $(CMAKE_CXXFLAGS) ";
  316. compileCommand += extraCompileFlags;
  317. if(shared)
  318. {
  319. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  320. }
  321. compileCommand += "$(INCLUDE_FLAGS) -c ";
  322. compileCommand +=
  323. cmSystemTools::EscapeSpaces(source.GetFullPath().c_str());
  324. compileCommand += " /Fo";
  325. compileCommand += objectFile;
  326. }
  327. m_QuoteNextCommand = false;
  328. this->OutputMakeRule(fout,
  329. comment.c_str(),
  330. objectFile.c_str(),
  331. cmSystemTools::EscapeSpaces(
  332. source.GetFullPath().c_str()).c_str(),
  333. compileCommand.c_str());
  334. }
  335. void cmNMakeMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  336. const char* name,
  337. const cmTarget &t)
  338. {
  339. std::string target = m_LibraryOutputPath + name + ".dll";
  340. std::string depend = "$(";
  341. depend += name;
  342. depend += "_SRC_OBJS) $(" + std::string(name) + "_DEPEND_LIBS)";
  343. std::string command = "link /dll @<<\n";
  344. command += "$(" + std::string(name) + "_SRC_OBJS) /out:";
  345. std::string dllpath = m_LibraryOutputPath + std::string(name) + ".dll ";
  346. command += cmSystemTools::EscapeSpaces(dllpath.c_str());
  347. std::strstream linklibs;
  348. this->OutputLinkLibraries(linklibs, name, t);
  349. linklibs << std::ends;
  350. command += linklibs.str();
  351. delete [] linklibs.str();
  352. const std::vector<cmSourceFile>& sources = t.GetSourceFiles();
  353. for(std::vector<cmSourceFile>::const_iterator i = sources.begin();
  354. i != sources.end(); ++i)
  355. {
  356. if(i->GetSourceExtension() == "def")
  357. {
  358. command += "/DEF:";
  359. command += i->GetFullPath();
  360. }
  361. }
  362. command += "\n<<\n";
  363. m_QuoteNextCommand = false;
  364. this->OutputMakeRule(fout, "rules for a shared library",
  365. target.c_str(),
  366. depend.c_str(),
  367. command.c_str());
  368. }
  369. void cmNMakeMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  370. const char* name,
  371. const cmTarget &target)
  372. {
  373. this->OutputSharedLibraryRule(fout, name, target);
  374. }
  375. void cmNMakeMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  376. const char* name,
  377. const cmTarget &)
  378. {
  379. std::string target = m_LibraryOutputPath + std::string(name) + ".lib";
  380. std::string depend = "$(";
  381. depend += std::string(name) + "_SRC_OBJS)";
  382. std::string command = "link -lib @<<\n\t/nologo /out:";
  383. std::string libpath = m_LibraryOutputPath + std::string(name) + ".lib";
  384. command += cmSystemTools::EscapeSpaces(libpath.c_str());
  385. command += " $(";
  386. command += std::string(name) + "_SRC_OBJS)";
  387. command += "\n<<\n";
  388. std::string comment = "rule to build static library: ";
  389. comment += name;
  390. m_QuoteNextCommand = false;
  391. this->OutputMakeRule(fout,
  392. comment.c_str(),
  393. target.c_str(),
  394. depend.c_str(),
  395. command.c_str());
  396. }
  397. void cmNMakeMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  398. const char* name,
  399. const cmTarget &t)
  400. {
  401. std::string target = m_ExecutableOutputPath + name;
  402. target += ".exe";
  403. std::string depend = "$(";
  404. depend += std::string(name) + "_SRC_OBJS) $(" + std::string(name) + "_DEPEND_LIBS)";
  405. std::string command =
  406. "$(CMAKE_CXX_COMPILER) $(CMAKE_CXXFLAGS) ";
  407. command += "$(" + std::string(name) + "_SRC_OBJS) ";
  408. std::string path = m_ExecutableOutputPath + name + ".exe";
  409. command += " /Fe" +
  410. cmSystemTools::EscapeSpaces(path.c_str());
  411. command += " /link ";
  412. if(t.GetType() == cmTarget::WIN32_EXECUTABLE)
  413. {
  414. command += " /subsystem:windows ";
  415. }
  416. std::strstream linklibs;
  417. this->OutputLinkLibraries(linklibs, 0, t);
  418. linklibs << std::ends;
  419. command += linklibs.str();
  420. std::string comment = "rule to build executable: ";
  421. comment += name;
  422. m_QuoteNextCommand = false;
  423. this->OutputMakeRule(fout,
  424. comment.c_str(),
  425. target.c_str(),
  426. depend.c_str(),
  427. command.c_str());
  428. }
  429. void cmNMakeMakefileGenerator::OutputLinkLibraries(std::ostream& fout,
  430. const char* targetLibrary,
  431. const cmTarget &tgt)
  432. {
  433. // Try to emit each search path once
  434. std::set<std::string> emitted;
  435. // Embed runtime search paths if possible and if required.
  436. // collect all the flags needed for linking libraries
  437. std::string linkLibs;
  438. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  439. for(std::vector<std::string>::iterator libDir = libdirs.begin();
  440. libDir != libdirs.end(); ++libDir)
  441. {
  442. std::string libpath = ShortPath(libDir->c_str());
  443. if(emitted.insert(libpath).second)
  444. {
  445. linkLibs += m_LibraryPathOption;
  446. linkLibs += libpath;
  447. linkLibs += " ";
  448. }
  449. }
  450. std::string librariesLinked;
  451. const cmTarget::LinkLibraries& libs = tgt.GetLinkLibraries();
  452. for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
  453. lib != libs.end(); ++lib)
  454. {
  455. // Don't link the library against itself!
  456. if(targetLibrary && (lib->first == targetLibrary)) continue;
  457. // ** should fix this later, it should check to see if this is
  458. // a debug build and add the library
  459. // don't look at debug libraries
  460. // if (lib->second == cmTarget::DEBUG) continue;
  461. // skip zero size library entries, this may happen
  462. // if a variable expands to nothing.
  463. if (lib->first.size() == 0) continue;
  464. if(emitted.insert(lib->first).second)
  465. {
  466. cmRegularExpression reg(".*\\.lib$");
  467. // if it ends in .lib, then it is a full path and should
  468. // be escaped, and does not need .lib added
  469. if(reg.find(lib->first))
  470. {
  471. librariesLinked += ShortPath(lib->first.c_str());
  472. librariesLinked += " ";
  473. }
  474. else
  475. {
  476. librariesLinked += lib->first;
  477. librariesLinked += ".lib ";
  478. }
  479. }
  480. }
  481. linkLibs += librariesLinked;
  482. fout << linkLibs;
  483. fout << "$(CMAKE_STANDARD_WINDOWS_LIBRARIES) ";
  484. }
  485. std::string cmNMakeMakefileGenerator::GetOutputExtension(const char* s)
  486. {
  487. std::string sourceExtension = s;
  488. if(sourceExtension == "def")
  489. {
  490. return "";
  491. }
  492. if(sourceExtension == "ico" || sourceExtension == "rc2")
  493. {
  494. return "";
  495. }
  496. if(sourceExtension == "rc")
  497. {
  498. return ".res";
  499. }
  500. return ".obj";
  501. }
  502. void cmNMakeMakefileGenerator::OutputIncludeMakefile(std::ostream& fout,
  503. const char* file)
  504. {
  505. fout << "!include " << file << "\n";
  506. }
  507. bool cmNMakeMakefileGenerator::SamePath(const char* path1, const char* path2)
  508. {
  509. // first check to see if they are the same anyway
  510. if (strcmp(path1, path2) == 0)
  511. {
  512. return true;
  513. }
  514. // next short path and lower case both of them for the compare
  515. return
  516. cmSystemTools::LowerCase(ShortPath(path1)) ==
  517. cmSystemTools::LowerCase(ShortPath(path2));
  518. }
  519. void cmNMakeMakefileGenerator::OutputBuildLibraryInDir(std::ostream& fout,
  520. const char* path,
  521. const char* ,
  522. const char* fullpath)
  523. {
  524. std::string currentDir = m_Makefile->GetCurrentOutputDirectory();
  525. cmSystemTools::ConvertToWindowsSlashes(currentDir);
  526. fout << cmSystemTools::EscapeSpaces(fullpath)
  527. << ":\n\tcd " << cmSystemTools::EscapeSpaces(path)
  528. << "\n\t$(MAKE) " << cmSystemTools::EscapeSpaces(fullpath)
  529. << "\n\tcd " <<
  530. cmSystemTools::EscapeSpaces(currentDir.c_str()) << "\n";
  531. }