]> git.tdb.fi Git - builder.git/commitdiff
Rewrite BuildInfo to be compiler-agnostic
authorMikko Rasa <tdb@tdb.fi>
Fri, 27 Apr 2012 18:04:23 +0000 (21:04 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 8 Jul 2012 21:08:49 +0000 (00:08 +0300)
source/binarypackage.cpp
source/buildinfo.cpp
source/buildinfo.h
source/component.cpp
source/gnucompiler.cpp
source/gnulinker.cpp
source/pkgconfiggenerator.cpp
source/sourcepackage.cpp

index f804da08394c31fc3489109a46b1abf9eb9ef5fd..bc3e5c929ec41af7d6a5b515f2ac2a17501c50b1 100644 (file)
@@ -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;
index 61e3162a2732b00cdec169bf2f0dcd75d7e41cef..463357dd32d37d19dd31266bc538f7e288051c46 100644 (file)
@@ -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<BuildInfo>(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)
index c7c89ac649e5a6089c0e8adf95b163f6d3e691c8..f10e21e5aeee4f3a24c78cfbca194b897c13b16d 100644 (file)
@@ -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<std::string, std::string> 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);
index 7db9880c051b52633310b4bddfacf04b867283c7..7ef6dbed43ef0262cc1f4a0411c4ad10dbf38802 100644 (file)
@@ -76,15 +76,7 @@ void Component::create_build_info()
        }
 
        if(type==PROGRAM)
-       {
-               string strip = pkg.get_config().get_option("strip").value;
-               if(lexical_cast<bool>(strip))
-                       build_info.ldflags.push_back("-s");
-       }
-       else if(type==LIBRARY)
-       {
-               build_info.cflags.push_back("-fPIC");
-       }
+               build_info.strip = lexical_cast<bool>(pkg.get_config().get_option("strip").value);
 
        build_info.unique();
 }
index 026ec8c8a015c08b40ef8e8b74edc37a8c12ed36..4fa293b1b650153024e5601d86043e62115e9591 100644 (file)
@@ -44,12 +44,28 @@ Task *GnuCompiler::run(const Target &target) const
        const BuildInfo &binfo = comp.get_build_info();
        for(list<string>::const_iterator i=binfo.warnings.begin(); i!=binfo.warnings.end(); ++i)
                argv.push_back("-W"+*i);
-       for(list<string>::const_iterator i=binfo.cflags.begin(); i!=binfo.cflags.end(); ++i)
-               argv.push_back(*i);
        for(list<string>::const_iterator i=binfo.incpath.begin(); i!=binfo.incpath.end(); ++i)
                argv.push_back("-I"+*i);
-       for(list<string>::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();
index a14dd0bd984f096af2831ce2d6677d0e60b2436d..a8b07f8499bdc8307a8378a93c46147dbb70ad5e 100644 (file)
@@ -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<string>::const_iterator i=binfo.ldflags.begin(); i!=binfo.ldflags.end(); ++i)
-               argv.push_back(*i);
        for(list<string>::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();
index 708a9130ed63e6c158df084dbba9b01ae29b2369..555d8c56db797bc5fda9e2e72842bcea4726fc51 100644 (file)
@@ -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');
 }
index 754a314a49835997f48f170f73b113a7beeb7087..60276daff288bb48cc2292bf12e91f5b88d882e2 100644 (file)
@@ -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<unsigned>(optimize);
        }
 
        if(lexical_cast<bool>(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<bool>(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())