cmTryCompileCommand.cxx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmTryCompileCommand.h"
  4. #include <cm/optional>
  5. #include "cmConfigureLog.h"
  6. #include "cmCoreTryCompile.h"
  7. #include "cmExecutionStatus.h"
  8. #include "cmMakefile.h"
  9. #include "cmMessageType.h"
  10. #include "cmRange.h"
  11. #include "cmState.h"
  12. #include "cmStateTypes.h"
  13. #include "cmStringAlgorithms.h"
  14. #include "cmValue.h"
  15. #include "cmake.h"
  16. namespace {
  17. #ifndef CMAKE_BOOTSTRAP
  18. void WriteTryCompileEvent(cmConfigureLog& log, cmMakefile const& mf,
  19. cmTryCompileResult const& compileResult)
  20. {
  21. static const std::vector<unsigned long> LogVersionsWithTryCompileV1{ 1 };
  22. if (log.IsAnyLogVersionEnabled(LogVersionsWithTryCompileV1)) {
  23. log.BeginEvent("try_compile-v1");
  24. log.WriteBacktrace(mf);
  25. cmCoreTryCompile::WriteTryCompileEventFields(log, compileResult);
  26. log.EndEvent();
  27. }
  28. }
  29. #endif
  30. }
  31. bool cmTryCompileCommand(std::vector<std::string> const& args,
  32. cmExecutionStatus& status)
  33. {
  34. cmMakefile& mf = status.GetMakefile();
  35. if (args.size() < 3) {
  36. mf.IssueMessage(
  37. MessageType::FATAL_ERROR,
  38. "The try_compile() command requires at least 3 arguments.");
  39. return false;
  40. }
  41. if (mf.GetCMakeInstance()->GetWorkingMode() == cmake::FIND_PACKAGE_MODE) {
  42. mf.IssueMessage(
  43. MessageType::FATAL_ERROR,
  44. "The try_compile() command is not supported in --find-package mode.");
  45. return false;
  46. }
  47. cmStateEnums::TargetType targetType = cmStateEnums::EXECUTABLE;
  48. cmValue tt = mf.GetDefinition("CMAKE_TRY_COMPILE_TARGET_TYPE");
  49. if (cmNonempty(tt)) {
  50. if (*tt == cmState::GetTargetTypeName(cmStateEnums::EXECUTABLE)) {
  51. targetType = cmStateEnums::EXECUTABLE;
  52. } else if (*tt ==
  53. cmState::GetTargetTypeName(cmStateEnums::STATIC_LIBRARY)) {
  54. targetType = cmStateEnums::STATIC_LIBRARY;
  55. } else {
  56. mf.IssueMessage(
  57. MessageType::FATAL_ERROR,
  58. cmStrCat("Invalid value '", *tt,
  59. "' for CMAKE_TRY_COMPILE_TARGET_TYPE. Only '",
  60. cmState::GetTargetTypeName(cmStateEnums::EXECUTABLE),
  61. "' and '",
  62. cmState::GetTargetTypeName(cmStateEnums::STATIC_LIBRARY),
  63. "' are allowed."));
  64. return false;
  65. }
  66. }
  67. cmCoreTryCompile tc(&mf);
  68. cmCoreTryCompile::Arguments arguments =
  69. tc.ParseArgs(cmMakeRange(args), false);
  70. if (!arguments) {
  71. return true;
  72. }
  73. if (cm::optional<cmTryCompileResult> compileResult =
  74. tc.TryCompileCode(arguments, targetType)) {
  75. #ifndef CMAKE_BOOTSTRAP
  76. if (cmConfigureLog* log = mf.GetCMakeInstance()->GetConfigureLog()) {
  77. WriteTryCompileEvent(*log, mf, *compileResult);
  78. }
  79. #endif
  80. }
  81. // if They specified clean then we clean up what we can
  82. if (tc.SrcFileSignature) {
  83. if (!mf.GetCMakeInstance()->GetDebugTryCompile()) {
  84. tc.CleanupFiles(tc.BinaryDirectory);
  85. }
  86. }
  87. return true;
  88. }