ENH: Cleanup builtin chrpath support

- Move computation of extended build-tree rpath
    to cmComputeLinkInformation
  - Only enable the extended build-tree rpath if
    the target will be installed
  - Generalize the interface of file(CHRPATH)
  - When changing the rpath on installation only
    replace the part generated by CMake because
    the native tools (ex SunCC on Linux) might have
    added their own part to the rpath
This commit is contained in:
Brad King 2008-03-02 14:35:23 -05:00
parent 16a415dd0c
commit d732de4a8a
7 changed files with 131 additions and 32 deletions

View File

@ -1463,6 +1463,18 @@ std::string cmComputeLinkInformation::GetRPathString(bool for_install)
// Add this path. // Add this path.
rpath += *ri; rpath += *ri;
} }
// If the rpath will be replaced at install time make sure it is
// long enough now.
if(!for_install && this->RuntimeUseChrpath)
{
std::string::size_type minLength = this->GetChrpathString().length();
while(rpath.length() < minLength)
{
rpath += this->GetRuntimeSep();
}
}
return rpath; return rpath;
} }

View File

@ -1333,25 +1333,81 @@ bool cmFileCommand::HandleInstallDestination(cmFileInstaller& installer,
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
bool cmFileCommand::HandleChrpathCommand(std::vector<std::string> const& args) bool cmFileCommand::HandleChrpathCommand(std::vector<std::string> const& args)
{ {
if(args.size() != 3) // Evaluate arguments.
const char* file = 0;
const char* oldRPath = 0;
const char* newRPath = 0;
enum Doing { DoingNone, DoingFile, DoingOld, DoingNew };
Doing doing = DoingNone;
for(unsigned int i=1; i < args.size(); ++i)
{ {
this->SetError("CHRPATH must be given a file and a new rpath."); if(args[i] == "OLD_RPATH")
{
doing = DoingOld;
}
else if(args[i] == "NEW_RPATH")
{
doing = DoingNew;
}
else if(args[i] == "FILE")
{
doing = DoingFile;
}
else if(doing == DoingFile)
{
file = args[i].c_str();
doing = DoingNone;
}
else if(doing == DoingOld)
{
oldRPath = args[i].c_str();
doing = DoingNone;
}
else if(doing == DoingNew)
{
newRPath = args[i].c_str();
doing = DoingNone;
}
else
{
cmOStringStream e;
e << "CHRPATH given unknown argument " << args[i];
this->SetError(e.str().c_str());
return false;
}
}
if(!file)
{
this->SetError("CHRPATH not given FILE option.");
return false; return false;
} }
if(!cmSystemTools::FileExists(args[1].c_str(), true)) if(!oldRPath)
{ {
this->SetError("CHRPATH given file that does not exist."); this->SetError("CHRPATH not given OLD_RPATH option.");
return false;
}
if(!newRPath)
{
this->SetError("CHRPATH not given NEW_RPATH option.");
return false;
}
if(!cmSystemTools::FileExists(file, true))
{
cmOStringStream e;
e << "CHRPATH given FILE \"" << file << "\" that does not exist.";
this->SetError(e.str().c_str());
return false; return false;
} }
std::string emsg; std::string emsg;
if(cmSystemTools::ChangeRPath(args[1], args[2], &emsg)) if(cmSystemTools::ChangeRPath(file, oldRPath, newRPath, &emsg))
{ {
return true; return true;
} }
else else
{ {
cmOStringStream e; cmOStringStream e;
e << "CHRPATH could not write new RPATH to the file: " e << "CHRPATH could not write new RPATH \""
<< newRPath << "\" to the file \"" << file << "\": "
<< emsg; << emsg;
this->SetError(e.str().c_str()); this->SetError(e.str().c_str());
return false; return false;

View File

@ -552,15 +552,8 @@ cmInstallTargetGenerator
::AddChrpathPatchRule(std::ostream& os, Indent const& indent, ::AddChrpathPatchRule(std::ostream& os, Indent const& indent,
const char* config, std::string const& toDestDirPath) const char* config, std::string const& toDestDirPath)
{ {
if(this->ImportLibrary || // Skip the chrpath if the target does not need it.
!(this->Target->GetType() == cmTarget::SHARED_LIBRARY || if(this->ImportLibrary || !this->Target->IsChrpathUsed())
this->Target->GetType() == cmTarget::MODULE_LIBRARY ||
this->Target->GetType() == cmTarget::EXECUTABLE))
{
return;
}
if(!this->Target->IsChrpathUsed())
{ {
return; return;
} }
@ -573,12 +566,16 @@ cmInstallTargetGenerator
return; return;
} }
// Construct the original rpath string to be replaced.
std::string oldRpath = cli->GetRPathString(false);
// Get the install RPATH from the link information. // Get the install RPATH from the link information.
std::string newRpath = cli->GetChrpathString(); std::string newRpath = cli->GetChrpathString();
// Write a rule to run chrpath to set the install-tree RPATH // Write a rule to run chrpath to set the install-tree RPATH
os << indent os << indent << "FILE(CHRPATH FILE \"" << toDestDirPath << "\"\n"
<< "FILE(CHRPATH \"" << toDestDirPath << "\" \"" << newRpath << "\")\n"; << indent << " OLD_RPATH \"" << oldRpath << "\"\n"
<< indent << " NEW_RPATH \"" << newRpath << "\")\n";
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------

View File

@ -1591,17 +1591,6 @@ void cmLocalGenerator::OutputLinkLibraries(std::ostream& fout,
// All rpath entries are combined ("-Wl,-rpath,a:b:c"). // All rpath entries are combined ("-Wl,-rpath,a:b:c").
std::string rpath = cli.GetRPathString(relink); std::string rpath = cli.GetRPathString(relink);
// If not relinking, make sure the rpath string is long enough to
// support a subsequent chrpath on installation.
if(!relink)
{
std::string::size_type minLength = cli.GetChrpathString().size();
while(rpath.size() < minLength)
{
rpath += cli.GetRuntimeSep();
}
}
// Store the rpath option in the stream. // Store the rpath option in the stream.
if(!rpath.empty()) if(!rpath.empty())
{ {

View File

@ -2198,18 +2198,41 @@ bool cmSystemTools::GuessLibrarySOName(std::string const& fullPath,
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
bool cmSystemTools::ChangeRPath(std::string const& file, bool cmSystemTools::ChangeRPath(std::string const& file,
std::string const& oldRPath,
std::string const& newRPath, std::string const& newRPath,
std::string* emsg) std::string* emsg)
{ {
#if defined(CMAKE_USE_ELF_PARSER) #if defined(CMAKE_USE_ELF_PARSER)
unsigned long rpathPosition = 0; unsigned long rpathPosition = 0;
unsigned long rpathSize = 0; unsigned long rpathSize = 0;
std::string rpathSuffix;
{ {
cmELF elf(file.c_str()); cmELF elf(file.c_str());
if(cmELF::StringEntry const* se = elf.GetRPath()) if(cmELF::StringEntry const* se = elf.GetRPath())
{ {
// Make sure the current rpath begins with the old rpath.
if(se->Value.length() < oldRPath.length() ||
se->Value.substr(0, oldRPath.length()) != oldRPath)
{
// If it begins with the new rpath instead then it is okay.
if(se->Value.length() >= newRPath.length() &&
se->Value.substr(0, newRPath.length()) == newRPath)
{
return true;
}
if(emsg)
{
*emsg = "The current RPATH does not begin with that specified.";
}
return false;
}
// Store information about the entry.
rpathPosition = se->Position; rpathPosition = se->Position;
rpathSize = se->Size; rpathSize = se->Size;
// Store the part of the path we must preserve.
rpathSuffix = se->Value.substr(oldRPath.length(), oldRPath.npos);
} }
else if(newRPath.empty()) else if(newRPath.empty())
{ {
@ -2221,14 +2244,19 @@ bool cmSystemTools::ChangeRPath(std::string const& file,
{ {
if(emsg) if(emsg)
{ {
*emsg = "No valid ELF RPATH entry exists in the file."; *emsg = "No valid ELF RPATH entry exists in the file; ";
*emsg += elf.GetErrorMessage();
} }
return false; return false;
} }
} }
// Compute the full new rpath.
std::string rpath = newRPath;
rpath += rpathSuffix;
// Make sure there is enough room to store the new rpath and at // Make sure there is enough room to store the new rpath and at
// least one null terminator. // least one null terminator.
if(rpathSize < newRPath.length()+1) if(rpathSize < rpath.length()+1)
{ {
if(emsg) if(emsg)
{ {
@ -2259,8 +2287,8 @@ bool cmSystemTools::ChangeRPath(std::string const& file,
// Write the new rpath. Follow it with enough null terminators to // Write the new rpath. Follow it with enough null terminators to
// fill the string table entry. // fill the string table entry.
f << newRPath; f << rpath;
for(unsigned long i=newRPath.length(); i < rpathSize; ++i) for(unsigned long i=rpath.length(); i < rpathSize; ++i)
{ {
f << '\0'; f << '\0';
} }
@ -2280,6 +2308,7 @@ bool cmSystemTools::ChangeRPath(std::string const& file,
} }
#else #else
(void)file; (void)file;
(void)oldRPath;
(void)newRPath; (void)newRPath;
(void)emsg; (void)emsg;
return false; return false;

View File

@ -383,6 +383,7 @@ public:
/** Try to set the RPATH in an ELF binary. */ /** Try to set the RPATH in an ELF binary. */
static bool ChangeRPath(std::string const& file, static bool ChangeRPath(std::string const& file,
std::string const& oldRPath,
std::string const& newRPath, std::string const& newRPath,
std::string* emsg = 0); std::string* emsg = 0);

View File

@ -3007,6 +3007,21 @@ void cmTarget::GetLanguages(std::set<cmStdString>& languages) const
bool cmTarget::IsChrpathUsed() bool cmTarget::IsChrpathUsed()
{ {
#if defined(CMAKE_USE_ELF_PARSER) #if defined(CMAKE_USE_ELF_PARSER)
// Only certain target types have an rpath.
if(!(this->GetType() == cmTarget::SHARED_LIBRARY ||
this->GetType() == cmTarget::MODULE_LIBRARY ||
this->GetType() == cmTarget::EXECUTABLE))
{
return false;
}
// If the target will not be installed we do not need to change its
// rpath.
if(!this->GetHaveInstallRule())
{
return false;
}
// Skip chrpath if skipping rpath altogether. // Skip chrpath if skipping rpath altogether.
if(this->Makefile->IsOn("CMAKE_SKIP_RPATH")) if(this->Makefile->IsOn("CMAKE_SKIP_RPATH"))
{ {