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