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