From: Mikko Rasa Date: Fri, 27 Apr 2012 18:04:23 +0000 (+0300) Subject: Rewrite BuildInfo to be compiler-agnostic X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=45c778b1976de1a3093e0c6fb5e2a94c42073dcb;p=builder.git Rewrite BuildInfo to be compiler-agnostic --- diff --git a/source/binarypackage.cpp b/source/binarypackage.cpp index f804da0..bc3e5c9 100644 --- a/source/binarypackage.cpp +++ b/source/binarypackage.cpp @@ -43,11 +43,19 @@ BinaryPackage *BinaryPackage::from_pkgconfig(Builder &builder, const string &nam if(!i->compare(0, 2, "-I")) binfo.incpath.push_back(i->substr(2)); else if(!i->compare(0, 2, "-D")) - binfo.defines.push_back(i->substr(2)); + { + string::size_type equals = i->find('='); + if(equals!=string::npos) + binfo.defines[i->substr(2, equals-2)] = i->substr(equals+1); + else + binfo.defines[i->substr(2)] = string(); + } else if(!i->compare(0, 2, "-L")) binfo.libpath.push_back(i->substr(2)); else if(!i->compare(0, 2, "-l")) binfo.libs.push_back(i->substr(2)); + else if(*i=="-pthread") + binfo.threads = true; } return pkg; diff --git a/source/buildinfo.cpp b/source/buildinfo.cpp index 61e3162..463357d 100644 --- a/source/buildinfo.cpp +++ b/source/buildinfo.cpp @@ -23,26 +23,36 @@ void unique(StringList &l) } +BuildInfo::BuildInfo(): + threads(false), + debug(false), + optimize(0), + strip(false) +{ } + void BuildInfo::update_from(const BuildInfo &bi, UpdateLevel level) { - cflags.insert(cflags.end(), bi.cflags.begin(), bi.cflags.end()); - defines.insert(defines.end(), bi.defines.begin(), bi.defines.end()); + for(DefineMap::const_iterator i=bi.defines.begin(); i!=bi.defines.end(); ++i) + defines[i->first] = i->second; incpath.insert(incpath.end(), bi.incpath.begin(), bi.incpath.end()); if(level!=CHAINED) { - ldflags.insert(ldflags.end(), bi.ldflags.begin(), bi.ldflags.end()); libpath.insert(libpath.end(), bi.libpath.begin(), bi.libpath.end()); libs.insert(libs.end(), bi.libs.begin(), bi.libs.end()); } warnings.insert(warnings.end(), bi.warnings.begin(), bi.warnings.end()); + threads = bi.threads; + if(level==LOCAL) + { + debug = bi.debug; + optimize = bi.optimize; + strip = bi.strip; + } } void BuildInfo::unique() { - ::unique(cflags); - ::unique(defines); ::unique(incpath); - ::unique(ldflags); ::unique(libpath); ::unique(libs); @@ -72,18 +82,15 @@ void BuildInfo::unique() BuildInfo::Loader::Loader(BuildInfo &bi): DataFile::ObjectLoader(bi) { - add("cflag", &Loader::cflag); - add("incpath", &Loader::incpath); - add("define", &Loader::define); - add("ldflag", &Loader::ldflag); - add("libpath", &Loader::libpath); - add("library", &Loader::library); - add("warning", &Loader::warning); -} - -void BuildInfo::Loader::cflag(const std::string &s) -{ - obj.cflags.push_back(s); + add("debug", &BuildInfo::debug); + add("define", &Loader::define); + add("incpath", &Loader::incpath); + add("libpath", &Loader::libpath); + add("library", &Loader::library); + add("optimize", &BuildInfo::optimize); + add("strip", &BuildInfo::strip); + add("threads", &BuildInfo::threads); + add("warning", &Loader::warning); } void BuildInfo::Loader::incpath(const std::string &s) @@ -91,14 +98,9 @@ void BuildInfo::Loader::incpath(const std::string &s) obj.incpath.push_back(s); } -void BuildInfo::Loader::define(const std::string &s) -{ - obj.defines.push_back(s); -} - -void BuildInfo::Loader::ldflag(const std::string &s) +void BuildInfo::Loader::define(const std::string &d, const std::string &v) { - obj.ldflags.push_back(s); + obj.defines[d] = v; } void BuildInfo::Loader::libpath(const std::string &s) diff --git a/source/buildinfo.h b/source/buildinfo.h index c7c89ac..f10e21e 100644 --- a/source/buildinfo.h +++ b/source/buildinfo.h @@ -18,10 +18,8 @@ public: public: Loader(BuildInfo &); private: - void cflag(const std::string &); void incpath(const std::string &); - void define(const std::string &); - void ldflag(const std::string &); + void define(const std::string &, const std::string &); void libpath(const std::string &); void library(const std::string &); void warning(const std::string &); @@ -34,13 +32,19 @@ public: CHAINED }; - StringList cflags; - StringList defines; + typedef std::map DefineMap; + + DefineMap defines; StringList incpath; - StringList ldflags; StringList libpath; StringList libs; StringList warnings; + bool threads; + bool debug; + int optimize; + bool strip; + + BuildInfo(); /** Adds another BuildInfo to the end of this one. */ void update_from(const BuildInfo &, UpdateLevel = LOCAL); diff --git a/source/component.cpp b/source/component.cpp index 7db9880..7ef6dbe 100644 --- a/source/component.cpp +++ b/source/component.cpp @@ -76,15 +76,7 @@ void Component::create_build_info() } if(type==PROGRAM) - { - string strip = pkg.get_config().get_option("strip").value; - if(lexical_cast(strip)) - build_info.ldflags.push_back("-s"); - } - else if(type==LIBRARY) - { - build_info.cflags.push_back("-fPIC"); - } + build_info.strip = lexical_cast(pkg.get_config().get_option("strip").value); build_info.unique(); } diff --git a/source/gnucompiler.cpp b/source/gnucompiler.cpp index 026ec8c..4fa293b 100644 --- a/source/gnucompiler.cpp +++ b/source/gnucompiler.cpp @@ -44,12 +44,28 @@ Task *GnuCompiler::run(const Target &target) const const BuildInfo &binfo = comp.get_build_info(); for(list::const_iterator i=binfo.warnings.begin(); i!=binfo.warnings.end(); ++i) argv.push_back("-W"+*i); - for(list::const_iterator i=binfo.cflags.begin(); i!=binfo.cflags.end(); ++i) - argv.push_back(*i); for(list::const_iterator i=binfo.incpath.begin(); i!=binfo.incpath.end(); ++i) argv.push_back("-I"+*i); - for(list::const_iterator i=binfo.defines.begin(); i!=binfo.defines.end(); ++i) - argv.push_back("-D"+*i); + for(BuildInfo::DefineMap::const_iterator i=binfo.defines.begin(); i!=binfo.defines.end(); ++i) + { + if(i->second.empty()) + argv.push_back(format("-D%s", i->first)); + else + argv.push_back(format("-D%s=%s", i->first, i->second)); + } + if(binfo.debug) + argv.push_back("-ggdb"); + if(binfo.optimize) + { + if(binfo.optimize<0) + argv.push_back("-Os"); + else + argv.push_back(format("-O%d", binfo.optimize)); + } + if(binfo.threads) + argv.push_back("-pthread"); + if(comp.get_type()==Component::LIBRARY) + argv.push_back("-fPIC"); const Architecture &arch = builder.get_current_arch(); const Architecture &native_arch = builder.get_native_arch(); diff --git a/source/gnulinker.cpp b/source/gnulinker.cpp index a14dd0b..a8b07f8 100644 --- a/source/gnulinker.cpp +++ b/source/gnulinker.cpp @@ -71,6 +71,7 @@ Task *GnuLinker::run(const Target &target) const if(shlib) { argv.push_back("-shared"); + argv.push_back("-fPIC"); if(!shlib->get_soname().empty()) argv.push_back("-Wl,-soname,"+shlib->get_soname()); } @@ -78,10 +79,12 @@ Task *GnuLinker::run(const Target &target) const argv.push_back("-static"); const BuildInfo &binfo = comp.get_build_info(); - for(list::const_iterator i=binfo.ldflags.begin(); i!=binfo.ldflags.end(); ++i) - argv.push_back(*i); for(list::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i) argv.push_back("-L"+*i); + if(binfo.strip) + argv.push_back("-s"); + if(binfo.threads) + argv.push_back("-pthread"); const Architecture &arch = builder.get_current_arch(); const Architecture &native_arch = builder.get_native_arch(); diff --git a/source/pkgconfiggenerator.cpp b/source/pkgconfiggenerator.cpp index 708a913..555d8c5 100644 --- a/source/pkgconfiggenerator.cpp +++ b/source/pkgconfiggenerator.cpp @@ -58,16 +58,17 @@ void PkgConfigGenerator::Worker::main() IO::print(out, " -L%s", FS::relative(*i, builder.get_prefix()).str()); for(StringList::const_iterator i=binfo.libs.begin(); i!=binfo.libs.end(); ++i) IO::print(out, " -l%s", *i); - for(StringList::const_iterator i=binfo.ldflags.begin(); i!=binfo.ldflags.end(); ++i) - IO::print(out, " %s", *i); + if(binfo.threads) + out.write("-pthread"); out.put('\n'); IO::print(out, "Cflags:"); for(StringList::const_iterator i=binfo.incpath.begin(); i!=binfo.incpath.end(); ++i) IO::print(out, " -I%s", FS::relative(*i, builder.get_prefix()).str()); - for(StringList::const_iterator i=binfo.defines.begin(); i!=binfo.defines.end(); ++i) - IO::print(out, " -D%s", *i); - for(StringList::const_iterator i=binfo.cflags.begin(); i!=binfo.cflags.end(); ++i) - IO::print(out, " %s", *i); + for(BuildInfo::DefineMap::const_iterator i=binfo.defines.begin(); i!=binfo.defines.end(); ++i) + if(i->second.empty()) + IO::print(out, " -D%s", i->first); + else + IO::print(out, " -D%s=%s", i->first, i->second); out.put('\n'); } diff --git a/source/sourcepackage.cpp b/source/sourcepackage.cpp index 754a314..60276da 100644 --- a/source/sourcepackage.cpp +++ b/source/sourcepackage.cpp @@ -198,19 +198,21 @@ void SourcePackage::create_build_info() string optimize = config.get_option("optimize").value; if(!optimize.empty() && optimize!="0") { - build_info.cflags.push_back("-O"+optimize); - build_info.ldflags.push_back("-O"+optimize); + if(optimize=="s" || optimize=="size") + build_info.optimize = -1; + else + build_info.optimize = lexical_cast(optimize); } if(lexical_cast(config.get_option("debug").value)) { - build_info.cflags.push_back("-ggdb"); - build_info.defines.push_back("DEBUG"); + build_info.debug = true; + build_info.defines["DEBUG"] = "1"; } for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i) if(lexical_cast(config.get_option("with_"+i->name).value)) - build_info.cflags.push_back("-DWITH_"+toupper(i->name)); + build_info.defines["WITH_"+toupper(i->name)] = "1"; for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i) if(i->eval())