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