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