December 01, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=7044

           Summary: [PATCH] Add -sL swith to pass linker flags at the end
                    of the linker command
           Product: D
           Version: D1 & D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: llucax@gmail.com


--- Comment #0 from Leandro Lucarella <llucax@gmail.com> 2011-12-01 07:52:28 PST ---
There are some cases where linker flags order do matter (for example for linking libraries). In those cases DMD provide no mechanism for altering the order. This patches tries to minimize this problem by adding an option (-sL) to add flags at the end of the linker command line. This is specially useful to include libraries after the standard library passed with -debuglib or -defaultlib.

Patch available as a pull request: https://github.com/D-Programming-Language/dmd/pull/497

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 02, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7044



--- Comment #1 from Leandro Lucarella <leandro.lucarella@sociomantic.com> 2012-02-02 04:18:56 PST ---
By the way, I don't find the solution elegant at all, but it's the only solution I could think of. Maybe it would be better to use the same order of the command line switches when passing them to the linker, even for the -defaultlib and -debuglib.

Examples:

dmd -L-L/opt/lib -defaultlib=blah -L-lfoo -> gcc -L/opt/lib -lblah -lfoo dmd -L-lbar -defaultlib=blah -> gcc -lbar -lblah

The problem is what to do then with the switches that are automatically added by DMD. Maybe those should be just moved to dmd.conf (except for -Xlinker of course), because I think they are not really needed by the the generated code but by the runtime, which can be changed via -defaultlib.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 03, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7044


Leandro Lucarella <leandro.lucarella@sociomantic.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[PATCH] Add -sL swith to    |Missing a way to control
                   |pass linker flags at the    |the order of arguments
                   |end of the linker command   |passed to the linker makes
                   |                            |impossible to link some
                   |                            |programs
         OS/Version|All                         |Linux


--- Comment #2 from Leandro Lucarella <leandro.lucarella@sociomantic.com> 2012-07-03 16:48:26 PDT ---
Changed the title to describe the problem more generally instead of describing a possible solution.

I think the way to go is the one described in comment 1. I'm willing to implement it if there is consensus on moving all the linker option out of the compiler to the configuration file.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 21, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7044


Leandro Lucarella <leandro.lucarella@sociomantic.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|pull                        |


--- Comment #3 from Leandro Lucarella <leandro.lucarella@sociomantic.com> 2012-10-21 11:05:35 PDT ---
See the (deprecated) pull request for more discussion on the topic.

Here I propose a solution that's simple, backwards compatible, and works the same way DMD already does in terms of environment variables and switches.

We just need to extend -defaultlib and -debuglib to accept not only the name of the standard library but any other linker flag needed. A simple heuristic should be enough, like if the argument starts with "-" or "/" it should be considered raw arguments to pass to the linker instead of just a library name.

This way you could use just -defaultlib=phobos2 or a more complex -defaultlib="-lphobos2 -lrt -ldl". When using the (deprecated) old way, we can still add the needed hardcoded linker flags until at some point it can be not supported anymore and just pass it as raw linker arguments wihout any heuristic to detect the old behaviour.

This way you can control which runtime to use either in the command line, in the dmd.conf or in the DFLAGS enviroment variable, with the usual overriding rules (you can specify a system default in the dmd.conf file, override it in your user's setup or Makefile using the DFLAGS environment variable and even override that using regular command line arguments when invoking dmd.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 22, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7044


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com


--- Comment #4 from Walter Bright <bugzilla@digitalmars.com> 2012-10-21 22:23:24 PDT ---
The problem is the order of -defaultlib and -L is fixed as:

1. -L flags
2. libraries on the command line
3. libraries specified by pragma(lib)
4. standard libraries (also -debuglib, -defaultlib libraries)

I suggest the straightforward approach of the order of (1), (2), and (4) be
determined by the order in which they appear on the command line.

That leaves (3), and I suggest that be inserted immediately before (4).

This may break existing build systems, though.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 22, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7044



--- Comment #5 from Leandro Lucarella <leandro.lucarella@sociomantic.com> 2012-10-22 02:10:58 PDT ---
(In reply to comment #4)
> The problem is the order of -defaultlib and -L is fixed as:
> 
> 1. -L flags
> 2. libraries on the command line
> 3. libraries specified by pragma(lib)
> 4. standard libraries (also -debuglib, -defaultlib libraries)
> 
> I suggest the straightforward approach of the order of (1), (2), and (4) be
> determined by the order in which they appear on the command line.

This is good enough to change the runtime in the dmd.conf file but is not ideal for overriding a default later, because the -L flags are not replaced as the -defaultlib option is, just appended.

For example, I have a default runtime which needs to link to librt, so I have
this in dmd.conf:
DFLAGS=-defaultlib=runtime1 -L-lrt

Then I want to compile some program using another runtime that depends on libz,
if I write:
dmd -defaultlib=runtime2 -Llz

The best we can do is to get these link options: -lrt -lruntime2 -lz
Which includes -lrt, which is not needed by runtime2 (and it might not even be
available in the environment runtime2 is compiled).

I think we need a flag to override the default runtime library *and* all its dependencies in one go.

> That leaves (3), and I suggest that be inserted immediately before (4).

I guess it makes sense, if you want complete control on the ordering then you have to switch to using -L instead of pragma(lib).

> This may break existing build systems, though.

This is one good point about the solution I proposed before, is completely backwards compatible.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 08, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=7044



--- Comment #6 from Leandro Lucarella <leandro.lucarella@sociomantic.com> 2013-01-08 07:44:36 PST ---
Full (slightly updated) proposal:

* Change -defaultlib and -debuglib to accept an arbitrary number of linker
  flags. Example: -defaultlib="-lphobos2 -lrt -ldl".

* Remove all linker flags from DMD that are not strictly needed by the compiler
  generated code (i.e. all the linker flags needed only by the runtime).

* Update the sample configuration file to set -defaultlib and -debuglib
  appropriately to make the runtime work.

* Change the order in which options are passed to the linker like this
  (from left to right):

  * Options passed with -L through the command line
  * Options passed with -L through environment variables
  * Libraries specified by pragma(lib)
  * Options passed with -defaultlib/-debuglib (command line option overrides
    environment variables and environment variables override config files)
  * Options needed for the compiler generated code to work (if any)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 08, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=7044



--- Comment #7 from Walter Bright <bugzilla@digitalmars.com> 2013-01-08 12:44:18 PST ---
This is getting complex enough that I have to go back to basics and point out that all dmd is doing is building a command line that is sent to gcc to do the actual work.

So I suggest, why not, when your needs for the command line are this complex, simply use gcc to do the link step instead of passing through dmd as an intermediary?

To see what command dmd formulates and passes to gcc, compile with -verbose.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 09, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=7044



--- Comment #8 from Leandro Lucarella <leandro.lucarella@sociomantic.com> 2013-01-09 04:11:47 PST ---
(In reply to comment #7)
> This is getting complex enough that I have to go back to basics and point out that all dmd is doing is building a command line that is sent to gcc to do the actual work.
> 
> So I suggest, why not, when your needs for the command line are this complex, simply use gcc to do the link step instead of passing through dmd as an intermediary?
> 
> To see what command dmd formulates and passes to gcc, compile with -verbose.

Because is not an option to ask to do that to all your users when you're providing an alternative runtime. The -defaultlib as it is, is useless.

And I don't see the complexity in this, is just changing the -defaultlib flag really, and making the compiler really agnostic about the implementation details of the runtime.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 09, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=7044



--- Comment #9 from Leandro Lucarella <leandro.lucarella@sociomantic.com> 2013-01-09 04:27:55 PST ---
BTW, as things are now, you can't compile a program using curl wrapper in phobos, so this doesn't even apply only to the runtime: http://stackoverflow.com/questions/10095150/std-net-curl-linker-errors-in-linux

To fix this, you'll have to change the compiler now, while if we implement this proposal, it will enough to update the sample config file.

curl.d
import std.net.curl, std.stdio;
void main()
{
    writeln(get("dlang.org"));
}

dmd -L-lcurl curl.d
/home/luca/dmd/dmd2-git/src/../../phobos/generated/linux/release/64/libphobos2.a(curl.o):
In function `_D3std3net4curl4Curl19_sharedStaticCtor28FZv':
std/net/curl.d:(.text._D3std3net4curl4Curl19_sharedStaticCtor28FZv+0xf):
undefined reference to `curl_global_init'
(... more errors ...)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
« First   ‹ Prev
1 2 3
Top | Discussion index | About this forum | D home