@@ -0,0 +1,639 @@
{ stdenv , callPackage , lib , fetchurl , fetchFromGitHub , installShellFiles
, runCommand , runCommandCC , makeWrapper , recurseIntoAttrs
# this package (through the fixpoint glass)
, bazel_self
# needed only for the updater
, bazel_3
, lr , xe , zip , unzip , bash , writeCBin , coreutils
, which , gawk , gnused , gnutar , gnugrep , gzip , findutils
# updater
, python27 , python3 , writeScript
# Apple dependencies
, cctools , libcxx , CoreFoundation , CoreServices , Foundation
# Allow to independently override the jdks used to build and run respectively
, buildJdk , runJdk
, buildJdkName
, runtimeShell
# Downstream packages for tests
, bazel-watcher
# Always assume all markers valid (this is needed because we remove markers; they are non-deterministic).
# Also, don't clean up environment variables (so that NIX_ environment variables are passed to compilers).
, enableNixHacks ? false
, gcc-unwrapped
, autoPatchelfHook
, file
, substituteAll
, writeTextFile
}:
let
version = " 4 . 0 . 0 " ;
sourceRoot = " . " ;
src = fetchurl {
url = " h t t p s : / / g i t h u b . c o m / b a z e l b u i l d / b a z e l / r e l e a s e s / d o w n l o a d / ${ version } / b a z e l - ${ version } - d i s t . z i p " ;
sha256 = " 1 l f d x 5 4 d p z w r q y s g 5 n g q h q 7 a 0 i 0 1 x k 9 8 1 c r d 4 p d k 4 j b 5 f 0 7 g h l 6 k " ;
} ;
# Update with `eval $(nix-build -A bazel.updater)`,
# then add new dependencies from the dict in ./src-deps.json as required.
srcDeps = lib . attrsets . attrValues srcDepsSet ;
srcDepsSet =
let
srcs = ( builtins . fromJSON ( builtins . readFile ./src-deps.json ) ) ;
toFetchurl = d : lib . attrsets . nameValuePair d . name ( fetchurl {
urls = d . urls ;
sha256 = d . sha256 ;
} ) ;
in builtins . listToAttrs ( map toFetchurl [
srcs . desugar_jdk_libs
srcs . io_bazel_skydoc
srcs . bazel_skylib
srcs . io_bazel_rules_sass
srcs . platforms
( if stdenv . hostPlatform . isDarwin
then srcs . " j a v a _ t o o l s _ j a v a c 1 1 _ d a r w i n - v 1 0 . 5 . z i p "
else srcs . " j a v a _ t o o l s _ j a v a c 1 1 _ l i n u x - v 1 0 . 5 . z i p " )
srcs . " c o v e r a g e _ o u t p u t _ g e n e r a t o r - v 2 . 5 . z i p "
srcs . build_bazel_rules_nodejs
srcs . " a n d r o i d _ t o o l s _ p k g - 0 . 1 9 . 0 r c 3 . t a r . g z "
srcs . bazel_toolchains
srcs . com_github_grpc_grpc
srcs . upb
srcs . com_google_protobuf
srcs . rules_pkg
srcs . rules_cc
srcs . rules_java
srcs . rules_proto
srcs . com_google_absl
srcs . com_github_google_re2
srcs . com_github_cares_cares
] ) ;
distDir = runCommand " b a z e l - d e p s " { } ''
m k d i r - p $o u t
f o r i i n ${ builtins . toString srcDeps } ; d o c p $i $o u t / $( s t r i p H a s h $i ) ; d o n e
'' ;
defaultShellPath = lib . makeBinPath
# Keep this list conservative. For more exotic tools, prefer to use
# @rules_nixpkgs to pull in tools from the nix repository. Example:
#
# WORKSPACE:
#
# nixpkgs_git_repository(
# name = "nixpkgs",
# revision = "def5124ec8367efdba95a99523dd06d918cb0ae8",
# )
#
# # This defines an external Bazel workspace.
# nixpkgs_package(
# name = "bison",
# repositories = { "nixpkgs": "@nixpkgs//:default.nix" },
# )
#
# some/BUILD.bazel:
#
# genrule(
# ...
# cmd = "$(location @bison//:bin/bison) -other -args",
# tools = [
# ...
# "@bison//:bin/bison",
# ],
# )
#
[ bash coreutils findutils gawk gnugrep gnutar gnused gzip which unzip file zip python27 python3 ] ;
# Java toolchain used for the build and tests
javaToolchain = " @ b a z e l _ t o o l s / / t o o l s / j d k : t o o l c h a i n _ ${ buildJdkName } " ;
platforms = lib . platforms . linux ++ lib . platforms . darwin ;
# This repository is fetched by bazel at runtime
# however it contains prebuilt java binaries, with wrong interpreter
# and libraries path.
# We prefetch it, patch it, and override it in a global bazelrc.
system = if stdenv . hostPlatform . isDarwin then " d a r w i n " else " l i n u x " ;
arch = stdenv . hostPlatform . parsed . cpu . name ;
remote_java_tools = stdenv . mkDerivation {
name = " r e m o t e _ j a v a _ t o o l s _ ${ system } " ;
src = srcDepsSet . " j a v a _ t o o l s _ j a v a c 1 1 _ ${ system } - v 1 0 . 5 . z i p " ;
nativeBuildInputs = [ autoPatchelfHook unzip ] ;
buildInputs = [ gcc-unwrapped ] ;
sourceRoot = " . " ;
buildPhase = ''
m k d i r $o u t ;
'' ;
installPhase = ''
c p - R a * $o u t /
t o u c h $o u t / W O R K S P A C E
'' ;
} ;
bazelRC = writeTextFile {
name = " b a z e l - r c " ;
text = ''
s t a r t u p - - s e r v e r _ j a v a b a s e = ${ runJdk }
# C a n ' t u s e ' c o m m o n ' ; h t t p s : / / g i t h u b . c o m / b a z e l b u i l d / b a z e l / i s s u e s / 3 0 5 4
# M o s t c o m m a n d s i n h e r i t f r o m ' b u i l d ' a n y w a y .
b u i l d - - d i s t d i r = ${ distDir }
f e t c h - - d i s t d i r = ${ distDir }
q u e r y - - d i s t d i r = ${ distDir }
b u i l d - - o v e r r i d e _ r e p o s i t o r y = ${ remote_java_tools . name } = ${ remote_java_tools }
f e t c h - - o v e r r i d e _ r e p o s i t o r y = ${ remote_java_tools . name } = ${ remote_java_tools }
q u e r y - - o v e r r i d e _ r e p o s i t o r y = ${ remote_java_tools . name } = ${ remote_java_tools }
# P r o v i d e a d e f a u l t j a v a t o o l c h a i n , t h i s w i l l b e t h e s a m e a s ${ runJdk }
b u i l d - - h o s t _ j a v a b a s e = ' @ l o c a l _ j d k / / : j d k '
# l o a d d e f a u l t l o c a t i o n f o r t h e s y s t e m w i d e c o n f i g u r a t i o n
t r y - i m p o r t / e t c / b a z e l . b a z e l r c
'' ;
} ;
in
stdenv . mkDerivation rec {
pname = " b a z e l " ;
inherit version ;
meta = with lib ; {
homepage = " h t t p s : / / g i t h u b . c o m / b a z e l b u i l d / b a z e l / " ;
description = " B u i l d t o o l t h a t b u i l d s c o d e q u i c k l y a n d r e l i a b l y " ;
license = licenses . asl20 ;
maintainers = [ maintainers . mboes ] ;
inherit platforms ;
} ;
inherit src ;
inherit sourceRoot ;
patches = [
# On Darwin, the last argument to gcc is coming up as an empty string. i.e: ''
# This is breaking the build of any C target. This patch removes the last
# argument if it's found to be an empty string.
../trim-last-argument-to-gcc-if-empty.patch
# On Darwin, using clang 6 to build fails because of a linker error (see #105573),
# but using clang 7 fails because libarclite_macosx.a cannot be found when linking
# the xcode_locator tool.
# This patch removes using the -fobjc-arc compiler option and makes the code
# compile without automatic reference counting. Caveat: this leaks memory, but
# we accept this fact because xcode_locator is only a short-lived process used during the build.
./no-arc.patch
# --experimental_strict_action_env (which may one day become the default
# see bazelbuild/bazel#2574) hardcodes the default
# action environment to a non hermetic value (e.g. "/usr/local/bin").
# This is non hermetic on non-nixos systems. On NixOS, bazel cannot find the required binaries.
# So we are replacing this bazel paths by defaultShellPath,
# improving hermeticity and making it work in nixos.
( substituteAll {
src = ../strict_action_env.patch ;
strictActionEnvPatch = defaultShellPath ;
} )
# bazel reads its system bazelrc in /etc
# override this path to a builtin one
( substituteAll {
src = ../bazel_rc.patch ;
bazelSystemBazelRCPath = bazelRC ;
} )
] ++ lib . optional enableNixHacks ../nix-hacks.patch ;
# Additional tests that check bazel’ s functionality. Execute
#
# nix-build . -A bazel.tests
#
# in the nixpkgs checkout root to exercise them locally.
passthru . tests =
let
runLocal = name : attrs : script :
let
attrs' = removeAttrs attrs [ " b u i l d I n p u t s " ] ;
buildInputs = [ python3 ] ++ ( attrs . buildInputs or [ ] ) ;
in
runCommandCC name ( {
inherit buildInputs ;
preferLocalBuild = true ;
meta . platforms = platforms ;
} // attrs' ) script ;
# bazel wants to extract itself into $install_dir/install every time it runs,
# so let’ s do that only once.
extracted = bazelPkg :
let install_dir =
# `install_base` field printed by `bazel info`, minus the hash.
# yes, this path is kinda magic. Sorry.
" $ H O M E / . c a c h e / b a z e l / _ b a z e l _ n i x b l d " ;
in runLocal " b a z e l - e x t r a c t e d - h o m e d i r " { passthru . install_dir = install_dir ; } ''
e x p o r t H O M E = $( m k t e m p - d )
t o u c h W O R K S P A C E # y e a h , e v e r y t h i n g s u c k s
i n s t a l l _ b a s e = " $( ${ bazelPkg } / b i n / b a z e l i n f o | g r e p i n s t a l l _ b a s e ) "
# a s s e r t i t ’ s a c t u a l l y b e l o w i n s t a l l _ d i r
[ [ " $i n s t a l l _ b a s e " = ~ ${ install_dir } ] ] \
| | ( e c h o " o h n o ! $i n s t a l l _ b a s e b u t w e a r e \
t r y i n g t o c o p y ${ install_dir } t o $o u t i n s t e a d ! " ; e x i t 1 )
c p - R ${ install_dir } $o u t
'' ;
bazelTest = { name , bazelScript , workspaceDir , bazelPkg , buildInputs ? [ ] }:
let
be = extracted bazelPkg ;
in runLocal name { inherit buildInputs ; } (
# skip extraction caching on Darwin, because nobody knows how Darwin works
( lib . optionalString ( ! stdenv . hostPlatform . isDarwin ) ''
# s e t u p h o m e w i t h p r e - u n p a c k e d b a z e l
e x p o r t H O M E = $( m k t e m p - d )
m k d i r - p ${ be . install_dir }
c p - R ${ be } / i n s t a l l ${ be . install_dir }
# h t t p s : / / s t a c k o v e r f l o w . c o m / q u e s t i o n s / 4 7 7 7 5 6 6 8 / b a z e l - h o w - t o - s k i p - c o r r u p t - i n s t a l l a t i o n - o n - c e n t o s 6
# B a z e l c h e c k s w h e t h e r t h e m t i m e o f t h e i n s t a l l d i r f i l e s
# i s > 9 y e a r s i n t h e f u t u r e , o t h e r w i s e i t e x t r a c t s i t s e l f a g a i n .
# s e e P o s i x F i l e M T i m e : : I s U n t a m p e r e d i n s r c / m a i n / c p p / u t i l
# W h a t t h e h e l l b a z e l .
${ lr } / b i n / l r - 0 - U ${ be . install_dir } | ${ xe } / b i n / x e - N 0 - 0 t o u c h - - d a t e = " 9 y e a r s 6 m o n t h s " { }
'' )
+
''
# N o t e h t t p s : / / g i t h u b . c o m / b a z e l b u i l d / b a z e l / i s s u e s / 5 7 6 3 # i s s u e c o m m e n t - 4 5 6 3 7 4 6 0 9
# a b o u t w h y t o c r e a t e a s u b d i r f o r t h e w o r k s p a c e .
c p - r ${ workspaceDir } w d & & c h m o d u + w w d & & c d w d
${ bazelScript }
t o u c h $o u t
'' ) ;
bazelWithNixHacks = bazel_self . override { enableNixHacks = true ; } ;
bazel-examples = fetchFromGitHub {
owner = " b a z e l b u i l d " ;
repo = " e x a m p l e s " ;
rev = " 4 1 8 3 f c 7 0 9 c 2 6 a 0 0 3 6 6 6 6 5 e 2 d 6 0 d 7 0 5 2 1 d c 0 b 4 0 5 d " ;
sha256 = " 1 m m 4 a w x 6 s a 0 m y i z 9 j 4 h w p 7 1 r p r 7 y h 8 v i h f 3 z m 1 5 n 2 i i 6 x b 8 2 r 3 1 k " ;
} ;
in ( if ! stdenv . hostPlatform . isDarwin then {
# `extracted` doesn’ t work on darwin
shebang = callPackage ../shebang-test.nix { inherit runLocal extracted bazelTest distDir ; } ;
} else { } ) // {
bashTools = callPackage ../bash-tools-test.nix { inherit runLocal bazelTest distDir ; } ;
cpp = callPackage ../cpp-test.nix { inherit runLocal bazelTest bazel-examples distDir ; } ;
java = callPackage ../java-test.nix { inherit runLocal bazelTest bazel-examples distDir ; } ;
protobuf = callPackage ../protobuf-test.nix { inherit runLocal bazelTest distDir ; } ;
pythonBinPath = callPackage ../python-bin-path-test.nix { inherit runLocal bazelTest distDir ; } ;
bashToolsWithNixHacks = callPackage ../bash-tools-test.nix { inherit runLocal bazelTest distDir ; bazel = bazelWithNixHacks ; } ;
cppWithNixHacks = callPackage ../cpp-test.nix { inherit runLocal bazelTest bazel-examples distDir ; bazel = bazelWithNixHacks ; } ;
javaWithNixHacks = callPackage ../java-test.nix { inherit runLocal bazelTest bazel-examples distDir ; bazel = bazelWithNixHacks ; } ;
protobufWithNixHacks = callPackage ../protobuf-test.nix { inherit runLocal bazelTest distDir ; bazel = bazelWithNixHacks ; } ;
pythonBinPathWithNixHacks = callPackage ../python-bin-path-test.nix { inherit runLocal bazelTest distDir ; bazel = bazelWithNixHacks ; } ;
# downstream packages using buildBazelPackage
# fixed-output hashes of the fetch phase need to be spot-checked manually
downstream = recurseIntoAttrs ( {
inherit bazel-watcher ;
}
# dm-sonnet is only packaged for linux
// ( lib . optionalAttrs stdenv . isLinux {
# TODO(timokau) dm-sonnet is broken currently
# dm-sonnet-linux = python3.pkgs.dm-sonnet;
} ) ) ;
} ;
src_for_updater = stdenv . mkDerivation rec {
name = " u p d a t e r - s o u r c e s " ;
inherit src ;
buildInputs = [ unzip ] ;
inherit sourceRoot ;
installPhase = ''
c p - r . " $o u t "
'' ;
} ;
# update the list of workspace dependencies
passthru . updater = writeScript " u p d a t e - b a z e l - d e p s . s h " ''
# ! ${ runtimeShell }
( c d " ${ src_for_updater } " & &
B A Z E L _ U S E _ C P P _ O N L Y _ T O O L C H A I N = 1 \
" ${ bazel_3 } " / b i n / b a z e l \
q u e r y ' k i n d ( h t t p _ a r c h i v e , / / e x t e r n a l : a l l ) + k i n d ( h t t p _ f i l e , / / e x t e r n a l : a l l ) + k i n d ( d i s t d i r _ t a r , / / e x t e r n a l : a l l ) + k i n d ( g i t _ r e p o s i t o r y , / / e x t e r n a l : a l l ) ' \
- - l o a d i n g _ p h a s e _ t h r e a d s = 1 \
- - o u t p u t b u i l d ) \
| " ${ python3 } " / b i n / p y t h o n 3 " ${ ./update-srcDeps.py } " \
" ${ builtins . toString ./src-deps.json } "
'' ;
# Necessary for the tests to pass on Darwin with sandbox enabled.
# Bazel starts a local server and needs to bind a local address.
__darwinAllowLocalNetworking = true ;
# Bazel expects several utils to be available in Bash even without PATH. Hence this hack.
customBash = writeCBin " b a s h " ''
# i n c l u d e < s t d i o . h >
# i n c l u d e < s t d l i b . h >
# i n c l u d e < s t r i n g . h >
# i n c l u d e < u n i s t d . h >
e x t e r n c h a r * * e n v i r o n ;
i n t m a i n ( i n t a r g c , c h a r * a r g v [ ] ) {
c h a r * p a t h = g e t e n v ( " P A T H " ) ;
c h a r * p a t h T o A p p e n d = " ${ defaultShellPath } " ;
c h a r * n e w P a t h ;
i f ( p a t h ! = N U L L ) {
i n t l e n g t h = s t r l e n ( p a t h ) + 1 + s t r l e n ( p a t h T o A p p e n d ) + 1 ;
n e w P a t h = m a l l o c ( l e n g t h * s i z e o f ( c h a r ) ) ;
s n p r i n t f ( n e w P a t h , l e n g t h , " % s : % s " , p a t h , p a t h T o A p p e n d ) ;
} e l s e {
n e w P a t h = p a t h T o A p p e n d ;
}
s e t e n v ( " P A T H " , n e w P a t h , 1 ) ;
e x e c v e ( " ${ bash } / b i n / b a s h " , a r g v , e n v i r o n ) ;
r e t u r n 0 ;
}
'' ;
postPatch = let
darwinPatches = ''
b a z e l L i n k F l a g s ( ) {
e v a l s e t - - " $N I X _ L D F L A G S "
l o c a l f l a g
f o r f l a g i n " $@ " ; d o
p r i n t f ' - W l , % s ' " $f l a g "
d o n e
}
# D i s a b l e B a z e l ' s X c o d e t o o l c h a i n d e t e c t i o n w h i c h w o u l d c o n f i g u r e c o m p i l e r s
# a n d l i n k e r s f r o m X c o d e i n s t e a d o f f r o m P A T H
e x p o r t B A Z E L _ U S E _ C P P _ O N L Y _ T O O L C H A I N = 1
# E x p l i c i t l y c o n f i g u r e g c o v s i n c e w e d o n ' t h a v e i t o n D a r w i n , s o a u t o d e t e c t i o n f a i l s
e x p o r t G C O V = ${ coreutils } / b i n / f a l s e
# F r a m e w o r k s e a r c h p a t h s a r e n ' t a d d e d b y b i n t o o l s h o o k
# h t t p s : / / g i t h u b . c o m / N i x O S / n i x p k g s / p u l l / 4 1 9 1 4
e x p o r t N I X _ L D F L A G S + = " - F ${ CoreFoundation } / L i b r a r y / F r a m e w o r k s - F ${ CoreServices } / L i b r a r y / F r a m e w o r k s - F ${ Foundation } / L i b r a r y / F r a m e w o r k s "
# l i b c x x i n c l u d e s a r e n ' t a d d e d b y l i b c x x h o o k
# h t t p s : / / g i t h u b . c o m / N i x O S / n i x p k g s / p u l l / 4 1 5 8 9
e x p o r t N I X _ C F L A G S _ C O M P I L E = " $N I X _ C F L A G S _ C O M P I L E - i s y s t e m ${ libcxx } / i n c l u d e / c + + / v 1 "
# d o n ' t u s e s y s t e m i n s t a l l e d X c o d e t o r u n c l a n g , u s e N i x c l a n g i n s t e a d
s e d - i - E " s ; / u s r / b i n / x c r u n ( - - s d k m a c o s x ) ? c l a n g ; ${ stdenv . cc } / b i n / c l a n g $N I X _ C F L A G S _ C O M P I L E $( b a z e l L i n k F l a g s ) - f r a m e w o r k C o r e F o u n d a t i o n ; g " \
s c r i p t s / b o o t s t r a p / c o m p i l e . s h \
s r c / t o o l s / x c o d e / r e a l p a t h / B U I L D \
s r c / t o o l s / x c o d e / s t d r e d i r e c t / B U I L D \
t o o l s / o s x / B U I L D
s u b s t i t u t e I n P l a c e s c r i p t s / b o o t s t r a p / c o m p i l e . s h - - r e p l a c e ' - m m a c o s x - v e r s i o n - m i n = 1 0 . 9 ' " "
# n i x p k g s ' s l i b S y s t e m c a n n o t u s e p t h r e a d h e a d e r s d i r e c t l y , m u s t i m p o r t G C D h e a d e r s i n s t e a d
s e d - i - e " / # i n c l u d e < p t h r e a d \ / s p a w n . h > / i # i n c l u d e < d i s p a t c h / d i s p a t c h . h > " s r c / m a i n / c p p / b l a z e _ u t i l _ d a r w i n . c c
# c l a n g i n s t a l l e d f r o m X c o d e h a s a c o m p a t i b i l i t y w r a p p e r t h a t f o r w a r d s
# i n v o c a t i o n s o f g c c t o c l a n g , b u t v a n i l l a c l a n g d o e s n ' t
s e d - i - e ' s ; _ f i n d _ g e n e r i c ( r e p o s i t o r y _ c t x , " g c c " , " C C " , o v e r r i d e n _ t o o l s ) ; _ f i n d _ g e n e r i c ( r e p o s i t o r y _ c t x , " c l a n g " , " C C " , o v e r r i d e n _ t o o l s ) ; g ' t o o l s / c p p / u n i x _ c c _ c o n f i g u r e . b z l
s e d - i - e ' s ; / u s r / b i n / l i b t o o l ; ${ cctools } / b i n / l i b t o o l ; g ' t o o l s / c p p / u n i x _ c c _ c o n f i g u r e . b z l
w r a p p e r s = ( t o o l s / c p p / o s x _ c c _ w r a p p e r . s h t o o l s / c p p / o s x _ c c _ w r a p p e r . s h . t p l )
f o r w r a p p e r i n " ''$ { w r a p p e r s [ @ ] } " ; d o
s e d - i - e " s , / u s r / b i n / i n s t a l l _ n a m e _ t o o l , ${ cctools } / b i n / i n s t a l l _ n a m e _ t o o l , g " $w r a p p e r
d o n e
'' ;
genericPatches = ''
# S u b s t i t u t e j 2 o b j c a n d o b j c w r a p p e r ' s p y t h o n s h e b a n g t o p l a i n p y t h o n p a t h .
# T h e s e s c r i p t s e x p l i c i t l y d e p e n d o n P y t h o n 2 . 7 , h e n c e w e u s e p y t h o n 2 7 .
# S e e a l s o ` p o s t F i x u p ` w h e r e p y t h o n 2 7 i s a d d e d t o $o u t / n i x - s u p p o r t
s u b s t i t u t e I n P l a c e t o o l s / j 2 o b j c / j 2 o b j c _ h e a d e r _ m a p . p y - - r e p l a c e " $! / u s r / b i n / p y t h o n 2 . 7 " " # ! ${ python27 } / b i n / p y t h o n "
s u b s t i t u t e I n P l a c e t o o l s / j 2 o b j c / j 2 o b j c _ w r a p p e r . p y - - r e p l a c e " $! / u s r / b i n / p y t h o n 2 . 7 " " # ! ${ python27 } / b i n / p y t h o n "
s u b s t i t u t e I n P l a c e t o o l s / o b j c / j 2 o b j c _ d e a d _ c o d e _ p r u n e r . p y - - r e p l a c e " $! / u s r / b i n / p y t h o n 2 . 7 " " # ! ${ python27 } / b i n / p y t h o n "
# m d 5 s u m i s p a r t o f c o r e u t i l s
s e d - i ' s | / s b i n / m d 5 | m d 5 s u m | ' \
s r c / B U I L D
# r e p l a c e i n i t i a l v a l u e o f p y t h o n S h e b a n g v a r i a b l e i n B a z e l P y t h o n S e m a n t i c s . j a v a
s u b s t i t u t e I n P l a c e s r c / m a i n / j a v a / c o m / g o o g l e / d e v t o o l s / b u i l d / l i b / b a z e l / r u l e s / p y t h o n / B a z e l P y t h o n S e m a n t i c s . j a v a \
- - r e p l a c e ' " # ! / u s r / b i n / e n v " + p y t h o n E x e c u t a b l e N a m e ' " \ " # ! ${ python3 } / b i n / p y t h o n \ " "
# s u b s t i t u t e I n P l a c e i s r a t h e r s l o w , s o p r e f i l t e r t h e f i l e s w i t h g r e p
g r e p - r l Z / b i n s r c / m a i n / j a v a / c o m / g o o g l e / d e v t o o l s | w h i l e I F S = " " r e a d - r - d " " p a t h ; d o
# I f y o u a d d m o r e r e p l a c e m e n t s h e r e , y o u m u s t c h a n g e t h e g r e p a b o v e !
# O n l y f i l e s c o n t a i n i n g / b i n a r e t a k e n i n t o a c c o u n t .
# W e d e f a u l t t o p y t h o n 3 w h e r e p o s s i b l e . S e e a l s o ` p o s t F i x u p ` w h e r e
# p y t h o n 3 i s a d d e d t o $o u t / n i x - s u p p o r t
s u b s t i t u t e I n P l a c e " $p a t h " \
- - r e p l a c e / b i n / b a s h ${ customBash } / b i n / b a s h \
- - r e p l a c e " / u s r / b i n / e n v b a s h " ${ customBash } / b i n / b a s h \
- - r e p l a c e " / u s r / b i n / e n v p y t h o n " ${ python3 } / b i n / p y t h o n \
- - r e p l a c e / u s r / b i n / e n v ${ coreutils } / b i n / e n v \
- - r e p l a c e / b i n / t r u e ${ coreutils } / b i n / t r u e
d o n e
# b a z e l t e s t r u n n e r i n c l u d e r e f e r e n c e s t o / b i n / b a s h
s u b s t i t u t e I n P l a c e t o o l s / b u i l d _ r u l e s / t e s t _ r u l e s . b z l \
- - r e p l a c e / b i n / b a s h ${ customBash } / b i n / b a s h
f o r i i n $( f i n d t o o l s / c p p / - t y p e f )
d o
s u b s t i t u t e I n P l a c e $i \
- - r e p l a c e / b i n / b a s h ${ customBash } / b i n / b a s h
d o n e
# F i x u p s c r i p t s t h a t g e n e r a t e s c r i p t s . N o t f i x e d u p b y p a t c h S h e b a n g s b e l o w .
s u b s t i t u t e I n P l a c e s c r i p t s / b o o t s t r a p / c o m p i l e . s h \
- - r e p l a c e / b i n / b a s h ${ customBash } / b i n / b a s h
# a d d n i x e n v i r o n m e n t v a r s t o . b a z e l r c
c a t > > . b a z e l r c < < E O F
# L i m i t t h e r e s o u r c e s B a z e l i s a l l o w e d t o u s e d u r i n g t h e b u i l d t o 1 / 2 t h e
# a v a i l a b l e R A M a n d 3 / 4 t h e a v a i l a b l e C P U c o r e s . T h i s s h o u l d h e l p a v o i d
# o v e r w h e l m i n g t h e b u i l d m a c h i n e .
b u i l d - - l o c a l _ r a m _ r e s o u r c e s = H O S T _ R A M * . 5
b u i l d - - l o c a l _ c p u _ r e s o u r c e s = H O S T _ C P U S * . 7 5
b u i l d - - d i s t d i r = ${ distDir }
f e t c h - - d i s t d i r = ${ distDir }
b u i l d - - c o p t = " $( e c h o $N I X _ C F L A G S _ C O M P I L E | s e d - e ' s / / " - - c o p t = " / g ' ) "
b u i l d - - h o s t _ c o p t = " $( e c h o $N I X _ C F L A G S _ C O M P I L E | s e d - e ' s / / " - - h o s t _ c o p t = " / g ' ) "
b u i l d - - l i n k o p t = " $( e c h o $( < ${ stdenv . cc } / n i x - s u p p o r t / l i b c x x - l d f l a g s ) | s e d - e ' s / / " - - l i n k o p t = " / g ' ) "
b u i l d - - h o s t _ l i n k o p t = " $( e c h o $( < ${ stdenv . cc } / n i x - s u p p o r t / l i b c x x - l d f l a g s ) | s e d - e ' s / / " - - h o s t _ l i n k o p t = " / g ' ) "
b u i l d - - l i n k o p t = " - W l , $( e c h o $N I X _ L D F L A G S | s e d - e ' s / / " - - l i n k o p t = " - W l , / g ' ) "
b u i l d - - h o s t _ l i n k o p t = " - W l , $( e c h o $N I X _ L D F L A G S | s e d - e ' s / / " - - h o s t _ l i n k o p t = " - W l , / g ' ) "
b u i l d - - h o s t _ j a v a b a s e = ' @ l o c a l _ j d k / / : j d k '
b u i l d - - h o s t _ j a v a _ t o o l c h a i n = ' ${ javaToolchain } '
b u i l d - - v e r b o s e _ f a i l u r e s
E O F
# a d d t h e s a m e e n v i r o n m e n t v a r s t o c o m p i l e . s h
s e d - e " / \ $c o m m a n d \ \ \ \ $/ a - - c o p t = \ " $( e c h o $N I X _ C F L A G S _ C O M P I L E | s e d - e ' s / / " - - c o p t = \ " / g ' ) \ " \ \ \ \ " \
- e " / \ $c o m m a n d \ \ \ \ $/ a - - h o s t _ c o p t = \ " $( e c h o $N I X _ C F L A G S _ C O M P I L E | s e d - e ' s / / " - - h o s t _ c o p t = \ " / g ' ) \ " \ \ \ \ " \
- e " / \ $c o m m a n d \ \ \ \ $/ a - - l i n k o p t = \ " $( e c h o $( < ${ stdenv . cc } / n i x - s u p p o r t / l i b c x x - l d f l a g s ) | s e d - e ' s / / " - - l i n k o p t = \ " / g ' ) \ " \ \ \ \ " \
- e " / \ $c o m m a n d \ \ \ \ $/ a - - h o s t _ l i n k o p t = \ " $( e c h o $( < ${ stdenv . cc } / n i x - s u p p o r t / l i b c x x - l d f l a g s ) | s e d - e ' s / / " - - h o s t _ l i n k o p t = \ " / g ' ) \ " \ \ \ \ " \
- e " / \ $c o m m a n d \ \ \ \ $/ a - - l i n k o p t = \ " - W l , $( e c h o $N I X _ L D F L A G S | s e d - e ' s / / " - - l i n k o p t = \ " - W l , / g ' ) \ " \ \ \ \ " \
- e " / \ $c o m m a n d \ \ \ \ $/ a - - h o s t _ l i n k o p t = \ " - W l , $( e c h o $N I X _ L D F L A G S | s e d - e ' s / / " - - h o s t _ l i n k o p t = \ " - W l , / g ' ) \ " \ \ \ \ " \
- e " / \ $c o m m a n d \ \ \ \ $/ a - - h o s t _ j a v a b a s e = ' @ l o c a l _ j d k / / : j d k ' \ \ \ \ " \
- e " / \ $c o m m a n d \ \ \ \ $/ a - - h o s t _ j a v a _ t o o l c h a i n = ' ${ javaToolchain } ' \ \ \ \ " \
- e " / \ $c o m m a n d \ \ \ \ $/ a - - v e r b o s e _ f a i l u r e s \ \ \ \ " \
- i s c r i p t s / b o o t s t r a p / c o m p i l e . s h
# T h i s i s n e c e s s a r y t o a v o i d :
# " e r r o r : n o v i s i b l e @ i n t e r f a c e f o r ' N S D i c t i o n a r y ' d e c l a r e s t h e s e l e c t o r
# ' i n i t W i t h C o n t e n t s O f U R L : e r r o r : ' "
# T h i s c a n b e r e m o v e d w h e n t h e a p p l e _ s d k i s u p g r a d e d b e y o n d 1 0 . 1 3 +
s e d - i ' / i n i t W i t h C o n t e n t s O f U R L : v e r s i o n P l i s t U r l / {
N
s / e r r o r : n i l \ ] ; / \ ] ; /
} ' t o o l s / o s x / x c o d e _ l o c a t o r . m
# a p p e n d t h e P A T H w i t h d e f a u l t S h e l l P a t h i n t o o l s / b a s h / r u n f i l e s / r u n f i l e s . b a s h
e c h o " P A T H = \ $P A T H : ${ defaultShellPath } " > > r u n f i l e s . b a s h . t m p
c a t t o o l s / b a s h / r u n f i l e s / r u n f i l e s . b a s h > > r u n f i l e s . b a s h . t m p
m v r u n f i l e s . b a s h . t m p t o o l s / b a s h / r u n f i l e s / r u n f i l e s . b a s h
p a t c h S h e b a n g s .
'' ;
in lib . optionalString stdenv . hostPlatform . isDarwin darwinPatches
+ genericPatches ;
buildInputs = [
buildJdk
python3
] ;
# when a command can’ t be found in a bazel build, you might also
# need to add it to `defaultShellPath`.
nativeBuildInputs = [
installShellFiles
zip
python3
unzip
makeWrapper
which
customBash
] ++ lib . optionals ( stdenv . isDarwin ) [ cctools libcxx CoreFoundation CoreServices Foundation ] ;
# Bazel makes extensive use of symlinks in the WORKSPACE.
# This causes problems with infinite symlinks if the build output is in the same location as the
# Bazel WORKSPACE. This is why before executing the build, the source code is moved into a
# subdirectory.
# Failing to do this causes "infinite symlink expansion detected"
preBuildPhases = [ " p r e B u i l d P h a s e " ] ;
preBuildPhase = ''
m k d i r b a z e l _ s r c
s h o p t - s d o t g l o b e x t g l o b
m v ! ( b a z e l _ s r c ) b a z e l _ s r c
'' ;
# Needed to build fish completion
propagatedBuildInputs = [ python3 . pkgs . absl-py ] ;
buildPhase = ''
# I n c r e a s i n g m e m o r y d u r i n g c o m p i l a t i o n m i g h t b e n e c e s s a r y .
# e x p o r t B A Z E L _ J A V A C _ O P T S = " - J - X m x 2 g - J - X m s 2 0 0 m "
# I f E M B E D _ L A B E L i s n ' t s e t , i t ' d b e a u t o - d e t e c t e d f r o m C H A N G E L O G . m d
# a n d ` g i t r e v - p a r s e - - s h o r t H E A D ` w h i c h w o u l d r e s u l t i n
# " 3 . 7 . 0 - ( @ n o n - g i t ) " d u e t o n o n - g i t b u i l d a n d i n c o m p l e t e c h a n g e l o g .
# A c t u a l b a z e l r e l e a s e s u s e s c r i p t s / r e l e a s e / c o m m o n . s h w h i c h i s b a s e d
# o n b r a n c h / t a g i n f o r m a t i o n w h i c h w e d o n ' t h a v e w i t h t a r b a l l r e l e a s e s .
# N o t e t h a t . b a z e l v e r s i o n i s a l w a y s c o r r e c t a n d i s b a s e d o n b a z e l - *
# e x e c u t a b l e n a m e , v e r s i o n c h e c k s s h o u l d w o r k f i n e
e x p o r t E M B E D _ L A B E L = " ${ version } - ( @ n o n - g i t ) "
${ customBash } / b i n / b a s h . / b a z e l _ s r c / c o m p i l e . s h
. / b a z e l _ s r c / s c r i p t s / g e n e r a t e _ b a s h _ c o m p l e t i o n . s h \
- - b a z e l = . / b a z e l _ s r c / o u t p u t / b a z e l \
- - o u t p u t = . / b a z e l _ s r c / o u t p u t / b a z e l - c o m p l e t e . b a s h \
- - p r e p e n d = . / b a z e l _ s r c / s c r i p t s / b a z e l - c o m p l e t e - h e a d e r . b a s h \
- - p r e p e n d = . / b a z e l _ s r c / s c r i p t s / b a z e l - c o m p l e t e - t e m p l a t e . b a s h
${ python3 } / b i n / p y t h o n 3 . / b a z e l _ s r c / s c r i p t s / g e n e r a t e _ f i s h _ c o m p l e t i o n . p y \
- - b a z e l = . / b a z e l _ s r c / o u t p u t / b a z e l \
- - o u t p u t = . / b a z e l _ s r c / o u t p u t / b a z e l - c o m p l e t e . f i s h
'' ;
installPhase = ''
m k d i r - p $o u t / b i n
# o f f i c i a l w r a p p e r s c r i p t s t h a t s e a r c h e s f o r $W O R K S P A C E _ R O O T / t o o l s / b a z e l
# i f i t c a n ’ t f i n d s o m e t h i n g i n t o o l s , i t c a l l s $o u t / b i n / b a z e l - { v e r s i o n } - { o s _ a r c h }
# T h e b i n a r y _ m u s t _ e x i s t w i t h t h i s n a m i n g i f y o u r p r o j e c t c o n t a i n s a . b a z e l v e r s i o n
# f i l e .
c p . / b a z e l _ s r c / s c r i p t s / p a c k a g e s / b a z e l . s h $o u t / b i n / b a z e l
m v . / b a z e l _ s r c / o u t p u t / b a z e l $o u t / b i n / b a z e l - ${ version } - ${ system } - ${ arch }
# s h e l l c o m p l e t i o n f i l e s
i n s t a l l S h e l l C o m p l e t i o n - - b a s h \
- - n a m e b a z e l . b a s h \
. / b a z e l _ s r c / o u t p u t / b a z e l - c o m p l e t e . b a s h
i n s t a l l S h e l l C o m p l e t i o n - - z s h \
- - n a m e _ b a z e l \
. / b a z e l _ s r c / s c r i p t s / z s h _ c o m p l e t i o n / _ b a z e l
i n s t a l l S h e l l C o m p l e t i o n - - f i s h \
- - n a m e b a z e l . f i s h \
. / b a z e l _ s r c / o u t p u t / b a z e l - c o m p l e t e . f i s h
'' ;
doInstallCheck = true ;
installCheckPhase = ''
e x p o r t T E S T _ T M P D I R = $( p w d )
h e l l o _ t e s t ( ) {
$o u t / b i n / b a z e l t e s t \
- - t e s t _ o u t p u t = e r r o r s \
- - j a v a _ t o o l c h a i n = ' ${ javaToolchain } ' \
e x a m p l e s / c p p : h e l l o - s u c c e s s _ t e s t \
e x a m p l e s / j a v a - n a t i v e / s r c / t e s t / j a v a / c o m / e x a m p l e / m y p r o j e c t : h e l l o
}
c d . / b a z e l _ s r c
# t e s t w h e t h e r $W O R K S P A C E _ R O O T / t o o l s / b a z e l w o r k s
m k d i r - p t o o l s
c a t > t o o l s / b a z e l < < " E O F "
# ! ${ runtimeShell } - e
e x i t 1
E O F
c h m o d + x t o o l s / b a z e l
# f i r s t c a l l s h o u l d f a i l i f t o o l s / b a z e l i s u s e d
! h e l l o _ t e s t
c a t > t o o l s / b a z e l < < " E O F "
# ! ${ runtimeShell } - e
e x e c " $B A Z E L _ R E A L " " $@ "
E O F
# s e c o n d c a l l s u c c e e d s b e c a u s e i t d e f e r s t o $o u t / b i n / b a z e l - { v e r s i o n } - { o s _ a r c h }
h e l l o _ t e s t
'' ;
# Save paths to hardcoded dependencies so Nix can detect them.
postFixup = ''
m k d i r - p $o u t / n i x - s u p p o r t
e c h o " ${ customBash } ${ defaultShellPath } " > > $o u t / n i x - s u p p o r t / d e p e n d s
# T h e t e m p l a t e s g e t t a r ’ d u p i n t o a . j a r ,
# s o n i x c a n ’ t d e t e c t p y t h o n i s n e e d e d i n t h e r u n t i m e c l o s u r e
# S o m e o f t h e s c r i p t s e x p l i c i t l y d e p e n d o n P y t h o n 2 . 7 . O t h e r w i s e , w e
# d e f a u l t t o u s i n g p y t h o n 3 . T h e r e f o r e , b o t h p y t h o n 2 7 a n d p y t h o n 3 a r e
# r u n t i m e d e p e n d e n c i e s .
e c h o " ${ python27 } " > > $o u t / n i x - s u p p o r t / d e p e n d s
e c h o " ${ python3 } " > > $o u t / n i x - s u p p o r t / d e p e n d s
'' + lib . optionalString stdenv . isDarwin ''
e c h o " ${ cctools } " > > $o u t / n i x - s u p p o r t / d e p e n d s
'' ;
dontStrip = true ;
dontPatchELF = true ;
}