From 6dd1577b8a61a0455102a171453a4a807a00d861 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Tue, 7 Jun 2022 20:38:24 +0200 Subject: [PATCH] nix: cleanup status-go mobile build derivation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- nix/scripts/build.sh | 2 +- nix/status-go/default.nix | 5 +- nix/status-go/mobile/build.nix | 85 +++++++++----------------------- nix/status-go/mobile/default.nix | 17 +++---- 4 files changed, 32 insertions(+), 77 deletions(-) diff --git a/nix/scripts/build.sh b/nix/scripts/build.sh index e5088676ce..98e85892ff 100755 --- a/nix/scripts/build.sh +++ b/nix/scripts/build.sh @@ -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}" diff --git a/nix/status-go/default.nix b/nix/status-go/default.nix index f56a32e93d..1c71ae5087 100644 --- a/nix/status-go/default.nix +++ b/nix/status-go/default.nix @@ -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 { diff --git a/nix/status-go/mobile/build.nix b/nix/status-go/mobile/build.nix index d76bb81926..93d0c7b8e9 100644 --- a/nix/status-go/mobile/build.nix +++ b/nix/status-go/mobile/build.nix @@ -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" ]; } diff --git a/nix/status-go/mobile/default.nix b/nix/status-go/mobile/default.nix index e8c80fef4a..c048b3583c 100644 --- a/nix/status-go/mobile/default.nix +++ b/nix/status-go/mobile/default.nix @@ -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; }; }