cmCoreTryCompile: Refactor forwarding of variables to test project

De-duplicate the logic that constructs the cmake `-D` flag used to pass
variables into the test project cache.  Also subsume variables that were
propagated by generating `set()` commands in the project and pass them
as cache entries instead.
This commit is contained in:
Brad King 2016-05-24 15:09:11 -04:00
parent 6052e4b3bf
commit fb4791b37c
1 changed files with 60 additions and 74 deletions

View File

@ -20,6 +20,28 @@
#include <assert.h> #include <assert.h>
static std::string const kCMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN =
"CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN";
static std::string const kCMAKE_C_COMPILER_TARGET = "CMAKE_C_COMPILER_TARGET";
static std::string const kCMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN =
"CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN";
static std::string const kCMAKE_CXX_COMPILER_TARGET =
"CMAKE_CXX_COMPILER_TARGET";
static std::string const kCMAKE_ENABLE_EXPORTS = "CMAKE_ENABLE_EXPORTS";
static std::string const kCMAKE_LINK_SEARCH_END_STATIC =
"CMAKE_LINK_SEARCH_END_STATIC";
static std::string const kCMAKE_LINK_SEARCH_START_STATIC =
"CMAKE_LINK_SEARCH_START_STATIC";
static std::string const kCMAKE_OSX_ARCHITECTURES = "CMAKE_OSX_ARCHITECTURES";
static std::string const kCMAKE_OSX_DEPLOYMENT_TARGET =
"CMAKE_OSX_DEPLOYMENT_TARGET";
static std::string const kCMAKE_OSX_SYSROOT = "CMAKE_OSX_SYSROOT";
static std::string const kCMAKE_POSITION_INDEPENDENT_CODE =
"CMAKE_POSITION_INDEPENDENT_CODE";
static std::string const kCMAKE_SYSROOT = "CMAKE_SYSROOT";
static std::string const kCMAKE_TRY_COMPILE_OSX_ARCHITECTURES =
"CMAKE_TRY_COMPILE_OSX_ARCHITECTURES";
int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv, int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
bool isTryRun) bool isTryRun)
{ {
@ -383,76 +405,44 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
fname.c_str()); fname.c_str());
} }
/* for the TRY_COMPILEs we want to be able to specify the architecture. // Forward a set of variables to the inner project cache.
So the user can set CMAKE_OSX_ARCHITECTURES to i386;ppc and then set {
CMAKE_TRY_COMPILE_OSX_ARCHITECTURES first to i386 and then to ppc to std::set<std::string> vars;
have the tests run for each specific architecture. Since vars.insert(kCMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN);
cmLocalGenerator doesn't allow building for "the other" vars.insert(kCMAKE_C_COMPILER_TARGET);
architecture only via CMAKE_OSX_ARCHITECTURES. vars.insert(kCMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN);
*/ vars.insert(kCMAKE_CXX_COMPILER_TARGET);
if (this->Makefile->GetDefinition("CMAKE_TRY_COMPILE_OSX_ARCHITECTURES") != vars.insert(kCMAKE_ENABLE_EXPORTS);
0) { vars.insert(kCMAKE_LINK_SEARCH_END_STATIC);
std::string flag = "-DCMAKE_OSX_ARCHITECTURES="; vars.insert(kCMAKE_LINK_SEARCH_START_STATIC);
flag += this->Makefile->GetSafeDefinition( vars.insert(kCMAKE_OSX_ARCHITECTURES);
"CMAKE_TRY_COMPILE_OSX_ARCHITECTURES"); vars.insert(kCMAKE_OSX_DEPLOYMENT_TARGET);
cmakeFlags.push_back(flag); vars.insert(kCMAKE_OSX_SYSROOT);
} else if (this->Makefile->GetDefinition("CMAKE_OSX_ARCHITECTURES") != 0) { vars.insert(kCMAKE_POSITION_INDEPENDENT_CODE);
std::string flag = "-DCMAKE_OSX_ARCHITECTURES="; vars.insert(kCMAKE_SYSROOT);
flag += this->Makefile->GetSafeDefinition("CMAKE_OSX_ARCHITECTURES");
cmakeFlags.push_back(flag); /* for the TRY_COMPILEs we want to be able to specify the architecture.
} So the user can set CMAKE_OSX_ARCHITECTURES to i386;ppc and then set
/* on APPLE also pass CMAKE_OSX_SYSROOT to the try_compile */ CMAKE_TRY_COMPILE_OSX_ARCHITECTURES first to i386 and then to ppc to
if (this->Makefile->GetDefinition("CMAKE_OSX_SYSROOT") != 0) { have the tests run for each specific architecture. Since
std::string flag = "-DCMAKE_OSX_SYSROOT="; cmLocalGenerator doesn't allow building for "the other"
flag += this->Makefile->GetSafeDefinition("CMAKE_OSX_SYSROOT"); architecture only via CMAKE_OSX_ARCHITECTURES.
cmakeFlags.push_back(flag); */
} if (const char* tcArchs = this->Makefile->GetDefinition(
/* on APPLE also pass CMAKE_OSX_DEPLOYMENT_TARGET to the try_compile */ kCMAKE_TRY_COMPILE_OSX_ARCHITECTURES)) {
if (this->Makefile->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET") != 0) { vars.erase(kCMAKE_OSX_ARCHITECTURES);
std::string flag = "-DCMAKE_OSX_DEPLOYMENT_TARGET="; std::string flag = "-DCMAKE_OSX_ARCHITECTURES=" + std::string(tcArchs);
flag += this->Makefile->GetSafeDefinition("CMAKE_OSX_DEPLOYMENT_TARGET"); cmakeFlags.push_back(flag);
cmakeFlags.push_back(flag); }
}
if (const char* cxxDef = for (std::set<std::string>::iterator vi = vars.begin(); vi != vars.end();
this->Makefile->GetDefinition("CMAKE_CXX_COMPILER_TARGET")) { ++vi) {
std::string flag = "-DCMAKE_CXX_COMPILER_TARGET="; std::string const& var = *vi;
flag += cxxDef; if (const char* val = this->Makefile->GetDefinition(var)) {
cmakeFlags.push_back(flag); std::string flag = "-D" + var + "=" + val;
} cmakeFlags.push_back(flag);
if (const char* cDef = }
this->Makefile->GetDefinition("CMAKE_C_COMPILER_TARGET")) { }
std::string flag = "-DCMAKE_C_COMPILER_TARGET=";
flag += cDef;
cmakeFlags.push_back(flag);
}
if (const char* tcxxDef = this->Makefile->GetDefinition(
"CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN")) {
std::string flag = "-DCMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN=";
flag += tcxxDef;
cmakeFlags.push_back(flag);
}
if (const char* tcDef = this->Makefile->GetDefinition(
"CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN")) {
std::string flag = "-DCMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN=";
flag += tcDef;
cmakeFlags.push_back(flag);
}
if (const char* rootDef = this->Makefile->GetDefinition("CMAKE_SYSROOT")) {
std::string flag = "-DCMAKE_SYSROOT=";
flag += rootDef;
cmakeFlags.push_back(flag);
}
if (this->Makefile->GetDefinition("CMAKE_POSITION_INDEPENDENT_CODE") !=
0) {
fprintf(fout, "set(CMAKE_POSITION_INDEPENDENT_CODE \"ON\")\n");
}
if (const char* lssDef =
this->Makefile->GetDefinition("CMAKE_LINK_SEARCH_START_STATIC")) {
fprintf(fout, "set(CMAKE_LINK_SEARCH_START_STATIC \"%s\")\n", lssDef);
}
if (const char* lssDef =
this->Makefile->GetDefinition("CMAKE_LINK_SEARCH_END_STATIC")) {
fprintf(fout, "set(CMAKE_LINK_SEARCH_END_STATIC \"%s\")\n", lssDef);
} }
/* Set the appropriate policy information for ENABLE_EXPORTS */ /* Set the appropriate policy information for ENABLE_EXPORTS */
@ -461,10 +451,6 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
cmPolicies::NEW cmPolicies::NEW
? "NEW" ? "NEW"
: "OLD"); : "OLD");
if (const char* ee =
this->Makefile->GetDefinition("CMAKE_ENABLE_EXPORTS")) {
fprintf(fout, "set(CMAKE_ENABLE_EXPORTS %s)\n", ee);
}
if (targetType == cmState::EXECUTABLE) { if (targetType == cmState::EXECUTABLE) {
/* Put the executable at a known location (for COPY_FILE). */ /* Put the executable at a known location (for COPY_FILE). */