October 21, 2022

Hi D-community.

I try to build and run very simple code on Alpine docker image - but have no luck with static builds and LTO.

The desired aim is to be able build it similar to C code compilation:

gcc leibniz.c -o leibniz -O3 -s -static -flto -march=native -mtune=native -fomit-frame-pointer -fno-signed-zeros -fno-trapping-math -fassociative-math

I'd like to find working examples for both LDC and GDC for static compilation. If you have one, share it here please.

For LDC I've tried to install this packages:

RUN apk add --no-cache ldc gcc ldc-static binutils-gold

and build it with parameters:

cc=gcc ldc2 -O3 -release -mcpu=native -flto=full -linker=gold -flto-binary=/usr/bin/ld.gold -defaultlib=phobos2-ldc-lto,druntime-ldc-lto -m64 -static leibniz.d

Some issues that I've faced while trying different options:

  • 'cc=clang' suddenly is not working. It still saying "can't find cc"
  • Without explicit 'flto-binary=' the error was about "LLVMgold (plugin) not found".

The code:

import std.stdio;
import std.file;

double pi = 1.0;
uint rounds;

void main() {
    auto file = File("./rounds.txt", "r");
    file.readf!"%d\n"(rounds);
    rounds += 2u;
    foreach(i; 2u .. rounds) {
        double x = -1.0 + 2.0 * (i & 0x1);
        pi += (x / (2u * i - 1u));
    }
    pi *= 4.0;
    writefln("%.16f", pi);
}