1 #include <msp/fs/dir.h>
2 #include <msp/fs/utils.h>
3 #include <msp/strings/format.h>
6 #include "externaltask.h"
7 #include "gnucompiler.h"
8 #include "objectfile.h"
9 #include "sourcefile.h"
10 #include "sourcepackage.h"
15 GnuCompiler::GnuCompiler(Builder &b, const string &t, const string &n):
20 Target *GnuCompiler::create_target(const list<Target *> &sources, const std::string &) const
23 throw invalid_argument("GnuCCompiler::create_target");
24 SourceFile &source = dynamic_cast<SourceFile &>(*sources.front());
25 ObjectFile *obj = new ObjectFile(builder, *source.get_component(), source);
30 Task *GnuCompiler::run(const Target &target) const
32 const ObjectFile &object = dynamic_cast<const ObjectFile &>(target);
33 const Component &comp = object.get_component();
35 ExternalTask::Arguments argv;
39 const BuildInfo &binfo = comp.get_build_info();
40 for(list<string>::const_iterator i=binfo.warnings.begin(); i!=binfo.warnings.end(); ++i)
41 argv.push_back("-W"+*i);
42 for(list<string>::const_iterator i=binfo.incpath.begin(); i!=binfo.incpath.end(); ++i)
43 argv.push_back("-I"+*i);
44 for(BuildInfo::DefineMap::const_iterator i=binfo.defines.begin(); i!=binfo.defines.end(); ++i)
47 argv.push_back(format("-D%s", i->first));
49 argv.push_back(format("-D%s=%s", i->first, i->second));
52 argv.push_back("-ggdb");
56 argv.push_back("-Os");
58 argv.push_back(format("-O%d", binfo.optimize));
61 argv.push_back("-pthread");
62 if(comp.get_type()==Component::LIBRARY)
63 argv.push_back("-fPIC");
65 const Architecture &arch = builder.get_current_arch();
66 const Architecture &native_arch = builder.get_native_arch();
67 if(arch.get_bits()!=native_arch.get_bits())
68 argv.push_back(format("-m%d", arch.get_bits()));
70 const string &cpu = arch.get_cpu();
72 argv.push_back("-march="+cpu);
74 FS::Path obj_path = object.get_path();
75 FS::Path src_path = object.get_source().get_path();
76 FS::Path work_dir = comp.get_package().get_source();
79 argv.push_back(relative(obj_path, work_dir).str());
80 argv.push_back(relative(src_path, work_dir).str());
82 if(!builder.get_dry_run())
83 FS::mkpath(FS::dirname(obj_path), 0755);
85 return new ExternalTask(argv, work_dir);