1 #include <msp/fs/dir.h>
2 #include <msp/fs/utils.h>
3 #include <msp/strings/format.h>
4 #include "architecture.h"
7 #include "externaltask.h"
8 #include "gnucompiler.h"
9 #include "objectfile.h"
10 #include "sourcefile.h"
11 #include "sourcepackage.h"
16 GnuCompiler::GnuCompiler(Builder &b, const string &t, const string &n):
19 executable = builder.get_vfs().find_binary(n);
21 const Architecture &arch = builder.get_current_arch();
23 system_path.push_back("/usr/include");
25 system_path.push_back("/usr/"+arch.get_cross_prefix()+"/include");
28 Target *GnuCompiler::create_target(const list<Target *> &sources, const std::string &) const
31 throw invalid_argument("GnuCCompiler::create_target");
32 SourceFile &source = dynamic_cast<SourceFile &>(*sources.front());
33 ObjectFile *obj = new ObjectFile(builder, *source.get_component(), source);
38 Task *GnuCompiler::run(const Target &target) const
40 const ObjectFile &object = dynamic_cast<const ObjectFile &>(target);
41 const Component &comp = object.get_component();
43 ExternalTask::Arguments argv;
44 argv.push_back(executable->get_path().str());
47 const BuildInfo &binfo = comp.get_build_info();
48 for(BuildInfo::WordList::const_iterator i=binfo.warnings.begin(); i!=binfo.warnings.end(); ++i)
49 argv.push_back("-W"+*i);
50 for(BuildInfo::PathList::const_iterator i=binfo.incpath.begin(); i!=binfo.incpath.end(); ++i)
51 argv.push_back("-I"+i->str());
52 for(BuildInfo::DefineMap::const_iterator i=binfo.defines.begin(); i!=binfo.defines.end(); ++i)
55 argv.push_back(format("-D%s", i->first));
57 argv.push_back(format("-D%s=%s", i->first, i->second));
60 argv.push_back("-ggdb");
64 argv.push_back("-Os");
66 argv.push_back(format("-O%d", binfo.optimize));
69 argv.push_back("-pthread");
70 if(comp.get_type()==Component::LIBRARY)
71 argv.push_back("-fPIC");
73 const Architecture &arch = builder.get_current_arch();
74 const Architecture &native_arch = builder.get_native_arch();
75 if(arch.get_bits()!=native_arch.get_bits())
76 argv.push_back(format("-m%d", arch.get_bits()));
78 const string &cpu = arch.get_cpu();
80 argv.push_back("-march="+cpu);
82 FS::Path obj_path = object.get_path();
83 FS::Path src_path = object.get_source().get_path();
84 FS::Path work_dir = comp.get_package().get_source();
87 argv.push_back(relative(obj_path, work_dir).str());
88 argv.push_back(relative(src_path, work_dir).str());
90 if(!builder.get_dry_run())
91 FS::mkpath(FS::dirname(obj_path), 0755);
93 return new ExternalTask(argv, work_dir);