From 3581b96caa1d7bd6c02e85baf72ce28b5128e5e7 Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Mon, 5 Nov 2012 12:46:26 +0100 Subject: [PATCH] Process the INTERFACE_PIC property from linked dependencies This allows a dependee to inform a target that it should have its POSITION_INDEPENDENT_CODE property set to ON, or OFF. The value of the POSITION_INDEPENDENT_CODE property, if set, must be consistent with any INTERFACE_POSITION_INDEPENDENT_CODE properties on dependees. Add a test covering the consistency checks on platforms where they run. --- Source/cmGlobalXCodeGenerator.cxx | 6 ++++-- Source/cmLocalGenerator.cxx | 17 ++++++++++++++--- Source/cmLocalGenerator.h | 2 +- Source/cmMakefileTargetGenerator.cxx | 3 ++- Source/cmNinjaTargetGenerator.cxx | 3 ++- Source/cmTarget.cxx | 14 ++++++++++++++ Tests/RunCMake/CMakeLists.txt | 3 +++ .../PositionIndependentCode/CMakeLists.txt | 8 ++++++++ .../Conflict1-result.txt | 1 + .../Conflict1-stderr.txt | 3 +++ .../PositionIndependentCode/Conflict1.cmake | 7 +++++++ .../Conflict2-result.txt | 1 + .../Conflict2-stderr.txt | 3 +++ .../PositionIndependentCode/Conflict2.cmake | 9 +++++++++ .../Conflict3-result.txt | 1 + .../Conflict3-stderr.txt | 4 ++++ .../PositionIndependentCode/Conflict3.cmake | 12 ++++++++++++ .../PositionIndependentCode/RunCMakeTest.cmake | 5 +++++ Tests/RunCMake/PositionIndependentCode/main.cpp | 5 +++++ 19 files changed, 99 insertions(+), 8 deletions(-) create mode 100644 Tests/RunCMake/PositionIndependentCode/CMakeLists.txt create mode 100644 Tests/RunCMake/PositionIndependentCode/Conflict1-result.txt create mode 100644 Tests/RunCMake/PositionIndependentCode/Conflict1-stderr.txt create mode 100644 Tests/RunCMake/PositionIndependentCode/Conflict1.cmake create mode 100644 Tests/RunCMake/PositionIndependentCode/Conflict2-result.txt create mode 100644 Tests/RunCMake/PositionIndependentCode/Conflict2-stderr.txt create mode 100644 Tests/RunCMake/PositionIndependentCode/Conflict2.cmake create mode 100644 Tests/RunCMake/PositionIndependentCode/Conflict3-result.txt create mode 100644 Tests/RunCMake/PositionIndependentCode/Conflict3-stderr.txt create mode 100644 Tests/RunCMake/PositionIndependentCode/Conflict3.cmake create mode 100644 Tests/RunCMake/PositionIndependentCode/RunCMakeTest.cmake create mode 100644 Tests/RunCMake/PositionIndependentCode/main.cpp diff --git a/Source/cmGlobalXCodeGenerator.cxx b/Source/cmGlobalXCodeGenerator.cxx index 4c26b829b..2cfe4daba 100644 --- a/Source/cmGlobalXCodeGenerator.cxx +++ b/Source/cmGlobalXCodeGenerator.cxx @@ -1639,14 +1639,16 @@ void cmGlobalXCodeGenerator::CreateBuildSettings(cmTarget& target, if(strcmp(lang, "CXX") == 0) { this->CurrentLocalGenerator->AddLanguageFlags(cflags, "C", configName); - this->CurrentLocalGenerator->AddCMP0018Flags(cflags, &target, "C"); + this->CurrentLocalGenerator->AddCMP0018Flags(cflags, &target, + "C", configName); } // Add language-specific flags. this->CurrentLocalGenerator->AddLanguageFlags(flags, lang, configName); // Add shared-library flags if needed. - this->CurrentLocalGenerator->AddCMP0018Flags(flags, &target, lang); + this->CurrentLocalGenerator->AddCMP0018Flags(flags, &target, + lang, configName); } else if(binary) { diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index b557ca19b..ecf6b413a 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -1984,7 +1984,8 @@ void cmLocalGenerator::AddSharedFlags(std::string& flags, //---------------------------------------------------------------------------- void cmLocalGenerator::AddCMP0018Flags(std::string &flags, cmTarget* target, - std::string const& lang) + std::string const& lang, + const char *config) { int targetType = target->GetType(); @@ -1997,8 +1998,18 @@ void cmLocalGenerator::AddCMP0018Flags(std::string &flags, cmTarget* target, } else { - // Add position independendent flags, if needed. - if (target->GetPropertyAsBool("POSITION_INDEPENDENT_CODE")) + if (target->GetType() == cmTarget::OBJECT_LIBRARY) + { + if (target->GetPropertyAsBool("POSITION_INDEPENDENT_CODE")) + { + this->AddPositionIndependentFlags(flags, lang, targetType); + } + return; + } + + if (target->GetLinkInterfaceDependentBoolProperty( + "POSITION_INDEPENDENT_CODE", + config)) { this->AddPositionIndependentFlags(flags, lang, targetType); } diff --git a/Source/cmLocalGenerator.h b/Source/cmLocalGenerator.h index 63559d750..b2ff0c48f 100644 --- a/Source/cmLocalGenerator.h +++ b/Source/cmLocalGenerator.h @@ -142,7 +142,7 @@ public: void AddLanguageFlags(std::string& flags, const char* lang, const char* config); void AddCMP0018Flags(std::string &flags, cmTarget* target, - std::string const& lang); + std::string const& lang, const char *config); void AddConfigVariableFlags(std::string& flags, const char* var, const char* config); ///! Append flags to a string. diff --git a/Source/cmMakefileTargetGenerator.cxx b/Source/cmMakefileTargetGenerator.cxx index 9bf6b7d9e..64fcfcefa 100644 --- a/Source/cmMakefileTargetGenerator.cxx +++ b/Source/cmMakefileTargetGenerator.cxx @@ -268,7 +268,8 @@ std::string cmMakefileTargetGenerator::GetFlags(const std::string &l) this->AddFortranFlags(flags); } - this->LocalGenerator->AddCMP0018Flags(flags, this->Target, lang); + this->LocalGenerator->AddCMP0018Flags(flags, this->Target, + lang, this->ConfigName); // Add include directory flags. this->AddIncludeFlags(flags, lang); diff --git a/Source/cmNinjaTargetGenerator.cxx b/Source/cmNinjaTargetGenerator.cxx index 15842e44f..0f484da4c 100644 --- a/Source/cmNinjaTargetGenerator.cxx +++ b/Source/cmNinjaTargetGenerator.cxx @@ -147,7 +147,8 @@ cmNinjaTargetGenerator::ComputeFlagsForObject(cmSourceFile *source, // Add shared-library flags if needed. this->LocalGenerator->AddCMP0018Flags(flags, this->Target, - language.c_str()); + language.c_str(), + this->GetConfigName()); // Add include directory flags. { diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx index c8f9c1ddb..2cfb1bfd5 100644 --- a/Source/cmTarget.cxx +++ b/Source/cmTarget.cxx @@ -877,6 +877,20 @@ void cmTarget::DefineProperties(cmake *cm) "CMAKE_POSITION_INDEPENDENT_CODE if it is set when a target is " "created."); + cm->DefineProperty + ("INTERFACE_POSITION_INDEPENDENT_CODE", cmProperty::TARGET, + "Whether consumers need to create a position-independent target", + "The INTERFACE_POSITION_INDEPENDENT_CODE property informs consumers of " + "this target whether they must set their POSITION_INDEPENDENT_CODE " + "property to ON. If this property is set to ON, then the " + "POSITION_INDEPENDENT_CODE property on all consumers will be set to " + "ON. Similarly, if this property is set to OFF, then the " + "POSITION_INDEPENDENT_CODE property on all consumers will be set to " + "OFF. If this property is undefined, then consumers will determine " + "their POSITION_INDEPENDENT_CODE property by other means. Consumers " + "must ensure that the targets that they link to have a consistent " + "requirement for their INTERFACE_POSITION_INDEPENDENT_CODE property."); + cm->DefineProperty ("POST_INSTALL_SCRIPT", cmProperty::TARGET, "Deprecated install support.", diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index fec64b57c..9b133b299 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -50,6 +50,9 @@ add_RunCMake_test(GeneratorExpression) add_RunCMake_test(TargetPropertyGeneratorExpressions) add_RunCMake_test(Languages) add_RunCMake_test(ObjectLibrary) +if(NOT WIN32) + add_RunCMake_test(PositionIndependentCode) +endif() add_RunCMake_test(build_command) add_RunCMake_test(find_package) diff --git a/Tests/RunCMake/PositionIndependentCode/CMakeLists.txt b/Tests/RunCMake/PositionIndependentCode/CMakeLists.txt new file mode 100644 index 000000000..22577da4a --- /dev/null +++ b/Tests/RunCMake/PositionIndependentCode/CMakeLists.txt @@ -0,0 +1,8 @@ + +cmake_minimum_required(VERSION 2.8) +project(${RunCMake_TEST} CXX) + +# MSVC creates extra targets which pollute the stderr unless we set this. +set(CMAKE_SUPPRESS_REGENERATION TRUE) + +include(${RunCMake_TEST}.cmake) diff --git a/Tests/RunCMake/PositionIndependentCode/Conflict1-result.txt b/Tests/RunCMake/PositionIndependentCode/Conflict1-result.txt new file mode 100644 index 000000000..d00491fd7 --- /dev/null +++ b/Tests/RunCMake/PositionIndependentCode/Conflict1-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/PositionIndependentCode/Conflict1-stderr.txt b/Tests/RunCMake/PositionIndependentCode/Conflict1-stderr.txt new file mode 100644 index 000000000..cb0710986 --- /dev/null +++ b/Tests/RunCMake/PositionIndependentCode/Conflict1-stderr.txt @@ -0,0 +1,3 @@ +CMake Error: Property POSITION_INDEPENDENT_CODE on target "conflict" does +not match the INTERFACE_POSITION_INDEPENDENT_CODE property requirement +of dependency "piciface". diff --git a/Tests/RunCMake/PositionIndependentCode/Conflict1.cmake b/Tests/RunCMake/PositionIndependentCode/Conflict1.cmake new file mode 100644 index 000000000..242bec37a --- /dev/null +++ b/Tests/RunCMake/PositionIndependentCode/Conflict1.cmake @@ -0,0 +1,7 @@ + +add_library(piciface UNKNOWN IMPORTED) +set_property(TARGET piciface PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE ON) + +add_executable(conflict "main.cpp") +set_property(TARGET conflict PROPERTY POSITION_INDEPENDENT_CODE OFF) +target_link_libraries(conflict piciface) diff --git a/Tests/RunCMake/PositionIndependentCode/Conflict2-result.txt b/Tests/RunCMake/PositionIndependentCode/Conflict2-result.txt new file mode 100644 index 000000000..d00491fd7 --- /dev/null +++ b/Tests/RunCMake/PositionIndependentCode/Conflict2-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/PositionIndependentCode/Conflict2-stderr.txt b/Tests/RunCMake/PositionIndependentCode/Conflict2-stderr.txt new file mode 100644 index 000000000..ecd04928d --- /dev/null +++ b/Tests/RunCMake/PositionIndependentCode/Conflict2-stderr.txt @@ -0,0 +1,3 @@ +CMake Error: The INTERFACE_POSITION_INDEPENDENT_CODE property of "picoff" does +not agree with the value of POSITION_INDEPENDENT_CODE already determined +for "conflict". diff --git a/Tests/RunCMake/PositionIndependentCode/Conflict2.cmake b/Tests/RunCMake/PositionIndependentCode/Conflict2.cmake new file mode 100644 index 000000000..215d08dae --- /dev/null +++ b/Tests/RunCMake/PositionIndependentCode/Conflict2.cmake @@ -0,0 +1,9 @@ + +add_library(picon UNKNOWN IMPORTED) +set_property(TARGET picon PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE ON) + +add_library(picoff UNKNOWN IMPORTED) +set_property(TARGET picoff PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE OFF) + +add_executable(conflict "main.cpp") +target_link_libraries(conflict picon picoff) diff --git a/Tests/RunCMake/PositionIndependentCode/Conflict3-result.txt b/Tests/RunCMake/PositionIndependentCode/Conflict3-result.txt new file mode 100644 index 000000000..d00491fd7 --- /dev/null +++ b/Tests/RunCMake/PositionIndependentCode/Conflict3-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/PositionIndependentCode/Conflict3-stderr.txt b/Tests/RunCMake/PositionIndependentCode/Conflict3-stderr.txt new file mode 100644 index 000000000..0254e5557 --- /dev/null +++ b/Tests/RunCMake/PositionIndependentCode/Conflict3-stderr.txt @@ -0,0 +1,4 @@ +Property POSITION_INDEPENDENT_CODE on target "conflict" is +implied to be FALSE because it was used to determine the link libraries +already. The INTERFACE_POSITION_INDEPENDENT_CODE property on +dependency "picon" is in conflict. diff --git a/Tests/RunCMake/PositionIndependentCode/Conflict3.cmake b/Tests/RunCMake/PositionIndependentCode/Conflict3.cmake new file mode 100644 index 000000000..bf669bf4f --- /dev/null +++ b/Tests/RunCMake/PositionIndependentCode/Conflict3.cmake @@ -0,0 +1,12 @@ + +add_library(picoff UNKNOWN IMPORTED) + +add_library(picon UNKNOWN IMPORTED) +set_property(TARGET picon PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE ON) + +add_executable(conflict "main.cpp") +target_link_libraries(conflict picon) +set_property(TARGET conflict APPEND PROPERTY + LINK_LIBRARIES + $<$>>:picoff> +) diff --git a/Tests/RunCMake/PositionIndependentCode/RunCMakeTest.cmake b/Tests/RunCMake/PositionIndependentCode/RunCMakeTest.cmake new file mode 100644 index 000000000..64a340c9c --- /dev/null +++ b/Tests/RunCMake/PositionIndependentCode/RunCMakeTest.cmake @@ -0,0 +1,5 @@ +include(RunCMake) + +run_cmake(Conflict1) +run_cmake(Conflict2) +run_cmake(Conflict3) diff --git a/Tests/RunCMake/PositionIndependentCode/main.cpp b/Tests/RunCMake/PositionIndependentCode/main.cpp new file mode 100644 index 000000000..31ba48280 --- /dev/null +++ b/Tests/RunCMake/PositionIndependentCode/main.cpp @@ -0,0 +1,5 @@ + +int main(int,char**) +{ + return 0; +}