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