Commit 5e328c6d authored by Brad King's avatar Brad King Committed by Kitware Robot

Merge topic 'objc-trycompile' into release-3.16

7447aa4b ObjC: Add try_compile support
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Merge-request: !4006
parents 8bfd4447 7447aa4b
...@@ -136,13 +136,19 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv, ...@@ -136,13 +136,19 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
std::string copyFile; std::string copyFile;
std::string copyFileError; std::string copyFileError;
std::string cStandard; std::string cStandard;
std::string objcStandard;
std::string cxxStandard; std::string cxxStandard;
std::string objcxxStandard;
std::string cudaStandard; std::string cudaStandard;
std::string cStandardRequired; std::string cStandardRequired;
std::string cxxStandardRequired; std::string cxxStandardRequired;
std::string objcStandardRequired;
std::string objcxxStandardRequired;
std::string cudaStandardRequired; std::string cudaStandardRequired;
std::string cExtensions; std::string cExtensions;
std::string cxxExtensions; std::string cxxExtensions;
std::string objcExtensions;
std::string objcxxExtensions;
std::string cudaExtensions; std::string cudaExtensions;
std::vector<std::string> targets; std::vector<std::string> targets;
std::vector<std::string> linkOptions; std::vector<std::string> linkOptions;
...@@ -154,12 +160,18 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv, ...@@ -154,12 +160,18 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
bool didCopyFileError = false; bool didCopyFileError = false;
bool didCStandard = false; bool didCStandard = false;
bool didCxxStandard = false; bool didCxxStandard = false;
bool didObjCStandard = false;
bool didObjCxxStandard = false;
bool didCudaStandard = false; bool didCudaStandard = false;
bool didCStandardRequired = false; bool didCStandardRequired = false;
bool didCxxStandardRequired = false; bool didCxxStandardRequired = false;
bool didObjCStandardRequired = false;
bool didObjCxxStandardRequired = false;
bool didCudaStandardRequired = false; bool didCudaStandardRequired = false;
bool didCExtensions = false; bool didCExtensions = false;
bool didCxxExtensions = false; bool didCxxExtensions = false;
bool didObjCExtensions = false;
bool didObjCxxExtensions = false;
bool didCudaExtensions = false; bool didCudaExtensions = false;
bool useSources = argv[2] == "SOURCES"; bool useSources = argv[2] == "SOURCES";
std::vector<std::string> sources; std::vector<std::string> sources;
...@@ -176,12 +188,18 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv, ...@@ -176,12 +188,18 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
DoingCopyFileError, DoingCopyFileError,
DoingCStandard, DoingCStandard,
DoingCxxStandard, DoingCxxStandard,
DoingObjCStandard,
DoingObjCxxStandard,
DoingCudaStandard, DoingCudaStandard,
DoingCStandardRequired, DoingCStandardRequired,
DoingCxxStandardRequired, DoingCxxStandardRequired,
DoingObjCStandardRequired,
DoingObjCxxStandardRequired,
DoingCudaStandardRequired, DoingCudaStandardRequired,
DoingCExtensions, DoingCExtensions,
DoingCxxExtensions, DoingCxxExtensions,
DoingObjCExtensions,
DoingObjCxxExtensions,
DoingCudaExtensions, DoingCudaExtensions,
DoingSources, DoingSources,
DoingCMakeInternal DoingCMakeInternal
...@@ -212,6 +230,12 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv, ...@@ -212,6 +230,12 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
} else if (argv[i] == "CXX_STANDARD") { } else if (argv[i] == "CXX_STANDARD") {
doing = DoingCxxStandard; doing = DoingCxxStandard;
didCxxStandard = true; didCxxStandard = true;
} else if (argv[i] == "OBJC_STANDARD") {
doing = DoingObjCStandard;
didObjCStandard = true;
} else if (argv[i] == "OBJCXX_STANDARD") {
doing = DoingObjCxxStandard;
didObjCxxStandard = true;
} else if (argv[i] == "CUDA_STANDARD") { } else if (argv[i] == "CUDA_STANDARD") {
doing = DoingCudaStandard; doing = DoingCudaStandard;
didCudaStandard = true; didCudaStandard = true;
...@@ -221,6 +245,12 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv, ...@@ -221,6 +245,12 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
} else if (argv[i] == "CXX_STANDARD_REQUIRED") { } else if (argv[i] == "CXX_STANDARD_REQUIRED") {
doing = DoingCxxStandardRequired; doing = DoingCxxStandardRequired;
didCxxStandardRequired = true; didCxxStandardRequired = true;
} else if (argv[i] == "OBJC_STANDARD_REQUIRED") {
doing = DoingObjCStandardRequired;
didObjCStandardRequired = true;
} else if (argv[i] == "OBJCXX_STANDARD_REQUIRED") {
doing = DoingObjCxxStandardRequired;
didObjCxxStandardRequired = true;
} else if (argv[i] == "CUDA_STANDARD_REQUIRED") { } else if (argv[i] == "CUDA_STANDARD_REQUIRED") {
doing = DoingCudaStandardRequired; doing = DoingCudaStandardRequired;
didCudaStandardRequired = true; didCudaStandardRequired = true;
...@@ -230,6 +260,12 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv, ...@@ -230,6 +260,12 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
} else if (argv[i] == "CXX_EXTENSIONS") { } else if (argv[i] == "CXX_EXTENSIONS") {
doing = DoingCxxExtensions; doing = DoingCxxExtensions;
didCxxExtensions = true; didCxxExtensions = true;
} else if (argv[i] == "OBJC_EXTENSIONS") {
doing = DoingObjCExtensions;
didObjCExtensions = true;
} else if (argv[i] == "OBJCXX_EXTENSIONS") {
doing = DoingObjCxxExtensions;
didObjCxxExtensions = true;
} else if (argv[i] == "CUDA_EXTENSIONS") { } else if (argv[i] == "CUDA_EXTENSIONS") {
doing = DoingCudaExtensions; doing = DoingCudaExtensions;
didCudaExtensions = true; didCudaExtensions = true;
...@@ -285,6 +321,12 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv, ...@@ -285,6 +321,12 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
} else if (doing == DoingCxxStandard) { } else if (doing == DoingCxxStandard) {
cxxStandard = argv[i]; cxxStandard = argv[i];
doing = DoingNone; doing = DoingNone;
} else if (doing == DoingObjCStandard) {
objcStandard = argv[i];
doing = DoingNone;
} else if (doing == DoingObjCxxStandard) {
objcxxStandard = argv[i];
doing = DoingNone;
} else if (doing == DoingCudaStandard) { } else if (doing == DoingCudaStandard) {
cudaStandard = argv[i]; cudaStandard = argv[i];
doing = DoingNone; doing = DoingNone;
...@@ -294,6 +336,12 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv, ...@@ -294,6 +336,12 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
} else if (doing == DoingCxxStandardRequired) { } else if (doing == DoingCxxStandardRequired) {
cxxStandardRequired = argv[i]; cxxStandardRequired = argv[i];
doing = DoingNone; doing = DoingNone;
} else if (doing == DoingObjCStandardRequired) {
objcStandardRequired = argv[i];
doing = DoingNone;
} else if (doing == DoingObjCxxStandardRequired) {
objcxxStandardRequired = argv[i];
doing = DoingNone;
} else if (doing == DoingCudaStandardRequired) { } else if (doing == DoingCudaStandardRequired) {
cudaStandardRequired = argv[i]; cudaStandardRequired = argv[i];
doing = DoingNone; doing = DoingNone;
...@@ -303,6 +351,12 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv, ...@@ -303,6 +351,12 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
} else if (doing == DoingCxxExtensions) { } else if (doing == DoingCxxExtensions) {
cxxExtensions = argv[i]; cxxExtensions = argv[i];
doing = DoingNone; doing = DoingNone;
} else if (doing == DoingObjCExtensions) {
objcExtensions = argv[i];
doing = DoingNone;
} else if (doing == DoingObjCxxExtensions) {
objcxxExtensions = argv[i];
doing = DoingNone;
} else if (doing == DoingCudaExtensions) { } else if (doing == DoingCudaExtensions) {
cudaExtensions = argv[i]; cudaExtensions = argv[i];
doing = DoingNone; doing = DoingNone;
...@@ -754,16 +808,20 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv, ...@@ -754,16 +808,20 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
fprintf(fout, ")\n"); fprintf(fout, ")\n");
bool const testC = testLangs.find("C") != testLangs.end(); bool const testC = testLangs.find("C") != testLangs.end();
bool const testObjC = testLangs.find("OBJC") != testLangs.end();
bool const testCxx = testLangs.find("CXX") != testLangs.end(); bool const testCxx = testLangs.find("CXX") != testLangs.end();
bool const testObjCxx = testLangs.find("OBJCXX") != testLangs.end();
bool const testCuda = testLangs.find("CUDA") != testLangs.end(); bool const testCuda = testLangs.find("CUDA") != testLangs.end();
bool warnCMP0067 = false; bool warnCMP0067 = false;
bool honorStandard = true; bool honorStandard = true;
if (!didCStandard && !didCxxStandard && !didCudaStandard && if (!didCStandard && !didCxxStandard && !didObjCStandard &&
!didCStandardRequired && !didCxxStandardRequired && !didObjCxxStandard && !didCudaStandard && !didCStandardRequired &&
!didCudaStandardRequired && !didCExtensions && !didCxxExtensions && !didCxxStandardRequired && !didObjCStandardRequired &&
!didCudaExtensions) { !didObjCxxStandardRequired && !didCudaStandardRequired &&
!didCExtensions && !didCxxExtensions && !didObjCExtensions &&
!didObjCxxExtensions && !didCudaExtensions) {
switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0067)) { switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0067)) {
case cmPolicies::WARN: case cmPolicies::WARN:
warnCMP0067 = this->Makefile->PolicyOptionalWarningEnabled( warnCMP0067 = this->Makefile->PolicyOptionalWarningEnabled(
...@@ -786,45 +844,42 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv, ...@@ -786,45 +844,42 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
} }
if (honorStandard || warnCMP0067) { if (honorStandard || warnCMP0067) {
if (testC) {
if (!didCStandard) { auto testLanguage =
cStandard = this->LookupStdVar("CMAKE_C_STANDARD", warnCMP0067); [&](bool testLang, bool didLangStandard, bool didLangStandardRequired,
} bool didLangExtensions, std::string& langStandard,
if (!didCStandardRequired) { std::string& langStandardRequired, std::string& langExtensions,
cStandardRequired = const std::string& lang) {
this->LookupStdVar("CMAKE_C_STANDARD_REQUIRED", warnCMP0067); if (testLang) {
} if (!didLangStandard) {
if (!didCExtensions) { langStandard = this->LookupStdVar(
cExtensions = this->LookupStdVar("CMAKE_C_EXTENSIONS", warnCMP0067); cmStrCat("CMAKE_", lang, "_STANDARD"), warnCMP0067);
} }
} if (!didLangStandardRequired) {
if (testCxx) { langStandardRequired = this->LookupStdVar(
if (!didCxxStandard) { cmStrCat("CMAKE_", lang, "_STANDARD_REQUIRED"), warnCMP0067);
cxxStandard = this->LookupStdVar("CMAKE_CXX_STANDARD", warnCMP0067); }
} if (!didLangExtensions) {
if (!didCxxStandardRequired) { langExtensions = this->LookupStdVar(
cxxStandardRequired = cmStrCat("CMAKE_", lang, "_EXTENSIONS"), warnCMP0067);
this->LookupStdVar("CMAKE_CXX_STANDARD_REQUIRED", warnCMP0067); }
} }
if (!didCxxExtensions) { };
cxxExtensions =
this->LookupStdVar("CMAKE_CXX_EXTENSIONS", warnCMP0067); testLanguage(testC, didCStandard, didCStandardRequired, didCExtensions,
} cStandard, cStandardRequired, cExtensions, "C");
} testLanguage(testObjC, didObjCStandard, didObjCStandardRequired,
if (testCuda) { didObjCExtensions, objcStandard, objcStandardRequired,
if (!didCudaStandard) { objcExtensions, "OBJC");
cudaStandard = testLanguage(testCxx, didCxxStandard, didCxxStandardRequired,
this->LookupStdVar("CMAKE_CUDA_STANDARD", warnCMP0067); didCxxExtensions, cxxStandard, cxxStandardRequired,
} cxxExtensions, "CXX");
if (!didCudaStandardRequired) { testLanguage(testObjCxx, didObjCxxStandard, didObjCxxStandardRequired,
cudaStandardRequired = didObjCxxExtensions, objcxxStandard, objcxxStandardRequired,
this->LookupStdVar("CMAKE_CUDA_STANDARD_REQUIRED", warnCMP0067); objcxxExtensions, "OBJCXX");
} testLanguage(testCuda, didCudaStandard, didCudaStandardRequired,
if (!didCudaExtensions) { didCudaExtensions, cudaStandard, cudaStandardRequired,
cudaExtensions = cudaExtensions, "CUDA");
this->LookupStdVar("CMAKE_CUDA_EXTENSIONS", warnCMP0067);
}
}
} }
if (!this->WarnCMP0067.empty()) { if (!this->WarnCMP0067.empty()) {
...@@ -841,44 +896,37 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv, ...@@ -841,44 +896,37 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
this->Makefile->IssueMessage(MessageType::AUTHOR_WARNING, w.str()); this->Makefile->IssueMessage(MessageType::AUTHOR_WARNING, w.str());
} }
if (testC) { auto writeLanguageProperties = [&](bool testLang,
if (!cStandard.empty()) { const std::string& langStandard,
writeProperty(fout, targetName, "C_STANDARD", cStandard); const std::string& langStandardRequired,
} const std::string& langExtensions,
if (!cStandardRequired.empty()) { const std::string& lang) {
writeProperty(fout, targetName, "C_STANDARD_REQUIRED", if (testLang) {
cStandardRequired); if (!langStandard.empty()) {
} writeProperty(fout, targetName, cmStrCat(lang, "_STANDARD"),
if (!cExtensions.empty()) { langStandard);
writeProperty(fout, targetName, "C_EXTENSIONS", cExtensions); }
} if (!langStandardRequired.empty()) {
} writeProperty(fout, targetName, cmStrCat(lang, "_STANDARD_REQUIRED"),
langStandardRequired);
if (testCxx) { }
if (!cxxStandard.empty()) { if (!langExtensions.empty()) {
writeProperty(fout, targetName, "CXX_STANDARD", cxxStandard); writeProperty(fout, targetName, cmStrCat(lang, "_EXTENSIONS"),
} langExtensions);
if (!cxxStandardRequired.empty()) { }
writeProperty(fout, targetName, "CXX_STANDARD_REQUIRED",
cxxStandardRequired);
}
if (!cxxExtensions.empty()) {
writeProperty(fout, targetName, "CXX_EXTENSIONS", cxxExtensions);
}
}
if (testCuda) {
if (!cudaStandard.empty()) {
writeProperty(fout, targetName, "CUDA_STANDARD", cudaStandard);
}
if (!cudaStandardRequired.empty()) {
writeProperty(fout, targetName, "CUDA_STANDARD_REQUIRED",
cudaStandardRequired);
}
if (!cudaExtensions.empty()) {
writeProperty(fout, targetName, "CUDA_EXTENSIONS", cudaExtensions);
} }
} };
writeLanguageProperties(testC, cStandard, cStandardRequired, cExtensions,
"C");
writeLanguageProperties(testObjC, objcStandard, objcStandardRequired,
objcExtensions, "OBJC");
writeLanguageProperties(testCxx, cxxStandard, cxxStandardRequired,
cxxExtensions, "CXX");
writeLanguageProperties(testObjCxx, objcxxStandard, objcxxStandardRequired,
objcxxExtensions, "OBJCXX");
writeLanguageProperties(testCuda, cudaStandard, cudaStandardRequired,
cudaExtensions, "CUDA");
if (!linkOptions.empty()) { if (!linkOptions.empty()) {
std::vector<std::string> options; std::vector<std::string> options;
......
...@@ -313,6 +313,8 @@ function(add_RunCMake_test_try_compile) ...@@ -313,6 +313,8 @@ function(add_RunCMake_test_try_compile)
CMAKE_CXX_COMPILER_VERSION CMAKE_CXX_COMPILER_VERSION
CMAKE_CXX_STANDARD_DEFAULT CMAKE_CXX_STANDARD_DEFAULT
CMake_TEST_CUDA CMake_TEST_CUDA
CMAKE_OBJC_STANDARD_DEFAULT
CMAKE_OBJCXX_STANDARD_DEFAULT
) )
if(DEFINED ${var}) if(DEFINED ${var})
list(APPEND try_compile_ARGS -D${var}=${${var}}) list(APPEND try_compile_ARGS -D${var}=${${var}})
......
^CMake Error at .*/Tests/RunCMake/try_compile/ObjCStandard-build/CMakeFiles/CMakeTmp/CMakeLists.txt:[0-9]+ \(add_executable\):
OBJC_STANDARD is set to invalid value '3'
+
CMake Error at ObjCStandard.cmake:[0-9]+ \(try_compile\):
Failed to generate test project build system.
Call Stack \(most recent call first\):
CMakeLists.txt:[0-9]+ \(include\)$
enable_language(OBJC)
try_compile(result ${CMAKE_CURRENT_BINARY_DIR}
SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src.m
OBJC_STANDARD 3
OUTPUT_VARIABLE out
)
message("try_compile output:\n${out}")
^CMake Error at .*/Tests/RunCMake/try_compile/ObjCxxStandard-build/CMakeFiles/CMakeTmp/CMakeLists.txt:[0-9]+ \(add_executable\):
OBJCXX_STANDARD is set to invalid value '3'
+
CMake Error at ObjCxxStandard.cmake:[0-9]+ \(try_compile\):
Failed to generate test project build system.
Call Stack \(most recent call first\):
CMakeLists.txt:[0-9]+ \(include\)$
enable_language(OBJCXX)
try_compile(result ${CMAKE_CURRENT_BINARY_DIR}
SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src.mm
OBJCXX_STANDARD 3
OUTPUT_VARIABLE out
)
message("try_compile output:\n${out}")
...@@ -37,11 +37,17 @@ if(CMAKE_C_STANDARD_DEFAULT) ...@@ -37,11 +37,17 @@ if(CMAKE_C_STANDARD_DEFAULT)
elseif(DEFINED CMAKE_C_STANDARD_DEFAULT) elseif(DEFINED CMAKE_C_STANDARD_DEFAULT)
run_cmake(CStandardNoDefault) run_cmake(CStandardNoDefault)
endif() endif()
if(CMAKE_OBJC_STANDARD_DEFAULT)
run_cmake(ObjCStandard)
endif()
if(CMAKE_CXX_STANDARD_DEFAULT) if(CMAKE_CXX_STANDARD_DEFAULT)
run_cmake(CxxStandard) run_cmake(CxxStandard)
elseif(DEFINED CMAKE_CXX_STANDARD_DEFAULT) elseif(DEFINED CMAKE_CXX_STANDARD_DEFAULT)
run_cmake(CxxStandardNoDefault) run_cmake(CxxStandardNoDefault)
endif() endif()
if(CMAKE_OBJCXX_STANDARD_DEFAULT)
run_cmake(ObjCxxStandard)
endif()
if(CMake_TEST_CUDA) if(CMake_TEST_CUDA)
if(CMAKE_HOST_WIN32) if(CMAKE_HOST_WIN32)
run_cmake(CudaStandardNoDefault) run_cmake(CudaStandardNoDefault)
......
int main()
{
return 0;
}
int main()
{
return 0;
}
...@@ -365,6 +365,25 @@ if (APPLE) ...@@ -365,6 +365,25 @@ if (APPLE)
TEST_ASSERT(SIMPLE_OBJCXX_RUN_SHOULD_WORK "CHECK_OBJCXX_SOURCE_RUNS() failed, but should have succeeded") TEST_ASSERT(SIMPLE_OBJCXX_RUN_SHOULD_WORK "CHECK_OBJCXX_SOURCE_RUNS() failed, but should have succeeded")
TEST_FAIL(OBJCXX_RUN_SHOULD_FAIL "CHECK_OBJCXX_SOURCE_RUNS() succeeds, but should have failed") TEST_FAIL(OBJCXX_RUN_SHOULD_FAIL "CHECK_OBJCXX_SOURCE_RUNS() succeeds, but should have failed")
TEST_ASSERT(OBJCXX_RUN_SHOULD_WORK "CHECK_OBJCXX_SOURCE_RUNS() failed, but should have succeeded") TEST_ASSERT(OBJCXX_RUN_SHOULD_WORK "CHECK_OBJCXX_SOURCE_RUNS() failed, but should have succeeded")
# try to compile a file that should compile
try_compile(SHOULD_PASS
${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
${TryCompile_SOURCE_DIR}/pass.m
OUTPUT_VARIABLE TRY_OUT)
if(NOT SHOULD_PASS)
message(SEND_ERROR "should pass failed ${TRY_OUT}")
endif()
# try to compile a file that should not compile
try_compile(SHOULD_FAIL
${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
${TryCompile_SOURCE_DIR}/fail.m
OUTPUT_VARIABLE TRY_OUT)
if(SHOULD_FAIL)
message(SEND_ERROR "Should fail passed ${TRY_OUT}")
endif()
endif() endif()
####################################################################### #######################################################################
......
int main()
{
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment