Merge pull request #1021 from justincormack/c-compile

Use docker run to compile C code not docker build
This commit is contained in:
Riyaz Faizullabhoy 2017-01-16 17:44:47 +00:00 committed by GitHub
commit 56d5a5a711
13 changed files with 143 additions and 73 deletions

View File

@ -0,0 +1,15 @@
FROM alpine:3.5
RUN \
apk update && apk upgrade && \
apk add \
curl \
gcc \
git \
libc-dev \
linux-headers \
util-linux-dev \
&& true
COPY compile.sh /usr/bin/
ENTRYPOINT ["/usr/bin/compile.sh"]

View File

@ -0,0 +1,29 @@
.PHONY: tag push
BASE=alpine:3.5
IMAGE=c-compile
default: push
hash: Dockerfile compile.sh
DOCKER_CONTENT_TRUST=1 docker pull $(BASE)
tar cf - $^ | docker build --no-cache -t $(IMAGE):build -
docker run --rm --entrypoint=/bin/sh $(IMAGE):build -c 'cat /lib/apk/db/installed /usr/bin/compile.sh | sha1sum' | sed 's/ .*//' > hash
push: hash
docker pull mobylinux/$(IMAGE):$(shell cat hash) || \
(docker tag $(IMAGE):build mobylinux/$(IMAGE):$(shell cat hash) && \
docker push mobylinux/$(IMAGE):$(shell cat hash))
docker rmi $(IMAGE):build
rm -f hash
tag: hash
docker pull mobylinux/$(IMAGE):$(shell cat hash) || \
docker tag $(IMAGE):build mobylinux/$(IMAGE):$(shell cat hash)
docker rmi $(IMAGE):build
rm -f hash
clean:
rm -f hash
.DELETE_ON_ERROR:

View File

@ -0,0 +1,56 @@
#!/bin/sh
# This is designed to compile a single package to a single binary
# so it makes some assumptions about things to simplify config
# to output a single binary (in a tarball) just use -o file
# use --docker to output a tarball for input to docker build -
set -e
usage() {
echo "Usage: -o file"
exit 1
}
[ $# = 0 ] && usage
while [ $# -gt 0 ]
do
flag="$1"
case "$flag" in
-o)
[ $# -eq 1 ] && usage
out="$2"
mkdir -p "$(dirname $2)"
shift
;;
-l*)
LIBS="$LIBS $1"
shift
;;
*)
echo "Unknown option $1"
exit 1
esac
shift
done
[ -z "$out" ] && usage
package=$(basename "$out")
dir="/src/$package"
mkdir -p $dir
# untar input
tar xf - -C $dir
(
cd $dir
CFILES=$(find . -name '*.c')
cc -static -O2 -Wall -Werror -o ../../$out $CFILES $LIBS
)
tar cf - $out
exit 0

View File

@ -1,10 +0,0 @@
# Tag: b77cfc4ad0033d4366df830ed697afc7bab458a2
FROM mobylinux/alpine-build-c@sha256:53739ea6042cb0ac39cf6e262012c1c4224206b2c9b719569fe7efa3a381348c
COPY . /9pmount-vsock
WORKDIR /9pmount-vsock
RUN cc -Wall -Werror 9pmount-vsock.c hvsock.c -lpthread -o 9pmount-vsock
CMD ["tar", "cf", "-", "9pmount-vsock"]

View File

@ -1,12 +1,13 @@
DEPS=Dockerfile $(wildcard *.c *.h)
# Tag: ac075fed7c87e4af30d8490ae0504166cceb0df3
C_COMPILE=mobylinux/c-compile@sha256:0e82d441ce112d638f904a08199c76b022c065a2dbf8908bb366755267d4417f
9pmount-vsock: $(DEPS)
mkdir -p sbin
BUILD=$$( tar cf - $(DEPS) | docker build -q - ) && \
[ -n "$$BUILD" ] && \
echo "Built $$BUILD" && \
docker run --rm --net=none $$BUILD | tar xf - -C sbin
default: sbin/9pmount-vsock
DEPS=$(wildcard *.c *.h)
sbin/9pmount-vsock: $(DEPS)
mkdir -p $(dir $@)
tar cf - $(DEPS) | docker run --rm --net=none --log-driver=none -i $(C_COMPILE) -o $@ -lpthread | tar xf -
clean:
rm -rf sbin

View File

@ -1,10 +0,0 @@
# Tag: b77cfc4ad0033d4366df830ed697afc7bab458a2
FROM mobylinux/alpine-build-c@sha256:53739ea6042cb0ac39cf6e262012c1c4224206b2c9b719569fe7efa3a381348c
COPY . /nc-vsock
WORKDIR /nc-vsock
RUN cc -Wall -Werror -o nc-vsock nc-vsock.c -luuid
CMD ["tar", "cf", "-", "nc-vsock"]

View File

@ -1,11 +1,13 @@
DEPS=Dockerfile $(wildcard *.c *.h)
# Tag: ac075fed7c87e4af30d8490ae0504166cceb0df3
C_COMPILE=mobylinux/c-compile@sha256:0e82d441ce112d638f904a08199c76b022c065a2dbf8908bb366755267d4417f
default: usr/bin/nc-vsock
DEPS=$(wildcard *.c *.h)
usr/bin/nc-vsock: $(DEPS)
mkdir -p usr/bin
BUILD=$$( tar cf - $(DEPS) | docker build -q - ) && \
[ -n "$$BUILD" ] && \
echo "Built $$BUILD" && \
docker run --rm --net=none $$BUILD | tar xf - -C usr/bin
mkdir -p $(dir $@)
tar cf - $(DEPS) | docker run --rm --net=none --log-driver=none -i $(C_COMPILE) -o $@ -luuid | tar xf -
clean:
rm -rf usr

View File

@ -193,7 +193,7 @@ static int hvsock_listen(const char *port_str)
static int tcp_connect(const char *node, const char *service)
{
int fd;
int fd = -1;
int ret;
const struct addrinfo hints = {
.ai_family = AF_INET,

View File

@ -1,10 +0,0 @@
# Tag: b77cfc4ad0033d4366df830ed697afc7bab458a2
FROM mobylinux/alpine-build-c@sha256:53739ea6042cb0ac39cf6e262012c1c4224206b2c9b719569fe7efa3a381348c
COPY . /tap-vsockd
WORKDIR /tap-vsockd
RUN cc -Wall -Werror tap-vsockd.c hvsock.c protocol.c -lpthread -o tap-vsockd
CMD ["tar", "cf", "-", "tap-vsockd"]

View File

@ -1,11 +1,13 @@
DEPS=Dockerfile $(wildcard *.c *.h)
# Tag: ac075fed7c87e4af30d8490ae0504166cceb0df3
C_COMPILE=mobylinux/c-compile@sha256:0e82d441ce112d638f904a08199c76b022c065a2dbf8908bb366755267d4417f
default: sbin/tap-vsockd
DEPS=$(wildcard *.c *.h)
sbin/tap-vsockd: $(DEPS)
mkdir -p sbin
BUILD=$$( tar cf - $(DEPS) | docker build -q - ) && \
[ -n "$$BUILD" ] && \
echo "Built $$BUILD" && \
docker run --rm --net=none $$BUILD | tar xf - -C sbin
mkdir -p $(dir $@)
tar cf - $(DEPS) | docker run --rm --net=none --log-driver=none -i $(C_COMPILE) -o $@ -lpthread | tar xf -
clean:
rm -rf sbin

View File

@ -1,10 +0,0 @@
# Tag: b77cfc4ad0033d4366df830ed697afc7bab458a2
FROM mobylinux/alpine-build-c@sha256:53739ea6042cb0ac39cf6e262012c1c4224206b2c9b719569fe7efa3a381348c
COPY . /transfused
WORKDIR /transfused
RUN cc -g -static -Wall -Werror -o transfused transfused.c transfused_log.c transfused_vsock.c
CMD ["tar", "cf", "-", "transfused"]

View File

@ -1,11 +1,13 @@
DEPS=Dockerfile $(wildcard *.c *.h)
# Tag: ac075fed7c87e4af30d8490ae0504166cceb0df3
C_COMPILE=mobylinux/c-compile@sha256:0e82d441ce112d638f904a08199c76b022c065a2dbf8908bb366755267d4417f
default: sbin/transfused
DEPS=$(wildcard *.c *.h)
sbin/transfused: $(DEPS)
mkdir -p sbin
BUILD=$$( tar cf - $(DEPS) | docker build -q - ) && \
[ -n "$$BUILD" ] && \
echo "Built $$BUILD" && \
docker run --rm --net=none $$BUILD | tar xf - -C sbin
mkdir -p $(dir $@)
tar cf - $(DEPS) | docker run --rm --net=none --log-driver=none -i $(C_COMPILE) -o $@ | tar xf -
clean:
rm -rf sbin

View File

@ -97,7 +97,7 @@ void unlock(char *const descr, pthread_mutex_t *mutex)
int bind_socket(const char *socket)
{
int sock;
int sock = -1;
if (socket[0] == 0)
die(2, NULL, NULL, "Socket family required");
@ -118,7 +118,7 @@ int bind_socket(const char *socket)
int connect_socket(const char *socket)
{
int sock;
int sock = -1;
if (socket[0] == 0)
die(2, NULL, NULL, "Socket family required");
@ -549,12 +549,15 @@ void start_writer(connection_t *connection, int fuse)
void negotiate_notify_channel(char *mount_point, int notify_sock)
{
int len = strlen(mount_point);
char hdr[6];
struct {
uint32_t len;
uint16_t channel;
} __attribute__((packed)) hdr;
*((uint32_t *)hdr) = 6 + len;
*((uint16_t *)(hdr + 4)) = TRANSFUSE_NOTIFY_CHANNEL;
hdr.len = 6 + len;
hdr.channel = TRANSFUSE_NOTIFY_CHANNEL;
write_exactly("negotiate_notify_channel hdr", notify_sock, hdr, 6);
write_exactly("negotiate_notify_channel hdr", notify_sock, &hdr, 6);
write_exactly("negotiate_notify_channel mnt",
notify_sock, mount_point, len);
}
@ -1087,7 +1090,7 @@ void serve(parameters *params)
char subproto_selector;
pthread_t child;
connection_t *conn;
void *(*connection_handler_thread)(void *);
void *(*connection_handler_thread)(void *) = NULL;
if (listen(params->data_sock, 16))
die(1, NULL, "listen", "");