Skip to content

Commit

Permalink
fix Issue 10711 - shared phobos library should not depend on _Dmain
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Aug 15, 2013
1 parent 59c0ebe commit 0082669
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
50 changes: 49 additions & 1 deletion src/func.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// Copyright (c) 1999-2013 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
Expand All @@ -25,8 +25,10 @@
#include "template.h"
#include "hdrgen.h"
#include "target.h"
#include "parse.h"

void functionToCBuffer2(TypeFunction *t, OutBuffer *buf, HdrGenState *hgs, int mod, const char *kind);
void genCmain(Scope *sc);

/********************************* FuncDeclaration ****************************/

Expand Down Expand Up @@ -868,6 +870,9 @@ void FuncDeclaration::semantic(Scope *sc)
}
}

if (fbody && isMain() && sc->module == sc->module->importedFrom)
genCmain(sc);

return;
}

Expand Down Expand Up @@ -3442,6 +3447,49 @@ FuncDeclaration *FuncDeclaration::genCfunc(Parameters *args, Type *treturn, Iden
return fd;
}

/************************************
* Generate C main() in response to seeing D main().
* This used to be in druntime, but contained a reference to _Dmain
* which didn't work when druntime was made into a dll and was linked
* to a program, such as a C++ program, that didn't have a _Dmain.
*/

void genCmain(Scope *sc)
{
/* The D code to be generated is provided as D source code in the form of a string.
* Note that Solaris, for unknown reasons, requires both a main() and an _main()
*/
static utf8_t code[] = "extern(C) {\n\
int _d_run_main(int argc, char **argv, void* mainFunc);\n\
int main(int argc, char **argv) { return _d_run_main(argc, argv, &main); }\n\
version (Solaris) int _main(int argc, char** argv) { return main(argc, argv); }\n\
}\n\
";

Parser p(sc->module, code, sizeof(code) / sizeof(code[0]), 0);
p.loc = Loc();
p.nextToken();
Dsymbols *decl = p.parseDeclDefs(0);
assert(p.token.value == TOKeof);

sc = sc->push();
sc->parent = sc->module->importedFrom;
sc->stc = 0;

for (size_t i = 0; i < decl->dim; ++i)
{ Dsymbol *s = (*decl)[i];
sc->module->importedFrom->members->push(s);
s->addMember(sc, sc->module->importedFrom, 1);
}

for (size_t i = 0; i < decl->dim; ++i)
{ Dsymbol *s = (*decl)[i];
s->semantic(sc);
}

sc->pop();
}

const char *FuncDeclaration::kind()
{
return "function";
Expand Down
1 change: 1 addition & 0 deletions test/runnable/extra-files/hello-profile.d.trace.def
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

FUNCTIONS
main
_D5hello8showargsFAAyaZv

0 comments on commit 0082669

Please sign in to comment.