cmCoreTryCompile.h 4.2 KB

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