ultimatepp/uppsrc/ide/Builders/GccBuilder.icpp
cxl b10efedc2f ide: Double link issue resolved #897
git-svn-id: svn://ultimatepp.org/upp/trunk@7844 f0d560ea-af0d-0410-9eb7-867de7ffcac7
2014-10-30 20:31:48 +00:00

538 lines
15 KiB
Text

#include "Builders.h"
void GccBuilder::AddFlags(Index<String>& cfg)
{
}
String GccBuilder::CompilerName() const
{
if(!IsNull(compiler)) return compiler;
return HasFlag("GCC_ARM") ? "arm-wince-pe-c++" : "c++";
}
String GccBuilder::CmdLine(const String& package, const Package& pkg)
{
String cc = CompilerName();
cc << " -c ";
cc << IncludesDefinesTargetTime(package, pkg);
if(HasFlag("GCC32"))
cc << " -m32";
return cc;
}
void GccBuilder::BinaryToObject(String objfile, CParser& binscript, String basedir,
const String& package, const Package& pkg)
{
String fo = BrcToC(binscript, basedir);
String tmpfile = ForceExt(objfile, ".c");
SaveFile(tmpfile, fo);
String cc = CmdLine(package, pkg);
cc << " -c -o " << GetHostPathQ(objfile) << " -x c " << GetHostPathQ(tmpfile);
int slot = AllocSlot();
if(slot < 0 || !Run(cc, slot, objfile, 1))
throw Exc(NFormat("Error compiling binary object '%s'.", objfile));
}
bool GccBuilder::BuildPackage(const String& package, Vector<String>& linkfile, Vector<String>& immfile,
String& linkoptions, const Vector<String>& all_uses, const Vector<String>& all_libraries,
int opt)
{
int i;
String packagepath = PackagePath(package);
Package pkg;
pkg.Load(packagepath);
String packagedir = GetFileFolder(packagepath);
ChDir(packagedir);
PutVerbose("cd " + packagedir);
IdeConsoleBeginGroup(package);
Vector<String> obj;
bool is_shared = HasFlag("SO");
String shared_ext = (HasFlag("WIN32") ? ".dll" : ".so");
String cc = CmdLine(package, pkg);
if(HasFlag("WIN32") && HasFlag("MT"))
cc << " -mthreads";
if(HasFlag("DEBUG_MINIMAL"))
cc << (HasFlag("WIN32") ? " -g1" : " -ggdb -g1");
if(HasFlag("DEBUG_FULL"))
cc << (HasFlag("WIN32") ? " -g2" : " -ggdb -g2");
String fuse_cxa_atexit;
if(is_shared /*&& !HasFlag("MAIN")*/) {
cc << " -shared -fPIC";
fuse_cxa_atexit = " -fuse-cxa-atexit";
}
if(!HasFlag("SHARED") && !is_shared)
cc << " -static ";
// else if(!HasFlag("WIN32")) // TRC 05/03/08: dynamic fPIC doesn't seem to work in MinGW
// cc << " -dynamic -fPIC "; // TRC 05/03/30: dynamic fPIC doesn't seem to work in GCC either :-)
cc << ' ' << Gather(pkg.option, config.GetKeys());
cc << " -fexceptions ";
if (HasFlag("OSX11")) {
if (HasFlag("POWERPC"))
cc << " -arch ppc";
if (HasFlag("X86"))
cc << " -arch i386";
}
if(HasFlag("SSE2")) {
cc << " -msse2";
if(!HasFlag("CLANG"))
cc << " -mfpmath=sse";
}
String cc_speed = cc;
bool release = false;
if(HasFlag("DEBUG"))
cc << " -D_DEBUG " << debug_options;
else {
release = true;
cc << ' ' << release_size_options;
cc_speed << ' ' << release_options;
if(opt == R_SPEED || pkg.optimize_speed)
cc = cc_speed;
}
Vector<String> sfile, isfile;
Vector<String> soptions, isoptions;
Vector<bool> optimize, ioptimize;
bool error = false;
for(i = 0; i < pkg.GetCount(); i++) {
if(!IdeIsBuilding())
return false;
if(!pkg[i].separator) {
String gop = Gather(pkg[i].option, config.GetKeys());
Vector<String> srcfile = CustomStep(pkg[i], package, error);
if(srcfile.GetCount() == 0)
error = true;
for(int j = 0; j < srcfile.GetCount(); j++) {
String fn = srcfile[j];
String ext = GetSrcType(fn);
if(ext == ".c" || ext == ".cpp" || ext == ".cc" || ext == ".cxx" || ext == ".m" || ext == ".mm"
|| ext == ".s" || ext == ".S"
|| ext == ".brc" || (ext == ".rc" && HasFlag("WIN32"))) {
sfile.Add(fn);
soptions.Add(gop);
optimize.Add(release && pkg[i].optimize_speed && opt == R_OPTIMAL);
}
else
if(ext == ".icpp") {
isfile.Add(fn);
isoptions.Add(gop);
ioptimize.Add(release && pkg[i].optimize_speed && opt == R_OPTIMAL);
}
else
if(ext == ".o")
obj.Add(fn);
else
if(ext == ".a" || ext == ".so")
linkfile.Add(fn);
}
}
}
if(HasFlag("BLITZ")) {
Blitz b = BlitzStep(sfile, soptions, obj, immfile, ".o", optimize);
if(b.build) {
PutConsole("BLITZ:" + b.info);
int slot = AllocSlot();
if(slot < 0 || !Run(String().Cat() << Join(cc, cpp_options) << ' '
<< GetHostPathQ(b.path) << " -o " << GetHostPathQ(b.object), slot, GetHostPath(b.object), b.count))
error = true;
}
}
int first_ifile = sfile.GetCount();
sfile.AppendPick(pick(isfile));
soptions.AppendPick(pick(isoptions));
optimize.AppendPick(pick(ioptimize));
int ccount = 0;
for(i = 0; i < sfile.GetCount(); i++) {
if(!IdeIsBuilding())
return false;
String fn = sfile[i];
String ext = ToLower(GetFileExt(fn));
bool rc = (ext == ".rc");
bool brc = (ext == ".brc");
bool init = (i >= first_ifile);
String objfile = CatAnyPath(outdir, GetFileTitle(fn) + (rc ? "$rc.o" : brc ? "$brc.o" : ".o"));
if(HdependFileTime(fn) > GetFileTime(GetHostPath(objfile))) {
PutConsole(GetFileName(fn));
int time = GetTickCount();
bool execerr = false;
if(rc) {
String exec;
exec << "windres -i " << GetHostPathQ(fn)
<< " -o " << GetHostPathQ(objfile) << IncludesShort(" --include-dir=", package, pkg)
<< DefinesTargetTime(" -D", package, pkg) + (HasFlag("DEBUG")?" -D_DEBUG":"");
PutVerbose(exec);
int slot = AllocSlot();
execerr = (slot < 0 || !Run(exec, slot, GetHostPath(objfile), 1));
}
else if(brc) {
try {
// String hfn = GetHostPath(fn);
String brcdata = LoadFile(fn);
if(brcdata.IsVoid())
throw Exc(NFormat("error reading file '%s'", fn));
CParser parser(brcdata, fn);
BinaryToObject(GetHostPath(objfile), parser, GetFileDirectory(fn), package, pkg);
}
catch(Exc e) {
PutConsole(e);
execerr = true;
}
}
else {
String exec = optimize[i] ? cc_speed : cc;
if(ext == ".c")
exec << Join(" -x c", c_options) << ' ';
else if(ext == ".s" || ext == ".S")
exec << " -x assembler-with-cpp ";
else if (ext == ".m" || ext == ".mm")
exec << fuse_cxa_atexit << " -x objective-c++ ";
else
exec << fuse_cxa_atexit << Join(" -x c++", cpp_options) << ' ';
exec << GetHostPathQ(fn) << " " << soptions[i] << " -o " << GetHostPathQ(objfile);
PutVerbose(exec);
int slot = AllocSlot();
execerr = (slot < 0 || !Run(exec, slot, GetHostPath(objfile), 1));
}
if(execerr)
DeleteFile(objfile);
error |= execerr;
PutVerbose("compiled in " + GetPrintTime(time));
ccount++;
}
immfile.Add(objfile);
if(init)
linkfile.Add(objfile);
else
obj.Add(objfile);
}
if(error) {
// if(ccount)
// PutCompileTime(time, ccount);
IdeConsoleEndGroup();
return false;
}
linkoptions << Gather(pkg.link, config.GetKeys());
if(linkoptions.GetCount())
linkoptions << ' ';
Vector<String> libs = Split(Gather(pkg.library, config.GetKeys()), ' ');
linkfile.Append(libs);
int libtime = GetTickCount();
if(!HasFlag("MAIN")) {
if(HasFlag("BLITZ") && !HasFlag("SO") || HasFlag("NOLIB")) {
linkfile.Append(obj); // Simply link everything as .o files...
IdeConsoleEndGroup();
// if(ccount)
// PutCompileTime(time, ccount);
return true;
}
IdeConsoleEndGroup();
if(!Wait())
return false;
String product;
if(is_shared)
product = GetSharedLibPath(package);
else
product = CatAnyPath(outdir, GetAnyFileName(package) + ".a");
String hproduct = GetHostPath(product);
Time producttime = GetFileTime(hproduct);
// LOG("hproduct = " << hproduct << ", time = " << producttime);
if(obj.GetCount()) {
linkfile.Add(GetHostPath(product));
immfile.Add(GetHostPath(product));
}
for(int i = 0; i < obj.GetCount(); i++)
if(GetFileTime(obj[i]) > producttime) {
String lib;
if(is_shared) {
lib = CompilerName();
lib << " -shared -fPIC -fuse-cxa-atexit";
if(!HasFlag("SHARED") && !is_shared)
lib << " -static";
// else if(!HasFlag("WIN32")) // TRC 05/03/08: dynamic fPIC causes trouble in MinGW
// lib << " -dynamic -fPIC"; // TRC 05/03/30: dynamic fPIC doesn't seem to work in GCC either :-)
if(HasFlag("GCC32"))
lib << " -m32";
Point p = ExtractVersion();
if(!IsNull(p.x) && HasFlag("WIN32")) {
lib << " -Xlinker --major-image-version -Xlinker " << p.x;
if(!IsNull(p.y))
lib << " -Xlinker --minor-image-version -Xlinker " << p.y;
}
lib << ' ' << Gather(pkg.link, config.GetKeys());
lib << " -o ";
}
else
lib = "ar -sr ";
lib << GetHostPathQ(product);
String llib;
for(int i = 0; i < obj.GetCount(); i++)
llib << ' ' << GetHostPathQ(obj[i]);
PutConsole("Creating library...");
DeleteFile(hproduct);
if(is_shared) {
for(int i = 0; i < libpath.GetCount(); i++)
llib << " -L" << GetHostPathQ(libpath[i]);
for(int i = 0; i < all_uses.GetCount(); i++)
llib << ' ' << GetHostPathQ(GetSharedLibPath(all_uses[i]));
for(int i = 0; i < all_libraries.GetCount(); i++)
llib << " -l" << GetHostPathQ(all_libraries[i]);
if(HasFlag("POSIX"))
llib << " -Wl,-soname," << GetSoname(product);
}
String tmpFileName;
if(lib.GetCount() + llib.GetCount() >= 8192)
{
tmpFileName = GetTempFileName();
// we can't simply put all data on a single line
// as it has a limit of around 130000 chars too, so we split
// in multiple lines
FileOut f(tmpFileName);
while(llib != "")
{
int found = 0;
bool quotes = false;
int lim = min(8192, llib.GetCount());
for(int i = 0; i < lim; i++)
{
char c = llib[i];
if(isspace(c) && !quotes)
found = i;
else if(c == '"')
quotes = !quotes;
}
if(!found)
found = llib.GetCount();
// replace all '\' with '/'
llib = UnixPath(llib);
f.PutLine(llib.Left(found));
llib.Remove(0, found);
}
f.Close();
lib << " @" << tmpFileName;
}
else
lib << llib;
int res = Execute(lib);
if(tmpFileName != "")
FileDelete(tmpFileName);
#ifdef PLATFORM_POSIX
String folder, libF, soF, linkF;
if(HasFlag("SO"))
{
folder = GetFileFolder(hproduct);
libF = GetFileName(hproduct);
soF = AppendFileName(folder, GetSoname(hproduct));
linkF = AppendFileName(folder, GetSoLinkName(hproduct));
}
#endif
if(res) {
DeleteFile(hproduct);
#ifdef PLATFORM_POSIX
if(HasFlag("SO")) {
DeleteFile(libF);
DeleteFile(linkF);
}
#endif
return false;
}
#ifdef PLATFORM_POSIX
if(HasFlag("SO"))
{
int r;
r = symlink(libF, soF);
r = symlink(libF, linkF);
(void)r;
}
#endif
PutConsole(String().Cat() << hproduct << " (" << GetFileInfo(hproduct).length
<< " B) created in " << GetPrintTime(libtime));
break;
}
return true;
}
IdeConsoleEndGroup();
obj.Append(linkfile);
linkfile = pick(obj);
return true;
}
bool GccBuilder::Link(const Vector<String>& linkfile, const String& linkoptions, bool createmap)
{
if(!Wait())
return false;
PutLinking();
int time = GetTickCount();
for(int i = 0; i < linkfile.GetCount(); i++)
if(GetFileTime(linkfile[i]) > targettime) {
Vector<String> lib;
String lnk = CompilerName();
if(HasFlag("GCC32"))
lnk << " -m32";
if(HasFlag("DLL"))
lnk << " -shared";
if(!HasFlag("SHARED") && !HasFlag("SO"))
lnk << " -static";
// else if(!HasFlag("WIN32")) // TRC 05/03/08: see above
// lnk << " -dynamic -fPIC"; // TRC 05/03/30: dynamic fPIC doesn't seem to work in GCC either :-)
if(HasFlag("WINCE"))
lnk << " -mwindowsce";
else if(HasFlag("WIN32")) {
lnk << " -mwindows";
if(HasFlag("MT"))
lnk << " -mthreads";
if(!HasFlag("GUI"))
lnk << " -mconsole";
}
lnk << " -o " << GetHostPathQ(target);
if(createmap)
lnk << " -Wl,-Map," << GetHostPathQ(GetFileDirectory(target) + GetFileTitle(target) + ".map");
if(HasFlag("DEBUG") || HasFlag("DEBUG_MINIMAL") || HasFlag("DEBUG_FULL"))
lnk << " -ggdb";
else
lnk << (!HasFlag("OSX11") ? " -Wl,-s" : "");
for(i = 0; i < libpath.GetCount(); i++)
lnk << " -L" << GetHostPathQ(libpath[i]);
// lnk << " -Wl,--gc-sections,-O,2 ";
if(!HasFlag("OSX11"))
lnk << " -Wl,-O,2 ";
lnk << linkoptions;
if (HasFlag("OSX11")) {
if (HasFlag("POWERPC"))
lnk << " -arch ppc";
if (HasFlag("X86"))
lnk << " -arch i386";
}
String lfilename;
if(HasFlag("OBJC")) {
String lfilename;
String linklist;
for(i = 0; i < linkfile.GetCount(); i++)
if(ToLower(GetFileExt(linkfile[i])) == ".o" || ToLower(GetFileExt(linkfile[i])) == ".a")
linklist << GetHostPath(linkfile[i]) << '\n';
String linklistM = "Producing link file list ...\n";
String odir = GetFileDirectory(linkfile[0]);
lfilename << GetHostPath(GetFileFolder(linkfile[0])) << ".LinkFileList";
linklistM << lfilename;
UPP::SaveFile(lfilename, linklist);
lnk << " -L" << GetHostPathQ(odir)
<< " -F" << GetHostPathQ(odir)
<< " -filelist " << lfilename << " ";
PutConsole( linklistM );
}
else
for(i = 0; i < linkfile.GetCount(); i++) {
if(ToLower(GetFileExt(linkfile[i])) == ".o")
lnk << ' ' << GetHostPathQ(linkfile[i]);
else
lib.Add(linkfile[i]);
}
if(!HasFlag("SOLARIS") && !HasFlag("OSX11") && !HasFlag("OBJC"))
lnk << " -Wl,--start-group ";
for(int pass = 0; pass < 2; pass++) {
if(pass == 1 && !HasFlag("SOLARIS") && !HasFlag("OSX11"))
lnk << " -Wl,--end-group";
for(i = 0; i < lib.GetCount(); i++) {
String ln = lib[i];
String ext = ToLower(GetFileExt(ln));
// unix shared libs shall have version number AFTER .so (sic)
// so we shall find the true extension....
if(HasFlag("POSIX") && ext != ".so")
{
const char *c = ln.Last();
while(--c >= ~ln)
if(!IsDigit(*c) && *c != '.')
break;
int pos = int(c - ~ln - 2);
if(pos >= 0 && ToLower(ln.Mid(pos, 3)) == ".so")
ext = ".so";
}
if(pass == 0) {
if(ext == ".a")
lnk << ' ' << GetHostPathQ(FindInDirs(libpath, lib[i]));
}
else
if(ext != ".a") {
if(ext == ".so" || ext == ".dll" || ext == ".lib")
lnk << ' ' << GetHostPathQ(FindInDirs(libpath, lib[i]));
else
lnk << " -l" << ln;
}
}
}
PutConsole("Linking...");
bool error = false;
CustomStep(".pre-link", Null, error);
if(!error && Execute(lnk) == 0) {
CustomStep(".post-link", Null, error);
PutConsole(String().Cat() << GetHostPath(target) << " (" << GetFileInfo(target).length
<< " B) linked in " << GetPrintTime(time));
return !error;
}
else {
DeleteFile(target);
return false;
}
}
PutConsole(String().Cat() << GetHostPath(target) << " (" << GetFileInfo(target).length
<< " B) is up to date.");
return true;
}
bool GccBuilder::Preprocess(const String& package, const String& file, const String& target, bool asmout)
{
Package pkg;
String packagepath = PackagePath(package);
pkg.Load(packagepath);
String packagedir = GetFileFolder(packagepath);
ChDir(packagedir);
PutVerbose("cd " + packagedir);
String cmd = CmdLine(package, pkg);
cmd << " " << Gather(pkg.option, config.GetKeys());
cmd << " -o " << target;
cmd << (asmout ? " -S " : " -E ") << GetHostPathQ(file);
return Execute(cmd);
}
Builder *CreateGccBuilder()
{
return new GccBuilder;
}
INITBLOCK
{
RegisterBuilder("GCC", CreateGccBuilder);
RegisterBuilder("CLANG", CreateGccBuilder);
RegisterBuilder("GCC32", CreateGccBuilder);
RegisterBuilder("GCC_ARM", CreateGccBuilder);
}