Fails to link on arm64 only when built through nix

Hi,

I’m having trouble getting a C++ package to build through nix when on Mac M1 (arm64 darwin). It builds fine from source without nix on the same machine, conversely the nix package builds fine on regular Linux machines.

The package is udpipe, which I’ve successfully built on Linux with

# https://github.com/apertium/packaging/blob/c7dd7d63d258893e2eb1704fca823ecac0efc652/tools/udpipe/debian/control

{ lib
, stdenv
, fetchgit
}:

stdenv.mkDerivation rec {
  pname = "udpipe";
  version = "1.2.1";

  src = fetchgit {
    url = "https://github.com/ufal/${pname}";
    rev = "e51f02d2744cdfd4a29efc1320644ea04d535f0b";
    sha256 = "1mpcpl9a57z0g42pn5w54v4cmxc2q63hblsyqphbp6hzhga6ngcl";
  };

  sourceRoot = "${src.name}/src";

  enableParallelBuilding = true;

  buildPhase = ''
    make MODE=release -j$NIX_BUILD_CORES exe server lib
  '';

  installPhase = ''
    mkdir -p $out/{bin,lib}
    cp -v udpipe $out/bin
    cp -v rest_server/udpipe_server $out/bin
    cp -v libudpipe.a $out/lib/
  '';

  meta = with lib; {
    homepage = "https://github.com/ufal/udpipe";
    description = " UDPipe: Trainable pipeline for tokenizing, tagging, lemmatizing and parsing Universal Treebanks and other CoNLL-U files";
    maintainers = [ lib.maintainers ];
    license = licenses.mpl20;
    platforms = platforms.all;
  };
}

(typically using nixpkgs rev 3024ba0b7 but I’ve also tried bb029673b)

When building on M1 Mac’s I get a load of ranlib and ld warnings about files being ignored and then finally no main defined:

                                                                                                                                
warning: /nix/store/jmxf2kh5f96s2gw8kvqlq3vckpx7lh2k-cctools-port-973.0.1/bin/ranlib: 
archive library: libudpipe.a the table of contents is empty (no object file members in the library define global symbols)                              

[...]

ld: warning: ignoring file .build/rest_server-microrestd-rest_server-rest_server.osx-clang-release.o, 
building for macOS-arm64 but attempting to link with file built for unknown-unsupported file format ( 
0xDEld: warning: ignoring file .build/utils-compressor_save.osx-clang-release.o, building for macOS-
arm64 but attempting to link with file built for unknown-unsupported file format ( 0xDE 0xC0 0x17 0x0B 0x00 0x00 0x00 0x00 0x14 0x00 0x00 0x00 0x68 0x18 0x02 0x00 ) 0xC0 0x17 0x0B 0x00 0x00 0x00 0x00 0x14 0x00 0x00 0x00 0x7C 0x82 0x01 0x00 )

ld: warning: ignoring file .build/rest_server-microrestd-rest_server-json_builder.osx-clang-release.o, 
building for macOS-arm64 but attempting to link with file built for unknown-unsupported file format ( 0xDE 0xC0 0x17 0x0B 0x00 0x00 0x00 0x00 0x14 0x00 0x00 0x00 0xA4 0x5E 0x00 0x00 )


Undefined symbols for architecture arm64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture arm64
clang-11: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Makefile:36: udpipe] Error 1
make: *** Waiting for unfinished jobs....
Undefined symbols for architecture arm64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture arm64
clang-11: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Makefile:36: rest_server/udpipe_server] Error 1
error: builder for '/nix/store/k79zhhhm6qfhxzrbfp3jfqyhwxdk7bfy-udpipe-1.2.1.drv' failed with exit code 2;
       last 10 log lines:
       > ld: symbol(s) not found for architecture arm64
       > clang-11: error: linker command failed with exit code 1 (use -v to see invocation)
       > make: *** [Makefile:36: udpipe] Error 1
       > make: *** Waiting for unfinished jobs....
       > Undefined symbols for architecture arm64:
       >   "_main", referenced from:
       >      implicit entry/start for main executable
       > ld: symbol(s) not found for architecture arm64
       > clang-11: error: linker command failed with exit code 1 (use -v to see invocation)
       > make: *** [Makefile:36: rest_server/udpipe_server] Error 1

The program builds fine on M1 if I just manually build (outside of nix) with whatever clang is already installed. Is there some special magic needed for linking on M1 ARM?

Full logs: http://sprunge.us/ZkEt6I

Logs when built outside of nix: http://sprunge.us/EXmpZN

I was trying to solve a similar issue and found Clang link time optimization not working on OSX · Issue #19098 · NixOS/nixpkgs · GitHub