cmCoreTryCompile.h 4.4 KB

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