Merge pull request #9 from brainstorm/dockcross

Dockcross with circle.yml test
This commit is contained in:
Matt McCormick 2016-05-31 23:21:10 -04:00
commit 140621f9c0
12 changed files with 396 additions and 31 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
dockcross

View File

@ -17,37 +17,46 @@ RUN apt-get update && apt-get -y install \
make \
ncurses-dev \
pkg-config \
libtool \
python \
rsync \
sed \
bison \
flex \
tar \
vim \
wget \
runit \
xz-utils && \
apt-get -y clean
# Build and install CMake from source.
WORKDIR /usr/src
RUN git clone git://cmake.org/cmake.git CMake && \
cd CMake && \
git checkout v3.4.3 && \
cd .. && mkdir CMake-build && cd CMake-build && \
/usr/src/CMake/bootstrap \
--parallel=$(nproc) \
--prefix=/usr && \
make -j$(nproc) && \
./bin/cmake -DCMAKE_USE_SYSTEM_CURL:BOOL=ON \
-DCMAKE_BUILD_TYPE:STRING=Release \
-DCMAKE_USE_OPENSSL:BOOL=ON . && \
make install && \
cd .. && \
rm -rf CMake*
cd CMake && \
git checkout v3.4.3 && \
cd .. && mkdir CMake-build && cd CMake-build && \
/usr/src/CMake/bootstrap \
--parallel=$(nproc) \
--prefix=/usr && \
make -j$(nproc) && \
./bin/cmake -DCMAKE_USE_SYSTEM_CURL:BOOL=ON \
-DCMAKE_BUILD_TYPE:STRING=Release \
-DCMAKE_USE_OPENSSL:BOOL=ON . && \
make install && \
cd .. && \
rm -rf CMake*
# Build and install Ninja from source
RUN git clone https://github.com/martine/ninja.git && \
cd ninja && \
git checkout v1.6.0 && \
python ./configure.py --bootstrap && \
./ninja && \
cp ./ninja /usr/bin/ && \
cd .. && rm -rf ninja
cd ninja && \
git checkout v1.6.0 && \
python ./configure.py --bootstrap && \
./ninja && \
cp ./ninja /usr/bin/ && \
cd .. && rm -rf ninja
WORKDIR /build
ENTRYPOINT ["/dockcross/entrypoint.sh"]
COPY imagefiles/entrypoint.sh imagefiles/dockcross /dockcross/

View File

@ -16,6 +16,9 @@ linux-x86:
linux-x64:
$(DOCKER) build -t $(IMAGE)-linux-x64 linux-x64
linux-armv5: base linux-armv5/Dockerfile linux-armv5/Toolchain.cmake
$(DOCKER) build -t $(IMAGE)-linux-armv5 linux-armv5
linux-armv6: base linux-armv6/Dockerfile linux-armv6/Toolchain.cmake
$(DOCKER) build -t $(IMAGE)-linux-armv6 linux-armv6
@ -34,6 +37,6 @@ windows-x64: base windows-x64/Dockerfile windows-x64/settings.mk
base: Dockerfile
$(DOCKER) build -t $(IMAGE)-base .
all: base android-arm darwin-x64 linux-x86 linux-x64 linux-armv6 linux-armv7 windows-x86 windows-x64
all: base android-arm darwin-x64 linux-x86 linux-x64 linux-armv5 linux-armv6 linux-armv7 windows-x86 windows-x64
.PHONY: all base android-arm darwin-x64 linux-x86 linux-x64 linux-armv6 linux-armv7 windows-x86 windows-x64
.PHONY: all base android-arm darwin-x64 linux-x86 linux-x64 linux-armv5 linux-armv6 linux-armv7 windows-x86 windows-x64

View File

@ -1,7 +1,91 @@
cross-compilers
===============
Dockerfiles for cross compiling environments
--------------------------------------------
---------------
Dockerfiles for cross compiling in a Docker container.
Features
========
* different toolchains for cross compiling
* commands in the container are run as the calling user, so that any created files have the expected ownership (ie. not root)
* make variables (`CC`, `LD` etc) are set to point to the appropriate tools in the container
* cmake and ninja are precompiled available with a toolchain file for cmake
* current directory is mounted as the container's workdir, `/build`
* works with boot2docker on OSX and Docker for Mac beta (1.11.1-beta12)
Installation
============
This image is not intended to be run manually. Instead, there is a helper script which comes bundled with the image.
To install the helper script, run the image with no arguments, and redirect the output to a file::
docker run CROSS_COMPILER_IMAGE_NAME > dockcross
chmod +x dockcross
mv dockcross ~/bin/
Usage
=====
For the impatient, here's a one-liner to compile a hello world for armv7::
docker run thewtex/cross-compiler-linux-armv7 > dockcross && chmod +x dockcross && ./dockcross gcc test/C/hello.c -o hello_arm
Note how invoking any toolchain command (make, gcc, etc...) is just a matter of prepending **dockcross** in the commandline:
`dockcross [command] [args...]`
Dockcross will thus execute the given command-line inside the container, along with all arguments passed after the command.
Alternatively, if the command matches one of the dockcross built-in commands (see below), that will be executed locally.
Built-in commands
=================
- `dockcross -- [command] [args...]`: Forces a command to run inside the container (in case of a name clash with a built-in command), use `--` before the command.
- `dockcross update-image`: Fetch the latest version of the docker image.
- `dockcross update-script`: Update the installed dockcross script with the one bundled in the image.
- `dockcross update`: Update both the docker image, and the dockcross script.
Configuration
=============
The following command-line options and environment variables are used. In all cases, the command-line option overrides the environment variable.
### DOCKCROSS_CONFIG / --config <path-to-config-file>
This file is sourced if it exists.
Default: `~/.dockcross`
### DOCKCROSS_IMAGE / --image <docker-image-name>
The docker image to run.
Default: thewtex/cross-compiler-linux-armv7
### DOCKCROSS_ARGS / --args <docker-run-args>
Extra arguments to pass to the `docker run` command.
Examples
========
1. **dockcross make**: Build the Makefile in the current directory.
2. **dockcross cmake -Bbuild -H. -GNinja***: Run CMake with a build directory "build" for the CMakeLists.txt in the current directory and generate `ninja` files.
3. **dockcross ninja -Cbuild**: Run ninja in the generated build directory.
4. **dockcross bash -c 'find . -name \*.o | sort > objects.txt'**
Note that commands are executed verbatim. If you require any shell processing for environment variable expansion or redirection, please use `bash -c 'command args...'`.
---
Credits go to `sdt/docker-raspberry-pi-cross-compiler <https://github.com/sdt/docker-raspberry-pi-cross-compiler>`_, who invented the base of this **dockcross** script.
CI status
---------
.. image:: https://circleci.com/gh/thewtex/cross-compilers/tree/master.svg?style=svg
:target: https://circleci.com/gh/thewtex/cross-compilers/tree/master

View File

@ -1,4 +1,8 @@
machine:
# XXX: btrfs circleCI fix, see: https://github.com/docker/docker/issues/9939 and https://github.com/Sabayon/docker-stage3-base-amd64/commit/8c1bf737113a278dd2f
pre:
- sudo curl -L -o /usr/bin/docker 'https://s3-external-1.amazonaws.com/circle-downloads/docker-1.9.0-circleci'
- sudo chmod 0755 /usr/bin/docker
services:
- docker
@ -40,6 +44,7 @@ test:
- make windows-x86:
timeout: 3000
- docker run --rm -v ~/cross-compilers/test/:/usr/src/test:ro thewtex/cross-compiler-windows-x86 python /usr/src/test/run.py --emulator /usr/bin/wine --exe-suffix ".exe"
- docker run thewtex/cross-compiler-linux-armv7 > dockcross && chmod +x dockcross && ./dockcross gcc test/C/hello.c -o hello_arm
deployment:
hub:

188
imagefiles/dockcross Executable file
View File

@ -0,0 +1,188 @@
#!/bin/bash
#------------------------------------------------------------------------------
# Helpers
#
err() {
echo -e >&2 ERROR: $@\\n
}
die() {
err $@
exit 1
}
has() {
# eg. has command update
local kind=$1
local name=$2
type -t $kind:$name | grep -q function
}
#------------------------------------------------------------------------------
# Command handlers
#
command:update-image() {
docker pull $FINAL_IMAGE
}
help:update-image() {
echo Pull the latest $FINAL_IMAGE .
}
command:update-script() {
if cmp -s <( docker run $FINAL_IMAGE ) $0; then
echo $0 is up to date
else
echo -n Updating $0 '... '
docker run $FINAL_IMAGE > $0 && echo ok
fi
}
help:update-image() {
echo Update $0 from $FINAL_IMAGE .
}
command:update() {
command:update-image
command:update-script
}
help:update() {
echo Pull the latest $FINAL_IMAGE, and then update $0 from that.
}
command:help() {
if [[ $# != 0 ]]; then
if ! has command $1; then
err \"$1\" is not an dockcross command
command:help
elif ! has help $1; then
err No help found for \"$1\"
else
help:$1
fi
else
cat >&2 <<ENDHELP
usage: dockcross command [args]
By default, runs the given command in an dockcross container.
Builtin commands:
update-image
update-script
update
For command help use: $0 help <command>
ENDHELP
fi
}
#------------------------------------------------------------------------------
# Option processing
#
while [[ $# != 0 ]]; do
case $1 in
--)
break
;;
--args)
ARG_ARGS="$2"
shift 2
;;
--config)
ARG_CONFIG="$2"
shift 2
;;
--image)
ARG_IMAGE="$2"
shift 2
;;
-*)
err Unknown option \"$1\"
command:help
exit
;;
*)
break
;;
esac
done
# The precedence for options is:
# 1. command-line arguments
# 2. environment variables
# 3. defaults
# Source the config file if it exists
DEFAULT_CONFIG=~/.dockcross
FINAL_CONFIG=${ARG_CONFIG-${DOCKCROSS_CONFIG-$DEFAULT_CONFIG}}
[[ -f "$FINAL_CONFIG" ]] && source "$FINAL_CONFIG"
# Set the docker image
DEFAULT_IMAGE=thewtex/cross-compiler-base
FINAL_IMAGE=${ARG_IMAGE-${DOCKCROSS_IMAGE-$DEFAULT_IMAGE}}
# Set the docker run extra args (if any)
FINAL_ARGS=${ARG_ARGS-${DOCKCROSS_ARGS}}
# If we are not running via boot2docker
if [ -z $DOCKER_HOST ]; then
USER_IDS="-e BUILDER_UID=$( id -u ) -e BUILDER_GID=$( id -g )"
fi
#------------------------------------------------------------------------------
# Command-line processing
#
if [[ $# == 0 ]]; then
command:help
exit
fi
case $1 in
--)
# Everything after this is the command-line to be executed
shift
;;
*)
# If this is a builtin command, execute it, otherwise fall through
if has command $1; then
command:$1 "${@:2}" # array slice skipping first element
exit $?
fi
;;
esac
#------------------------------------------------------------------------------
# Now, finally, run the command in a container
#
docker run -i -t --rm \
-v $PWD:/build \
$USER_IDS \
$FINAL_ARGS \
$FINAL_IMAGE "$@"
################################################################################
#
# This image is not intended to be run manually.
#
# To install the dockcross helper, run the following commands:
#
# docker run thewtex/cross-compiler-linux-armv7 > dockcross
# chmod +x dockcross
#
# You may then wish to move dockcross to somewhere in your path.
#
################################################################################

30
imagefiles/entrypoint.sh Executable file
View File

@ -0,0 +1,30 @@
#!/bin/bash
# This is the entrypoint script for the dockerfile. Executed in the
# container at runtime.
if [[ $# == 0 ]]; then
# Presumably the image has been run directly, so help the user get started.
cat /dockcross/dockcross
exit 0
fi
# If we are running docker natively, we want to create a user in the container
# with the same UID and GID as the user on the host machine, so that any files
# created are owned by that user. Without this they are all owned by root.
# If we are running from boot2docker, this is not necessary.
# The dockcross script sets the BUILDER_UID and BUILDER_GID vars.
if [[ -n $BUILDER_UID ]] && [[ -n $BUILDER_GID ]]; then
BUILDER_USER=dockcross-user
BUILDER_GROUP=dockcross-group
groupadd -o -g $BUILDER_GID $BUILDER_GROUP 2> /dev/null
useradd -o -g $BUILDER_GID -u $BUILDER_UID $BUILDER_USER 2> /dev/null
# Run the command as the specified user/group.
exec chpst -u :$BUILDER_UID:$BUILDER_GID "$@"
else
# Just run the command as root.
exec "$@"
fi

25
linux-armv5/Dockerfile Normal file
View File

@ -0,0 +1,25 @@
FROM thewtex/cross-compiler-base
MAINTAINER Matt McCormick "matt.mccormick@kitware.com"
# This is for ARMv5 "legacy" devices which do not support hard float VFP instructions.
# https://wiki.debian.org/CrossToolchains
RUN dpkg --add-architecture armel && \
apt-get update && \
apt-get install -y crossbuild-essential-armel
# The cross-compiling emulator
RUN apt-get update && apt-get install -y \
qemu-user \
qemu-user-static
ENV CROSS_TRIPLE arm-linux-gnueabi
ENV CROSS_ROOT /usr/bin
ENV AS=/usr/bin/${CROSS_TRIPLE}-as \
AR=/usr/bin/${CROSS_TRIPLE}-ar \
CC=/usr/bin/${CROSS_TRIPLE}-gcc \
CPP=/usr/bin/${CROSS_TRIPLE}-cpp \
CXX=/usr/bin/${CROSS_TRIPLE}-g++ \
LD=/usr/bin/${CROSS_TRIPLE}-ld
ENV QEMU_LD_PREFIX ${CROSS_ROOT}/libc
ENV QEMU_SET_ENV "LD_LIBRARY_PATH=${CROSS_ROOT}/lib:${CROSS_ROOT}/libc/lib/${CROSS_TRIPLE}/"

View File

@ -0,0 +1,16 @@
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(cross_triple "arm-linux-gnueabi")
set(CMAKE_C_COMPILER /usr/bin/${cross_triple}-gcc)
set(CMAKE_CXX_COMPILER /usr/bin/${cross_triple}-g++)
set(CMAKE_Fortran_COMPILER /usr/bin/${cross_triple}-gfortran)
set(CMAKE_FIND_ROOT_PATH /usr/${cross_triple} /usr/${cross_triple}/libc/usr)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_CROSSCOMPILING_EMULATOR /usr/bin/qemu-arm)

View File

@ -27,10 +27,14 @@ ENV AS=/usr/bin/${CROSS_TRIPLE}-as \
# of performance.
# See: https://wiki.debian.org/RaspberryPi
# We are also using the 4.7 version of the toolchain, so that glibc=2.13
ENV RASPBERRYPI_TOOLS_COMMIT 9c3d7b6ac692498dd36fec2872e0b55f910baac1
RUN curl -L https://github.com/raspberrypi/tools/archive/${RASPBERRYPI_TOOLS_COMMIT}.tar.gz | tar xvz --wildcards --no-anchored "*gcc-linaro-${CROSS_TRIPLE}-raspbian*" && \
rsync -av /usr/src/tools-${RASPBERRYPI_TOOLS_COMMIT}/arm-bcm2708/gcc-linaro-${CROSS_TRIPLE}-raspbian/ /usr/ && \
rm -rf /usr/src/tools-${RASPBERRYPI_TOOLS_COMMIT}
# Instead of cloning the whole repo (>1GB at the of writing this), we want to do a so-called "sparse checkout" with "shallow cloning":
# https://stackoverflow.com/questions/600079/is-there-any-way-to-clone-a-git-repositorys-sub-directory-only/13738951#13738951
RUN mkdir rpi_tools && cd rpi_tools && git init && git remote add -f origin https://github.com/raspberrypi/tools && \
git config core.sparseCheckout true && echo "arm-bcm2708/gcc-linaro-${CROSS_TRIPLE}-raspbian" >> .git/info/sparse-checkout && \
git pull --depth=1 origin master && rsync -av arm-bcm2708/gcc-linaro-${CROSS_TRIPLE}-raspbian/ /usr/ && rm -rf ../rpi_tools
# Allow dynamically linked executables to run with qemu-arm
ENV QEMU_LD_PREFIX ${CROSS_ROOT}/libc
ENV QEMU_SET_ENV "LD_LIBRARY_PATH=${CROSS_ROOT}/lib:${CROSS_ROOT}/libc/lib/${CROSS_TRIPLE}/"

View File

@ -25,7 +25,7 @@ ENV AS=/usr/bin/${CROSS_TRIPLE}-as \
CXX=/usr/bin/${CROSS_TRIPLE}-g++ \
LD=/usr/bin/${CROSS_TRIPLE}-ld
WORKDIR /usr/src
WORKDIR /build
# Note: Toolchain file support is currently in debian Experimental:
# https://wiki.debian.org/CrossToolchains#In_jessie_.28Debian_8.29

View File

@ -1,2 +1,2 @@
#!/bin/bash
exec ${0/${CROSS_TRIPLE}-/x86_64-linux-gnu-} -m32 "$@"
exec ${0/*${TOOLCHAIN}-/\/usr\/bin\/x86_64-linux-gnu-} -m32 "$@"