cmRulePlaceholderExpander.cxx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmRulePlaceholderExpander.h"
  4. #include <ctype.h>
  5. #include <string.h>
  6. #include <utility>
  7. #include "cmOutputConverter.h"
  8. #include "cmSystemTools.h"
  9. cmRulePlaceholderExpander::cmRulePlaceholderExpander(
  10. std::map<std::string, std::string> const& compilers,
  11. std::map<std::string, std::string> const& variableMappings,
  12. std::string const& compilerSysroot, std::string const& linkerSysroot)
  13. : Compilers(compilers)
  14. , VariableMappings(variableMappings)
  15. , CompilerSysroot(compilerSysroot)
  16. , LinkerSysroot(linkerSysroot)
  17. {
  18. }
  19. cmRulePlaceholderExpander::RuleVariables::RuleVariables()
  20. {
  21. memset(this, 0, sizeof(*this));
  22. }
  23. std::string cmRulePlaceholderExpander::ExpandRuleVariable(
  24. cmOutputConverter* outputConverter, std::string const& variable,
  25. const RuleVariables& replaceValues)
  26. {
  27. if (replaceValues.LinkFlags) {
  28. if (variable == "LINK_FLAGS") {
  29. return replaceValues.LinkFlags;
  30. }
  31. }
  32. if (replaceValues.Manifests) {
  33. if (variable == "MANIFESTS") {
  34. return replaceValues.Manifests;
  35. }
  36. }
  37. if (replaceValues.Flags) {
  38. if (variable == "FLAGS") {
  39. return replaceValues.Flags;
  40. }
  41. }
  42. if (replaceValues.Source) {
  43. if (variable == "SOURCE") {
  44. return replaceValues.Source;
  45. }
  46. }
  47. if (replaceValues.PreprocessedSource) {
  48. if (variable == "PREPROCESSED_SOURCE") {
  49. return replaceValues.PreprocessedSource;
  50. }
  51. }
  52. if (replaceValues.AssemblySource) {
  53. if (variable == "ASSEMBLY_SOURCE") {
  54. return replaceValues.AssemblySource;
  55. }
  56. }
  57. if (replaceValues.Object) {
  58. if (variable == "OBJECT") {
  59. return replaceValues.Object;
  60. }
  61. }
  62. if (replaceValues.ObjectDir) {
  63. if (variable == "OBJECT_DIR") {
  64. return replaceValues.ObjectDir;
  65. }
  66. }
  67. if (replaceValues.ObjectFileDir) {
  68. if (variable == "OBJECT_FILE_DIR") {
  69. return replaceValues.ObjectFileDir;
  70. }
  71. }
  72. if (replaceValues.Objects) {
  73. if (variable == "OBJECTS") {
  74. return replaceValues.Objects;
  75. }
  76. }
  77. if (replaceValues.ObjectsQuoted) {
  78. if (variable == "OBJECTS_QUOTED") {
  79. return replaceValues.ObjectsQuoted;
  80. }
  81. }
  82. if (replaceValues.Defines && variable == "DEFINES") {
  83. return replaceValues.Defines;
  84. }
  85. if (replaceValues.Includes && variable == "INCLUDES") {
  86. return replaceValues.Includes;
  87. }
  88. if (replaceValues.TargetPDB) {
  89. if (variable == "TARGET_PDB") {
  90. return replaceValues.TargetPDB;
  91. }
  92. }
  93. if (replaceValues.TargetCompilePDB) {
  94. if (variable == "TARGET_COMPILE_PDB") {
  95. return replaceValues.TargetCompilePDB;
  96. }
  97. }
  98. if (replaceValues.DependencyFile) {
  99. if (variable == "DEP_FILE") {
  100. return replaceValues.DependencyFile;
  101. }
  102. }
  103. if (replaceValues.Target) {
  104. if (variable == "TARGET_QUOTED") {
  105. std::string targetQuoted = replaceValues.Target;
  106. if (!targetQuoted.empty() && targetQuoted[0] != '\"') {
  107. targetQuoted = '\"';
  108. targetQuoted += replaceValues.Target;
  109. targetQuoted += '\"';
  110. }
  111. return targetQuoted;
  112. }
  113. if (variable == "TARGET_UNQUOTED") {
  114. std::string unquoted = replaceValues.Target;
  115. std::string::size_type sz = unquoted.size();
  116. if (sz > 2 && unquoted[0] == '\"' && unquoted[sz - 1] == '\"') {
  117. unquoted = unquoted.substr(1, sz - 2);
  118. }
  119. return unquoted;
  120. }
  121. if (replaceValues.LanguageCompileFlags) {
  122. if (variable == "LANGUAGE_COMPILE_FLAGS") {
  123. return replaceValues.LanguageCompileFlags;
  124. }
  125. }
  126. if (replaceValues.Target) {
  127. if (variable == "TARGET") {
  128. return replaceValues.Target;
  129. }
  130. }
  131. if (variable == "TARGET_IMPLIB") {
  132. return this->TargetImpLib;
  133. }
  134. if (variable == "TARGET_VERSION_MAJOR") {
  135. if (replaceValues.TargetVersionMajor) {
  136. return replaceValues.TargetVersionMajor;
  137. }
  138. return "0";
  139. }
  140. if (variable == "TARGET_VERSION_MINOR") {
  141. if (replaceValues.TargetVersionMinor) {
  142. return replaceValues.TargetVersionMinor;
  143. }
  144. return "0";
  145. }
  146. if (replaceValues.Target) {
  147. if (variable == "TARGET_BASE") {
  148. // Strip the last extension off the target name.
  149. std::string targetBase = replaceValues.Target;
  150. std::string::size_type pos = targetBase.rfind('.');
  151. if (pos != std::string::npos) {
  152. return targetBase.substr(0, pos);
  153. }
  154. return targetBase;
  155. }
  156. }
  157. }
  158. if (variable == "TARGET_SONAME" || variable == "SONAME_FLAG" ||
  159. variable == "TARGET_INSTALLNAME_DIR") {
  160. // All these variables depend on TargetSOName
  161. if (replaceValues.TargetSOName) {
  162. if (variable == "TARGET_SONAME") {
  163. return replaceValues.TargetSOName;
  164. }
  165. if (variable == "SONAME_FLAG" && replaceValues.SONameFlag) {
  166. return replaceValues.SONameFlag;
  167. }
  168. if (replaceValues.TargetInstallNameDir &&
  169. variable == "TARGET_INSTALLNAME_DIR") {
  170. return replaceValues.TargetInstallNameDir;
  171. }
  172. }
  173. return "";
  174. }
  175. if (replaceValues.LinkLibraries) {
  176. if (variable == "LINK_LIBRARIES") {
  177. return replaceValues.LinkLibraries;
  178. }
  179. }
  180. if (replaceValues.Language) {
  181. if (variable == "LANGUAGE") {
  182. return replaceValues.Language;
  183. }
  184. }
  185. if (replaceValues.CMTargetName) {
  186. if (variable == "TARGET_NAME") {
  187. return replaceValues.CMTargetName;
  188. }
  189. }
  190. if (replaceValues.CMTargetType) {
  191. if (variable == "TARGET_TYPE") {
  192. return replaceValues.CMTargetType;
  193. }
  194. }
  195. if (replaceValues.Output) {
  196. if (variable == "OUTPUT") {
  197. return replaceValues.Output;
  198. }
  199. }
  200. if (variable == "CMAKE_COMMAND") {
  201. return outputConverter->ConvertToOutputFormat(
  202. cmSystemTools::CollapseFullPath(cmSystemTools::GetCMakeCommand()),
  203. cmOutputConverter::SHELL);
  204. }
  205. std::map<std::string, std::string>::iterator compIt =
  206. this->Compilers.find(variable);
  207. if (compIt != this->Compilers.end()) {
  208. std::string ret = outputConverter->ConvertToOutputForExisting(
  209. this->VariableMappings["CMAKE_" + compIt->second + "_COMPILER"]);
  210. std::string const& compilerArg1 =
  211. this->VariableMappings["CMAKE_" + compIt->second + "_COMPILER_ARG1"];
  212. std::string const& compilerTarget =
  213. this->VariableMappings["CMAKE_" + compIt->second + "_COMPILER_TARGET"];
  214. std::string const& compilerOptionTarget =
  215. this->VariableMappings["CMAKE_" + compIt->second +
  216. "_COMPILE_OPTIONS_TARGET"];
  217. std::string const& compilerExternalToolchain =
  218. this->VariableMappings["CMAKE_" + compIt->second +
  219. "_COMPILER_EXTERNAL_TOOLCHAIN"];
  220. std::string const& compilerOptionExternalToolchain =
  221. this->VariableMappings["CMAKE_" + compIt->second +
  222. "_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN"];
  223. std::string const& compilerOptionSysroot =
  224. this->VariableMappings["CMAKE_" + compIt->second +
  225. "_COMPILE_OPTIONS_SYSROOT"];
  226. // if there is a required first argument to the compiler add it
  227. // to the compiler string
  228. if (!compilerArg1.empty()) {
  229. ret += " ";
  230. ret += compilerArg1;
  231. }
  232. if (!compilerTarget.empty() && !compilerOptionTarget.empty()) {
  233. ret += " ";
  234. ret += compilerOptionTarget;
  235. ret += compilerTarget;
  236. }
  237. if (!compilerExternalToolchain.empty() &&
  238. !compilerOptionExternalToolchain.empty()) {
  239. ret += " ";
  240. ret += compilerOptionExternalToolchain;
  241. ret += outputConverter->EscapeForShell(compilerExternalToolchain, true);
  242. }
  243. std::string sysroot;
  244. // Some platforms may use separate sysroots for compiling and linking.
  245. // If we detect link flags, then we pass the link sysroot instead.
  246. // FIXME: Use a more robust way to detect link line expansion.
  247. if (replaceValues.LinkFlags) {
  248. sysroot = this->LinkerSysroot;
  249. } else {
  250. sysroot = this->CompilerSysroot;
  251. }
  252. if (!sysroot.empty() && !compilerOptionSysroot.empty()) {
  253. ret += " ";
  254. ret += compilerOptionSysroot;
  255. ret += outputConverter->EscapeForShell(sysroot, true);
  256. }
  257. return ret;
  258. }
  259. std::map<std::string, std::string>::iterator mapIt =
  260. this->VariableMappings.find(variable);
  261. if (mapIt != this->VariableMappings.end()) {
  262. if (variable.find("_FLAG") == std::string::npos) {
  263. return outputConverter->ConvertToOutputForExisting(mapIt->second);
  264. }
  265. return mapIt->second;
  266. }
  267. return variable;
  268. }
  269. void cmRulePlaceholderExpander::ExpandRuleVariables(
  270. cmOutputConverter* outputConverter, std::string& s,
  271. const RuleVariables& replaceValues)
  272. {
  273. std::string::size_type start = s.find('<');
  274. // no variables to expand
  275. if (start == std::string::npos) {
  276. return;
  277. }
  278. std::string::size_type pos = 0;
  279. std::string expandedInput;
  280. while (start != std::string::npos && start < s.size() - 2) {
  281. std::string::size_type end = s.find('>', start);
  282. // if we find a < with no > we are done
  283. if (end == std::string::npos) {
  284. return;
  285. }
  286. char c = s[start + 1];
  287. // if the next char after the < is not A-Za-z then
  288. // skip it and try to find the next < in the string
  289. if (!isalpha(c)) {
  290. start = s.find('<', start + 1);
  291. } else {
  292. // extract the var
  293. std::string var = s.substr(start + 1, end - start - 1);
  294. std::string replace =
  295. this->ExpandRuleVariable(outputConverter, var, replaceValues);
  296. expandedInput += s.substr(pos, start - pos);
  297. expandedInput += replace;
  298. // move to next one
  299. start = s.find('<', start + var.size() + 2);
  300. pos = end + 1;
  301. }
  302. }
  303. // add the rest of the input
  304. expandedInput += s.substr(pos, s.size() - pos);
  305. s = expandedInput;
  306. }