summaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
authorStephen Kelly <steveire@gmail.com>2012-05-30 18:13:09 (GMT)
committerBrad King <brad.king@kitware.com>2012-06-12 19:38:48 (GMT)
commitbd3496300262bd26073ce03e020731c592249148 (patch)
treec5beb67ff16ac1da10ec76aebf8f62fc3a97b75f /Source
parent55d7aa4c44d78322df76679db768154568e51385 (diff)
downloadCMake-bd3496300262bd26073ce03e020731c592249148.zip
CMake-bd3496300262bd26073ce03e020731c592249148.tar.gz
CMake-bd3496300262bd26073ce03e020731c592249148.tar.bz2
Refactor generation of shared library flags
CMAKE_SHARED_LIBRARY_<lang>_FLAGS has flags on various platforms for a variety of purposes that are correlated with shared libraries but not exclusive to them. Refactor generation of these flags to use new purpose-specific platform variables CMAKE_<lang>_COMPILE_OPTIONS_DLL CMAKE_<lang>_COMPILE_OPTIONS_PIC CMAKE_<lang>_COMPILE_OPTIONS_PIE Activate the DLL flags specifically for shared libraries. Add a new POSITION_INDEPENDENT_CODE target property to activate PIC/PIE flags, and default to true for shared libraries to preserve default behavior. Initialize the new property from CMAKE_POSITION_INDEPENDENT_CODE to allow easy global configuration in projects. Although the default behavior is unchanged by this refactoring, the new approach ignores CMAKE_SHARED_LIBRARY_<lang>_FLAGS completely. We must leave it set in case projects reference the value. Furthermore, if a project modifies CMAKE_SHARED_LIBRARY_<lang>_FLAGS it expects the new value to be used. Add policy CMP0018 to handle compatibility with projects that modify this platform variable. Add a PositionIndependentCode test on platforms where we can get meaningful results.
Diffstat (limited to 'Source')
-rw-r--r--Source/cmCoreTryCompile.cxx4
-rw-r--r--Source/cmDocumentVariables.cxx8
-rw-r--r--Source/cmGlobalGenerator.cxx21
-rw-r--r--Source/cmGlobalGenerator.h3
-rw-r--r--Source/cmGlobalXCodeGenerator.cxx4
-rw-r--r--Source/cmLocalGenerator.cxx111
-rw-r--r--Source/cmLocalGenerator.h8
-rw-r--r--Source/cmMakefileTargetGenerator.cxx6
-rw-r--r--Source/cmNinjaTargetGenerator.cxx7
-rw-r--r--Source/cmPolicies.cxx28
-rw-r--r--Source/cmPolicies.h3
-rw-r--r--Source/cmTarget.cxx15
12 files changed, 199 insertions, 19 deletions
diff --git a/Source/cmCoreTryCompile.cxx b/Source/cmCoreTryCompile.cxx
index ed485e3..1ae7035 100644
--- a/Source/cmCoreTryCompile.cxx
+++ b/Source/cmCoreTryCompile.cxx
@@ -281,6 +281,10 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv)
flag += this->Makefile->GetSafeDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
cmakeFlags.push_back(flag);
}
+ if(this->Makefile->GetDefinition("CMAKE_POSITION_INDEPENDENT_CODE")!=0)
+ {
+ fprintf(fout, "SET(CMAKE_POSITION_INDEPENDENT_CODE \"ON\")\n");
+ }
/* Use a random file name to avoid rapid creation and deletion
of the same executable name (some filesystems fail on that). */
diff --git a/Source/cmDocumentVariables.cxx b/Source/cmDocumentVariables.cxx
index 9e33d75..592d931 100644
--- a/Source/cmDocumentVariables.cxx
+++ b/Source/cmDocumentVariables.cxx
@@ -1350,6 +1350,14 @@ void cmDocumentVariables::DefineVariables(cmake* cm)
"See that target property for additional information.",
false,
"Variables that Control the Build");
+ cm->DefineProperty
+ ("CMAKE_POSITION_INDEPENDENT_FLAGS", cmProperty::VARIABLE,
+ "Default value for POSITION_INDEPENDENT_CODE of targets.",
+ "This variable is used to initialize the "
+ "POSITION_INDEPENDENT_CODE property on all the targets. "
+ "See that target property for additional information.",
+ false,
+ "Variables that Control the Build");
// Variables defined when the a language is enabled These variables will
// also be defined whenever CMake has loaded its support for compiling (LANG)
diff --git a/Source/cmGlobalGenerator.cxx b/Source/cmGlobalGenerator.cxx
index b06cdb4..f3a7090 100644
--- a/Source/cmGlobalGenerator.cxx
+++ b/Source/cmGlobalGenerator.cxx
@@ -586,6 +586,16 @@ cmGlobalGenerator::EnableLanguage(std::vector<std::string>const& languages,
}
} // end if in try compile
} // end need test language
+ // Store the shared library flags so that we can satisfy CMP0018
+ std::string sharedLibFlagsVar = "CMAKE_SHARED_LIBRARY_";
+ sharedLibFlagsVar += lang;
+ sharedLibFlagsVar += "_FLAGS";
+ const char* sharedLibFlags =
+ mf->GetSafeDefinition(sharedLibFlagsVar.c_str());
+ if (sharedLibFlags)
+ {
+ this->LanguageToOriginalSharedLibFlags[lang] = sharedLibFlags;
+ }
} // end for each language
// Now load files that can override any settings on the platform or for
@@ -2107,6 +2117,17 @@ cmGlobalGenerator::GenerateRuleFile(std::string const& output) const
}
//----------------------------------------------------------------------------
+std::string cmGlobalGenerator::GetSharedLibFlagsForLanguage(
+ std::string const& l)
+{
+ if(this->LanguageToOriginalSharedLibFlags.count(l) > 0)
+ {
+ return this->LanguageToOriginalSharedLibFlags[l];
+ }
+ return "";
+}
+
+//----------------------------------------------------------------------------
void cmGlobalGenerator::AppendDirectoryForConfig(const char*, const char*,
const char*, std::string&)
{
diff --git a/Source/cmGlobalGenerator.h b/Source/cmGlobalGenerator.h
index 5254b89..0ac38a0 100644
--- a/Source/cmGlobalGenerator.h
+++ b/Source/cmGlobalGenerator.h
@@ -277,6 +277,8 @@ public:
i.e. "Can I build Debug and Release in the same tree?" */
virtual bool IsMultiConfig() { return false; }
+ std::string GetSharedLibFlagsForLanguage(std::string const& lang);
+
/** Generate an <output>.rule file path for a given command output. */
virtual std::string GenerateRuleFile(std::string const& output) const;
@@ -357,6 +359,7 @@ private:
std::map<cmStdString, cmStdString> LanguageToOutputExtension;
std::map<cmStdString, cmStdString> ExtensionToLanguage;
std::map<cmStdString, int> LanguageToLinkerPreference;
+ std::map<cmStdString, cmStdString> LanguageToOriginalSharedLibFlags;
// Record hashes for rules and outputs.
struct RuleHash { char Data[32]; };
diff --git a/Source/cmGlobalXCodeGenerator.cxx b/Source/cmGlobalXCodeGenerator.cxx
index 522f3da..938977b 100644
--- a/Source/cmGlobalXCodeGenerator.cxx
+++ b/Source/cmGlobalXCodeGenerator.cxx
@@ -1594,14 +1594,14 @@ void cmGlobalXCodeGenerator::CreateBuildSettings(cmTarget& target,
if(strcmp(lang, "CXX") == 0)
{
this->CurrentLocalGenerator->AddLanguageFlags(cflags, "C", configName);
- this->CurrentLocalGenerator->AddSharedFlags(cflags, lang, shared);
+ this->CurrentLocalGenerator->AddCMP0018Flags(cflags, &target, "C");
}
// Add language-specific flags.
this->CurrentLocalGenerator->AddLanguageFlags(flags, lang, configName);
// Add shared-library flags if needed.
- this->CurrentLocalGenerator->AddSharedFlags(flags, lang, shared);
+ this->CurrentLocalGenerator->AddCMP0018Flags(flags, &target, lang);
}
else if(binary)
{
diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx
index 8265d72..5fc5f05 100644
--- a/Source/cmLocalGenerator.cxx
+++ b/Source/cmLocalGenerator.cxx
@@ -1549,12 +1549,6 @@ void cmLocalGenerator::GetTargetFlags(std::string& linkLibs,
return;
}
this->AddLanguageFlags(flags, linkLanguage, buildType.c_str());
- std::string sharedFlagsVar = "CMAKE_SHARED_LIBRARY_";
- sharedFlagsVar += linkLanguage;
- sharedFlagsVar += "_FLAGS";
- flags += " ";
- flags += this->Makefile->GetSafeDefinition(sharedFlagsVar.c_str());
- flags += " ";
cmOStringStream linklibs;
this->OutputLinkLibraries(linklibs, target, false);
linkLibs = linklibs.str();
@@ -1963,6 +1957,111 @@ void cmLocalGenerator::AddSharedFlags(std::string& flags,
}
//----------------------------------------------------------------------------
+void cmLocalGenerator::AddCMP0018Flags(std::string &flags, cmTarget* target,
+ std::string const& lang)
+{
+ int targetType = target->GetType();
+
+ bool shared = ((targetType == cmTarget::SHARED_LIBRARY) ||
+ (targetType == cmTarget::MODULE_LIBRARY));
+
+ if (this->GetShouldUseOldFlags(shared, lang))
+ {
+ this->AddSharedFlags(flags, lang.c_str(), shared);
+ }
+ else
+ {
+ // Add position independendent flags, if needed.
+ if (target->GetPropertyAsBool("POSITION_INDEPENDENT_CODE"))
+ {
+ this->AddPositionIndependentFlags(flags, lang, targetType);
+ }
+ if (shared)
+ {
+ this->AppendFeatureOptions(flags, lang.c_str(), "DLL");
+ }
+ }
+}
+
+//----------------------------------------------------------------------------
+bool cmLocalGenerator::GetShouldUseOldFlags(bool shared,
+ const std::string &lang) const
+{
+ std::string originalFlags =
+ this->GlobalGenerator->GetSharedLibFlagsForLanguage(lang);
+ if (shared)
+ {
+ std::string flagsVar = "CMAKE_SHARED_LIBRARY_";
+ flagsVar += lang;
+ flagsVar += "_FLAGS";
+ const char* flags =
+ this->Makefile->GetSafeDefinition(flagsVar.c_str());
+
+ if (flags && flags != originalFlags)
+ {
+ switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0018))
+ {
+ case cmPolicies::WARN:
+ {
+ cmOStringStream e;
+ e << "Variable " << flagsVar << " has been modified. CMake "
+ "will ignore the POSITION_INDEPENDENT_CODE target property for "
+ "shared libraries and will use the " << flagsVar << " variable "
+ "instead. This may cause errors if the original content of "
+ << flagsVar << " was removed.\n"
+ << this->Makefile->GetPolicies()->GetPolicyWarning(
+ cmPolicies::CMP0018);
+
+ this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, e.str());
+ // fall through to OLD behaviour
+ }
+ case cmPolicies::OLD:
+ return true;
+ case cmPolicies::REQUIRED_IF_USED:
+ case cmPolicies::REQUIRED_ALWAYS:
+ case cmPolicies::NEW:
+ default:
+ return false;
+ }
+ }
+ }
+ return false;
+}
+
+//----------------------------------------------------------------------------
+void cmLocalGenerator::AddPositionIndependentFlags(std::string& flags,
+ std::string const& lang,
+ int targetType)
+{
+ const char* picFlags = 0;
+
+ if(targetType == cmTarget::EXECUTABLE)
+ {
+ std::string flagsVar = "CMAKE_";
+ flagsVar += lang;
+ flagsVar += "_COMPILE_OPTIONS_PIE";
+ picFlags = this->Makefile->GetSafeDefinition(flagsVar.c_str());
+ }
+ if (!picFlags)
+ {
+ std::string flagsVar = "CMAKE_";
+ flagsVar += lang;
+ flagsVar += "_COMPILE_OPTIONS_PIC";
+ picFlags = this->Makefile->GetSafeDefinition(flagsVar.c_str());
+ }
+ if (picFlags)
+ {
+ std::vector<std::string> options;
+ cmSystemTools::ExpandListArgument(picFlags, options);
+ for(std::vector<std::string>::const_iterator oi = options.begin();
+ oi != options.end(); ++oi)
+ {
+ this->AppendFlags(flags, this->EscapeForShell(oi->c_str()).c_str());
+ }
+ }
+}
+
+//----------------------------------------------------------------------------
void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
const char* var,
const char* config)
diff --git a/Source/cmLocalGenerator.h b/Source/cmLocalGenerator.h
index cb84a30..1d8a7d1 100644
--- a/Source/cmLocalGenerator.h
+++ b/Source/cmLocalGenerator.h
@@ -140,7 +140,8 @@ public:
void AddLanguageFlags(std::string& flags, const char* lang,
const char* config);
- void AddSharedFlags(std::string& flags, const char* lang, bool shared);
+ void AddCMP0018Flags(std::string &flags, cmTarget* target,
+ std::string const& lang);
void AddConfigVariableFlags(std::string& flags, const char* var,
const char* config);
///! Append flags to a string.
@@ -425,6 +426,11 @@ protected:
private:
std::string ConvertToOutputForExistingCommon(const char* remote,
std::string const& result);
+
+ void AddSharedFlags(std::string& flags, const char* lang, bool shared);
+ bool GetShouldUseOldFlags(bool shared, const std::string &lang) const;
+ void AddPositionIndependentFlags(std::string& flags, std::string const& l,
+ int targetType);
};
#endif
diff --git a/Source/cmMakefileTargetGenerator.cxx b/Source/cmMakefileTargetGenerator.cxx
index 408f287..df89275 100644
--- a/Source/cmMakefileTargetGenerator.cxx
+++ b/Source/cmMakefileTargetGenerator.cxx
@@ -245,9 +245,6 @@ std::string cmMakefileTargetGenerator::GetFlags(const std::string &l)
std::string flags;
const char *lang = l.c_str();
- bool shared = ((this->Target->GetType() == cmTarget::SHARED_LIBRARY) ||
- (this->Target->GetType() == cmTarget::MODULE_LIBRARY));
-
// Add language feature flags.
this->AddFeatureFlags(flags, lang);
@@ -260,8 +257,7 @@ std::string cmMakefileTargetGenerator::GetFlags(const std::string &l)
this->AddFortranFlags(flags);
}
- // Add shared-library flags if needed.
- this->LocalGenerator->AddSharedFlags(flags, lang, shared);
+ this->LocalGenerator->AddCMP0018Flags(flags, this->Target, lang);
// Add include directory flags.
this->AddIncludeFlags(flags, lang);
diff --git a/Source/cmNinjaTargetGenerator.cxx b/Source/cmNinjaTargetGenerator.cxx
index 80007f1..b3394bb 100644
--- a/Source/cmNinjaTargetGenerator.cxx
+++ b/Source/cmNinjaTargetGenerator.cxx
@@ -140,11 +140,8 @@ cmNinjaTargetGenerator::ComputeFlagsForObject(cmSourceFile *source,
// }
// Add shared-library flags if needed.
- {
- bool shared = ((this->Target->GetType() == cmTarget::SHARED_LIBRARY) ||
- (this->Target->GetType() == cmTarget::MODULE_LIBRARY));
- this->GetLocalGenerator()->AddSharedFlags(flags, language.c_str(), shared);
- }
+ this->LocalGenerator->AddCMP0018Flags(flags, this->Target,
+ language.c_str());
// TODO: Handle response file.
// Add include directory flags.
diff --git a/Source/cmPolicies.cxx b/Source/cmPolicies.cxx
index 37070b6..79af4d7 100644
--- a/Source/cmPolicies.cxx
+++ b/Source/cmPolicies.cxx
@@ -463,6 +463,34 @@ cmPolicies::cmPolicies()
"The OLD behaviour is to always prefer files from CMAKE_MODULE_PATH over "
"files from the CMake modules directory.",
2,8,4,0, cmPolicies::WARN);
+
+ this->DefinePolicy(
+ CMP0018, "CMP0018",
+ "Ignore CMAKE_SHARED_LIBRARY_<Lang>_FLAGS variable.",
+ "CMake 2.8.8 and lower compiled sources in SHARED and MODULE libraries "
+ "using the value of the undocumented CMAKE_SHARED_LIBRARY_<Lang>_FLAGS "
+ "platform variable. The variable contained platform-specific flags "
+ "needed to compile objects for shared libraries. Typically it included "
+ "a flag such as -fPIC for position independent code but also included "
+ "other flags needed on certain platforms. CMake 2.8.9 and higher "
+ "prefer instead to use the POSITION_INDEPENDENT_CODE target property to "
+ "determine what targets should be position independent, and new "
+ "undocumented platform variables to select flags while ignoring "
+ "CMAKE_SHARED_LIBRARY_<Lang>_FLAGS completely."
+ "\n"
+ "The default for either approach produces identical compilation flags, "
+ "but if a project modifies CMAKE_SHARED_LIBRARY_<Lang>_FLAGS from its "
+ "original value this policy determines which approach to use."
+ "\n"
+ "The OLD behavior for this policy is to ignore the "
+ "POSITION_INDEPENDENT_CODE property for all targets and use the modified "
+ "value of CMAKE_SHARED_LIBRARY_<Lang>_FLAGS for SHARED and MODULE "
+ "libraries."
+ "\n"
+ "The NEW behavior for this policy is to ignore "
+ "CMAKE_SHARED_LIBRARY_<Lang>_FLAGS whether it is modified or not and "
+ "honor the POSITION_INDEPENDENT_CODE target property.",
+ 2,8,9,0, cmPolicies::WARN);
}
cmPolicies::~cmPolicies()
diff --git a/Source/cmPolicies.h b/Source/cmPolicies.h
index 3106248..6932565 100644
--- a/Source/cmPolicies.h
+++ b/Source/cmPolicies.h
@@ -65,6 +65,9 @@ public:
/// target_link_libraries() fails if only argument is not a target
CMP0016,
CMP0017, ///< Prefer files in CMAKE_ROOT when including from CMAKE_ROOT
+ CMP0018, ///< Ignore language flags for shared libs, and adhere to
+ /// POSITION_INDEPENDENT_CODE property and *_COMPILE_OPTIONS_PI{E,C}
+ /// instead.
/** \brief Always the last entry.
*
diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx
index c5ef9ff..4f3f2c5 100644
--- a/Source/cmTarget.cxx
+++ b/Source/cmTarget.cxx
@@ -756,6 +756,14 @@ void cmTarget::DefineProperties(cmake *cm)
"(such as \"lib\") on a library name.");
cm->DefineProperty
+ ("POSITION_INDEPENDENT_CODE", cmProperty::TARGET,
+ "Whether to create a position-independent target",
+ "The POSITION_INDEPENDENT_CODE property determines whether position "
+ "independent executables or shared libraries will be created. "
+ "This property is true by default for SHARED and MODULE library "
+ "targets and false otherwise.");
+
+ cm->DefineProperty
("POST_INSTALL_SCRIPT", cmProperty::TARGET,
"Deprecated install support.",
"The PRE_INSTALL_SCRIPT and POST_INSTALL_SCRIPT properties are the "
@@ -1305,6 +1313,13 @@ void cmTarget::SetMakefile(cmMakefile* mf)
this->SetProperty("INCLUDE_DIRECTORIES",
this->Makefile->GetProperty("INCLUDE_DIRECTORIES"));
+ if(this->TargetTypeValue == cmTarget::SHARED_LIBRARY
+ || this->TargetTypeValue == cmTarget::MODULE_LIBRARY)
+ {
+ this->SetProperty("POSITION_INDEPENDENT_CODE", "True");
+ }
+ this->SetPropertyDefault("POSITION_INDEPENDENT_CODE", 0);
+
// Record current policies for later use.
this->PolicyStatusCMP0003 =
this->Makefile->GetPolicyStatus(cmPolicies::CMP0003);