From 02db43239b7e2fd1f6dede9fb7237470ca51b567 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 24 Nov 2009 11:16:38 -0500 Subject: Teach link_directories to recognize relative paths We create CMake Policy CMP0015 to make link_directories() treat relative paths with respect to the source tree while retaining compatibility. This makes it consistent with include_directories() and other commands. Changes based on patch from Alex. See issue #9697. --- Source/cmLinkDirectoriesCommand.cxx | 44 ++++++++++++++++++++++++++++++++++--- Source/cmLinkDirectoriesCommand.h | 2 ++ Source/cmPolicies.cxx | 15 +++++++++++++ Source/cmPolicies.h | 1 + 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/Source/cmLinkDirectoriesCommand.cxx b/Source/cmLinkDirectoriesCommand.cxx index 697376f..4412414 100644 --- a/Source/cmLinkDirectoriesCommand.cxx +++ b/Source/cmLinkDirectoriesCommand.cxx @@ -23,10 +23,48 @@ bool cmLinkDirectoriesCommand for(std::vector::const_iterator i = args.begin(); i != args.end(); ++i) { - std::string unixPath = *i; - cmSystemTools::ConvertToUnixSlashes(unixPath); - this->Makefile->AddLinkDirectory(unixPath.c_str()); + this->AddLinkDir(*i); } return true; } +//---------------------------------------------------------------------------- +void cmLinkDirectoriesCommand::AddLinkDir(std::string const& dir) +{ + std::string unixPath = dir; + cmSystemTools::ConvertToUnixSlashes(unixPath); + if(!cmSystemTools::FileIsFullPath(unixPath.c_str())) + { + bool convertToAbsolute = false; + cmOStringStream e; + e << "This command specifies the relative path\n" + << " " << unixPath << "\n" + << "as a link directory.\n"; + cmPolicies* policies = this->Makefile->GetPolicies(); + switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0015)) + { + case cmPolicies::WARN: + e << policies->GetPolicyWarning(cmPolicies::CMP0015); + this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, e.str()); + case cmPolicies::OLD: + // OLD behavior does not convert + break; + case cmPolicies::REQUIRED_IF_USED: + case cmPolicies::REQUIRED_ALWAYS: + e << policies->GetRequiredPolicyError(cmPolicies::CMP0015); + this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str()); + case cmPolicies::NEW: + // NEW behavior converts + convertToAbsolute = true; + break; + } + if (convertToAbsolute) + { + std::string tmp = this->Makefile->GetStartDirectory(); + tmp += "/"; + tmp += unixPath; + unixPath = tmp; + } + } + this->Makefile->AddLinkDirectory(unixPath.c_str()); +} diff --git a/Source/cmLinkDirectoriesCommand.h b/Source/cmLinkDirectoriesCommand.h index 73673c4..e5ff4f0 100644 --- a/Source/cmLinkDirectoriesCommand.h +++ b/Source/cmLinkDirectoriesCommand.h @@ -70,6 +70,8 @@ public: } cmTypeMacro(cmLinkDirectoriesCommand, cmCommand); +private: + void AddLinkDir(std::string const& dir); }; diff --git a/Source/cmPolicies.cxx b/Source/cmPolicies.cxx index 6b35b5b..2d41d40 100644 --- a/Source/cmPolicies.cxx +++ b/Source/cmPolicies.cxx @@ -406,6 +406,21 @@ cmPolicies::cmPolicies() "The OLD behavior for this policy is to silently ignore the problem. " "The NEW behavior for this policy is to report an error.", 2,8,0, cmPolicies::WARN); + + this->DefinePolicy( + CMP0015, "CMP0015", + "link_directories() treats paths relative to the source dir.", + "In CMake 2.6.4 and lower the link_directories() command passed relative " + "paths unchanged to the linker. " + "In CMake 2.8.1 and above the link_directories() command prefers to " + "interpret relative paths with respect to CMAKE_CURRENT_SOURCE_DIR, " + "which is consistent with include_directories() and other commands. " + "The OLD behavior for this policy is to use relative paths verbatim in " + "the linker command. " + "The NEW behavior for this policy is to convert relative paths to " + "absolute paths by appending the relative path to " + "CMAKE_CURRENT_SOURCE_DIR.", + 2,8,1, cmPolicies::WARN); } cmPolicies::~cmPolicies() diff --git a/Source/cmPolicies.h b/Source/cmPolicies.h index 4d1c3fc..aaa3ac0 100644 --- a/Source/cmPolicies.h +++ b/Source/cmPolicies.h @@ -50,6 +50,7 @@ public: CMP0012, // Recognize numbers and boolean constants in if() CMP0013, // Duplicate binary directories not allowed CMP0014, // Input directories must have CMakeLists.txt + CMP0015, // link_directories() treats paths relative to source dir // Always the last entry. Useful mostly to avoid adding a comma // the last policy when adding a new one. -- cgit v0.12