cmCoreTryCompile.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <map>
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include <cm/optional>
  10. #include <cm/string_view>
  11. #include "cmArgumentParser.h"
  12. #include "cmArgumentParserTypes.h"
  13. #include "cmList.h"
  14. #include "cmStateTypes.h"
  15. class cmConfigureLog;
  16. class cmMakefile;
  17. template <typename Iter>
  18. class cmRange;
  19. struct cmTryCompileResult
  20. {
  21. cm::optional<std::string> LogDescription;
  22. std::map<std::string, std::string> CMakeVariables;
  23. std::string SourceDirectory;
  24. std::string BinaryDirectory;
  25. bool VariableCached = true;
  26. std::string Variable;
  27. std::string Output;
  28. int ExitCode = 1;
  29. };
  30. /** \class cmCoreTryCompile
  31. * \brief Base class for cmTryCompileCommand and cmTryRunCommand
  32. *
  33. * cmCoreTryCompile implements the functionality to build a program.
  34. * It is the base class for cmTryCompileCommand and cmTryRunCommand.
  35. */
  36. class cmCoreTryCompile
  37. {
  38. public:
  39. cmCoreTryCompile(cmMakefile* mf)
  40. : Makefile(mf)
  41. {
  42. }
  43. struct Arguments : public ArgumentParser::ParseResult
  44. {
  45. Arguments(cmMakefile const* mf)
  46. : Makefile(mf)
  47. {
  48. }
  49. cmMakefile const* Makefile;
  50. enum class SourceType
  51. {
  52. Normal,
  53. CxxModule,
  54. Directory,
  55. };
  56. cm::optional<std::string> CompileResultVariable;
  57. cm::optional<std::string> BinaryDirectory;
  58. cm::optional<std::string> SourceDirectoryOrFile;
  59. cm::optional<std::string> ProjectName;
  60. cm::optional<std::string> TargetName;
  61. cm::optional<ArgumentParser::NonEmpty<
  62. std::vector<std::pair<std::string, SourceType>>>>
  63. Sources;
  64. cm::optional<ArgumentParser::NonEmpty<
  65. std::vector<std::pair<std::string, SourceType>>>>
  66. SourceFromContent;
  67. cm::optional<ArgumentParser::NonEmpty<
  68. std::vector<std::pair<std::string, SourceType>>>>
  69. SourceFromVar;
  70. cm::optional<ArgumentParser::NonEmpty<
  71. std::vector<std::pair<std::string, SourceType>>>>
  72. SourceFromFile;
  73. ArgumentParser::MaybeEmpty<std::vector<std::string>> CMakeFlags{
  74. 1, "CMAKE_FLAGS"
  75. }; // fake argv[0]
  76. cmList CompileDefs;
  77. cm::optional<ArgumentParser::MaybeEmpty<std::vector<std::string>>>
  78. LinkLibraries;
  79. ArgumentParser::MaybeEmpty<std::vector<std::string>> LinkOptions;
  80. cm::optional<std::string> LinkerLanguage;
  81. std::map<std::string, std::string> LangProps;
  82. std::string CMakeInternal;
  83. cm::optional<std::string> OutputVariable;
  84. cm::optional<std::string> CopyFileTo;
  85. cm::optional<std::string> CopyFileError;
  86. cm::optional<ArgumentParser::NonEmpty<std::string>> LogDescription;
  87. bool NoCache = false;
  88. bool NoLog = false;
  89. ArgumentParser::Continue SetSourceType(cm::string_view sourceType);
  90. SourceType SourceTypeContext = SourceType::Normal;
  91. std::string SourceTypeError;
  92. // Argument for try_run only.
  93. // Keep in sync with warnings in cmCoreTryCompile::ParseArgs.
  94. cm::optional<std::string> CompileOutputVariable;
  95. cm::optional<std::string> RunOutputVariable;
  96. cm::optional<std::string> RunOutputStdOutVariable;
  97. cm::optional<std::string> RunOutputStdErrVariable;
  98. cm::optional<std::string> RunWorkingDirectory;
  99. cm::optional<ArgumentParser::MaybeEmpty<std::vector<std::string>>> RunArgs;
  100. };
  101. Arguments ParseArgs(cmRange<std::vector<std::string>::const_iterator> args,
  102. bool isTryRun);
  103. /**
  104. * This is the core code for try compile. It is here so that other commands,
  105. * such as TryRun can access the same logic without duplication.
  106. *
  107. * This function requires at least two \p arguments and will crash if given
  108. * fewer.
  109. */
  110. cm::optional<cmTryCompileResult> TryCompileCode(
  111. Arguments& arguments, cmStateEnums::TargetType targetType);
  112. /**
  113. * Returns \c true if \p path resides within a CMake temporary directory,
  114. * otherwise returns \c false.
  115. */
  116. static bool IsTemporary(std::string const& path);
  117. /**
  118. * This deletes all the files created by TryCompileCode.
  119. * This way we do not have to rely on the timing and
  120. * dependencies of makefiles.
  121. */
  122. void CleanupFiles(std::string const& binDir);
  123. /**
  124. * This tries to find the (executable) file created by
  125. TryCompileCode. The result is stored in OutputFile. If nothing is found,
  126. the error message is stored in FindErrorMessage.
  127. */
  128. void FindOutputFile(std::string const& targetName);
  129. static void WriteTryCompileEventFields(
  130. cmConfigureLog& log, cmTryCompileResult const& compileResult);
  131. std::string BinaryDirectory;
  132. std::string OutputFile;
  133. std::string FindErrorMessage;
  134. bool SrcFileSignature = false;
  135. cmMakefile* Makefile;
  136. private:
  137. std::string WriteSource(std::string const& name, std::string const& content,
  138. char const* command) const;
  139. Arguments ParseArgs(cmRange<std::vector<std::string>::const_iterator> args,
  140. cmArgumentParser<Arguments> const& parser,
  141. std::vector<std::string>& unparsedArguments);
  142. };