]> git.tdb.fi Git - builder.git/blobdiff - source/sourcepackage.cpp
Move variable expansion from Config to SourcePackage
[builder.git] / source / sourcepackage.cpp
index e946d018a51953a44149a6888179500a2923fd32..a466089ae75aad13b049d5e5e3660a930f597ec3 100644 (file)
@@ -5,6 +5,7 @@ Copyright © 2007-2010  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
+#include <cstdlib>
 #include <msp/io/print.h>
 #include <msp/strings/lexicalcast.h>
 #include <msp/strings/utils.h>
@@ -78,6 +79,50 @@ LibMode SourcePackage::get_library_mode() const
                throw Exception("Unknown library mode");
 }
 
+string SourcePackage::expand_string(const string &str) const
+{
+       string result = str;
+       string::size_type dollar = 0;
+       unsigned n = 0;
+       while((dollar = result.find('$'))!=string::npos)
+       {
+               if(n>1000)
+                       throw Exception("Too much variable expansions");
+
+               string::size_type end;
+               string var;
+               if(dollar+1<result.size() && result[dollar+1]=='{')
+               {
+                       end = result.find('}', dollar+2);
+                       if(end==string::npos)
+                               throw Exception("Unterminated variable reference");
+                       var = result.substr(dollar+2, end-dollar-2);
+                       ++end;
+               }
+               else
+               {
+                       for(end=dollar+1; (isalnum(result[end]) || result[end]=='_'); ++end) ;
+                       var = result.substr(dollar+1, end-dollar-1);
+               }
+
+               string value;
+               if(config.is_option(var))
+                       value = config.get_option(var).value;
+               else if(var=="arch")
+                       value = builder.get_current_arch().get_name();
+               else if(var=="system")
+                       value = builder.get_current_arch().get_system();
+               else if(const char *ptr = getenv(var.c_str()))
+                       value = ptr;
+
+               result.replace(dollar, end-dollar, value);
+
+               ++n;
+       }
+
+       return result;
+}
+
 void SourcePackage::do_configure(const StringMap &opts, unsigned flag)
 {
        init_config();
@@ -117,11 +162,7 @@ void SourcePackage::do_configure(const StringMap &opts, unsigned flag)
        deps_cache.load();
 
        for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
-       {
-               const PackageList &reqs = i->get_requires();
-               for(PackageList::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
-                       (*j)->configure(opts, flag&2);
-       }
+               i->configure(opts, flag);
 }
 
 void SourcePackage::init_config()
@@ -274,5 +315,5 @@ void SourcePackage::Loader::tar_file(const string &f)
        SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
        for(ComponentList::iterator i=spkg.components.begin(); i!=spkg.components.end(); ++i)
                if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
-                       const_cast<PathList &>(i->get_sources()).push_back(spkg.source/f);
+                       const_cast<StringList &>(i->get_sources()).push_back((spkg.source/f).str());
 }