From d318e68423b46b681b9b649ba4132658cc29a6a7 Mon Sep 17 00:00:00 2001 From: Adrian Macneil Date: Tue, 1 Dec 2015 17:36:23 -0800 Subject: [PATCH] Add cross compilation scripts --- .gitignore | 26 ++------------------------ Dockerfile | 36 ++++++++++++++++++++++++++++-------- Dockerfile.build | 32 ++++++++++++++++++++++++++++++++ Makefile | 7 +++++-- build.sh | 18 ++++++++++++++++++ 5 files changed, 85 insertions(+), 34 deletions(-) create mode 100644 Dockerfile.build create mode 100755 build.sh diff --git a/.gitignore b/.gitignore index daf913b..6acf881 100644 --- a/.gitignore +++ b/.gitignore @@ -1,24 +1,2 @@ -# Compiled Object files, Static and Dynamic libs (Shared Objects) -*.o -*.a -*.so - -# Folders -_obj -_test - -# Architecture specific extensions/prefixes -*.[568vq] -[568vq].out - -*.cgo1.go -*.cgo2.c -_cgo_defun.c -_cgo_gotypes.go -_cgo_export.* - -_testmain.go - -*.exe -*.test -*.prof +.DS_Store +/dist diff --git a/Dockerfile b/Dockerfile index fb90e0f..4190442 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,19 +1,39 @@ -FROM alpine:edge +FROM golang:1.5.1 -ENV GOPATH /go -ENV PATH /go/bin:$PATH +ENV CGO_ENABLED 1 -# install build dependencies -RUN echo "http://dl-4.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories && \ - apk add -U --no-progress alpine-sdk go go-tools +# i386 cross compilation +RUN dpkg --add-architecture i386 && \ + apt-get update && \ + apt-get install -y libc6-dev-i386 && \ + rm -rf /var/lib/apt/lists/* + +# osx cross compilation +# ref: https://github.com/karalabe/xgo/blob/master/docker/base/Dockerfile +ENV OSX_SDK_FILE MacOSX10.9.sdk.tar.xz +ENV OSX_SDK_SHA256 ac7ccfd8dee95d9811ce50cdc154c8ec7181c51652b5a434445475af62c4fedb +RUN cd /opt && \ + git clone https://github.com/tpoechtrager/osxcross.git && \ + cd osxcross && \ + sed -i -e 's|-march=native||g' ./build_clang.sh ./wrapper/build.sh && \ + apt-get update && \ + ./tools/get_dependencies.sh && \ + rm -rf /var/lib/apt/lists/* && \ + curl -fSL -o ./tarballs/$OSX_SDK_FILE \ + https://s3.amazonaws.com/andrew-osx-sdks/$OSX_SDK_FILE && \ + echo "$OSX_SDK_SHA256 ./tarballs/$OSX_SDK_FILE" | sha256sum -c - && \ + UNATTENDED=1 OSX_VERSION_MIN=10.6 ./build.sh +ENV PATH /opt/osxcross/target/bin:$PATH + +# development dependencies RUN go get \ github.com/golang/lint/golint \ github.com/kisielk/errcheck \ golang.org/x/tools/cmd/vet # copy source files -COPY . /go/src/github.com/adrianmacneil/dbmate -WORKDIR /go/src/github.com/adrianmacneil/dbmate +COPY . $GOPATH/src/github.com/adrianmacneil/dbmate +WORKDIR $GOPATH/src/github.com/adrianmacneil/dbmate # build RUN go get -d -t -v diff --git a/Dockerfile.build b/Dockerfile.build new file mode 100644 index 0000000..bebd1e3 --- /dev/null +++ b/Dockerfile.build @@ -0,0 +1,32 @@ +# this container is used for compiling dbmate against multiple targets +FROM golang:1.5.1 + +ENV CGO_ENABLED 1 + +# i386 cross compilation +RUN dpkg --add-architecture i386 && \ + apt-get update && \ + apt-get install -y libc6-dev-i386 && \ + rm -rf /var/lib/apt/lists/* + +# osx cross compilation +# ref: https://github.com/karalabe/xgo/blob/master/docker/base/Dockerfile +ENV OSX_SDK_VERSION 10.9 +RUN cd /opt && \ + git clone https://github.com/tpoechtrager/osxcross.git && \ + cd osxcross && \ + sed -i -e 's|-march=native||g' ./build_clang.sh ./wrapper/build.sh && \ + apt-get update && \ + ./tools/get_dependencies.sh && \ + rm -rf /var/lib/apt/lists/* && \ + curl -fSL -o ./tarballs/MacOSX${OSX_SDK_VERSION}.sdk.tar.xz \ + https://s3.amazonaws.com/andrew-osx-sdks/MacOSX${OSX_SDK_VERSION}.sdk.tar.xz && \ + UNATTENDED=1 OSX_VERSION_MIN=10.6 ./build.sh +ENV PATH /opt/osxcross/target/bin:$PATH + +# copy source files +COPY . $GOPATH/src/github.com/adrianmacneil/dbmate +WORKDIR $GOPATH/src/github.com/adrianmacneil/dbmate + +# fetch dependencies +RUN go get -d -t -v diff --git a/Makefile b/Makefile index c0c3fdf..cfff146 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ DOCKER := docker-compose run dbmate -all: build lint test +all: container lint test build -build: +container: docker-compose build lint: @@ -12,3 +12,6 @@ lint: test: $(DOCKER) go test -v + +build: + docker-compose run dbmate ./build.sh diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..6929b28 --- /dev/null +++ b/build.sh @@ -0,0 +1,18 @@ +#!/bin/sh +# this script is intended to run inside the docker container +# e.g. docker-compose run dbmate ./build.sh + +set -ex + +BUILD_FLAGS="-ldflags -s" + +rm -rf dist + +GOOS=linux GOARCH=386 go build $BUILD_FLAGS \ + -o dist/dbmate-linux-i386 +GOOS=linux GOARCH=amd64 go build $BUILD_FLAGS \ + -o dist/dbmate-linux-amd64 +GOOS=darwin GOARCH=386 CC=o32-clang CXX=o32-clang++ go build $BUILD_FLAGS \ + -o dist/dbmate-osx-i386 +GOOS=darwin GOARCH=amd64 CC=o64-clang CXX=o64-clang++ go build $BUILD_FLAGS \ + -o dist/dbmate-osx-amd64