cmBorlandMakefileGenerator.cxx 15 KB

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