diff --git a/Dockerfile b/Dockerfile index ca44bec..d78621e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,5 @@ # See README.md FROM resin/rpi-raspbian:stretch -ADD build_from_scratch.sh / - RUN apt-get update # Needed for apt-key on the debian:stretch image. @@ -20,4 +18,7 @@ RUN apt-get install -y automake g++ libtool make curl git python unzip wget zip apt-get autoremove && \ apt-get clean -RUN cd / && bash ./build_from_scratch.sh +RUN cd / && \ + git clone https://github.com/ochafik/bazel.git -b build-from-scratch --depth=1 && \ + cd bazel && \ + VERBOSE=yes bash ./compile.sh diff --git a/README.md b/README.md index d3713d7..f896503 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,8 @@ Unfortunately, Raspbian doesn't have (yet) a package for Bazel. And Bazel doesn't provide (yet) a binary for armhf. And their instructions to build from source either implies you have a binary (which we don't) or that you battle with compilation errors. Which I did in -[ochafik/bazel](https://github.com/ochafik/bazel/tree/from-scratch) (hopefully -to be merged back into the original repo), and here I'm using it to build... +[ochafik/bazel](https://github.com/ochafik/bazel/tree/build-from-scratch) (hopefully +to be pulled back into the original repo), and here I'm using it to build... ## A Docker image to run Raspbian w/ Bazel on your Desktop @@ -51,38 +51,30 @@ docker run --rm -ti ochafik/rpi-raspbian-bazel /bin/bash ## From sources on a Raspberry Pi -Prerequisite: you'll need a large SD card, and the following packages (assumes -you already have an Oracle JDK8 installed, as is the case on NOOBS distros, -otherwise try `sudo apt-get install default-jdk`): +Prerequisite: you'll need a large SD card, and the following packages: ```bash sudo apt-get update sudo apt-get install -y automake g++ libtool make curl git python unzip wget zip +sudo apt-get install -y oracle-java8-installer oracle-java8-set-default + # Reclaim as much space as we can: we'll need it. sudo apt-get autoremove sudo apt-get clean ``` -Now download my [script](./build_from_scratch.sh) and convince yourself it's safe: -- It clones [ochafik/bazel](https://github.com/ochafik/bazel) - (my fork of [bazelbuild/bazel](https://github.com/bazelbuild/bazel)) -- Either your trust me, or you can fork my fork, check it doesn't contain dodgy - [diffs against the original repo](https://github.com/bazelbuild/bazel/compare/master...ochafik:from-scratch) - (assuming you trust the original, which I assume to be the case if you want to - use Bazel), edit the script to clone your fork instead (in case I sneakily - modified my fork after you scrutinized it), and proceed to the next command +Clone [ochafik/bazel](https://github.com/ochafik/bazel) (my fork of +[bazelbuild/bazel](https://github.com/bazelbuild/bazel)) and build it: ```bash -wget https://raw.githubusercontent.com/ochafik/rpi-raspbian-bazel/master/build_from_scratch.sh -# Do not trust scripts you download off the net! -less build_from_scratch.sh - -bash build_from_scratch.sh +git clone https://github.com/ochafik/bazel -b build-from-scratch --depth=1 +cd bazel +bash ./compile.sh ``` ## Building the Raspberry Pi Docker image: -We aim to publish images every now and then, but you may rebuild the image at +I aim to publish images to docker hub soon, but you may rebuild the image at any time with the following command (*takes a while*): ```bash @@ -106,5 +98,6 @@ And if you want to test the `Dockerfile` itself faster on an image that does not require QEMU: ```bash -docker build -t debian-bazel -f Dockerfile.debian . +cat Dockerfile | sed 's/resin\/rpi-raspbian:stretch/debian:stretch/' > Dockerfile.debian +docker build -t debian-bazel-build -f Dockerfile.debian . ``` diff --git a/build_debian_and_raspbian_docker_images.sh b/build_debian_and_raspbian_docker_images.sh index 49782d9..020644d 100755 --- a/build_debian_and_raspbian_docker_images.sh +++ b/build_debian_and_raspbian_docker_images.sh @@ -1,30 +1,51 @@ #!/bin/bash set -eux -if [[ ! -d dist-amd64 ]]; then - cat Dockerfile | sed 's/resin\/rpi-raspbian:stretch/debian:stretch/' > Dockerfile.debian - docker build -t debian-bazel-build -f Dockerfile.debian . - - mkdir dist-amd64 - id=$(docker create debian-bazel-build) - docker cp $id:bazel/output/bazel dist-amd64 - docker rm -v $id - echo " - FROM debian-bazel-build - COPY ./dist-amd64/bazel /usr/bin - " > Dockerfile.debian-slim - docker build -t debian-bazel -f Dockerfile.debian-slim . -fi - -if [[ ! -d dist-armhf ]]; then - docker build -t rpi-raspbian-bazel-build . - mkdir dist-armhf - id=$(docker create rpi-raspbian-bazel-build) - docker cp $id:bazel/output/bazel dist-armhf - docker rm -v $id - echo " - FROM rpi-raspbian-bazel-build - COPY ./dist-armhf/bazel /usr/bin - " > Dockerfile.raspbian-slim - docker build -t rpi-raspbian-bazel -f Dockerfile.raspbian-slim . -fi \ No newline at end of file +readonly BAZEL_OUTPUT=/bazel/output/bazel + +function build_bazel_docker() { + local build_image_base="$1" + local build_image_name="$2" + local normal_image_base="$3" + local normal_image_name="$4" + local slim_image_base="$5" + local slim_image_name="$6" + local dist_dir="$7" + + if [[ ! -d "$dist_dir" ]]; then + cat Dockerfile | \ + sed "s/resin\/rpi-raspbian:stretch/$(echo "$build_image_base" | sed 's/\//\\\//g')/" | + docker build -t "$build_image_name" - + + mkdir -p "$dist_dir" + id=$(docker create "$build_image_name") + docker cp "$id:$BAZEL_OUTPUT" "$dist_dir" + docker rm -v $id + + echo " + FROM $normal_image_base + COPY ./$dist_dir/bazel /usr/bin + " | \ + docker build -t "$normal_image_name" - + + echo " + FROM $slim_image_base + COPY ./$dist_dir/bazel /usr/bin + " | \ + docker build -t "$slim_image_name" - + fi +} + +# Use native Debian as a canary of sorts: this will fail way faster than the +# QEMU rasbian build. +build_bazel_docker \ + debian:stretch debian-bazel-build \ + debian:stretch debian-bazel \ + debian:stretch-slim debian-bazel-slim \ + dist-amd64 + +build_bazel_docker \ + resin/rpi-raspbian:stretch rpi-raspbian-bazel-build \ + resin/rpi-raspbian:stretch rpi-raspbian-bazel \ + resin/rpi-raspbian:stretch-slim rpi-raspbian-bazel-slim \ + dist-armhf diff --git a/build_from_scratch.sh b/build_from_scratch.sh deleted file mode 100755 index 466d504..0000000 --- a/build_from_scratch.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/bash -# -# Builds Bazel from scratch in $PWD/bazel/output/bazel. -# (tested on Raspbian & Debian with Bazel 0.8.0) -# -# Prerequisites: -# automake g++ libtool make curl git oracle-java8-jdk python unzip wget zip -# -set -eux - -git clone https://github.com/ochafik/bazel.git -b build-from-scratch --depth=1 -cd bazel - -pushd third_party/protobuf/3.4.0 - export PROTOC_DIST=$PWD/dist - mkdir -p $PROTOC_DIST - ./autogen.sh - ./configure --prefix=$PROTOC_DIST - make install - export PROTOC=$PROTOC_DIST/bin/protoc -popd - -pushd third_party/grpc/compiler/src/java_plugin/cpp - g++ -std=c++11 \ - -I$PROTOC_DIST/include \ - -w -O2 -export-dynamic \ - *.cpp $PROTOC_DIST/lib/libproto{c,buf}.a \ - -o protoc-gen-grpc-java - export GRPC_JAVA_PLUGIN=$PWD/protoc-gen-grpc-java -popd - -bash ./compile.sh