Thread overview
Cross-compiler targeting macOS
Apr 07, 2021
Jacob Carlborg
Apr 07, 2021
H. S. Teoh
Apr 07, 2021
Guillaume Piolat
Apr 08, 2021
Jacob Carlborg
Apr 08, 2021
H. S. Teoh
Apr 09, 2021
Jacob Carlborg
Apr 10, 2021
kinke
Apr 11, 2021
Jacob Carlborg
April 07, 2021

Docker LDC Darwin

I would like to announce a new project I'm working on: docker-ldc-darwin [1]. The project consists of a Dockerfile for building a Docker image which has all the necessary tools to cross-compile D applications targeting macOS x86-64.

Features

  • Uses Apple's ld64 linker so all features that are being used when linking on macOS should work. No reliance on custom linkers that need to be kept up to date with the system linker
  • Ships with the full macOS SDK (for macOS version 10.15.4)
  • No reliance on hacks like allowing undefined symbols when linking like Zig and Rust does
  • Minimal Docker image. Only the exact files that are required to run the compiler and linker are included. Not even a shell is included
  • The Docker image, all tools and resources are fully reproducible and automated. No manual steps involved of uploading to random cloud storage accounts

The following tools are included:

  • dub
  • ldc2
  • ldmd2
  • rdmd
  • ld64 (linker)
  • clang (C compiler, used for linking)

Building

Prerequisites

Building the Docker Image

Follow the steps below to build the Docker images by running the commands in
the terminal:

  1. Clone the git repository:
    git clone https://github.com/d-cross-compiler/docker-ldc-darwin && cd docker-ldc-darwin
    
  2. Build the Docker image:
    docker build -t ldc-x86_64-apple-macos .
    

Usage

By default, the ldc2 compiler will be invoked.

Compiling Hello World

Compile Hello World:

$ uname
Darwin
$ cat <<EOF > main.d
import std;

void main()
{
    writeln("Hello World");
}
EOF
$ docker run --rm -v "$(pwd):/work" ldc-x86_64-apple-macos main.d
$ ./main
Hello World

For more information and examples, see the readme [1].

[1] https://github.com/d-cross-compiler/docker-ldc-darwin

--
/Jacob Carlborg

April 07, 2021
On Wed, Apr 07, 2021 at 12:24:40PM +0000, Jacob Carlborg via Digitalmars-d-announce wrote:
> # Docker LDC Darwin
> 
> I would like to announce a new project I'm working on: docker-ldc-darwin [1]. The project consists of a Dockerfile for building a Docker image which has all the necessary tools to cross-compile D applications targeting macOS x86-64.
[...]

Thanks!!! This is what I've been looking for, for a long time!


T

-- 
Holding a grudge is like drinking poison and hoping the other person dies. -- seen on the 'Net
April 07, 2021

On Wednesday, 7 April 2021 at 12:24:40 UTC, Jacob Carlborg wrote:

>

Docker LDC Darwin

I would like to announce a new project I'm working on: docker-ldc-darwin [1]. The project consists of a Dockerfile for building a Docker image which has all the necessary tools to cross-compile D applications targeting macOS x86-64.

Dumb question maybe but: in what use cases should this be used?

April 08, 2021
On 2021-04-07 17:27, Guillaume Piolat wrote:

> Dumb question maybe but: in what use cases should this be used?

I don't know, ask H. S. Teoh :D.

I know some people have asked for it. I did it mostly because I knew how to do it and do it properly. I general I don't see the point to cross-compile (unless it's required, like mobile an embedded), because it seems like people want to use cross-compiling because they don't have the target system. But eventually you need to test the result and then you do need the target system to be able to run it. But perhaps if you target Windows you can then use Wine to run the executable. Seem to be something similar for macOS [1]. But if you can run the result using Wine you should be able to run the compiler using Wine as well. Perhaps it's less of a hassle to cross-compile, I don't know.

If you're targeting Linux on non-native architectures you can use qemu. Seems pretty easy if you have a statically linked binary and use qemu user emulation.

There's also free public CI services that target macOS, no need to cross-compile and it can run the code as well.

I did have a use case at my previous job. The production systems were running Linux but all developers were using macOS. We created a custom tool for the developers, which then needed to target macOS. It was a GUI application so Docker wasn't an option. We only had access to Linux CI runners so I used cross-compiling. It couldn't test the result, but at least it could build it and publish it. That's when I setup the first incarnation of this project [2]. In this new incarnation, I've fixed the main problem of the first incarnation: reproducibility.

[1] https://www.darlinghq.org
[2] https://github.com/jacob-carlborg/docker-ldc-darwin

-- 
/Jacob Carlborg
April 08, 2021
On Thu, Apr 08, 2021 at 10:23:27AM +0200, Jacob Carlborg via Digitalmars-d-announce wrote:
> On 2021-04-07 17:27, Guillaume Piolat wrote:
> 
> > Dumb question maybe but: in what use cases should this be used?
> 
> I don't know, ask H. S. Teoh :D.
> 
> I know some people have asked for it. I did it mostly because I knew how to do it and do it properly. I general I don't see the point to cross-compile (unless it's required, like mobile an embedded), because it seems like people want to use cross-compiling because they don't have the target system. But eventually you need to test the result and then you do need the target system to be able to run it.

That's the main reason for me, anyway, can't speak for others.  And yes,
ideally you'd want to own the target system as well so that you can test
it, but sometimes you just want to share a personal project with someone
running on say MacOS, and it doesn't seem to make sense to buy a Mac
just to be able to share that one program.  So cross-compiling would
be a much better solution.


> But perhaps if you target Windows you can then use Wine to run the executable.  Seem to be something similar for macOS [1]. But if you can run the result using Wine you should be able to run the compiler using Wine as well. Perhaps it's less of a hassle to cross-compile, I don't know.

IME, test results from Wine are not reliable. It's a good first pass to make sure you didn't do anything obviously broken, but just because something runs well in Wine does not guarantee it will run well on an actual Windows box.

But still, even then I'd rather cross-compile, because then I can just do everything on a single development machine instead of having to install and maintain multiple development toolchains across different machines. Otherwise it's just a lot of unnecessary hassle having to sync source code between different development environments and switch between computers just to build a set of release binaries, say.  On a single development environment with cross-compilation, I can just setup the build script to build all binaries for all platforms at once, without any of these hassles.


> If you're targeting Linux on non-native architectures you can use qemu.  Seems pretty easy if you have a statically linked binary and use qemu user emulation.

For testing, yeah I'd do that. For builds, I'd rather centralize everything on a single development environment.


> There's also free public CI services that target macOS, no need to cross-compile and it can run the code as well.

That's good to know.  Still, I'd rather keep things independent of a network connection in case I ever find myself in a place without one.


> I did have a use case at my previous job. The production systems were running Linux but all developers were using macOS. We created a custom tool for the developers, which then needed to target macOS. It was a GUI application so Docker wasn't an option. We only had access to Linux CI runners so I used cross-compiling. It couldn't test the result, but at least it could build it and publish it. That's when I setup the first incarnation of this project [2]. In this new incarnation, I've fixed the main problem of the first incarnation: reproducibility.
> 
> [1] https://www.darlinghq.org
> [2] https://github.com/jacob-carlborg/docker-ldc-darwin
[...]

Thanks for this, it is very helpful.


T

-- 
Change is inevitable, except from a vending machine.
April 09, 2021
On 2021-04-08 18:36, H. S. Teoh wrote:

> Thanks for this, it is very helpful.

You're welcome. I'm glad that it's useful to someone.

I just created a tag [1] (no changes yet), if I would like to make some changes in the future.

[1] https://github.com/d-cross-compiler/docker-ldc-darwin/tree/v0.0.1

-- 
/Jacob Carlborg
April 10, 2021

Thanks Jacob, I'm sure this was quite a bit of work, and opening up proprietary SDKs for non-native systems is always welcome. Thumbs up!

April 11, 2021
On 2021-04-10 15:50, kinke wrote:
> Thanks Jacob, I'm sure this was quite a bit of work, and opening up proprietary SDKs for non-native systems is always welcome. Thumbs up!

Thanks.

-- 
/Jacob Carlborg