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}" "${@}" ${GIT_ROOT}/nix/scripts/gcroots.sh "${TARGET}" "${@}"
# Run the actual build # 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) nixResultPath=$(nix-build "${nixOpts[@]}" "${@}" default.nix)
echo -e "\n${YLW}Extracting result${RST}: ${BLD}${nixResultPath}${RST}" echo -e "\n${YLW}Extracting result${RST}: ${BLD}${nixResultPath}${RST}"

View File

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

View File

@ -1,10 +1,10 @@
{ lib, stdenv, utils, buildGoPackage { lib, utils, buildGoPackage, androidPkgs, openjdk, gomobile, xcodeWrapper
, go, androidPkgs, openjdk, gomobile, xcodeWrapper , meta
# object with source attributes , source
, meta, source
, platform ? "android" , platform ? "android"
, platformVersion ? "23"
, architectures ? [ "arm64" "arm" "x86" ] , architectures ? [ "arm64" "arm" "x86" ]
, goBuildFlags ? [ ] , goBuildFlags ? [ "-x" ]
, goBuildLdFlags ? [ ] , goBuildLdFlags ? [ ]
, outputFileName ? "status-go-${source.shortRev}-${platform}.aar" }: , outputFileName ? "status-go-${source.shortRev}-${platform}.aar" }:
@ -12,12 +12,7 @@
{ secretsFile ? "" }: { secretsFile ? "" }:
let let
inherit (stdenv) isDarwin; inherit (lib) concatStringsSep optionalString optional;
inherit (lib) concatStringsSep concatMapStrings optionalString makeBinPath optional;
removeReferences = [ go ];
removeExpr = refs: ''remove-references-to ${concatMapStrings (ref: " -t ${ref}") refs}'';
# formatted for use with -target # formatted for use with -target
targetArchs = map (a: "${platform}/${a}") architectures; targetArchs = map (a: "${platform}/${a}") architectures;
@ -28,78 +23,44 @@ in buildGoPackage {
inherit meta; inherit meta;
inherit (source) src goPackagePath; inherit (source) src goPackagePath;
extraSrcPaths = [ gomobile ];
nativeBuildInputs = [ gomobile ] nativeBuildInputs = [ gomobile ]
++ optional (platform == "android") openjdk ++ optional (platform == "android") openjdk
++ optional isDarwin xcodeWrapper; ++ optional (platform == "ios") xcodeWrapper;
# Fixes Cgo related build failures (see https://github.com/NixOS/nixpkgs/issues/25959 ) ldflags = concatStringsSep " " (goBuildLdFlags
hardeningDisable = [ "fortify" ]; ++ lib.optionals (secretsFile != "") ["-X node.OpenseaKeyFromEnv=$OPENSEA_API_KEY"]);
phases = [ ANDROID_HOME = optionalString (platform == "android") androidPkgs.sdk;
"unpackPhase" "secretsPhase" "configurePhase"
"buildPhase" "installPhase" "fixupPhase"
];
# if secretsFile is not set we use generate keystore # Ensure XCode is present for iOS, instead of failing at the end of the build.
secretsPhase = if (secretsFile != "") then '' preConfigure = optionalString (platform == "ios") utils.enforceXCodeAvailable;
# If secretsFile is not set we use generate keystore.
preBuild = if (secretsFile != "") then ''
source "${secretsFile}" source "${secretsFile}"
'' else '' '' else ''
echo "No secrets provided!" echo "No secrets provided!"
''; '';
# Ensure XCode is present for iOS build, instead of failing at the end of the build buildPhase = ''
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 ''
runHook preBuild runHook preBuild
runHook renameImports
echo -e "\nBuilding for targets: ${concatStringsSep "," targetArchs}\n" echo -e "\nBuilding for targets: ${concatStringsSep "," targetArchs}\n"
${gomobile}/bin/gomobile bind \ gomobile bind \
-target=${concatStringsSep "," targetArchs} \
-ldflags="${CGO_LDFLAGS}" \
${optionalString (platform == "android") "-androidapi 23"} \
${optionalString (platform == "ios") "-iosversion=8.0"} \
${concatStringsSep " " goBuildFlags} \ ${concatStringsSep " " goBuildFlags} \
-ldflags="$ldflags" \
-target=${concatStringsSep "," targetArchs} \
${optionalString (platform == "android") "-androidapi=${platformVersion}"} \
${optionalString (platform == "ios") "-iosversion=${platformVersion}"} \
-o ${outputFileName} \ -o ${outputFileName} \
${source.goPackagePath}/mobile ${source.goPackagePath}/mobile
rm -rf $NIX_GOWORKDIR
runHook postBuild 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 = '' installPhase = ''
mkdir -p $out mkdir -p $out
mv ${outputFileName} $out/ cp -r ${outputFileName} $out/
''; '';
outputs = [ "out" ];
} }

View File

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