Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
(maint) add default and parallel builds.
Browse files Browse the repository at this point in the history
  • Loading branch information
nanliu committed Oct 12, 2016
1 parent 63d33ca commit b73747e
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 34 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ install:
- cd $SNAP_SOURCE # change dir into source
- make
script:
- make check 2>&1 # Run test suite
- make test 2>&1 # Run test suite
notifications:
email: false
slack:
secure: VkbZLIc2RH8yf3PtIAxUNPdAu3rQQ7yQx0GcK124JhbEnZGaHyK615V0rbG7HcVmYKGPdB0cXqZiLBDKGqGKb2zR1NepOe1nF03jxGSpPq8jIFeEXSJGEYGL34ScDzZZGuG6qwbjFcXiW5lqn6t8igzp7v2+URYBaZo5ktCS2xY=
before_deploy:
- make all
- "./scripts/pre_deploy.sh"
deploy:
provider: s3
Expand Down
18 changes: 14 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ ARCH = $(shell uname -m)

default:
$(MAKE) deps
$(MAKE) all
$(MAKE) snap
$(MAKE) plugins
deps:
bash -c "./scripts/deps.sh"
test:
Expand All @@ -33,10 +34,19 @@ test-medium:
bash -c "./scripts/test.sh medium"
test-large:
bash -c "./scripts/test.sh large"
check:
$(MAKE) test
all:
# NOTE:
# By default compiles will use all cpu cores, use BUILD_JOBS to control number
# of parallel builds: `BUILD_JOBS=2 make plugins`
#
# Build only snapd/snapctl
snap:
bash -c "./scripts/build_snap.sh"
# Build only plugins
plugins:
bash -c "./scripts/build_plugins.sh"
# Build snap and plugins for all platforms
all:
bash -c "./scripts/build_all.sh"
install:
cp build/$(OS)/$(ARCH)/snapd /usr/local/bin/
cp build/$(OS)/$(ARCH)/snapctl /usr/local/bin/
Expand Down
2 changes: 1 addition & 1 deletion plugin/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func PluginPath() string {
arch = runtime.GOARCH
}

fpath := path.Join(BuildPath, runtime.GOOS, arch)
fpath := path.Join(BuildPath, runtime.GOOS, arch, "plugins")
return fpath
}

Expand Down
22 changes: 22 additions & 0 deletions scripts/build_all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

set -e
set -u
set -o pipefail

__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# shellcheck source=scripts/common.sh
. "${__dir}/common.sh"

export GOOS=linux
export GOARCH=amd64
"${__dir}/build_snap.sh" &
"${__dir}/build_plugins.sh" &

export GOOS=darwin
export GOARCH=amd64
"${__dir}/build_snap.sh" &
"${__dir}/build_plugins.sh" &

wait
11 changes: 7 additions & 4 deletions scripts/build_plugin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ __proj_dir="$(dirname "$__dir")"
# shellcheck source=scripts/common.sh
. "${__dir}/common.sh"

build_dir="${__proj_dir}/build"
plugin_dir="${build_dir}/${GOOS}/x86_64"
if [[ "${GOARCH}" == "amd64" ]]; then
build_dir="${__proj_dir}/build/${GOOS}/x86_64/plugins"
else
build_dir="${__proj_dir}/build/${GOOS}/${GOARCH}/plugins"
fi

plugin_src_path=$1
plugin_name=$(basename "${plugin_src_path}")
go_build=(go build -a -ldflags "-w")

_debug "plugin source: ${plugin_src_path}"
_info "building ${plugin_name}"
_info "building ${plugin_name} for ${GOOS}/${GOARCH}"

(cd "${plugin_src_path}" && "${go_build[@]}" -o "${plugin_dir}/${plugin_name}" . || exit 1)
(cd "${plugin_src_path}" && "${go_build[@]}" -o "${build_dir}/${plugin_name}" . || exit 1)
62 changes: 62 additions & 0 deletions scripts/build_plugins.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/bash

#http://www.apache.org/licenses/LICENSE-2.0.txt
#
#
#Copyright 2015 Intel Corporation
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.

set -e
set -u
set -o pipefail

__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__proj_dir="$(dirname "$__dir")"

# shellcheck source=scripts/common.sh
. "${__dir}/common.sh"

_info "project path: ${__proj_dir}"

git_version=$(_git_version)
go_build=(go build -ldflags "-w -X main.gitversion=${git_version}")

_info "git commit: $(git log --pretty=format:"%H" -1)"

# Disable CGO for builds.
export CGO_ENABLED=0

# rebuild binaries:
export GOOS=${GOOS:-$(uname -s | tr '[:upper:]' '[:lower:]')}
export GOARCH=${GOARCH:-"amd64"}

OS=$(uname -s)
if [[ "${OS}" == "Darwin" ]]; then
p=$(type -p sysctl > /dev/null && sysctl -n hw.ncpu || echo "1")
elif [[ "${OS}" == "Linux" ]]; then
p=$(type -p nproc > /dev/null && nproc || echo "1")
else
p="1"
fi
p=${BUILD_JOBS:-"${p}"}

if [[ "${GOARCH}" == "amd64" ]]; then
build_path="${__proj_dir}/build/${GOOS}/x86_64"
else
build_path="${__proj_dir}/build/${GOOS}/${GOARCH}"
fi

mkdir -p "${build_path}/plugins"
_info "building plugins for ${GOOS}/${GOARCH} in ${p} parallels"
find "${__proj_dir}/plugin/" -type d -iname "snap-*" -print0 | xargs -0 -n 1 -P $p -I{} "${__dir}/build_plugin.sh" {}
35 changes: 11 additions & 24 deletions scripts/build_snap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
#See the License for the specific language governing permissions and
#limitations under the License.

git_branch=$(git symbolic-ref HEAD 2> /dev/null | cut -b 12-)
git_branch="${git_branch:-test}"
git_sha=$(git log --pretty=format:"%h" -1)
git_version=$(git describe --always --exact-match 2> /dev/null || echo "${git_branch}-${git_sha}")

set -e
set -u
set -o pipefail
Expand All @@ -34,7 +29,7 @@ __proj_dir="$(dirname "$__dir")"

_info "project path: ${__proj_dir}"

build_path="${__proj_dir}/build"
git_version=$(_git_version)
go_build=(go build -ldflags "-w -X main.gitversion=${git_version}")

_info "snap build version: ${git_version}"
Expand All @@ -44,24 +39,16 @@ _info "git commit: $(git log --pretty=format:"%H" -1)"
export CGO_ENABLED=0

# rebuild binaries:
export GOOS=linux
export GOARCH=amd64
bin_path="${build_path}/${GOOS}/x86_64"
mkdir -p "${bin_path}"
_info "building snapd/snapctl for ${GOOS}/${GOARCH}"
"${go_build[@]}" -o "${bin_path}/snapd" . || exit 1
(cd "${__proj_dir}/cmd/snapctl" && "${go_build[@]}" -o "${bin_path}/snapctl" . || exit 1)
export GOOS=${GOOS:-$(uname -s | tr '[:upper:]' '[:lower:]')}
export GOARCH=${GOARCH:-"amd64"}

_info "building plugins for ${GOOS}/${GOARCH}"
find "${__proj_dir}/plugin/" -type d -iname "snap-*" -print0 | xargs -0 -n 1 -I{} "${__dir}/build_plugin.sh" {}
if [[ "${GOARCH}" == "amd64" ]]; then
build_path="${__proj_dir}/build/${GOOS}/x86_64"
else
build_path="${__proj_dir}/build/${GOOS}/${GOARCH}"
fi

export GOOS=darwin
export GOARCH=amd64
bin_path="${build_path}/${GOOS}/x86_64"
mkdir -p "${bin_path}"
mkdir -p "${build_path}"
_info "building snapd/snapctl for ${GOOS}/${GOARCH}"
"${go_build[@]}" -o "${bin_path}/snapd" . || exit 1
(cd "${__proj_dir}/cmd/snapctl" && "${go_build[@]}" -o "${bin_path}/snapctl" . || exit 1)

_info "building plugins for ${GOOS}/${GOARCH}"
find "${__proj_dir}/plugin/" -type d -iname "snap-*" -print0 | xargs -0 -n 1 -I{} "${__dir}/build_plugin.sh" {}
"${go_build[@]}" -o "${build_path}/snapd" . || exit 1
(cd "${__proj_dir}/cmd/snapctl" && "${go_build[@]}" -o "${build_path}/snapctl" . || exit 1)
8 changes: 8 additions & 0 deletions scripts/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,11 @@ _go_test() {
_go_cover() {
go tool cover -func "profile-${TEST_TYPE}.cov"
}

_git_version() {
git_branch=$(git symbolic-ref HEAD 2> /dev/null | cut -b 12-)
git_branch="${git_branch:-test}"
git_sha=$(git log --pretty=format:"%h" -1)
git_version=$(git describe --always --exact-match 2> /dev/null || echo "${git_branch}-${git_sha}")
echo "${git_version}"
}

0 comments on commit b73747e

Please sign in to comment.