+/* $Id$
+
+This file is part of builder
+Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include <iostream>
+#include <msp/strings/lexicalcast.h>
+#include <msp/strings/utils.h>
+#include "binarypackage.h"
+#include "builder.h"
+#include "misc.h"
+#include "sourcepackage.h"
+
+using namespace std;
+using namespace Msp;
+
+/**
+Creates a buildable package.
+*/
+SourcePackage::SourcePackage(Builder &b, const string &n, const Path::Path &s):
+ Package(b, n),
+ source(s),
+ config(*this),
+ deps_cache(*this)
+{
+ tar_files.push_back(source/"Build");
+}
+
+Msp::Path::Path SourcePackage::get_temp_dir() const
+{
+ return source/config.get_option("tempdir").value/config.get_option("profile").value;
+}
+
+Msp::Path::Path SourcePackage::get_out_dir() const
+{
+ return source/config.get_option("outdir").value;
+}
+
+/**
+Checks which kinds of things the components of this package install.
+
+@return A bitmask of installed things
+*/
+unsigned SourcePackage::get_install_flags()
+{
+ unsigned flags=0;
+ for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
+ {
+ if(i->get_install())
+ {
+ if(i->get_type()==Component::PROGRAM)
+ flags|=BIN;
+ else if(i->get_type()==Component::LIBRARY || i->get_type()==Component::MODULE)
+ flags|=LIB;
+ }
+ if(!i->get_install_headers().empty())
+ flags|=INCLUDE;
+ }
+
+ return flags;
+}
+
+LibMode SourcePackage::get_library_mode() const
+{
+ const string &mode=config.get_option("staticlibs").value;
+ if(mode=="all")
+ return ALL_STATIC;
+ else if(mode=="local")
+ return LOCAL_STATIC;
+ else if(mode=="none")
+ return DYNAMIC;
+ else
+ throw Exception("Unknown library mode");
+}
+
+/**
+Tries to resolve all references to dependency packages.
+*/
+void SourcePackage::resolve_refs()
+{
+ Package::resolve_refs();
+
+ for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
+ if(i->get_package())
+ all_reqs.push_back(i->get_package());
+
+ for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
+ {
+ i->resolve_refs();
+ const PkgRefList &creqs=i->get_requires();
+ for(PkgRefList::const_iterator j=creqs.begin(); j!=creqs.end(); ++j)
+ if(j->get_package())
+ all_reqs.push_back(j->get_package());
+ }
+
+ for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
+ {
+ i->resolve_refs();
+ const PkgRefList &creqs=i->get_requires();
+ for(PkgRefList::const_iterator j=creqs.begin(); j!=creqs.end(); ++j)
+ if(j->get_package())
+ all_reqs.push_back(j->get_package());
+ }
+}
+
+/*** private ***/
+
+/**
+Processes configuration options that were most likely obtained from the command
+line.
+*/
+void SourcePackage::do_configure(const StringMap &opts, unsigned flag)
+{
+ init_config();
+
+ StringMap::const_iterator prof=opts.find("profile");
+ if(prof!=opts.end() && flag)
+ config.select_profile(prof->second);
+ else
+ config.select_last_profile();
+
+ if(flag && config.update(opts))
+ {
+ if(builder.get_verbose()>=2)
+ cout<<"Configuration of "<<name<<" changed\n";
+ if(!builder.get_dry_run())
+ config.save();
+ }
+
+ config.finish();
+
+ for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
+ if(i->eval())
+ {
+ const PkgRefList &reqs=i->get_requires();
+ requires.insert(requires.end(), reqs.begin(), reqs.end());
+ build_info.add(i->get_build_info());
+ }
+
+ for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
+ {
+ BinaryPackage *bpkg=dynamic_cast<BinaryPackage *>(*i);
+ if(bpkg && bpkg->get_need_path())
+ bpkg->set_path(config.get_option(bpkg->get_name()+"_path").value);
+ }
+
+ deps_cache.load();
+
+ for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
+ (*i)->configure(opts, flag&2);
+}
+
+/**
+Initializes configuration options.
+*/
+void SourcePackage::init_config()
+{
+ config.add_option("profile", "default", "Configuration profile");
+ config.add_option("tempdir", "temp", "Directory for storing temporary files");
+ config.add_option("outdir", ".", "Directory to put build results in");
+ config.add_option("optimize", "0", "Apply compiler optimizations");
+ config.add_option("strip", "0", "Strip symbols from programs");
+ config.add_option("debug", "0", "Produce debugging symbols");
+ config.add_option("cpu", "auto", "CPU type to optimize for");
+ config.add_option("arch", "native", "Architecture for cross-compiling");
+ config.add_option("staticlibs", "local", "Use static libraries");
+
+ unsigned flags=get_install_flags();
+ if(flags)
+ config.add_option("prefix", "$HOME/local", "Installation prefix");
+ /*if(flags&INCLUDE)
+ config.add_option("includedir", "$prefix/include", "Header installation directory");
+ if(flags&BIN)
+ config.add_option("includedir", "$prefix/bin", "Binary installation directory");
+ if(flags&LIB)
+ config.add_option("includedir", "$prefix/lib", "Library installation directory");
+ if(flags&DATA)
+ config.add_option("includedir", "$prefix/share", "Data installation directory");*/
+
+ for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
+ config.add_option("with_"+i->name, "0", i->descr);
+
+ for(PackageList::const_iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
+ {
+ BinaryPackage *bpkg=dynamic_cast<BinaryPackage *>(*i);
+ if(bpkg && bpkg->get_need_path())
+ config.add_option(bpkg->get_name()+"_path", "", "Path for "+bpkg->get_name());
+ }
+}
+
+/**
+Fills in build info based on configuration. All required packages must be
+configured when this is called.
+*/
+void SourcePackage::create_build_info()
+{
+ for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
+ {
+ Package *pkg=i->get_package();
+ if(!pkg)
+ continue;
+ const BuildInfo &ebi=pkg->get_exported_binfo();
+ build_info.add(ebi);
+
+ export_binfo.cflags.insert(export_binfo.cflags.end(), ebi.cflags.begin(), ebi.cflags.end());
+ export_binfo.incpath.insert(export_binfo.incpath.end(), ebi.incpath.begin(), ebi.incpath.end());
+ export_binfo.defines.insert(export_binfo.defines.end(), ebi.defines.begin(), ebi.defines.end());
+ }
+
+ build_info.cflags.push_back("-Wall");
+ build_info.cflags.push_back("-Wshadow");
+ build_info.cflags.push_back("-Wextra");
+ build_info.cflags.push_back("-Wpointer-arith");
+ build_info.cflags.push_back("-Wconversion");
+ build_info.cflags.push_back("-Werror");
+
+ unsigned flags=get_install_flags();
+
+ if(flags&INCLUDE)
+ export_binfo.incpath.push_back((Path::Path(config.get_option("prefix").value)/"include").str());
+ if(flags&LIB)
+ export_binfo.libpath.push_back((Path::Path(config.get_option("prefix").value)/"lib").str());
+
+ string optimize=config.get_option("optimize").value;
+ if(lexical_cast<unsigned>(optimize))
+ {
+ build_info.cflags.push_back("-O"+optimize);
+ build_info.ldflags.push_back("-O"+optimize);
+ string cpu=config.get_option("cpu").value;
+ if(cpu!="auto")
+ build_info.cflags.push_back("-march="+cpu);
+ }
+
+ if(lexical_cast<bool>(config.get_option("debug").value))
+ {
+ build_info.cflags.push_back("-ggdb");
+ build_info.defines.push_back("DEBUG");
+ }
+
+ 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.unique();
+
+ for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
+ {
+ i->create_build_info();
+ if(i->get_type()==Component::LIBRARY)
+ export_binfo.libs.push_back(i->get_name());
+ }
+
+ export_binfo.unique();
+}
+
+
+SourcePackage::Loader::Loader(Package &p):
+ Package::Loader(p)
+{
+ add("version", &SourcePackage::version);
+ add("description", &SourcePackage::description);
+ add("build_info", &Loader::build_info);
+ add("feature", &Loader::feature);
+ add("if", &Loader::condition);
+ add("program", &Loader::program);
+ add("library", &Loader::library);
+ add("module", &Loader::module);
+ add("headers", &Loader::headers);
+ add("tar_file", &Loader::tar_file);
+}
+
+void SourcePackage::Loader::feature(const string &n, const string &d)
+{
+ static_cast<SourcePackage &>(pkg).features.push_back(Feature(n, d));
+}
+
+void SourcePackage::Loader::condition(const string &c)
+{
+ SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
+ Condition cond(spkg, c);
+ load_sub(cond);
+ spkg.conditions.push_back(cond);
+}
+
+void SourcePackage::Loader::program(const string &n)
+{
+ SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
+ Component prog(spkg, Component::PROGRAM, n);
+ load_sub(prog);
+ spkg.components.push_back(prog);
+}
+
+void SourcePackage::Loader::library(const string &n)
+{
+ SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
+ Component prog(spkg, Component::LIBRARY, n);
+ load_sub(prog);
+ spkg.components.push_back(prog);
+}
+
+void SourcePackage::Loader::module(const string &n)
+{
+ SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
+ Component prog(spkg, Component::MODULE, n);
+ load_sub(prog);
+ spkg.components.push_back(prog);
+}
+
+void SourcePackage::Loader::headers(const string &n)
+{
+ SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
+ Component prog(spkg, Component::HEADERS, n);
+ load_sub(prog);
+ spkg.components.push_back(prog);
+}
+
+void SourcePackage::Loader::build_info()
+{
+ load_sub(static_cast<SourcePackage &>(pkg).build_info);
+}
+
+void SourcePackage::Loader::tar_file(const string &f)
+{
+ SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
+ spkg.tar_files.push_back(spkg.source/f);
+}