Xcode: Refactor generation of per-language compiler flags

This commit is contained in:
Brad King 2015-02-06 11:01:48 -05:00
parent 098160d5f2
commit 9924486f8a
1 changed files with 69 additions and 71 deletions

View File

@ -1743,7 +1743,6 @@ void cmGlobalXCodeGenerator::CreateBuildSettings(cmTarget& target,
return; return;
} }
std::string flags;
std::string defFlags; std::string defFlags;
bool shared = ((target.GetType() == cmTarget::SHARED_LIBRARY) || bool shared = ((target.GetType() == cmTarget::SHARED_LIBRARY) ||
(target.GetType() == cmTarget::MODULE_LIBRARY)); (target.GetType() == cmTarget::MODULE_LIBRARY));
@ -1752,19 +1751,15 @@ void cmGlobalXCodeGenerator::CreateBuildSettings(cmTarget& target,
(target.GetType() == cmTarget::EXECUTABLE) || (target.GetType() == cmTarget::EXECUTABLE) ||
shared); shared);
std::string lang = target.GetLinkerLanguage(configName); // Compute the compilation flags for each language.
std::string cflags; std::set<std::string> languages;
if(!lang.empty()) target.GetLanguages(languages, configName);
std::map<std::string, std::string> cflags;
for (std::set<std::string>::iterator li = languages.begin();
li != languages.end(); ++li)
{ {
// for c++ projects get the c flags as well std::string const& lang = *li;
if(lang == "CXX") std::string& flags = cflags[lang];
{
this->CurrentLocalGenerator->AddLanguageFlags(cflags, "C", configName);
this->CurrentLocalGenerator->AddCMP0018Flags(cflags, &target,
"C", configName);
this->CurrentLocalGenerator->
AddCompileOptions(cflags, &target, "C", configName);
}
// Add language-specific flags. // Add language-specific flags.
this->CurrentLocalGenerator->AddLanguageFlags(flags, lang, configName); this->CurrentLocalGenerator->AddLanguageFlags(flags, lang, configName);
@ -1779,13 +1774,15 @@ void cmGlobalXCodeGenerator::CreateBuildSettings(cmTarget& target,
this->CurrentLocalGenerator-> this->CurrentLocalGenerator->
AddCompileOptions(flags, &target, lang, configName); AddCompileOptions(flags, &target, lang, configName);
} }
else if(binary)
{ std::string lang = target.GetLinkerLanguage(configName);
if(binary && lang.empty())
{
cmSystemTools::Error cmSystemTools::Error
("CMake can not determine linker language for target: ", ("CMake can not determine linker language for target: ",
target.GetName().c_str()); target.GetName().c_str());
return; return;
} }
// Add define flags // Add define flags
this->CurrentLocalGenerator-> this->CurrentLocalGenerator->
@ -2178,53 +2175,58 @@ void cmGlobalXCodeGenerator::CreateBuildSettings(cmTarget& target,
buildSettings->AddAttribute("HEADER_SEARCH_PATHS", buildSettings->AddAttribute("HEADER_SEARCH_PATHS",
dirs.CreateList()); dirs.CreateList());
} }
std::string oflagc = this->ExtractFlag("-O", cflags);
bool same_gflags = true;
std::map<std::string, std::string> gflags;
std::string const* last_gflag = 0;
char optLevel[2]; char optLevel[2];
optLevel[0] = '0'; optLevel[0] = '0';
optLevel[1] = 0; optLevel[1] = 0;
if(oflagc.size() == 3)
// Minimal map of flags to build settings.
for (std::set<std::string>::iterator li = languages.begin();
li != languages.end(); ++li)
{ {
optLevel[0] = oflagc[2]; std::string& flags = cflags[*li];
} std::string& gflag = gflags[*li];
if(oflagc.size() == 2) std::string oflag = this->ExtractFlag("-O", flags);
{ if(oflag.size() == 3)
optLevel[0] = '1'; {
} optLevel[0] = oflag[2];
std::string oflag = this->ExtractFlag("-O", flags); }
if(oflag.size() == 3) if(oflag.size() == 2)
{ {
optLevel[0] = oflag[2]; optLevel[0] = '1';
} }
if(oflag.size() == 2) gflag = this->ExtractFlag("-g", flags);
{ // put back gdwarf-2 if used since there is no way
optLevel[0] = '1'; // to represent it in the gui, but we still want debug yes
} if(gflag == "-gdwarf-2")
std::string gflagc = this->ExtractFlag("-g", cflags); {
// put back gdwarf-2 if used since there is no way flags += " ";
// to represent it in the gui, but we still want debug yes flags += gflag;
if(gflagc == "-gdwarf-2") }
{ if (last_gflag && *last_gflag != gflag)
cflags += " "; {
cflags += gflagc; same_gflags = false;
} }
std::string gflag = this->ExtractFlag("-g", flags); last_gflag = &gflag;
if(gflag == "-gdwarf-2")
{
flags += " ";
flags += gflag;
} }
const char* debugStr = "YES"; const char* debugStr = "YES";
// We can't set the Xcode flag differently depending on the language, if (!same_gflags)
// so put them back in this case.
if( (lang == "CXX") && gflag != gflagc )
{ {
cflags += " "; // We can't set the Xcode flag differently depending on the language,
cflags += gflagc; // so put them back in this case.
flags += " "; for (std::set<std::string>::iterator li = languages.begin();
flags += gflag; li != languages.end(); ++li)
{
cflags[*li] += " ";
cflags[*li] += gflags[*li];
}
debugStr = "NO"; debugStr = "NO";
} }
if( gflag == "-g0" || gflag.size() == 0 ) else if (last_gflag && (last_gflag->empty() || *last_gflag == "-g0"))
{ {
debugStr = "NO"; debugStr = "NO";
} }
@ -2239,24 +2241,20 @@ void cmGlobalXCodeGenerator::CreateBuildSettings(cmTarget& target,
this->CreateString("NO")); this->CreateString("NO"));
buildSettings->AddAttribute("GCC_INLINES_ARE_PRIVATE_EXTERN", buildSettings->AddAttribute("GCC_INLINES_ARE_PRIVATE_EXTERN",
this->CreateString("NO")); this->CreateString("NO"));
if(lang == "CXX") for (std::set<std::string>::iterator li = languages.begin();
li != languages.end(); ++li)
{ {
flags += " "; std::string flags = cflags[*li] + " " + defFlags;
flags += defFlags; if (*li == "CXX")
buildSettings->AddAttribute("OTHER_CPLUSPLUSFLAGS", {
this->CreateString(flags.c_str())); buildSettings->AddAttribute("OTHER_CPLUSPLUSFLAGS",
cflags += " "; this->CreateString(flags.c_str()));
cflags += defFlags; }
buildSettings->AddAttribute("OTHER_CFLAGS", else if (*li == "C")
this->CreateString(cflags.c_str())); {
buildSettings->AddAttribute("OTHER_CFLAGS",
} this->CreateString(flags.c_str()));
else }
{
flags += " ";
flags += defFlags;
buildSettings->AddAttribute("OTHER_CFLAGS",
this->CreateString(flags.c_str()));
} }
// Add Fortran source format attribute if property is set. // Add Fortran source format attribute if property is set.