-
Notifications
You must be signed in to change notification settings - Fork 6
/
crossBuild.nix
85 lines (80 loc) · 2.14 KB
/
crossBuild.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
{ pkgs }:
let
mkCrossBuildShell =
{ GOOS
, GOARCH
, CC
, buildInputs ? [ ]
, nativeBuildInputs ? [ ]
, staticBuild
}:
pkgs.mkShell {
inherit nativeBuildInputs;
shellHook = ''
export GOOS=${GOOS}
export GOARCH=${GOARCH}
export CC=${CC}
export STATIC_BUILD=${if staticBuild then "1" else "0"}
export CGO_ENABLED="1"
'';
buildInputs = [ pkgs.go ] ++ buildInputs;
};
in
{
x86_64-windows =
let
mingwPkgs = pkgs.pkgsCross.mingwW64;
cc = mingwPkgs.stdenv.cc;
in
mkCrossBuildShell {
GOOS = "windows";
GOARCH = "amd64";
CC = "${cc}/bin/x86_64-w64-mingw32-gcc";
buildInputs = [
cc
mingwPkgs.windows.mingw_w64_pthreads
];
staticBuild = true;
};
x86_64-linux =
let
muslPkgs = pkgs.pkgsCross.musl64;
cc = muslPkgs.gcc;
static-pcsclite = muslPkgs.pcsclite.overrideAttrs (attrs: {
configureFlags = attrs.configureFlags ++ [ "--enable-static" "--disable-shared" ];
});
in
mkCrossBuildShell {
GOOS = "linux";
GOARCH = "amd64";
CC = "${cc}/bin/gcc";
nativeBuildInputs = [ muslPkgs.pkg-config ];
buildInputs = [
cc
static-pcsclite
];
staticBuild = true;
};
# NOTE: building darwin binaries is only supported on macOS.
# Cross compilation for darwin doesn't work very well with the nix ecosystem.
# It only works on macOS and even there, cross compilation between architectures
# is not yet supported. Given these limitations, it makes better sense to use
# macOS's clang instead of a C compiler from nix. By doing so, we can build
# we can build for both arm64 and amd64.
darwin =
let
darwinShell = arch:
# Because we're using macOS's own toolchain, it is unnecessary to include nix's
# PCSC package in the build shell.
mkCrossBuildShell {
GOOS = "darwin";
GOARCH = arch;
CC = "/usr/bin/clang";
staticBuild = false;
};
in
{
aarch64 = darwinShell "arm64";
x86_64 = darwinShell "amd64";
};
}