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