Merge topic 'position-independent-targets'

bd34963 Refactor generation of shared library flags
55d7aa4 Add platform variable for flags specific to shared libraries
31d7a0f Add platform variables for position independent code flags
This commit is contained in:
David Cole 2012-06-12 16:01:04 -04:00 committed by CMake Topic Stage
commit c95d1baa19
47 changed files with 420 additions and 29 deletions

View File

@ -100,6 +100,18 @@ IF(NOT CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS)
SET(CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS ${CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS})
ENDIF(NOT CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS)
IF(NOT CMAKE_CXX_COMPILE_OPTIONS_PIC)
SET(CMAKE_CXX_COMPILE_OPTIONS_PIC ${CMAKE_C_COMPILE_OPTIONS_PIC})
ENDIF(NOT CMAKE_CXX_COMPILE_OPTIONS_PIC)
IF(NOT CMAKE_CXX_COMPILE_OPTIONS_PIE)
SET(CMAKE_CXX_COMPILE_OPTIONS_PIE ${CMAKE_C_COMPILE_OPTIONS_PIE})
ENDIF(NOT CMAKE_CXX_COMPILE_OPTIONS_PIE)
IF(NOT CMAKE_CXX_COMPILE_OPTIONS_DLL)
SET(CMAKE_CXX_COMPILE_OPTIONS_DLL ${CMAKE_C_COMPILE_OPTIONS_DLL})
ENDIF(NOT CMAKE_CXX_COMPILE_OPTIONS_DLL)
IF(NOT CMAKE_SHARED_LIBRARY_CXX_FLAGS)
SET(CMAKE_SHARED_LIBRARY_CXX_FLAGS ${CMAKE_SHARED_LIBRARY_C_FLAGS})
ENDIF(NOT CMAKE_SHARED_LIBRARY_CXX_FLAGS)

View File

@ -74,6 +74,18 @@ ENDIF()
# catch any modules
SET(CMAKE_NEEDS_REQUIRES_STEP_Fortran_FLAG 1)
IF(NOT CMAKE_Fortran_COMPILE_OPTIONS_PIC)
SET(CMAKE_Fortran_COMPILE_OPTIONS_PIC ${CMAKE_C_COMPILE_OPTIONS_PIC})
ENDIF(NOT CMAKE_Fortran_COMPILE_OPTIONS_PIC)
IF(NOT CMAKE_Fortran_COMPILE_OPTIONS_PIE)
SET(CMAKE_Fortran_COMPILE_OPTIONS_PIE ${CMAKE_C_COMPILE_OPTIONS_PIE})
ENDIF(NOT CMAKE_Fortran_COMPILE_OPTIONS_PIE)
IF(NOT CMAKE_Fortran_COMPILE_OPTIONS_DLL)
SET(CMAKE_Fortran_COMPILE_OPTIONS_DLL ${CMAKE_C_COMPILE_OPTIONS_DLL})
ENDIF(NOT CMAKE_Fortran_COMPILE_OPTIONS_DLL)
# Create a set of shared library variable specific to Fortran
# For 90% of the systems, these are the same flags as the C versions
# so if these are not set just copy the flags from the c version

View File

@ -21,6 +21,10 @@ set(__COMPILER_GNU 1)
macro(__compiler_gnu lang)
# Feature flags.
set(CMAKE_${lang}_VERBOSE_FLAG "-v")
set(CMAKE_${lang}_COMPILE_OPTIONS_PIC "-fPIC")
if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 3.4)
set(CMAKE_${lang}_COMPILE_OPTIONS_PIE "-fPIE")
endif()
set(CMAKE_SHARED_LIBRARY_${lang}_FLAGS "-fPIC")
set(CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS "-shared")

View File

@ -20,6 +20,9 @@ set(__COMPILER_SCO 1)
macro(__compiler_sco lang)
# Feature flags.
set(CMAKE_${lang}_COMPILE_OPTIONS_PIC -Kpic)
set(CMAKE_${lang}_COMPILE_OPTIONS_PIE -Kpie)
set(CMAKE_${lang}_COMPILE_OPTIONS_DLL -belf)
set(CMAKE_SHARED_LIBRARY_${lang}_FLAGS "-Kpic -belf")
set(CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS "-belf -Wl,-Bexport")
endmacro()

View File

@ -1,5 +1,7 @@
SET(CMAKE_C_VERBOSE_FLAG "-#")
SET(CMAKE_C_COMPILE_OPTIONS_PIC -KPIC)
SET(CMAKE_C_COMPILE_OPTIONS_PIE -KPIE)
SET(CMAKE_SHARED_LIBRARY_C_FLAGS "-KPIC")
SET(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-G")
SET(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG "-R")

View File

@ -1,5 +1,7 @@
SET(CMAKE_CXX_VERBOSE_FLAG "-v")
SET(CMAKE_CXX_COMPILE_OPTIONS_PIC -KPIC)
SET(CMAKE_CXX_COMPILE_OPTIONS_PIE -KPIE)
SET(CMAKE_SHARED_LIBRARY_CXX_FLAGS "-KPIC")
SET(CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS "-G")
SET(CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG "-R")

View File

@ -1,6 +1,8 @@
SET(BEOS 1)
SET(CMAKE_DL_LIBS root be)
SET(CMAKE_C_COMPILE_OPTIONS_PIC "-fPIC")
SET(CMAKE_C_COMPILE_OPTIONS_PIE "-fPIE")
SET(CMAKE_SHARED_LIBRARY_C_FLAGS "-fPIC")
SET(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-nostart")
SET(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG "-Wl,-rpath,")

View File

@ -85,11 +85,15 @@ set(CMAKE_DL_LIBS "dl")
macro(__BlueGeneP_set_dynamic_flags compiler_id lang)
if (${compiler_id} STREQUAL XL)
# Flags for XL compilers if we explicitly detected XL
set(CMAKE_${lang}_COMPILE_OPTIONS_PIC "-qpic")
set(CMAKE_${lang}_COMPILE_OPTIONS_PIE "-qpie")
set(CMAKE_SHARED_LIBRARY_${lang}_FLAGS "-qpic")
set(CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS "-qmkshrobj -qnostaticlink")
set(BGP_${lang}_DYNAMIC_EXE_FLAGS "-qnostaticlink -qnostaticlink=libgcc")
else()
# Assume flags for GNU compilers (if the ID is GNU *or* anything else).
set(CMAKE_${lang}_COMPILE_OPTIONS_PIC "-fPIC")
set(CMAKE_${lang}_COMPILE_OPTIONS_PIE "-fPIE")
set(CMAKE_SHARED_LIBRARY_${lang}_FLAGS "-fPIC")
set(CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS "-shared")
set(BGP_${lang}_DYNAMIC_EXE_FLAGS "-dynamic")

View File

@ -35,7 +35,10 @@ macro(__cygwin_compiler_gnu lang)
set(CMAKE_${lang}_LINK_EXECUTABLE
"<CMAKE_${lang}_COMPILER> <FLAGS> <CMAKE_${lang}_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> -Wl,--out-implib,<TARGET_IMPLIB> ${CMAKE_GNULD_IMAGE_VERSION} <LINK_LIBRARIES>")
set(CMAKE_SHARED_LIBRARY_${lang}_FLAGS "") # No -fPIC on cygwin
# No -fPIC on cygwin
set(CMAKE_${lang}_COMPILE_OPTIONS_PIC "")
set(CMAKE_${lang}_COMPILE_OPTIONS_PIE "")
set(CMAKE_SHARED_LIBRARY_${lang}_FLAGS "")
# Initialize C link type selection flags. These flags are used when
# building a shared library, shared module, or executable that links

View File

@ -1,6 +1,8 @@
IF(EXISTS /usr/include/dlfcn.h)
SET(CMAKE_DL_LIBS "")
SET(CMAKE_SHARED_LIBRARY_C_FLAGS "-fPIC") # -pic
SET(CMAKE_C_COMPILE_OPTIONS_PIC "-fPIC")
SET(CMAKE_C_COMPILE_OPTIONS_PIE "-fPIE")
SET(CMAKE_SHARED_LIBRARY_C_FLAGS "-fPIC") # -pic
SET(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-shared") # -shared
SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") # +s, flag for exe link to use shared lib
SET(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG "-Wl,-rpath,") # -rpath

View File

@ -19,6 +19,7 @@ endif()
set(__HPUX_COMPILER_HP 1)
macro(__hpux_compiler_hp lang)
set(CMAKE_${lang}_COMPILE_OPTIONS_PIC "+Z")
set(CMAKE_SHARED_LIBRARY_${lang}_FLAGS "+Z")
set(CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS "-Wl,-E,+nodefaultrpath -b -L/usr/lib")
set(CMAKE_SHARED_LIBRARY_LINK_${lang}_FLAGS "-Wl,+s,-E,+nodefaultrpath")

View File

@ -1,6 +1,8 @@
SET(BEOS 1)
SET(CMAKE_DL_LIBS root be)
SET(CMAKE_C_COMPILE_OPTIONS_PIC "-fPIC")
SET(CMAKE_C_COMPILE_OPTIONS_PIE "-fPIE")
SET(CMAKE_SHARED_LIBRARY_C_FLAGS "-fPIC")
SET(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-nostart")
SET(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG "-Wl,-rpath,")

View File

@ -31,6 +31,8 @@ if(NOT XIAR)
endif(NOT XIAR)
macro(__linux_compiler_intel lang)
set(CMAKE_${lang}_COMPILE_OPTIONS_PIC "-fPIC")
set(CMAKE_${lang}_COMPILE_OPTIONS_PIE "-fPIE")
set(CMAKE_SHARED_LIBRARY_${lang}_FLAGS "-fPIC")
set(CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS "-shared")

View File

@ -20,6 +20,8 @@ set(__LINUX_COMPILER_PGI 1)
macro(__linux_compiler_pgi lang)
# Shared library compile and link flags.
set(CMAKE_${lang}_COMPILE_OPTIONS_PIC "-fPIC")
set(CMAKE_${lang}_COMPILE_OPTIONS_PIE "-fPIE")
set(CMAKE_SHARED_LIBRARY_${lang}_FLAGS "-fPIC")
set(CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS "-shared")
endmacro()

View File

@ -20,6 +20,8 @@ set(__LINUX_COMPILER_PATHSCALE 1)
macro(__linux_compiler_pathscale lang)
# Shared library compile and link flags.
set(CMAKE_${lang}_COMPILE_OPTIONS_PIC "-fPIC")
set(CMAKE_${lang}_COMPILE_OPTIONS_PIE "-fPIE")
set(CMAKE_SHARED_LIBRARY_${lang}_FLAGS "-fPIC")
set(CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS "-shared")
endmacro()

View File

@ -1,6 +1,10 @@
IF(CMAKE_SYSTEM MATCHES "MP-RAS-02*.")
SET(CMAKE_C_COMPILE_OPTIONS_PIC -K PIC)
SET(CMAKE_C_COMPILE_OPTIONS_PIE -K PIE)
SET(CMAKE_SHARED_LIBRARY_C_FLAGS "-K PIC")
ELSE(CMAKE_SYSTEM MATCHES "MP-RAS-02*.")
SET(CMAKE_C_COMPILE_OPTIONS_PIC -K PIC)
SET(CMAKE_C_COMPILE_OPTIONS_PIE -K PIE)
SET(CMAKE_SHARED_LIBRARY_C_FLAGS "-K PIC")
SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "-Wl,-Bexport")
ENDIF(CMAKE_SYSTEM MATCHES "MP-RAS-02*.")

View File

@ -1,6 +1,8 @@
IF(EXISTS /usr/include/dlfcn.h)
SET(CMAKE_DL_LIBS "")
SET(CMAKE_SHARED_LIBRARY_C_FLAGS "-fPIC") # -pic
SET(CMAKE_C_COMPILE_OPTIONS_PIC "-fPIC")
SET(CMAKE_C_COMPILE_OPTIONS_PIE "-fPIE")
SET(CMAKE_SHARED_LIBRARY_C_FLAGS "-fPIC") # -pic
SET(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-shared") # -shared
SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") # +s, flag for exe link to use shared lib
SET(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG "-Wl,-rpath,") # -rpath

View File

@ -4,7 +4,9 @@ IF(CMAKE_SYSTEM MATCHES "OSF1-1.[012]")
ENDIF(CMAKE_SYSTEM MATCHES "OSF1-1.[012]")
IF(CMAKE_SYSTEM MATCHES "OSF1-1.*")
# OSF/1 1.3 from OSF using ELF, and derivatives, including AD2
SET(CMAKE_SHARED_LIBRARY_C_FLAGS "-fpic") # -pic
SET(CMAKE_C_COMPILE_OPTIONS_PIC "-fpic")
SET(CMAKE_C_COMPILE_OPTIONS_PIE "-fpie")
SET(CMAKE_SHARED_LIBRARY_C_FLAGS "-fpic") # -pic
SET(CMAKE_SHARED_LIBRARY_CXX_FLAGS "-fpic") # -pic
ENDIF(CMAKE_SYSTEM MATCHES "OSF1-1.*")

View File

@ -1,2 +1,4 @@
SET(CMAKE_C_COMPILE_OPTIONS_PIC -K PIC)
SET(CMAKE_C_COMPILE_OPTIONS_PIE "")
SET(CMAKE_SHARED_LIBRARY_C_FLAGS "-K PIC")
INCLUDE(Platform/UnixPaths)

View File

@ -1,6 +1,8 @@
IF(CMAKE_SYSTEM MATCHES "SunOS-4.*")
SET(CMAKE_SHARED_LIBRARY_C_FLAGS "-PIC")
SET(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-shared -Wl,-r")
SET(CMAKE_C_COMPILE_OPTIONS_PIC "-PIC")
SET(CMAKE_C_COMPILE_OPTIONS_PIE "-PIE")
SET(CMAKE_SHARED_LIBRARY_C_FLAGS "-PIC")
SET(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-shared -Wl,-r")
SET(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG "-Wl,-R")
SET(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG_SEP ":")
ENDIF(CMAKE_SYSTEM MATCHES "SunOS-4.*")

View File

@ -1,3 +1,5 @@
SET(CMAKE_C_COMPILE_OPTIONS_PIC -K PIC)
SET(CMAKE_C_COMPILE_OPTIONS_PIE "")
SET(CMAKE_SHARED_LIBRARY_C_FLAGS "-K PIC")
SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "-Wl,-Bexport")
INCLUDE(Platform/UnixPaths)

View File

@ -1,3 +1,5 @@
SET(CMAKE_C_COMPILE_OPTIONS_PIC -K PIC)
SET(CMAKE_C_COMPILE_OPTIONS_PIE "")
SET(CMAKE_SHARED_LIBRARY_C_FLAGS "-K PIC")
SET(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-Wl,-Bexport")
INCLUDE(Platform/UnixPaths)

View File

@ -76,7 +76,8 @@ SET (CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO_INIT ${CMAKE_SHARED_LINKER_FLAGS_R
macro(__embarcadero_language lang)
set(CMAKE_SHARED_LIBRARY_${lang}_FLAGS "${_tD}")
set(CMAKE_${lang}_COMPILE_OPTIONS_DLL "${_tD}") # Note: This variable is a ';' separated list
set(CMAKE_SHARED_LIBRARY_${lang}_FLAGS "${_tD}") # ... while this is a space separated string.
# compile a source file into an object file
# place <DEFINES> outside the response file because Borland refuses

View File

@ -77,7 +77,11 @@ macro(__windows_compiler_gnu lang)
endforeach(type)
endif()
set(CMAKE_SHARED_LIBRARY_${lang}_FLAGS "") # No -fPIC on Windows
# No -fPIC on Windows
set(CMAKE_${lang}_COMPILE_OPTIONS_PIC "")
set(CMAKE_${lang}_COMPILE_OPTIONS_PIE "")
set(CMAKE_SHARED_LIBRARY_${lang}_FLAGS "")
set(CMAKE_${lang}_USE_RESPONSE_FILE_FOR_OBJECTS ${__WINDOWS_GNU_LD_RESPONSE})
set(CMAKE_${lang}_USE_RESPONSE_FILE_FOR_INCLUDES 1)

View File

@ -20,7 +20,8 @@ SET (CMAKE_SHARED_LINKER_FLAGS_DEBUG_INIT "debug all" )
SET (CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO_INIT "debug all" )
SET (CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO_INIT "debug all" )
set (CMAKE_SHARED_LIBRARY_C_FLAGS "-bd" )
set(CMAKE_C_COMPILE_OPTIONS_DLL "-bd") # Note: This variable is a ';' separated list
set(CMAKE_SHARED_LIBRARY_C_FLAGS "-bd") # ... while this is a space separated string.
SET(CMAKE_RC_COMPILER "rc" )

View File

@ -10,7 +10,9 @@
SET(CMAKE_DL_LIBS "dl")
SET(CMAKE_SHARED_LIBRARY_C_FLAGS "-fPIC") # -pic
SET(CMAKE_C_COMPILE_OPTIONS_PIC "-fPIC")
SET(CMAKE_C_COMPILE_OPTIONS_PIE "-fPIE")
SET(CMAKE_SHARED_LIBRARY_C_FLAGS "-fPIC") # -pic
SET(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-shared") # -shared
SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") # +s, flag for exe link to use shared lib
SET(CMAKE_SHARED_LIBRARY_SONAME_C_FLAG "-Wl,-soname,")

View File

@ -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). */

View File

@ -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)

View File

@ -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
@ -2106,6 +2116,17 @@ cmGlobalGenerator::GenerateRuleFile(std::string const& output) const
return ruleFile;
}
//----------------------------------------------------------------------------
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&)

View File

@ -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;
@ -359,6 +361,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]; };

View File

@ -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)
{

View File

@ -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();
@ -1962,6 +1956,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,

View File

@ -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

View File

@ -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);

View File

@ -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.

View File

@ -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()

View File

@ -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.
*

View File

@ -755,6 +755,14 @@ void cmTarget::DefineProperties(cmake *cm)
"A target property that can be set to override the prefix "
"(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.",
@ -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);

View File

@ -315,6 +315,24 @@ IF(BUILD_TESTING)
ADD_TEST_MACRO(Module.GenerateExportHeader GenerateExportHeader)
if (APPLE OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(-fPIE run_pic_test)
else()
if (CMAKE_CXX_COMPILER_ID MATCHES "PGI"
OR CMAKE_CXX_COMPILER_ID MATCHES "PathScale"
OR CMAKE_SYSTEM_NAME MATCHES "IRIX64"
OR CMAKE_CXX_COMPILER_ID MATCHES "Intel")
set(run_pic_test 0)
else()
set(run_pic_test 1)
endif()
endif()
if (run_pic_test)
ADD_TEST_MACRO(PositionIndependentTargets PositionIndependentTargets)
endif()
ADD_TEST(LinkFlags-prepare
${CMAKE_CTEST_COMMAND} -C \${CTEST_CONFIGURATION_TYPE}
--build-and-test

View File

@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 2.8)
project(PositionIndependentTargets)
include(CheckCXXSourceCompiles)
include_directories("${CMAKE_CURRENT_SOURCE_DIR}") # For pic_test.h
add_subdirectory(global)
add_subdirectory(targets)
add_executable(PositionIndependentTargets main.cpp)

View File

@ -0,0 +1,37 @@
set(CMAKE_POSITION_INDEPENDENT_CODE True)
add_executable(test_target_executable_global
"${CMAKE_CURRENT_SOURCE_DIR}/../pic_main.cpp"
)
add_library(test_target_shared_library_global
SHARED "${CMAKE_CURRENT_SOURCE_DIR}/../pic_lib.cpp"
)
set_target_properties(test_target_shared_library_global
PROPERTIES DEFINE_SYMBOL PIC_TEST_BUILD_DLL
)
add_library(test_target_static_library_global
STATIC "${CMAKE_CURRENT_SOURCE_DIR}/../pic_lib.cpp"
)
set_target_properties(test_target_static_library_global
PROPERTIES COMPILE_DEFINITIONS PIC_TEST_STATIC_BUILD
)
file(READ
"${CMAKE_CURRENT_SOURCE_DIR}/../pic_test.h"
PIC_HEADER_CONTENT
)
check_cxx_source_compiles(
"
${PIC_HEADER_CONTENT}
int main(int,char**) { return 0; }\n"
PIC_TRY_COMPILE_RESULT
)
if (NOT PIC_TRY_COMPILE_RESULT)
message(SEND_ERROR "TRY_COMPILE with content requiring __PIC__ failed. ${OUTPUT}")
endif()

View File

@ -0,0 +1,2 @@
int main(int,char**) { return 0; }

View File

@ -0,0 +1,12 @@
#include "pic_test.h"
class PIC_TEST_EXPORT Dummy
{
int dummy();
};
int Dummy::dummy()
{
return 0;
}

View File

@ -0,0 +1,4 @@
#include "pic_test.h"
int main(int,char**) { return 0; }

View File

@ -0,0 +1,20 @@
#if defined(__ELF__)
# if !defined(__PIC__)
# error "The POSITION_INDEPENDENT_CODE property should cause __PIC__ to be defined on ELF platforms."
# endif
#endif
#if defined(PIC_TEST_STATIC_BUILD)
# define PIC_TEST_EXPORT
#else
# if defined(_WIN32) || defined(WIN32) /* Win32 version */
# ifdef PIC_TEST_BUILD_DLL
# define PIC_TEST_EXPORT __declspec(dllexport)
# else
# define PIC_TEST_EXPORT __declspec(dllimport)
# endif
# else
# define PIC_TEST_EXPORT
# endif
#endif

View File

@ -0,0 +1,20 @@
add_executable(test_target_executable_properties "${CMAKE_CURRENT_SOURCE_DIR}/../pic_main.cpp")
set_target_properties(test_target_executable_properties
PROPERTIES
POSITION_INDEPENDENT_CODE True
)
add_library(test_target_shared_library_properties SHARED "${CMAKE_CURRENT_SOURCE_DIR}/../pic_lib.cpp")
set_target_properties(test_target_shared_library_properties
PROPERTIES
POSITION_INDEPENDENT_CODE True
DEFINE_SYMBOL PIC_TEST_BUILD_DLL
)
add_library(test_target_static_library_properties STATIC "${CMAKE_CURRENT_SOURCE_DIR}/../pic_lib.cpp")
set_target_properties(test_target_static_library_properties
PROPERTIES
POSITION_INDEPENDENT_CODE True
COMPILE_DEFINITIONS PIC_TEST_STATIC_BUILD
)

View File

@ -30,6 +30,9 @@ CMAKE_SHARED_LIBRARY_RUNTIME_FLAG == "${CMAKE_SHARED_LIBRARY_RUNTIME_FLAG}"
CMAKE_SHARED_LIBRARY_RUNTIME_FLAG_SEP == "${CMAKE_SHARED_LIBRARY_RUNTIME_FLAG_SEP}"
CMAKE_SHARED_LIBRARY_LINK_STATIC_C_FLAGS == "${CMAKE_SHARED_LIBRARY_LINK_STATIC_C_FLAGS}"
CMAKE_SHARED_LIBRARY_LINK_DYNAMIC_C_FLAGS == "${CMAKE_SHARED_LIBRARY_LINK_DYNAMIC_C_FLAGS}"
CMAKE_C_COMPILE_OPTIONS_PIC == "${CMAKE_C_COMPILE_OPTIONS_PIC}"
CMAKE_C_COMPILE_OPTIONS_PIE == "${CMAKE_C_COMPILE_OPTIONS_PIE}"
CMAKE_C_COMPILE_OPTIONS_DLL == "${CMAKE_C_COMPILE_OPTIONS_DLL}"
// C shared module flags
CMAKE_SHARED_MODULE_C_FLAGS == "${CMAKE_SHARED_MODULE_C_FLAGS}"
@ -49,6 +52,9 @@ CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG == "${CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLA
CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG_SEP == "${CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG_SEP}"
CMAKE_SHARED_LIBRARY_LINK_STATIC_CXX_FLAGS == "${CMAKE_SHARED_LIBRARY_LINK_STATIC_CXX_FLAGS}"
CMAKE_SHARED_LIBRARY_LINK_DYNAMIC_CXX_FLAGS == "${CMAKE_SHARED_LIBRARY_LINK_DYNAMIC_CXX_FLAGS}"
CMAKE_CXX_COMPILE_OPTIONS_PIC == "${CMAKE_CXX_COMPILE_OPTIONS_PIC}"
CMAKE_CXX_COMPILE_OPTIONS_PIE == "${CMAKE_CXX_COMPILE_OPTIONS_PIE}"
CMAKE_CXX_COMPILE_OPTIONS_DLL == "${CMAKE_CXX_COMPILE_OPTIONS_DLL}"
// CXX shared module flags
CMAKE_SHARED_MODULE_CREATE_CXX_FLAGS == "${CMAKE_SHARED_MODULE_CREATE_CXX_FLAGS}"