www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 24555] New: macOS: Add shared druntime/Phobos

https://issues.dlang.org/show_bug.cgi?id=24555

          Issue ID: 24555
           Summary: macOS: Add shared druntime/Phobos (libphobos2.dylib)
           Product: D
           Version: D2
          Hardware: All
                OS: Mac OS X
            Status: NEW
          Severity: normal
          Priority: P1
         Component: druntime
          Assignee: nobody puremagic.com
          Reporter: kinke gmx.net

While DMD and its druntime support a shared libphobos2.so for most ELF
platforms, there's no Mach-O support for macOS yet. LDC does support shared
druntime/Phobos on macOS/iOS, and upstreaming shouldn't be very hard.

The main adaptations are required in druntime's `rt.sections_elf_shared`
module, extending it to work with Mach-O binaries too. LDC's version is here:
https://github.com/ldc-developers/ldc/blob/master/runtime/druntime/src/rt/sections_elf_shared.d.
In LDC, we've already extended it to Windows too, so the file/diff is
unfortunately pretty messy, and the whole module could very much use a
refactoring (and a better name!!!). LDC uses `rt.sections_elf_shared` for all
major platforms, for static *and* shared druntime; all the other upstream
rt.sections* implementations are unused and could probably be dropped upstream
too in time.

Then each binary needs to register itself with druntime, calling
`_d_dso_registry`. DMD does this by emitting a `d_dso_init` function into every
compiled object file, like LDC used to. We then switched to doing this in
druntime directly (no magic compiler-generated code anymore, which for Mach-O
would need an extra helper variable + function), via some special little
module:
https://github.com/ldc-developers/ldc/blob/master/runtime/druntime/src/rt/dso.d.
If adopting that approach, make sure to read the comments to understand what
needs to be done, like bundling that precompiled object file with the compiler
installation packages, and linking it automatically into every binary linked
against *shared* druntime. And note that DMD would then need to adopt some
` hidden` UDA too; `dso.d` absolutely needs that functionality (of making
symbols binary-internal only).

I'd propose adopting LDC's `-link-defaultlib-shared` too, instead of DMD's
platform-specific `-defaultlib={libphobos2.so,libphobos2.dylib,phobos2.dll}`.
Linking in the bundled `dso.o` would become easier then too.

After that, adapting variable `SHARED` in druntime's Makefile (setting it to 1
on macOS) might be enough.

--
May 17