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"
5 #include "cmCoreTryCompile.h"
6 #include "cmExecutionStatus.h"
7 #include "cmMakefile.h"
8 #include "cmMessageType.h"
11 #include "cmStateTypes.h"
12 #include "cmStringAlgorithms.h"
16 bool cmTryCompileCommand(std::vector<std::string> const& args,
17 cmExecutionStatus& status)
19 cmMakefile& mf = status.GetMakefile();
21 if (args.size() < 3) {
23 MessageType::FATAL_ERROR,
24 "The try_compile() command requires at least 3 arguments.");
28 if (mf.GetCMakeInstance()->GetWorkingMode() == cmake::FIND_PACKAGE_MODE) {
30 MessageType::FATAL_ERROR,
31 "The try_compile() command is not supported in --find-package mode.");
35 cmStateEnums::TargetType targetType = cmStateEnums::EXECUTABLE;
36 cmValue tt = mf.GetDefinition("CMAKE_TRY_COMPILE_TARGET_TYPE");
38 if (*tt == cmState::GetTargetTypeName(cmStateEnums::EXECUTABLE)) {
39 targetType = cmStateEnums::EXECUTABLE;
41 cmState::GetTargetTypeName(cmStateEnums::STATIC_LIBRARY)) {
42 targetType = cmStateEnums::STATIC_LIBRARY;
45 MessageType::FATAL_ERROR,
46 cmStrCat("Invalid value '", *tt,
47 "' for CMAKE_TRY_COMPILE_TARGET_TYPE. Only '",
48 cmState::GetTargetTypeName(cmStateEnums::EXECUTABLE),
50 cmState::GetTargetTypeName(cmStateEnums::STATIC_LIBRARY),
56 cmCoreTryCompile tc(&mf);
57 cmCoreTryCompile::Arguments arguments =
58 tc.ParseArgs(cmMakeRange(args), false);
62 tc.TryCompileCode(arguments, targetType);
64 // if They specified clean then we clean up what we can
65 if (tc.SrcFileSignature) {
66 if (!mf.GetCMakeInstance()->GetDebugTryCompile()) {
67 tc.CleanupFiles(tc.BinaryDirectory);