cmBorlandMakefileGenerator.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 "cmBorlandMakefileGenerator.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. #include <stdio.h>
  23. cmBorlandMakefileGenerator::cmBorlandMakefileGenerator()
  24. {
  25. this->SetLibraryPathOption("-L");
  26. this->SetLibraryLinkOption("");
  27. }
  28. cmBorlandMakefileGenerator::~cmBorlandMakefileGenerator()
  29. {
  30. }
  31. void cmBorlandMakefileGenerator::EnableLanguage(const char*)
  32. {
  33. // now load the settings
  34. if(!m_Makefile->GetDefinition("CMAKE_ROOT"))
  35. {
  36. cmSystemTools::Error(
  37. "CMAKE_ROOT has not been defined, bad GUI or driver program");
  38. return;
  39. }
  40. std::string outdir = m_Makefile->GetCurrentOutputDirectory();
  41. if(outdir.find('-') != std::string::npos)
  42. {
  43. std::string message = "The Borland command line tools do not support path names that have - in them. Please re-name your output directory and use _ instead of -.";
  44. message += "\nYour path currently is: ";
  45. message += outdir;
  46. cmSystemTools::Error(message.c_str());
  47. }
  48. if(!this->GetLanguageEnabled("CXX"))
  49. {
  50. std::string fpath =
  51. m_Makefile->GetDefinition("CMAKE_ROOT");
  52. fpath += "/Templates/CMakeBorlandWindowsSystemConfig.cmake";
  53. m_Makefile->ReadListFile(NULL,fpath.c_str());
  54. this->SetLanguageEnabled("CXX");
  55. }
  56. }
  57. void cmBorlandMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  58. {
  59. fout << "# NMake Makefile generated by cmake\n";
  60. const char* variables =
  61. "# general varibles used in the makefile\n"
  62. "\n"
  63. "CMAKE_STANDARD_WINDOWS_LIBRARIES = @CMAKE_STANDARD_WINDOWS_LIBRARIES@\n"
  64. "CMAKE_C_FLAGS = @CMAKE_C_FLAGS@ @BUILD_FLAGS@\n"
  65. "CMAKE_OBJECT_FILE_SUFFIX = @CMAKE_OBJECT_FILE_SUFFIX@\n"
  66. "CMAKE_EXECUTABLE_SUFFIX = @CMAKE_EXECUTABLE_SUFFIX@\n"
  67. "CMAKE_STATICLIB_SUFFIX = @CMAKE_STATICLIB_SUFFIX@\n"
  68. "CMAKE_SHLIB_SUFFIX = @CMAKE_SHLIB_SUFFIX@\n"
  69. "CMAKE_SHLIB_CFLAGS = -tWR\n"
  70. "CMAKE_LINKER_FLAGS = @CMAKE_LINKER_FLAGS@ @LINKER_BUILD_FLAGS@\n"
  71. "CMAKE_CXX_FLAGS = -P @CMAKE_CXX_FLAGS@ @BUILD_FLAGS@\n"
  72. "!IF \"$(OS)\" == \"Windows_NT\"\n"
  73. "NULL=\n"
  74. "!ELSE \n"
  75. "NULL=nul\n"
  76. "!ENDIF \n";
  77. std::string buildType = "CMAKE_CXX_FLAGS_";
  78. buildType += m_Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  79. buildType = cmSystemTools::UpperCase(buildType);
  80. m_Makefile->AddDefinition("BUILD_FLAGS",
  81. m_Makefile->GetDefinition(
  82. buildType.c_str()));
  83. buildType = "CMAKE_LINKER_FLAGS_";
  84. buildType += m_Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  85. buildType = cmSystemTools::UpperCase(buildType);
  86. m_Makefile->AddDefinition("LINKER_BUILD_FLAGS",
  87. m_Makefile->GetDefinition(
  88. buildType.c_str()));
  89. std::string replaceVars = variables;
  90. m_Makefile->ExpandVariablesInString(replaceVars);
  91. std::string ccompiler = m_Makefile->GetDefinition("CMAKE_C_COMPILER");
  92. fout << "CMAKE_C_COMPILER = "
  93. << this->ConvertToOutputPath(ccompiler.c_str())
  94. << "\n";
  95. std::string cxxcompiler = m_Makefile->GetDefinition("CMAKE_CXX_COMPILER");
  96. fout << "CMAKE_CXX_COMPILER = "
  97. << this->ConvertToOutputPath(cxxcompiler.c_str())
  98. << "\n";
  99. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  100. {
  101. fout << "CMAKE_EDIT_COMMAND = "
  102. << this->ShortPath(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  103. << "\n";
  104. }
  105. std::string cmakecommand = m_Makefile->GetDefinition("CMAKE_COMMAND");
  106. fout << "CMAKE_COMMAND = "
  107. << this->ShortPath(cmakecommand.c_str()) << "\n";
  108. fout << "RM = " << this->ShortPath(cmakecommand.c_str()) << " -E remove -f\n";
  109. fout << replaceVars.c_str();
  110. fout << "CMAKE_CURRENT_SOURCE = "
  111. << ShortPath(m_Makefile->GetStartDirectory() )
  112. << "\n";
  113. fout << "CMAKE_CURRENT_BINARY = "
  114. << ShortPath(m_Makefile->GetStartOutputDirectory())
  115. << "\n";
  116. fout << "CMAKE_SOURCE_DIR = "
  117. << ShortPath(m_Makefile->GetHomeDirectory()) << "\n";
  118. fout << "CMAKE_BINARY_DIR = "
  119. << ShortPath(m_Makefile->GetHomeOutputDirectory() )
  120. << "\n";
  121. // Output Include paths
  122. fout << "INCLUDE_FLAGS = ";
  123. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  124. std::vector<std::string>::iterator i;
  125. fout << "-I" <<
  126. this->ShortPath(m_Makefile->GetStartDirectory()) << " ";
  127. for(i = includes.begin(); i != includes.end(); ++i)
  128. {
  129. std::string include = *i;
  130. // Don't output a -I for the standard include path "/usr/include".
  131. // This can cause problems with certain standard library
  132. // implementations because the wrong headers may be found first.
  133. fout << "-I" << this->ConvertToOutputPath(i->c_str()).c_str() << " ";
  134. }
  135. fout << m_Makefile->GetDefineFlags();
  136. fout << "\n\n";
  137. }
  138. void
  139. cmBorlandMakefileGenerator::
  140. OutputBuildObjectFromSource(std::ostream& fout,
  141. const char* shortName,
  142. const cmSourceFile& source,
  143. const char* extraCompileFlags,
  144. bool shared)
  145. {
  146. // force shared flag if building shared libraries
  147. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  148. {
  149. shared = true;
  150. }
  151. // Header files shouldn't have build rules.
  152. if(source.IsAHeaderFileOnly())
  153. {
  154. return;
  155. }
  156. std::string comment = "Build ";
  157. std::string objectFile = std::string(shortName) +
  158. this->GetOutputExtension(source.GetSourceExtension().c_str());
  159. objectFile = this->ConvertToOutputPath(objectFile.c_str());
  160. comment += objectFile + " From ";
  161. comment += source.GetFullPath();
  162. std::string compileCommand;
  163. std::string ext = source.GetSourceExtension();
  164. if(ext == "c" )
  165. {
  166. compileCommand = "$(CMAKE_C_COMPILER) $(CMAKE_C_FLAGS) ";
  167. compileCommand += extraCompileFlags;
  168. if(shared)
  169. {
  170. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  171. }
  172. compileCommand += " -o";
  173. compileCommand += objectFile;
  174. compileCommand += " $(INCLUDE_FLAGS) -c ";
  175. compileCommand +=
  176. this->ConvertToOutputPath(source.GetFullPath().c_str());
  177. }
  178. else if (ext == "rc")
  179. {
  180. compileCommand = "$(RC) -o\"";
  181. compileCommand += objectFile;
  182. compileCommand += "\" ";
  183. compileCommand +=
  184. this->ConvertToOutputPath(source.GetFullPath().c_str());
  185. }
  186. else if (ext == "def")
  187. {
  188. // no rule to output for this one
  189. return;
  190. }
  191. // assume c++ if not c rc or def
  192. else
  193. {
  194. compileCommand = "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_FLAGS) ";
  195. compileCommand += extraCompileFlags;
  196. if(shared)
  197. {
  198. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  199. }
  200. compileCommand += " -o";
  201. compileCommand += objectFile;
  202. compileCommand += " $(INCLUDE_FLAGS) -c ";
  203. compileCommand +=
  204. this->ConvertToOutputPath(source.GetFullPath().c_str());
  205. }
  206. this->OutputMakeRule(fout,
  207. comment.c_str(),
  208. objectFile.c_str(),
  209. this->ConvertToOutputPath(
  210. source.GetFullPath().c_str()).c_str(),
  211. compileCommand.c_str());
  212. }
  213. void cmBorlandMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  214. const char* name,
  215. const cmTarget &t)
  216. {
  217. std::string target = m_LibraryOutputPath + name;
  218. std::string libpath = target + ".lib";
  219. target += ".dll";
  220. target = this->ConvertToOutputPath(target.c_str());
  221. libpath = this->ConvertToOutputPath(libpath.c_str());
  222. std::string depend = "$(";
  223. depend += this->CreateMakeVariable(name, "_SRC_OBJS");
  224. depend += ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  225. std::string command = "$(CMAKE_CXX_COMPILER) -tWD $(CMAKE_SHLIB_CFLAGS) $(CMAKE_LINKER_FLAGS) @&&|\n";
  226. // must be executable name
  227. command += "-e";
  228. command += target;
  229. command += " ";
  230. std::strstream linklibs;
  231. this->OutputLinkLibraries(linklibs, name, t);
  232. linklibs << std::ends;
  233. // then the linker options -L and libraries (any other order will fail!)
  234. command += linklibs.str();
  235. delete [] linklibs.str();
  236. // then list of object files
  237. command += " $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  238. std::string command2 = "implib -w ";
  239. command2 += libpath + " " + target;
  240. const std::vector<cmSourceFile*>& sources = t.GetSourceFiles();
  241. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  242. i != sources.end(); ++i)
  243. {
  244. if((*i)->GetSourceExtension() == "def")
  245. {
  246. command += "";
  247. command += (*i)->GetFullPath();
  248. }
  249. }
  250. command += "\n|\n";
  251. std::string customCommands = this->CreateTargetRules(t, name);
  252. const char* cc = 0;
  253. if(customCommands.size() > 0)
  254. {
  255. cc = customCommands.c_str();
  256. }
  257. this->OutputMakeRule(fout, "rules for a shared library",
  258. target.c_str(),
  259. depend.c_str(),
  260. command.c_str(),
  261. command2.c_str(),
  262. cc);
  263. }
  264. void cmBorlandMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  265. const char* name,
  266. const cmTarget &target)
  267. {
  268. this->OutputSharedLibraryRule(fout, name, target);
  269. }
  270. void cmBorlandMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  271. const char* name,
  272. const cmTarget &t)
  273. {
  274. std::string target = m_LibraryOutputPath + std::string(name) + ".lib";
  275. target = this->ConvertToOutputPath(target.c_str());
  276. std::string depend = "$(";
  277. depend += this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  278. std::string command = "tlib @&&|\n\t /p512 /a ";
  279. command += target;
  280. command += " ";
  281. std::string deleteCommand = "if exist ";
  282. deleteCommand += target;
  283. deleteCommand += " del ";
  284. deleteCommand += target;
  285. command += " $(";
  286. command += this->CreateMakeVariable(name, "_SRC_OBJS_QUOTED") + ")";
  287. command += "\n|\n";
  288. std::string comment = "rule to build static library: ";
  289. comment += name;
  290. std::string customCommands = this->CreateTargetRules(t, name);
  291. const char* cc = 0;
  292. if(customCommands.size() > 0)
  293. {
  294. cc = customCommands.c_str();
  295. }
  296. this->OutputMakeRule(fout,
  297. comment.c_str(),
  298. target.c_str(),
  299. depend.c_str(),
  300. deleteCommand.c_str(),
  301. command.c_str(), cc);
  302. }
  303. void cmBorlandMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  304. const char* name,
  305. const cmTarget &t)
  306. {
  307. std::string target = m_ExecutableOutputPath + name + m_ExecutableExtension;
  308. target = this->ConvertToOutputPath(target.c_str());
  309. std::string depend = "$(";
  310. depend += this->CreateMakeVariable(name, "_SRC_OBJS") + ") $(" +
  311. this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  312. std::string command =
  313. "$(CMAKE_CXX_COMPILER) ";
  314. command += " $(CMAKE_LINKER_FLAGS) -e" + target;
  315. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  316. {
  317. command += " $(CMAKE_SHLIB_CFLAGS) ";
  318. }
  319. if(t.GetType() == cmTarget::WIN32_EXECUTABLE)
  320. {
  321. command += " -tWM ";
  322. }
  323. else
  324. {
  325. command += " -tWC ";
  326. }
  327. std::strstream linklibs;
  328. this->OutputLinkLibraries(linklibs, 0, t);
  329. linklibs << std::ends;
  330. command += linklibs.str();
  331. delete [] linklibs.str();
  332. command += " $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ")";
  333. std::string comment = "rule to build executable: ";
  334. comment += name;
  335. std::string customCommands = this->CreateTargetRules(t, name);
  336. const char* cc = 0;
  337. if(customCommands.size() > 0)
  338. {
  339. cc = customCommands.c_str();
  340. }
  341. this->OutputMakeRule(fout,
  342. comment.c_str(),
  343. target.c_str(),
  344. depend.c_str(),
  345. command.c_str(), cc);
  346. }
  347. std::string cmBorlandMakefileGenerator::GetOutputExtension(const char* s)
  348. {
  349. std::string sourceExtension = s;
  350. if(sourceExtension == "def")
  351. {
  352. return "";
  353. }
  354. if(sourceExtension == "ico" || sourceExtension == "rc2")
  355. {
  356. return "";
  357. }
  358. if(sourceExtension == "rc")
  359. {
  360. return ".res";
  361. }
  362. return ".obj";
  363. }
  364. bool cmBorlandMakefileGenerator::SamePath(const char* path1, const char* path2)
  365. {
  366. // first check to see if they are the same anyway
  367. if (strcmp(path1, path2) == 0)
  368. {
  369. return true;
  370. }
  371. // next short path and lower case both of them for the compare
  372. return
  373. cmSystemTools::LowerCase(ShortPath(path1)) ==
  374. cmSystemTools::LowerCase(ShortPath(path2));
  375. }
  376. // borland make does not support variables that are longer than 32
  377. // so use this function to rename any long ones
  378. std::string cmBorlandMakefileGenerator::CreateMakeVariable(const char* s, const char* s2)
  379. {
  380. std::string unmodified = s;
  381. unmodified += s2;
  382. // see if th
  383. std::map<cmStdString, cmStdString>::iterator i = m_MakeVariableMap.find(unmodified);
  384. if(i != m_MakeVariableMap.end())
  385. {
  386. return i->second;
  387. }
  388. std::string ret = unmodified;
  389. // if the string is greater the 32 chars it is an invalid vairable name
  390. // for borland make
  391. if(ret.size() > 32)
  392. {
  393. std::string str1 = s;
  394. std::string str2 = s2;
  395. // we must shorten the combined string by 4 charactors
  396. // keep no more than 24 charactors from the second string
  397. if(str2.size() > 24)
  398. {
  399. str2 = str2.substr(0, 24);
  400. }
  401. if(str1.size() + str2.size() > 27)
  402. {
  403. str1 = str1.substr(0, 27 - str2.size());
  404. }
  405. char buffer[5];
  406. int i = 0;
  407. sprintf(buffer, "%04d", i);
  408. ret = str1 + str2 + buffer;
  409. while(m_ShortMakeVariableMap.count(ret) && i < 1000)
  410. {
  411. ++i;
  412. sprintf(buffer, "%04d", i);
  413. ret = str1 + str2 + buffer;
  414. }
  415. if(i == 1000)
  416. {
  417. cmSystemTools::Error("Borland makefile varible length too long");
  418. return unmodified;
  419. }
  420. // once an unused variable is found
  421. m_ShortMakeVariableMap[ret] = "1";
  422. }
  423. // always make an entry into the unmodified to varible map
  424. m_MakeVariableMap[unmodified] = ret;
  425. return ret;
  426. }