cmOutputConverter.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <string>
  6. #include <cm/string_view>
  7. #include "cmStateSnapshot.h"
  8. class cmState;
  9. class cmOutputConverter
  10. {
  11. public:
  12. cmOutputConverter(cmStateSnapshot const& snapshot);
  13. /**
  14. * Convert the given remote path to a relative path with respect to
  15. * one of our common work directories. The path must use forward
  16. * slashes and not already be escaped or quoted.
  17. * The conversion is skipped if the paths are not both in the source
  18. * or both in the binary tree.
  19. */
  20. std::string MaybeRelativeToTopBinDir(std::string const& path) const;
  21. std::string MaybeRelativeToCurBinDir(std::string const& path) const;
  22. std::string const& GetRelativePathTopSource() const;
  23. std::string const& GetRelativePathTopBinary() const;
  24. void SetRelativePathTop(std::string const& topSource,
  25. std::string const& topBinary);
  26. enum OutputFormat
  27. {
  28. SHELL,
  29. WATCOMQUOTE,
  30. NINJAMULTI,
  31. RESPONSE
  32. };
  33. std::string ConvertToOutputFormat(cm::string_view source,
  34. OutputFormat output) const;
  35. std::string ConvertDirectorySeparatorsForShell(cm::string_view source) const;
  36. //! for existing files convert to output path and short path if spaces
  37. std::string ConvertToOutputForExisting(const std::string& remote,
  38. OutputFormat format = SHELL) const;
  39. void SetLinkScriptShell(bool linkScriptShell);
  40. /**
  41. * Flags to pass to Shell_GetArgument. These modify the generated
  42. * quoting and escape sequences to work under alternative
  43. * environments.
  44. */
  45. enum Shell_Flag_e
  46. {
  47. /** The target shell is in a makefile. */
  48. Shell_Flag_Make = (1 << 0),
  49. /** The target shell is in a VS project file. Do not use with
  50. Shell_Flag_Make. */
  51. Shell_Flag_VSIDE = (1 << 1),
  52. /** In a windows shell the argument is being passed to "echo". */
  53. Shell_Flag_EchoWindows = (1 << 2),
  54. /** The target shell is in a Watcom WMake makefile. */
  55. Shell_Flag_WatcomWMake = (1 << 3),
  56. /** The target shell is in a MinGW Make makefile. */
  57. Shell_Flag_MinGWMake = (1 << 4),
  58. /** The target shell is in a NMake makefile. */
  59. Shell_Flag_NMake = (1 << 5),
  60. /** Make variable reference syntax $(MAKEVAR) should not be escaped
  61. to allow a build tool to replace it. Replacement values
  62. containing spaces, quotes, backslashes, or other
  63. non-alphanumeric characters that have significance to some makes
  64. or shells produce undefined behavior. */
  65. Shell_Flag_AllowMakeVariables = (1 << 6),
  66. /** The target shell quoting uses extra single Quotes for Watcom tools. */
  67. Shell_Flag_WatcomQuote = (1 << 7),
  68. Shell_Flag_IsUnix = (1 << 8),
  69. Shell_Flag_UnescapeNinjaConfiguration = (1 << 9),
  70. Shell_Flag_IsResponse = (1 << 10)
  71. };
  72. std::string EscapeForShell(cm::string_view str, bool makeVars = false,
  73. bool forEcho = false, bool useWatcomQuote = false,
  74. bool unescapeNinjaConfiguration = false,
  75. bool forResponse = false) const;
  76. enum class WrapQuotes
  77. {
  78. Wrap,
  79. NoWrap,
  80. };
  81. static std::string EscapeForCMake(cm::string_view str,
  82. WrapQuotes wrapQuotes = WrapQuotes::Wrap);
  83. /** Compute an escaped version of the given argument for use in a
  84. windows shell. */
  85. static std::string EscapeWindowsShellArgument(cm::string_view arg,
  86. int shell_flags);
  87. enum FortranFormat
  88. {
  89. FortranFormatNone,
  90. FortranFormatFixed,
  91. FortranFormatFree
  92. };
  93. static FortranFormat GetFortranFormat(cm::string_view value);
  94. enum class FortranPreprocess
  95. {
  96. Unset,
  97. NotNeeded,
  98. Needed
  99. };
  100. static FortranPreprocess GetFortranPreprocess(cm::string_view value);
  101. protected:
  102. cmStateSnapshot StateSnapshot;
  103. private:
  104. cmState* GetState() const;
  105. static bool Shell_CharNeedsQuotes(char c, int flags);
  106. static cm::string_view::iterator Shell_SkipMakeVariables(
  107. cm::string_view::iterator begin, cm::string_view::iterator end);
  108. static bool Shell_ArgumentNeedsQuotes(cm::string_view in, int flags);
  109. static std::string Shell_GetArgument(cm::string_view in, int flags);
  110. bool LinkScriptShell = false;
  111. // The top-most directories for relative path conversion. Both the
  112. // source and destination location of a relative path conversion
  113. // must be underneath one of these directories (both under source or
  114. // both under binary) in order for the relative path to be evaluated
  115. // safely by the build tools.
  116. std::string RelativePathTopSource;
  117. std::string RelativePathTopBinary;
  118. enum class TopRelation
  119. {
  120. Separate,
  121. BinInSrc,
  122. SrcInBin,
  123. InSource,
  124. };
  125. TopRelation RelativePathTopRelation = TopRelation::Separate;
  126. void ComputeRelativePathTopSource();
  127. void ComputeRelativePathTopBinary();
  128. void ComputeRelativePathTopRelation();
  129. std::string MaybeRelativeTo(std::string const& local_path,
  130. std::string const& remote_path) const;
  131. };