Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

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 11, 2013
1 parent f76bc34 commit 5373575
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 42 deletions.
1 change: 1 addition & 0 deletions mak/MANIFEST
Expand Up @@ -171,6 +171,7 @@ MANIFEST=\
src\rt\deh2.d \
src\rt\dmain2.d \
src\rt\dylib_fixes.c \
src\rt\entrypoint.d \
src\rt\image.d \
src\rt\invariant.d \
src\rt\lifetime.d \
Expand Down
1 change: 1 addition & 0 deletions mak/SRCS
Expand Up @@ -84,6 +84,7 @@ SRCS=\
src\rt\deh.d \
src\rt\deh2.d \
src\rt\dmain2.d \
src\rt\entrypoint.d \
src\rt\invariant.d \
src\rt\lifetime.d \
src\rt\llmath.d \
Expand Down
56 changes: 14 additions & 42 deletions src/rt/dmain2.d
@@ -1,5 +1,5 @@
/**
* Contains main program entry point and support routines.
* Contains druntime startup and shutdown routines.
*
* Copyright: Copyright Digital Mars 2000 - 2013.
* License: Distributed under the
Expand Down Expand Up @@ -60,6 +60,7 @@ extern (C) void rt_moduleTlsCtor();
extern (C) void rt_moduleDtor();
extern (C) void rt_moduleTlsDtor();
extern (C) void thread_joinAll();
extern (C) bool runModuleUnitTests();

version (OSX)
{
Expand Down Expand Up @@ -352,52 +353,13 @@ extern (C) CArgs rt_cArgs()
return _cArgs;
}

/***********************************
* The D main() function supplied by the user's program
*
* It always has `_Dmain` symbol name and uses C calling convention.
* But DMD frontend returns its type as `extern(D)` because of Issue @@@9028@@@.
* As we need to deal with actual calling convention we have to mark it
* as `extern(C)` and use its symbol name.
*/
extern(C) int _Dmain(char[][] args);
alias extern(C) int function(char[][] args) MainFunc;

/***********************************
* Substitutes for the C main() function.
* Just calls into d_run_main with the default main function.
* Applications are free to implement their own
* main function and call the _d_run_main function
* themselves with any main function.
*/
extern (C) int main(int argc, char **argv)
{
auto result = _d_run_main(argc, argv, &_Dmain);
// Issue 10344: flush stdout and return nonzero on failure
if (.fflush(.stdout) != 0)
{
.fprintf(.stderr, "Failed to flush stdout: %s\n", .strerror(.errno));
if (result == 0)
{
result = EXIT_FAILURE;
}
}
return result;
}

version (Solaris) extern (C) int _main(int argc, char** argv)
{
// This is apparently needed on Solaris because the
// C tool chain seems to expect the main function
// to be called _main. It needs both not just one!
return main(argc, argv);
}

/***********************************
* Run the given main function.
* Its purpose is to wrap the D main()
* function and catch any unhandled exceptions.
*/
private alias extern(C) int function(char[][] args) MainFunc;

extern (C) int _d_run_main(int argc, char **argv, MainFunc mainFunc)
{
// Remember the original C argc/argv
Expand Down Expand Up @@ -659,5 +621,15 @@ extern (C) int _d_run_main(int argc, char **argv, MainFunc mainFunc)
_STD_critical_term();
_STD_monitor_staticdtor();

// Issue 10344: flush stdout and return nonzero on failure
if (.fflush(.stdout) != 0)
{
.fprintf(.stderr, "Failed to flush stdout: %s\n", .strerror(.errno));
if (result == 0)
{
result = EXIT_FAILURE;
}
}

return result;
}
49 changes: 49 additions & 0 deletions src/rt/entrypoint.d
@@ -0,0 +1,49 @@
/**
* Contains druntime entry point for console programs.
*
* Copyright: Copyright Digital Mars 2000 - 2013.
* License: Distributed under the
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
* (See accompanying file LICENSE)
* Authors: Walter Bright, Sean Kelly
* Source: $(DRUNTIMESRC src/rt/_entrypoint.d)
*/

module rt.entrypoint;

alias extern(C) int function(char[][] args) MainFunc;

extern (C) int _d_run_main(int argc, char **argv, MainFunc mainFunc);


/***********************************
* The D main() function supplied by the user's program
*
* It always has `_Dmain` symbol name and uses C calling convention.
* But DMD frontend returns its type as `extern(D)` because of Issue @@@9028@@@.
* As we need to deal with actual calling convention we have to mark it
* as `extern(C)` and use its symbol name.
*/
extern(C) int _Dmain(char[][] args);

/***********************************
* Substitutes for the C main() function.
* Just calls into d_run_main with the default main function.
* Applications are free to implement their own
* main function and call the _d_run_main function
* themselves with any main function.
*/
extern (C) int main(int argc, char **argv)
{
return _d_run_main(argc, argv, &_Dmain);
}

version (Solaris) extern (C) int _main(int argc, char** argv)
{
// This is apparently needed on Solaris because the
// C tool chain seems to expect the main function
// to be called _main. It needs both not just one!
return main(argc, argv);
}


0 comments on commit 5373575

Please sign in to comment.