cmCoreTryCompile.h 3.9 KB

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