cmTryCompileCommand.cxx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst 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. // Keep in sync with cmFileAPIConfigureLog's DumpEventKindNames.
  22. static std::vector<unsigned int> const LogVersionsWithTryCompileV1{ 1 };
  23. if (log.IsAnyLogVersionEnabled(LogVersionsWithTryCompileV1)) {
  24. log.BeginEvent("try_compile-v1", 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()->GetState()->GetRole() ==
  42. cmState::Role::FindPackage) {
  43. mf.IssueMessage(
  44. MessageType::FATAL_ERROR,
  45. "The try_compile() command is not supported in --find-package mode.");
  46. return false;
  47. }
  48. cmStateEnums::TargetType targetType = cmStateEnums::EXECUTABLE;
  49. cmValue tt = mf.GetDefinition("CMAKE_TRY_COMPILE_TARGET_TYPE");
  50. if (cmNonempty(tt)) {
  51. if (*tt == cmState::GetTargetTypeName(cmStateEnums::EXECUTABLE)) {
  52. targetType = cmStateEnums::EXECUTABLE;
  53. } else if (*tt ==
  54. cmState::GetTargetTypeName(cmStateEnums::STATIC_LIBRARY)) {
  55. targetType = cmStateEnums::STATIC_LIBRARY;
  56. } else {
  57. mf.IssueMessage(
  58. MessageType::FATAL_ERROR,
  59. cmStrCat("Invalid value '", *tt,
  60. "' for CMAKE_TRY_COMPILE_TARGET_TYPE. Only '",
  61. cmState::GetTargetTypeName(cmStateEnums::EXECUTABLE),
  62. "' and '",
  63. cmState::GetTargetTypeName(cmStateEnums::STATIC_LIBRARY),
  64. "' are allowed."));
  65. return false;
  66. }
  67. }
  68. cmCoreTryCompile tc(&mf);
  69. cmCoreTryCompile::Arguments arguments =
  70. tc.ParseArgs(cmMakeRange(args), false);
  71. if (!arguments) {
  72. return true;
  73. }
  74. cm::optional<cmTryCompileResult> compileResult =
  75. tc.TryCompileCode(arguments, targetType);
  76. #ifndef CMAKE_BOOTSTRAP
  77. if (compileResult && !arguments.NoLog) {
  78. if (cmConfigureLog* log = mf.GetCMakeInstance()->GetConfigureLog()) {
  79. WriteTryCompileEvent(*log, mf, *compileResult);
  80. }
  81. }
  82. #endif
  83. // if They specified clean then we clean up what we can
  84. if (tc.SrcFileSignature) {
  85. if (!mf.GetCMakeInstance()->GetDebugTryCompile()) {
  86. tc.CleanupFiles(tc.BinaryDirectory);
  87. }
  88. }
  89. return true;
  90. }