]> git.tdb.fi Git - builder.git/blob - source/gnucompiler.cpp
Support gcc's -iquote feature
[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 "architecture.h"
5 #include "builder.h"
6 #include "component.h"
7 #include "externaltask.h"
8 #include "gnucompiler.h"
9 #include "objectfile.h"
10 #include "sourcefile.h"
11 #include "sourcepackage.h"
12
13 using namespace std;
14 using namespace Msp;
15
16 GnuCompiler::GnuCompiler(Builder &b, const Architecture &a, const string &t, const string &c):
17         Tool(b, a, t)
18 {
19         string command = c;
20         if(architecture->is_cross())
21                 command = format("%s-%s", architecture->get_cross_prefix(), command);
22         executable = builder.get_vfs().find_binary(command);
23         if(!executable)
24                 builder.problem(string(), format("Can't find executable %s for tool %s", command, tag));
25
26         if(architecture->is_native())
27                 system_path.push_back("/usr/include");
28         else
29                 system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/include");
30 }
31
32 Target *GnuCompiler::create_target(const list<Target *> &sources, const string &) const
33 {
34         if(sources.size()!=1)
35                 throw invalid_argument("GnuCCompiler::create_target");
36         SourceFile &source = dynamic_cast<SourceFile &>(*sources.front());
37         ObjectFile *obj = new ObjectFile(builder, *source.get_component(), source);
38         obj->set_tool(*this);
39         return obj;
40 }
41
42 string GnuCompiler::create_build_signature(const BuildInfo &binfo) const
43 {
44         string result = FS::basename(executable->get_path());
45         if(!architecture->get_cpu().empty())
46         {
47                 result += ",m";
48                 result += architecture->get_cpu();
49         }
50         result += ',';
51         if(binfo.debug)
52                 result += 'g';
53         if(binfo.optimize)
54         {
55                 result += 'O';
56                 result += (binfo.optimize>0 ? '0'+binfo.optimize : 's');
57         }
58         return result;
59 }
60
61 Task *GnuCompiler::run(const Target &target) const
62 {
63         const ObjectFile &object = dynamic_cast<const ObjectFile &>(target);
64         const Component &comp = *object.get_component();
65
66         ExternalTask::Arguments argv;
67         argv.push_back(executable->get_path().str());
68         argv.push_back("-c");
69
70         const BuildInfo &binfo = comp.get_build_info_for_path(object.get_source().get_path());
71         if(binfo.warning_level>=1)
72         {
73                 argv.push_back("-Wall");
74                 if(binfo.warning_level>=2)
75                 {
76                         argv.push_back("-Wextra");
77                         argv.push_back("-Wundef");
78                 }
79                 if(binfo.warning_level>=3)
80                 {
81                         argv.push_back("-pedantic");
82                         argv.push_back("-Wno-long-long");
83                         argv.push_back("-Wshadow");
84                         if(tag=="CC")
85                         {
86                                 argv.push_back("-Wc++-compat");
87                                 argv.push_back("-Wstrict-prototypes");
88                         }
89                 }
90                 if(binfo.warning_level>=4)
91                 {
92                         // Some truly paranoid warnings
93                         argv.push_back("-Wstrict-overflow=4");
94                         argv.push_back("-Wfloat-equal");
95                         argv.push_back("-Wconversion");
96                         argv.push_back("-Wwrite-strings");
97                         argv.push_back("-Winline");
98                 }
99                 if(binfo.fatal_warnings)
100                         argv.push_back("-Werror");
101         }
102         for(BuildInfo::PathList::const_iterator i=binfo.local_incpath.begin(); i!=binfo.local_incpath.end(); ++i)
103         {
104                 argv.push_back("-iquote");
105                 argv.push_back(i->str());
106         }
107         for(BuildInfo::PathList::const_iterator i=binfo.incpath.begin(); i!=binfo.incpath.end(); ++i)
108                 argv.push_back("-I"+i->str());
109         for(BuildInfo::DefineMap::const_iterator i=binfo.defines.begin(); i!=binfo.defines.end(); ++i)
110         {
111                 if(i->second.empty())
112                         argv.push_back(format("-D%s", i->first));
113                 else
114                         argv.push_back(format("-D%s=%s", i->first, i->second));
115         }
116         if(binfo.debug)
117                 argv.push_back("-ggdb");
118         if(binfo.optimize)
119         {
120                 if(binfo.optimize<0)
121                         argv.push_back("-Os");
122                 else
123                         argv.push_back(format("-O%d", binfo.optimize));
124         }
125         if(binfo.threads)
126                 argv.push_back("-pthread");
127         if((comp.get_type()==Component::LIBRARY || comp.get_type()==Component::MODULE) && architecture->get_system()!="windows")
128                 argv.push_back("-fPIC");
129
130         const Architecture &native_arch = builder.get_native_arch();
131         if(architecture->get_bits()!=native_arch.get_bits())
132                 argv.push_back(format("-m%d", architecture->get_bits()));
133
134         const string &cpu = architecture->get_cpu();
135         if(!cpu.empty())
136                 argv.push_back("-march="+cpu);
137
138         FS::Path obj_path = object.get_path();
139         FS::Path src_path = object.get_source().get_path();
140         FS::Path work_dir = comp.get_package().get_source_directory();
141
142         argv.push_back("-o");
143         argv.push_back(relative(obj_path, work_dir).str());
144         argv.push_back(relative(src_path, work_dir).str());
145
146         return new ExternalTask(argv, work_dir);
147 }