]> git.tdb.fi Git - builder.git/blob - source/gnucompiler.cpp
051722832ea66402e515733fb51cd5c6ad78aa8e
[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 GnuCompiler::GnuCompiler(Builder &b, const Architecture &a, const string &t, const FS::Path &sysroot):
19         Tool(b, a, t)
20 {
21         if(!sysroot.empty())
22         {
23                 build_info.sysroot = sysroot;
24                 system_path.push_back(sysroot/"usr/include");
25         }
26         else 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 &)
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 void GnuCompiler::do_prepare()
62 {
63         executable = builder.get_vfs().find_binary(command);
64         if(executable)
65         {
66                 ExternalTask::Arguments argv;
67                 argv.push_back(executable->get_path().str());
68                 argv.push_back("-dumpversion");
69
70                 builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
71                 try
72                 {
73                         version = strip(ExternalTask::run_and_capture_output(argv));
74                         builder.get_logger().log("tools", format("%s version is %s", FS::basename(executable->get_path()), version));
75                 }
76                 catch(const runtime_error &)
77                 { }
78         }
79 }
80
81 Task *GnuCompiler::run(const Target &target) const
82 {
83         const ObjectFile &object = dynamic_cast<const ObjectFile &>(target);
84         const Component &comp = *object.get_component();
85
86         ExternalTask::Arguments argv;
87         argv.push_back(executable->get_path().str());
88         argv.push_back("-c");
89
90         BuildInfo binfo;
91         target.collect_build_info(binfo);
92
93         string tag_for_std = (tag=="OBJC" ? "CC" : tag);
94         if(binfo.standards.count(tag_for_std))
95                 argv.push_back("-std="+get_item(binfo.standards, tag_for_std));
96         if(tag=="OBJC" && binfo.standards.count(tag))
97                 argv.push_back("-fobjc-std="+get_item(binfo.standards, tag));
98
99         if(binfo.warning_level>=1)
100         {
101                 argv.push_back("-Wall");
102                 if(binfo.warning_level>=2)
103                 {
104                         argv.push_back("-Wextra");
105                         argv.push_back("-Wundef");
106                 }
107                 if(binfo.warning_level>=3)
108                 {
109                         argv.push_back("-pedantic");
110                         argv.push_back("-Wno-long-long");
111                         argv.push_back("-Wshadow");
112                         if(tag=="CC")
113                         {
114                                 argv.push_back("-Wc++-compat");
115                                 argv.push_back("-Wstrict-prototypes");
116                         }
117                 }
118                 if(binfo.warning_level>=4)
119                 {
120                         // Some truly paranoid warnings
121                         argv.push_back("-Wstrict-overflow=4");
122                         argv.push_back("-Wfloat-equal");
123                         argv.push_back("-Wconversion");
124                         argv.push_back("-Wwrite-strings");
125                         argv.push_back("-Winline");
126                 }
127                 if(binfo.fatal_warnings)
128                         argv.push_back("-Werror");
129         }
130
131         const FS::Path &sysroot = binfo.sysroot;
132         if(!sysroot.empty())
133                 argv.push_back("--sysroot="+sysroot.str());
134         for(BuildInfo::PathList::const_iterator i=binfo.local_incpath.begin(); i!=binfo.local_incpath.end(); ++i)
135         {
136                 argv.push_back("-iquote");
137                 argv.push_back(i->str());
138         }
139         for(BuildInfo::PathList::const_iterator i=binfo.incpath.begin(); i!=binfo.incpath.end(); ++i)
140                 argv.push_back("-I"+i->str());
141
142         for(BuildInfo::DefineMap::const_iterator i=binfo.defines.begin(); i!=binfo.defines.end(); ++i)
143         {
144                 if(i->second.empty())
145                         argv.push_back(format("-D%s", i->first));
146                 else
147                         argv.push_back(format("-D%s=%s", i->first, i->second));
148         }
149
150         if(binfo.debug)
151                 argv.push_back("-ggdb");
152         if(binfo.optimize)
153         {
154                 if(binfo.optimize<0)
155                         argv.push_back("-Os");
156                 else
157                         argv.push_back(format("-O%d", binfo.optimize));
158         }
159         if(binfo.threads && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
160                 argv.push_back("-pthread");
161         if(object.is_used_in_shared_library() && architecture->get_system()!="windows")
162                 argv.push_back("-fPIC");
163
164         const Architecture &native_arch = builder.get_native_arch();
165         if(architecture->is_native() && architecture->get_bits()!=native_arch.get_bits())
166                 argv.push_back(format("-m%d", architecture->get_bits()));
167
168         const string &cpu = architecture->get_cpu();
169         if(!cpu.empty())
170                 argv.push_back("-march="+cpu);
171
172         FS::Path obj_path = object.get_path();
173         FS::Path src_path = object.get_source().get_path();
174         FS::Path work_dir = comp.get_package().get_source_directory();
175
176         argv.push_back("-o");
177         argv.push_back(relative(obj_path, work_dir).str());
178         argv.push_back(relative(src_path, work_dir).str());
179
180         return new ExternalTask(argv, work_dir);
181 }