Fix Xcode project references to the source tree

Xcode project source file references need to always be relative to the
top of the source tree in order for SCM and debug symbols to work right.
We must even allow the relative paths to cross outside of the top source
or build directories.

For subdirectory project() command Xcode projects we use the source
directory containing the project() command as the top.  Relative paths
are generated accordingly for each subproject.

See issue #8481.
This commit is contained in:
Brad King 2009-09-22 16:18:31 -04:00
parent 3fe5f8d960
commit 61495cdaae
2 changed files with 34 additions and 56 deletions

View File

@ -302,9 +302,9 @@ void cmGlobalXCodeGenerator::SetGenerationRoot(cmLocalGenerator* root)
{
this->CurrentProject = root->GetMakefile()->GetProjectName();
this->SetCurrentLocalGenerator(root);
std::string outDir = this->CurrentMakefile->GetHomeOutputDirectory();
outDir =cmSystemTools::CollapseFullPath(outDir.c_str());
cmSystemTools::SplitPath(outDir.c_str(),
cmSystemTools::SplitPath(this->CurrentMakefile->GetCurrentDirectory(),
this->ProjectSourceDirectoryComponents);
cmSystemTools::SplitPath(this->CurrentMakefile->GetCurrentOutputDirectory(),
this->ProjectOutputDirectoryComponents);
this->CurrentXCodeHackMakefile =
@ -670,49 +670,18 @@ cmGlobalXCodeGenerator::CreateXCodeFileReference(cmSourceFile* sf,
fileRef->AddAttribute("lastKnownFileType",
this->CreateString(sourcecode.c_str()));
std::string path =
this->ConvertToRelativeForXCode(sf->GetFullPath().c_str());
std::string dir;
std::string file;
cmSystemTools::SplitProgramPath(sf->GetFullPath().c_str(),
dir, file);
// Try to make the path relative to the project root.
bool Absolute = true;
if (this->XcodeVersion >= 30)
{
std::string relative =
this->CurrentLocalGenerator->Convert(sf->GetFullPath().c_str(),
cmLocalGenerator::HOME,
cmLocalGenerator::MAKEFILE,
false);
relative = cmSystemTools::ConvertToOutputPath(relative.c_str());
if (!relative.empty() && relative[0] != '/')
{
Absolute = false;
path = relative;
}
}
fileRef->AddAttribute("name", this->CreateString(file.c_str()));
// Store the file path relative to the top of the source tree.
std::string path = this->RelativeToSource(sf->GetFullPath().c_str());
std::string name = cmSystemTools::GetFilenameName(path.c_str());
const char* sourceTree = (cmSystemTools::FileIsFullPath(path.c_str())?
"<absolute>" : "SOURCE_ROOT");
fileRef->AddAttribute("name", this->CreateString(name.c_str()));
fileRef->AddAttribute("path", this->CreateString(path.c_str()));
fileRef->AddAttribute("sourceTree", this->CreateString(sourceTree));
if(this->XcodeVersion == 15)
{
fileRef->AddAttribute("refType", this->CreateString("4"));
}
if(path.size() > 1 && path[0] == '.' && path[1] == '.')
{
fileRef->AddAttribute("sourceTree", this->CreateString("<group>"));
}
else if (Absolute)
{
fileRef->AddAttribute("sourceTree", this->CreateString("<absolute>"));
}
else
{
fileRef->AddAttribute("sourceTree", this->CreateString("SOURCE_ROOT"));
}
return fileRef;
}
@ -2610,24 +2579,14 @@ void cmGlobalXCodeGenerator
else
this->RootObject->AddAttribute("compatibilityVersion",
this->CreateString("Xcode 3.0"));
this->RootObject->AddAttribute("projectDirPath", this->CreateString(""));
}
// Point Xcode at the top of the source tree.
{
std::string proot = root->GetMakefile()->GetCurrentDirectory();
proot = this->ConvertToRelativeForXCode(proot.c_str());
if (this->XcodeVersion >= 30)
{
this->RootObject->AddAttribute("projectRoot",
this->CreateString(""));
this->RootObject->AddAttribute("projectDirPath",
this->CreateString(proot.c_str()));
}
else
{
this->RootObject->AddAttribute("projectRoot",
this->CreateString(proot.c_str()));
}
std::string pdir =
this->RelativeToBinary(root->GetMakefile()->GetCurrentDirectory());
this->RootObject->AddAttribute("projectDirPath",
this->CreateString(pdir.c_str()));
this->RootObject->AddAttribute("projectRoot", this->CreateString(""));
}
cmXCodeObject* configlist =
this->CreateObject(cmXCodeObject::XCConfigurationList);
@ -3066,6 +3025,22 @@ std::string cmGlobalXCodeGenerator::ConvertToRelativeForXCode(const char* p)
}
}
//----------------------------------------------------------------------------
std::string cmGlobalXCodeGenerator::RelativeToSource(const char* p)
{
// We force conversion because Xcode breakpoints do not work unless
// they are in a file named relative to the source tree.
return this->CurrentLocalGenerator->
ConvertToRelativePath(this->ProjectSourceDirectoryComponents, p, true);
}
//----------------------------------------------------------------------------
std::string cmGlobalXCodeGenerator::RelativeToBinary(const char* p)
{
return this->CurrentLocalGenerator->
ConvertToRelativePath(this->ProjectOutputDirectoryComponents, p);
}
//----------------------------------------------------------------------------
std::string cmGlobalXCodeGenerator::XCodeEscapePath(const char* p)
{

View File

@ -93,6 +93,8 @@ private:
std::vector<cmLocalGenerator*>&
generators);
std::string XCodeEscapePath(const char* p);
std::string RelativeToSource(const char* p);
std::string RelativeToBinary(const char* p);
std::string ConvertToRelativeForXCode(const char* p);
std::string ConvertToRelativeForMake(const char* p);
void CreateCustomCommands(cmXCodeObject* buildPhases,
@ -206,6 +208,7 @@ private:
std::string CurrentProject;
std::set<cmStdString> TargetDoneSet;
std::vector<std::string> CurrentOutputDirectoryComponents;
std::vector<std::string> ProjectSourceDirectoryComponents;
std::vector<std::string> ProjectOutputDirectoryComponents;
std::map<cmStdString, cmXCodeObject* > GroupMap;
std::map<cmStdString, cmXCodeObject* > GroupNameMap;