cmOutputConverter.h 5.6 KB

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