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_native())
21 system_path.push_back("/usr/include");
23 system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/include");
26 Target *GnuCompiler::create_target(const list<Target *> &sources, const string &)
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);
36 string GnuCompiler::create_build_signature(const BuildInfo &binfo) const
38 string result = FS::basename(executable->get_path());
39 if(!architecture->get_cpu().empty())
42 result += architecture->get_cpu();
50 result += (binfo.optimize>0 ? '0'+binfo.optimize : 's');
55 void GnuCompiler::do_prepare()
57 set_executable(command, true);
60 Task *GnuCompiler::run(const Target &target) const
62 const ObjectFile &object = dynamic_cast<const ObjectFile &>(target);
63 const Component &comp = *object.get_component();
65 ExternalTask::Arguments argv;
66 argv.push_back(executable->get_path().str());
69 const BuildInfo &binfo = comp.get_build_info_for_path(object.get_source().get_path());
70 if(binfo.warning_level>=1)
72 argv.push_back("-Wall");
73 if(binfo.warning_level>=2)
75 argv.push_back("-Wextra");
76 argv.push_back("-Wundef");
78 if(binfo.warning_level>=3)
80 argv.push_back("-pedantic");
81 argv.push_back("-Wno-long-long");
82 argv.push_back("-Wshadow");
85 argv.push_back("-Wc++-compat");
86 argv.push_back("-Wstrict-prototypes");
89 if(binfo.warning_level>=4)
91 // Some truly paranoid warnings
92 argv.push_back("-Wstrict-overflow=4");
93 argv.push_back("-Wfloat-equal");
94 argv.push_back("-Wconversion");
95 argv.push_back("-Wwrite-strings");
96 argv.push_back("-Winline");
98 if(binfo.fatal_warnings)
99 argv.push_back("-Werror");
101 for(BuildInfo::PathList::const_iterator i=binfo.local_incpath.begin(); i!=binfo.local_incpath.end(); ++i)
103 argv.push_back("-iquote");
104 argv.push_back(i->str());
106 for(BuildInfo::PathList::const_iterator i=binfo.incpath.begin(); i!=binfo.incpath.end(); ++i)
107 argv.push_back("-I"+i->str());
108 for(BuildInfo::DefineMap::const_iterator i=binfo.defines.begin(); i!=binfo.defines.end(); ++i)
110 if(i->second.empty())
111 argv.push_back(format("-D%s", i->first));
113 argv.push_back(format("-D%s=%s", i->first, i->second));
116 argv.push_back("-ggdb");
120 argv.push_back("-Os");
122 argv.push_back(format("-O%d", binfo.optimize));
125 argv.push_back("-pthread");
126 if((comp.get_type()==Component::LIBRARY || comp.get_type()==Component::MODULE) && architecture->get_system()!="windows")
127 argv.push_back("-fPIC");
129 const Architecture &native_arch = builder.get_native_arch();
130 if(architecture->get_bits()!=native_arch.get_bits())
131 argv.push_back(format("-m%d", architecture->get_bits()));
133 const string &cpu = architecture->get_cpu();
135 argv.push_back("-march="+cpu);
137 FS::Path obj_path = object.get_path();
138 FS::Path src_path = object.get_source().get_path();
139 FS::Path work_dir = comp.get_package().get_source_directory();
141 argv.push_back("-o");
142 argv.push_back(relative(obj_path, work_dir).str());
143 argv.push_back(relative(src_path, work_dir).str());
145 return new ExternalTask(argv, work_dir);