cmRulePlaceholderExpander.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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> compilers,
  11. std::map<std::string, std::string> variableMappings,
  12. std::string compilerSysroot, std::string linkerSysroot)
  13. : Compilers(std::move(compilers))
  14. , VariableMappings(std::move(variableMappings))
  15. , CompilerSysroot(std::move(compilerSysroot))
  16. , LinkerSysroot(std::move(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.SwiftLibraryName) {
  89. if (variable == "SWIFT_LIBRARY_NAME") {
  90. return replaceValues.SwiftLibraryName;
  91. }
  92. }
  93. if (replaceValues.SwiftModule) {
  94. if (variable == "SWIFT_MODULE") {
  95. return replaceValues.SwiftModule;
  96. }
  97. }
  98. if (replaceValues.SwiftModuleName) {
  99. if (variable == "SWIFT_MODULE_NAME") {
  100. return replaceValues.SwiftModuleName;
  101. }
  102. }
  103. if (replaceValues.SwiftOutputFileMap) {
  104. if (variable == "SWIFT_OUTPUT_FILE_MAP") {
  105. return replaceValues.SwiftOutputFileMap;
  106. }
  107. }
  108. if (replaceValues.SwiftSources) {
  109. if (variable == "SWIFT_SOURCES") {
  110. return replaceValues.SwiftSources;
  111. }
  112. }
  113. if (replaceValues.TargetPDB) {
  114. if (variable == "TARGET_PDB") {
  115. return replaceValues.TargetPDB;
  116. }
  117. }
  118. if (replaceValues.TargetCompilePDB) {
  119. if (variable == "TARGET_COMPILE_PDB") {
  120. return replaceValues.TargetCompilePDB;
  121. }
  122. }
  123. if (replaceValues.DependencyFile) {
  124. if (variable == "DEP_FILE") {
  125. return replaceValues.DependencyFile;
  126. }
  127. }
  128. if (replaceValues.Target) {
  129. if (variable == "TARGET_QUOTED") {
  130. std::string targetQuoted = replaceValues.Target;
  131. if (!targetQuoted.empty() && targetQuoted.front() != '\"') {
  132. targetQuoted = '\"';
  133. targetQuoted += replaceValues.Target;
  134. targetQuoted += '\"';
  135. }
  136. return targetQuoted;
  137. }
  138. if (variable == "TARGET_UNQUOTED") {
  139. std::string unquoted = replaceValues.Target;
  140. std::string::size_type sz = unquoted.size();
  141. if (sz > 2 && unquoted.front() == '\"' && unquoted.back() == '\"') {
  142. unquoted = unquoted.substr(1, sz - 2);
  143. }
  144. return unquoted;
  145. }
  146. if (replaceValues.LanguageCompileFlags) {
  147. if (variable == "LANGUAGE_COMPILE_FLAGS") {
  148. return replaceValues.LanguageCompileFlags;
  149. }
  150. }
  151. if (replaceValues.Target) {
  152. if (variable == "TARGET") {
  153. return replaceValues.Target;
  154. }
  155. }
  156. if (variable == "TARGET_IMPLIB") {
  157. return this->TargetImpLib;
  158. }
  159. if (variable == "TARGET_VERSION_MAJOR") {
  160. if (replaceValues.TargetVersionMajor) {
  161. return replaceValues.TargetVersionMajor;
  162. }
  163. return "0";
  164. }
  165. if (variable == "TARGET_VERSION_MINOR") {
  166. if (replaceValues.TargetVersionMinor) {
  167. return replaceValues.TargetVersionMinor;
  168. }
  169. return "0";
  170. }
  171. if (replaceValues.Target) {
  172. if (variable == "TARGET_BASE") {
  173. // Strip the last extension off the target name.
  174. std::string targetBase = replaceValues.Target;
  175. std::string::size_type pos = targetBase.rfind('.');
  176. if (pos != std::string::npos) {
  177. return targetBase.substr(0, pos);
  178. }
  179. return targetBase;
  180. }
  181. }
  182. }
  183. if (variable == "TARGET_SONAME" || variable == "SONAME_FLAG" ||
  184. variable == "TARGET_INSTALLNAME_DIR") {
  185. // All these variables depend on TargetSOName
  186. if (replaceValues.TargetSOName) {
  187. if (variable == "TARGET_SONAME") {
  188. return replaceValues.TargetSOName;
  189. }
  190. if (variable == "SONAME_FLAG" && replaceValues.SONameFlag) {
  191. return replaceValues.SONameFlag;
  192. }
  193. if (replaceValues.TargetInstallNameDir &&
  194. variable == "TARGET_INSTALLNAME_DIR") {
  195. return replaceValues.TargetInstallNameDir;
  196. }
  197. }
  198. return "";
  199. }
  200. if (replaceValues.LinkLibraries) {
  201. if (variable == "LINK_LIBRARIES") {
  202. return replaceValues.LinkLibraries;
  203. }
  204. }
  205. if (replaceValues.Language) {
  206. if (variable == "LANGUAGE") {
  207. return replaceValues.Language;
  208. }
  209. }
  210. if (replaceValues.CMTargetName) {
  211. if (variable == "TARGET_NAME") {
  212. return replaceValues.CMTargetName;
  213. }
  214. }
  215. if (replaceValues.CMTargetType) {
  216. if (variable == "TARGET_TYPE") {
  217. return replaceValues.CMTargetType;
  218. }
  219. }
  220. if (replaceValues.Output) {
  221. if (variable == "OUTPUT") {
  222. return replaceValues.Output;
  223. }
  224. }
  225. if (variable == "CMAKE_COMMAND") {
  226. return outputConverter->ConvertToOutputFormat(
  227. cmSystemTools::CollapseFullPath(cmSystemTools::GetCMakeCommand()),
  228. cmOutputConverter::SHELL);
  229. }
  230. auto compIt = this->Compilers.find(variable);
  231. if (compIt != this->Compilers.end()) {
  232. std::string ret = outputConverter->ConvertToOutputForExisting(
  233. this->VariableMappings["CMAKE_" + compIt->second + "_COMPILER"]);
  234. std::string const& compilerArg1 =
  235. this->VariableMappings["CMAKE_" + compIt->second + "_COMPILER_ARG1"];
  236. std::string const& compilerTarget =
  237. this->VariableMappings["CMAKE_" + compIt->second + "_COMPILER_TARGET"];
  238. std::string const& compilerOptionTarget =
  239. this->VariableMappings["CMAKE_" + compIt->second +
  240. "_COMPILE_OPTIONS_TARGET"];
  241. std::string const& compilerExternalToolchain =
  242. this->VariableMappings["CMAKE_" + compIt->second +
  243. "_COMPILER_EXTERNAL_TOOLCHAIN"];
  244. std::string const& compilerOptionExternalToolchain =
  245. this->VariableMappings["CMAKE_" + compIt->second +
  246. "_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN"];
  247. std::string const& compilerOptionSysroot =
  248. this->VariableMappings["CMAKE_" + compIt->second +
  249. "_COMPILE_OPTIONS_SYSROOT"];
  250. // if there is a required first argument to the compiler add it
  251. // to the compiler string
  252. if (!compilerArg1.empty()) {
  253. ret += " ";
  254. ret += compilerArg1;
  255. }
  256. if (!compilerTarget.empty() && !compilerOptionTarget.empty()) {
  257. ret += " ";
  258. ret += compilerOptionTarget;
  259. ret += compilerTarget;
  260. }
  261. if (!compilerExternalToolchain.empty() &&
  262. !compilerOptionExternalToolchain.empty()) {
  263. ret += " ";
  264. ret += compilerOptionExternalToolchain;
  265. ret += outputConverter->EscapeForShell(compilerExternalToolchain, true);
  266. }
  267. std::string sysroot;
  268. // Some platforms may use separate sysroots for compiling and linking.
  269. // If we detect link flags, then we pass the link sysroot instead.
  270. // FIXME: Use a more robust way to detect link line expansion.
  271. if (replaceValues.LinkFlags) {
  272. sysroot = this->LinkerSysroot;
  273. } else {
  274. sysroot = this->CompilerSysroot;
  275. }
  276. if (!sysroot.empty() && !compilerOptionSysroot.empty()) {
  277. ret += " ";
  278. ret += compilerOptionSysroot;
  279. ret += outputConverter->EscapeForShell(sysroot, true);
  280. }
  281. return ret;
  282. }
  283. auto mapIt = this->VariableMappings.find(variable);
  284. if (mapIt != this->VariableMappings.end()) {
  285. if (variable.find("_FLAG") == std::string::npos) {
  286. return outputConverter->ConvertToOutputForExisting(mapIt->second);
  287. }
  288. return mapIt->second;
  289. }
  290. return variable;
  291. }
  292. void cmRulePlaceholderExpander::ExpandRuleVariables(
  293. cmOutputConverter* outputConverter, std::string& s,
  294. const RuleVariables& replaceValues)
  295. {
  296. std::string::size_type start = s.find('<');
  297. // no variables to expand
  298. if (start == std::string::npos) {
  299. return;
  300. }
  301. std::string::size_type pos = 0;
  302. std::string expandedInput;
  303. while (start != std::string::npos && start < s.size() - 2) {
  304. std::string::size_type end = s.find('>', start);
  305. // if we find a < with no > we are done
  306. if (end == std::string::npos) {
  307. return;
  308. }
  309. char c = s[start + 1];
  310. // if the next char after the < is not A-Za-z then
  311. // skip it and try to find the next < in the string
  312. if (!isalpha(c)) {
  313. start = s.find('<', start + 1);
  314. } else {
  315. // extract the var
  316. std::string var = s.substr(start + 1, end - start - 1);
  317. std::string replace =
  318. this->ExpandRuleVariable(outputConverter, var, replaceValues);
  319. expandedInput += s.substr(pos, start - pos);
  320. expandedInput += replace;
  321. // move to next one
  322. start = s.find('<', start + var.size() + 2);
  323. pos = end + 1;
  324. }
  325. }
  326. // add the rest of the input
  327. expandedInput += s.substr(pos, s.size() - pos);
  328. s = expandedInput;
  329. }