1 #include <msp/fs/dir.h>
2 #include <msp/fs/utils.h>
3 #include <msp/strings/format.h>
4 #include "architecture.h"
7 #include "externaltask.h"
8 #include "gnucompiler.h"
9 #include "objectfile.h"
10 #include "sourcefile.h"
11 #include "sourcepackage.h"
16 GnuCompiler::GnuCompiler(Builder &b, const Architecture &a, const string &t, const string &c):
20 if(architecture->is_cross())
21 command = format("%s-%s", architecture->get_cross_prefix(), command);
22 executable = builder.get_vfs().find_binary(command);
24 if(architecture->is_native())
25 system_path.push_back("/usr/include");
27 system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/include");
30 Target *GnuCompiler::create_target(const list<Target *> &sources, const string &) const
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);
40 string GnuCompiler::create_build_signature(const BuildInfo &binfo) const
42 string result = FS::basename(executable->get_path());
43 if(!architecture->get_cpu().empty())
46 result += architecture->get_cpu();
54 result += (binfo.optimize>0 ? '0'+binfo.optimize : 's');
59 Task *GnuCompiler::run(const Target &target) const
61 const ObjectFile &object = dynamic_cast<const ObjectFile &>(target);
62 const Component &comp = *object.get_component();
64 ExternalTask::Arguments argv;
65 argv.push_back(executable->get_path().str());
68 const BuildInfo &binfo = comp.get_build_info();
69 if(binfo.warning_level>=1)
71 argv.push_back("-Wall");
72 if(binfo.warning_level>=2)
74 argv.push_back("-Wextra");
75 argv.push_back("-Wundef");
77 if(binfo.warning_level>=3)
79 argv.push_back("-pedantic");
80 argv.push_back("-Wno-long-long");
81 argv.push_back("-Wshadow");
84 argv.push_back("-Wc++-compat");
85 argv.push_back("-Wstrict-prototypes");
88 if(binfo.warning_level>=4)
90 // Some truly paranoid warnings
91 argv.push_back("-Wstrict-overflow=4");
92 argv.push_back("-Wfloat-equal");
93 argv.push_back("-Wconversion");
94 argv.push_back("-Wwrite-strings");
95 argv.push_back("-Winline");
97 if(binfo.fatal_warnings)
98 argv.push_back("-Werror");
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)
104 if(i->second.empty())
105 argv.push_back(format("-D%s", i->first));
107 argv.push_back(format("-D%s=%s", i->first, i->second));
110 argv.push_back("-ggdb");
114 argv.push_back("-Os");
116 argv.push_back(format("-O%d", binfo.optimize));
119 argv.push_back("-pthread");
120 if(comp.get_type()==Component::LIBRARY && architecture->get_system()!="windows")
121 argv.push_back("-fPIC");
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()));
127 const string &cpu = architecture->get_cpu();
129 argv.push_back("-march="+cpu);
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();
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());
139 return new ExternalTask(argv, work_dir);