nix: cleanup status-go mobile build derivation

While working on Nix builds for `go-waku` I figured this derivation
could use some cleanup, to make it shorter and more readable.

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2022-06-07 20:38:24 +02:00
parent 785386987b
commit 6dd1577b8a
No known key found for this signature in database
GPG Key ID: 09AA5403E54D9931
4 changed files with 32 additions and 77 deletions

View File

@ -58,7 +58,7 @@ nixOpts=(
${GIT_ROOT}/nix/scripts/gcroots.sh "${TARGET}" "${@}"
# Run the actual build
echo "${GRN}Running:${RST} nix-build "${nixOpts[@]}" "${@}" default.nix"
echo -e "${GRN}Running:${RST} ${BLD}nix-build "${nixOpts[@]}" ${@}${RST}"
nixResultPath=$(nix-build "${nixOpts[@]}" "${@}" default.nix)
echo -e "\n${YLW}Extracting result${RST}: ${BLD}${nixResultPath}${RST}"

View File

@ -28,12 +28,9 @@ let
"-s" # -s disabled symbol table
"-w" # -w disables DWARF debugging information
];
goBuildFlags = [ "-v" ];
in rec {
mobile = callPackage ./mobile {
inherit meta source goBuildFlags goBuildLdFlags;
inherit meta source goBuildLdFlags;
};
library = callPackage ./library {

View File

@ -1,10 +1,10 @@
{ lib, stdenv, utils, buildGoPackage
, go, androidPkgs, openjdk, gomobile, xcodeWrapper
# object with source attributes
, meta, source
{ lib, utils, buildGoPackage, androidPkgs, openjdk, gomobile, xcodeWrapper
, meta
, source
, platform ? "android"
, platformVersion ? "23"
, architectures ? [ "arm64" "arm" "x86" ]
, goBuildFlags ? [ ]
, goBuildFlags ? [ "-x" ]
, goBuildLdFlags ? [ ]
, outputFileName ? "status-go-${source.shortRev}-${platform}.aar" }:
@ -12,12 +12,7 @@
{ secretsFile ? "" }:
let
inherit (stdenv) isDarwin;
inherit (lib) concatStringsSep concatMapStrings optionalString makeBinPath optional;
removeReferences = [ go ];
removeExpr = refs: ''remove-references-to ${concatMapStrings (ref: " -t ${ref}") refs}'';
inherit (lib) concatStringsSep optionalString optional;
# formatted for use with -target
targetArchs = map (a: "${platform}/${a}") architectures;
@ -28,78 +23,44 @@ in buildGoPackage {
inherit meta;
inherit (source) src goPackagePath;
extraSrcPaths = [ gomobile ];
nativeBuildInputs = [ gomobile ]
++ optional (platform == "android") openjdk
++ optional isDarwin xcodeWrapper;
++ optional (platform == "ios") xcodeWrapper;
# Fixes Cgo related build failures (see https://github.com/NixOS/nixpkgs/issues/25959 )
hardeningDisable = [ "fortify" ];
ldflags = concatStringsSep " " (goBuildLdFlags
++ lib.optionals (secretsFile != "") ["-X node.OpenseaKeyFromEnv=$OPENSEA_API_KEY"]);
phases = [
"unpackPhase" "secretsPhase" "configurePhase"
"buildPhase" "installPhase" "fixupPhase"
];
ANDROID_HOME = optionalString (platform == "android") androidPkgs.sdk;
# if secretsFile is not set we use generate keystore
secretsPhase = if (secretsFile != "") then ''
# Ensure XCode is present for iOS, instead of failing at the end of the build.
preConfigure = optionalString (platform == "ios") utils.enforceXCodeAvailable;
# If secretsFile is not set we use generate keystore.
preBuild = if (secretsFile != "") then ''
source "${secretsFile}"
'' else ''
echo "No secrets provided!"
'';
# Ensure XCode is present for iOS build, instead of failing at the end of the build
preConfigure = optionalString (isDarwin && platform == "ios") utils.enforceXCodeAvailable;
# Build mobile libraries
preBuild = let
NIX_GOWORKDIR = "$NIX_BUILD_TOP/go-build";
in ''
mkdir ${NIX_GOWORKDIR}
export GO111MODULE=off
export GOPATH=${gomobile}:$GOPATH
export NIX_GOWORKDIR=${NIX_GOWORKDIR}
'' + optionalString (platform == "android") ''
export ANDROID_SDK_ROOT="${androidPkgs.sdk}"
export ANDROID_NDK_HOME="${androidPkgs.ndk}"
export PATH="${makeBinPath [ openjdk ]}:$PATH"
'';
# Build the Go library using gomobile for each of the configured platforms
buildPhase = let
ldFlags = [ "-extldflags=-Wl,--allow-multiple-definition" ]
++ lib.optionals (secretsFile != "") ["-X node.OpenseaKeyFromEnv=$OPENSEA_API_KEY"]
++ goBuildLdFlags;
CGO_LDFLAGS = concatStringsSep " " ldFlags;
in ''
buildPhase = ''
runHook preBuild
runHook renameImports
echo -e "\nBuilding for targets: ${concatStringsSep "," targetArchs}\n"
${gomobile}/bin/gomobile bind \
-target=${concatStringsSep "," targetArchs} \
-ldflags="${CGO_LDFLAGS}" \
${optionalString (platform == "android") "-androidapi 23"} \
${optionalString (platform == "ios") "-iosversion=8.0"} \
gomobile bind \
${concatStringsSep " " goBuildFlags} \
-ldflags="$ldflags" \
-target=${concatStringsSep "," targetArchs} \
${optionalString (platform == "android") "-androidapi=${platformVersion}"} \
${optionalString (platform == "ios") "-iosversion=${platformVersion}"} \
-o ${outputFileName} \
${source.goPackagePath}/mobile
rm -rf $NIX_GOWORKDIR
runHook postBuild
'';
# replace hardcoded paths to go package in /nix/store, otherwise Nix will fail the build
fixupPhase = ''
find $out -type f -exec ${removeExpr removeReferences} '{}' + || true
'';
installPhase = ''
mkdir -p $out
mv ${outputFileName} $out/
cp -r ${outputFileName} $out/
'';
outputs = [ "out" ];
}

View File

@ -1,22 +1,19 @@
{ callPackage
, meta, source
, goBuildFlags
, goBuildLdFlags }:
{ callPackage, meta, source, goBuildLdFlags }:
{
android = callPackage ./build.nix {
platform = "android";
# 386 is for android simulator
architectures = [ "arm" "arm64" "386" ];
platformVersion = "23";
architectures = [ "arm" "arm64" "386" ]; # 386 is for android simulator.
outputFileName = "status-go-${source.shortRev}.aar";
inherit meta source goBuildFlags goBuildLdFlags;
inherit meta source goBuildLdFlags;
};
ios = callPackage ./build.nix {
platform = "ios";
# amd64 is for ios simulator
architectures = [ "arm64" "amd64" ];
platformVersion = "8.0";
architectures = [ "arm64" "amd64" ]; # amd64 is for ios simulator.
outputFileName = "Statusgo.framework";
inherit meta source goBuildFlags goBuildLdFlags;
inherit meta source goBuildLdFlags;
};
}