]> git.tdb.fi Git - builder.git/blob - source/gnucompiler.cpp
Move C-specific stuff from SourceFile to CSourceFile
[builder.git] / source / gnucompiler.cpp
1 #include <msp/fs/dir.h>
2 #include <msp/fs/utils.h>
3 #include <msp/strings/format.h>
4 #include "builder.h"
5 #include "component.h"
6 #include "externaltask.h"
7 #include "gnucompiler.h"
8 #include "objectfile.h"
9 #include "sourcefile.h"
10 #include "sourcepackage.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 GnuCompiler::GnuCompiler(Builder &b, const string &t, const string &n):
16         Tool(b, t),
17         name(n)
18 { }
19
20 Target *GnuCompiler::create_target(const list<Target *> &sources, const std::string &) const
21 {
22         if(sources.size()!=1)
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);
26         obj->set_tool(*this);
27         return obj;
28 }
29
30 Task *GnuCompiler::run(const Target &target) const
31 {
32         const ObjectFile &object = dynamic_cast<const ObjectFile &>(target);
33         const Component &comp = object.get_component();
34
35         vector<string> argv;
36         argv.push_back(name);
37         argv.push_back("-c");
38
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)
45         {
46                 if(i->second.empty())
47                         argv.push_back(format("-D%s", i->first));
48                 else
49                         argv.push_back(format("-D%s=%s", i->first, i->second));
50         }
51         if(binfo.debug)
52                 argv.push_back("-ggdb");
53         if(binfo.optimize)
54         {
55                 if(binfo.optimize<0)
56                         argv.push_back("-Os");
57                 else
58                         argv.push_back(format("-O%d", binfo.optimize));
59         }
60         if(binfo.threads)
61                 argv.push_back("-pthread");
62         if(comp.get_type()==Component::LIBRARY)
63                 argv.push_back("-fPIC");
64
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()));
69
70         const string &cpu = arch.get_cpu();
71         if(!cpu.empty())
72                 argv.push_back("-march="+cpu);
73
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();
77
78         argv.push_back("-o");
79         argv.push_back(relative(obj_path, work_dir).str());
80         argv.push_back(relative(src_path, work_dir).str());
81
82         if(!builder.get_dry_run())
83                 FS::mkpath(FS::dirname(obj_path), 0755);
84
85         return new ExternalTask(argv, work_dir);
86 }