]> git.tdb.fi Git - builder.git/blob - source/gnucompiler.cpp
ea04fbeacfb79ca92f444b8b374b68f592477271
[builder.git] / source / gnucompiler.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/fs/dir.h>
3 #include <msp/fs/stat.h>
4 #include <msp/fs/utils.h>
5 #include <msp/strings/format.h>
6 #include <msp/strings/utils.h>
7 #include "architecture.h"
8 #include "builder.h"
9 #include "component.h"
10 #include "csourcefile.h"
11 #include "externaltask.h"
12 #include "gnucompiler.h"
13 #include "objcsourcefile.h"
14 #include "objectfile.h"
15 #include "sourcefile.h"
16 #include "sourcepackage.h"
17
18 using namespace std;
19 using namespace Msp;
20
21 namespace {
22
23 const char *cpus[] =
24 {
25         "athlonxp", "athlon-xp",
26         "armv7a",   "armv7-a",
27         0
28 };
29
30 }
31
32 GnuCompiler::GnuCompiler(Builder &b, const Architecture &a, const string &t, const FS::Path &sysroot):
33         Tool(b, a, t)
34 {
35         if(tag=="CC")
36         {
37                 input_suffixes.push_back(".c");
38                 aux_suffixes.push_back(".h");
39         }
40         else if(tag=="CXX")
41         {
42                 input_suffixes.push_back(".cpp");
43                 input_suffixes.push_back(".cc");
44                 aux_suffixes.push_back(".hpp");
45         }
46         else if(tag=="OBJC")
47         {
48                 input_suffixes.push_back(".m");
49                 build_info.libs.push_back("objc");
50         }
51         else
52                 throw invalid_argument("GnuCompiler::GnuCompiler");
53
54         set_command((tag=="CXX" ? "g++" : "gcc"), true);
55
56         if(!sysroot.empty())
57         {
58                 build_info.sysroot = sysroot;
59                 system_path.push_back(sysroot/"usr/include");
60         }
61         else if(architecture->is_native())
62                 system_path.push_back("/usr/include");
63         else
64                 system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/include");
65 }
66
67 Target *GnuCompiler::create_source(const Component &comp, const FS::Path &path) const
68 {
69         if(tag=="OBJC")
70                 return new ObjCSourceFile(builder, comp, path);
71         else
72                 return new CSourceFile(builder, comp, path);
73 }
74
75 Target *GnuCompiler::create_source(const FS::Path &path) const
76 {
77         if(tag=="OBJC")
78                 return new ObjCSourceFile(builder, path);
79         else
80                 return new CSourceFile(builder, path);
81 }
82
83 Target *GnuCompiler::create_target(const list<Target *> &sources, const string &)
84 {
85         if(sources.size()!=1)
86                 throw invalid_argument("GnuCCompiler::create_target");
87         SourceFile &source = dynamic_cast<SourceFile &>(*sources.front());
88         ObjectFile *obj = new ObjectFile(builder, *source.get_component(), source);
89         obj->set_tool(*this);
90         return obj;
91 }
92
93 string GnuCompiler::create_build_signature(const BuildInfo &binfo) const
94 {
95         string result = FS::basename(executable->get_path());
96         if(!architecture->get_cpu().empty())
97         {
98                 result += ",m";
99                 result += architecture->get_cpu();
100         }
101         result += ',';
102         if(binfo.debug)
103                 result += 'g';
104         if(binfo.optimize)
105         {
106                 result += 'O';
107                 result += (binfo.optimize>0 ? '0'+binfo.optimize : 's');
108         }
109         return result;
110 }
111
112 void GnuCompiler::do_prepare()
113 {
114         executable = builder.get_vfs().find_binary(command);
115         if(executable)
116         {
117                 ExternalTask::Arguments argv;
118                 argv.push_back(executable->get_path().str());
119                 argv.push_back("-dumpversion");
120
121                 builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
122                 try
123                 {
124                         version = strip(ExternalTask::run_and_capture_output(argv));
125                         builder.get_logger().log("tools", format("%s version is %s", FS::basename(executable->get_path()), version));
126
127                         if(tag=="CXX")
128                         {
129                                 const FS::Path &sysroot = build_info.sysroot;
130                                 FS::Path cxx_path = sysroot/"usr"/"include"/"c++"/version;
131                                 if(FS::is_dir(cxx_path))
132                                         system_path.push_back(cxx_path);
133                         }
134                 }
135                 catch(const runtime_error &)
136                 { }
137         }
138 }
139
140 Task *GnuCompiler::run(const Target &target) const
141 {
142         const ObjectFile &object = dynamic_cast<const ObjectFile &>(target);
143
144         ExternalTask::Arguments argv;
145         argv.push_back(executable->get_path().str());
146         argv.push_back("-c");
147
148         BuildInfo binfo;
149         target.collect_build_info(binfo);
150
151         string tag_for_std = (tag=="OBJC" ? "CC" : tag);
152         if(binfo.standards.count(tag_for_std))
153                 argv.push_back("-std="+get_item(binfo.standards, tag_for_std));
154         if(tag=="OBJC" && binfo.standards.count(tag))
155                 argv.push_back("-fobjc-std="+get_item(binfo.standards, tag));
156
157         if(binfo.warning_level>=1)
158         {
159                 argv.push_back("-Wall");
160                 if(binfo.warning_level>=2)
161                 {
162                         argv.push_back("-Wextra");
163                         argv.push_back("-Wundef");
164                 }
165                 if(binfo.warning_level>=3)
166                 {
167                         argv.push_back("-pedantic");
168                         argv.push_back("-Wno-long-long");
169                         argv.push_back("-Wshadow");
170                         if(tag=="CC")
171                         {
172                                 argv.push_back("-Wc++-compat");
173                                 argv.push_back("-Wstrict-prototypes");
174                         }
175                 }
176                 if(binfo.warning_level>=4)
177                 {
178                         // Some truly paranoid warnings
179                         argv.push_back("-Wstrict-overflow=4");
180                         argv.push_back("-Wfloat-equal");
181                         argv.push_back("-Wconversion");
182                         argv.push_back("-Wwrite-strings");
183                         argv.push_back("-Winline");
184                 }
185                 if(binfo.fatal_warnings)
186                 {
187                         argv.push_back("-Werror");
188                         argv.push_back("-Wno-error=deprecated-declarations");
189                 }
190         }
191
192         const FS::Path &sysroot = binfo.sysroot;
193         if(!sysroot.empty())
194                 argv.push_back("--sysroot="+sysroot.str());
195         for(BuildInfo::PathList::const_iterator i=binfo.local_incpath.begin(); i!=binfo.local_incpath.end(); ++i)
196         {
197                 argv.push_back("-iquote");
198                 argv.push_back(i->str());
199         }
200         for(BuildInfo::PathList::const_iterator i=binfo.incpath.begin(); i!=binfo.incpath.end(); ++i)
201                 argv.push_back("-I"+i->str());
202
203         for(BuildInfo::DefineMap::const_iterator i=binfo.defines.begin(); i!=binfo.defines.end(); ++i)
204         {
205                 if(i->second.empty())
206                         argv.push_back(format("-D%s", i->first));
207                 else
208                         argv.push_back(format("-D%s=%s", i->first, i->second));
209         }
210
211         if(binfo.debug)
212                 argv.push_back("-ggdb");
213         if(binfo.optimize)
214         {
215                 if(binfo.optimize<0)
216                         argv.push_back("-Os");
217                 else
218                         argv.push_back(format("-O%d", binfo.optimize));
219         }
220         if(binfo.threads && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
221                 argv.push_back("-pthread");
222         if(object.is_used_in_shared_library() && architecture->get_system()!="windows")
223                 argv.push_back("-fPIC");
224
225         const Architecture &native_arch = builder.get_native_arch();
226         if(architecture->is_native() && architecture->get_bits()!=native_arch.get_bits())
227                 argv.push_back(format("-m%d", architecture->get_bits()));
228
229         string cpu = architecture->get_cpu();
230         if(!cpu.empty())
231         {
232                 for(unsigned i=0; cpus[i]; i+=2)
233                         if(cpu==cpus[i])
234                         {
235                                 cpu = cpus[i+1];
236                                 break;
237                         }
238                 argv.push_back("-march="+cpu);
239         }
240
241         if(!architecture->get_fpu().empty())
242         {
243                 if(architecture->get_type()=="x86")
244                 {
245                         argv.push_back("-mfpmath="+architecture->get_fpu());
246                         if(architecture->get_fpu()=="sse")
247                                 argv.push_back("-msse2");
248                 }
249                 else if(architecture->get_type()=="arm")
250                 {
251                         argv.push_back("-mfpu="+architecture->get_fpu());
252                         argv.push_back("-mfloat-abi=softfp");
253                 }
254         }
255
256         FS::Path obj_path = object.get_path();
257         FS::Path src_path = object.get_source().get_path();
258         FS::Path work_dir = object.get_component()->get_package().get_source_directory();
259
260         argv.push_back("-o");
261         argv.push_back(relative(obj_path, work_dir).str());
262         argv.push_back(relative(src_path, work_dir).str());
263
264         return new ExternalTask(argv, work_dir);
265 }