From 60b0dc3b05b4531f64249ab122a59df4e8cb3cb2 Mon Sep 17 00:00:00 2001 From: Bassam Tabbara Date: Wed, 28 Sep 2016 16:15:42 -0700 Subject: [PATCH] Dependency checking in Makefile and other improvements Makefile now checks go dependencies and only builds what is needed. Refactored the Makefile into three parts for better maintenance. Add a buildall target that is the initial attempt at cross compiling castlectl. --- .gitignore | 8 +- Makefile | 228 +++++++++--------- build/build-container/Dockerfile | 59 ----- build/cephd.mk | 85 +++++++ build/cross-build/Dockerfile.in | 57 +++++ build/cross-build/Makefile | 21 ++ build/cross-build/entrypoint.sh | 19 ++ build/cross-build/toolchain.linux_amd64.cmake | 0 build/cross-build/toolchain.linux_arm64.cmake | 13 + build/golang.mk | 181 ++++++++++++++ build/publish-build-container.sh | 7 - build/run-build.sh | 23 -- build/run-cross-build | 41 ++++ circle.yml | 3 +- pkg/cephmgr/cephd/cephd.go | 6 +- 15 files changed, 536 insertions(+), 215 deletions(-) mode change 100755 => 100644 Makefile delete mode 100644 build/build-container/Dockerfile create mode 100644 build/cephd.mk create mode 100644 build/cross-build/Dockerfile.in create mode 100644 build/cross-build/Makefile create mode 100755 build/cross-build/entrypoint.sh create mode 100644 build/cross-build/toolchain.linux_amd64.cmake create mode 100644 build/cross-build/toolchain.linux_arm64.cmake create mode 100644 build/golang.mk delete mode 100755 build/publish-build-container.sh delete mode 100755 build/run-build.sh create mode 100755 build/run-cross-build diff --git a/.gitignore b/.gitignore index 409cf4502..839a4f4c6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,11 @@ +.glide/ +.gowork/ +.vscode/ + bin/ tools/ vendor/ -.vscode/ -.glide/ -.gopath/ + coverage.out coverage.html debug.test diff --git a/Makefile b/Makefile old mode 100755 new mode 100644 index 99d259971..9eec1f617 --- a/Makefile +++ b/Makefile @@ -1,46 +1,23 @@ -ORG = github.com/quantum -PROJ = castle -GOPATH = $(CURDIR)/.gopath -ORGPATH = $(GOPATH)/src/$(ORG) -REPOPATH = $(ORGPATH)/$(PROJ) -GOOS = $(shell go env GOOS) -GOARCH = $(shell go env GOARCH) +.PHONY: all +all: build -export GOPATH - -# TODO: support for profiling in ceph -# TODO: support for tracing in ceph -# TODO: jemalloc and static linking are currently broken due to https://github.com/jemalloc/jemalloc/issues/442 -# TODO: remove leveldb from ceph -# TODO: cross compile castlectl? -# TODO: can we strip -s all binaries? +# ==================================================================================== +# Build Options # Can be used for additional go build flags BUILDFLAGS ?= LDFLAGS ?= -TAGS ?= - -# Additional flags to use when calling cmake on ceph -CEPHD_CMAKE += \ - -DWITH_EMBEDDED=ON \ - -DWITH_FUSE=OFF \ - -DWITH_NSS=OFF \ - -DUSE_CRYPTOPP=ON \ - -DWITH_LEVELDB=OFF \ - -DWITH_XFS=OFF \ - -DWITH_OPENLDAP=OFF \ - -DWITH_MANPAGE=OFF \ - -DWITH_PROFILER=OFF +TAGS ?= # set to 1 for a completely static build. Otherwise if set to 0 # a dynamic binary is produced that requires glibc to be installed STATIC ?= 1 ifeq ($(STATIC),1) -BUILDFLAGS += -installsuffix cgo -LDFLAGS += -extldflags "-static" -TAGS += static +CASTLED_BUILDFLAGS += -installsuffix cgo +CASTLED_LDFLAGS += -extldflags "-static" +CASTLED_TAGS += static else -TAGS += dynamic +CASTLED_TAGS += dynamic endif # build a position independent executable. This implies dynamic linking @@ -50,31 +27,16 @@ ifeq ($(PIE),1) ifeq ($(STATIC),1) $(error PIE only supported with dynamic linking. Set STATIC=0.) endif -BUILDFLAGS += -buildmode=pie -TAGS += pie +CASTLED_BUILDFLAGS += -buildmode=pie +CASTLED_TAGS += pie endif -# Can be jemalloc, tcmalloc or glibc -ALLOCATOR ?= tcmalloc -ifeq ($(ALLOCATOR),jemalloc) -CEPHD_CMAKE += -DALLOCATOR=jemalloc -TAGS += jemalloc -else -ifeq ($(ALLOCATOR),tcmalloc) -CEPHD_CMAKE += -DALLOCATOR=tcmalloc -TAGS += tcmalloc -else -CEPHD_CMAKE += -DALLOCATOR=libc -endif -endif - -# additional go build tags -TAGS ?= - # if DEBUG is set to 1 debug information is perserved (i.e. not stripped). # the binary size is going to be much larger. DEBUG ?= 0 +CCACHE ?= 1 + # turn on more verbose build V ?= 0 ifeq ($(V),1) @@ -84,94 +46,120 @@ else MAKEFLAGS += --no-print-directory endif -# set to 0 to disable ccache support -# must also export CCACHE_DIR to take effect -CCACHE ?= 1 -ifeq ($(CCACHE),1) -CEPHD_CMAKE += -DWITH_CCACHE=ON -endif - # set the version number. ifeq ($(origin VERSION), undefined) VERSION = $(shell git describe --dirty --always) endif -LDFLAGS += -X $(ORG)/$(PROJ)/pkg/version.Version=$(VERSION) +LDFLAGS += -X $(REPO)/pkg/version.Version=$(VERSION) -GO_SOURCEDIRS=cmd pkg -GO_SOURCES := $(shell find $(GO_SOURCEDIRS) -name '*.go') -GO_PKGS=$(shell go list ./... | grep --invert-match vendor) +# ==================================================================================== +# Setup Go projects -.PHONY: all -all: build +GO_ORG=github.com/quantum +GO_PROJ=castle +include build/golang.mk -.PHONY: build -build: bin/castled bin/castlectl vet fmt +# add castlectl and castled. this will define all the necessary +# rules to build the projects and also set targets such as build, clean, etc. +$(eval $(call golang-project,castlectl,cmd/castlectl, \ + $(BUILDFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)')) +$(eval $(call golang-project,castled,cmd/castled, \ + $(BUILDFLAGS) $(CASTLED_BUILDFLAGS) -tags '$(TAGS) $(CASTLED_TAGS)' -ldflags '$(LDFLAGS) $(CASTLED_LDFLAGS)')) -.PHONY: release -release: build +# ==================================================================================== +# Setup Cephd -.PHONY: check -check: test +CEPHD_DEBUG = $(DEBUG) +CEPHD_CCACHE = $(CCACHE) +CEPHD_ALLOCATOR = $(ALLOCATOR) +include build/cephd.mk -$(REPOPATH): - @mkdir -p $(ORGPATH) - @ln -s ../../../.. $(REPOPATH) +# ==================================================================================== +# Targets -bin/castled: $(REPOPATH) vendor/vendor.stamp ceph/build $(GO_SOURCES) - @cd ceph/build && $(MAKE) cephd - @if test ceph/build/lib/libcephd.a -nt bin/castled -o ! -f bin/castled; then \ - if test $(DEBUG) -eq 0; then \ - strip -S ceph/build/lib/libcephd.a; \ - fi; \ - go build $(BUILDFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)' -o bin/castled ./cmd/castled; \ - fi +.PHONY: build test check vet fmt clean buildall cleanall -bin/castlectl: $(REPOPATH) vendor/vendor.stamp $(GO_SOURCES) - @go build $(BUILDFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)' -o bin/castlectl ./cmd/castlectl +build: go.build.castlectl -# we take a dependency on this Makefile since the ceph cmake config is in it -ceph/build: Makefile - @mkdir -p ceph/build - @cd ceph/build && cmake $(CEPHD_CMAKE) .. +check test: go.test.castlectl -.PHONY: vendor -vendor: tools/glide - @./tools/glide install +vet: go.vet.castlectl go.vet.castled -# the stamp file ensures that we run glide install once. every other time is manual -vendor/vendor.stamp: tools/glide - @./tools/glide install - @touch $@ +fmt: go.fmt.castlectl go.fmt.castled -.PHONY: test -test: build - @go test -cover -timeout 30s $(BUILDFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)' $(GO_PKGS) +clean: go.clean.castlectl go.clean.castled -.PHONY: vet -vet: $(REPOPATH) tools/glide $(GO_SOURCES) - @go vet $(shell ./tools/glide novendor) +cleanall: go.cleanall cephd.cleanall -.PHONY: fmt -fmt: $(REPOPATH) tools/glide $(GO_SOURCES) - @go fmt $(shell ./tools/glide novendor) +buildall: \ + go.build.castlectl.linux_amd64 \ + go.build.castlectl.linux_arm64 \ + go.build.castlectl.darwin_amd64 \ + go.build.castlectl.windows_amd64 -.PHONY: cleancommon -cleancommon: - @rm -rf bin - @rm -fr $(GOPATH) - @if [ -d ceph/src/rocksdb ]; then cd ceph/src/rocksdb && $(MAKE) clean > /dev/null; fi +ifeq ($(GO_HOST_OS),linux) +ifeq ($(GO_HOST_ARCH),amd64) -.PHONY: clean -clean: cleancommon - @if [ -d ceph/build ]; then cd ceph/build && $(MAKE) clean; fi +$(eval $(call cephd-build,linux_amd64)) -.PHONY: cleanall -cleanall: cleancommon - @rm -rf tools vendor - @rm -rf ceph/build +go.test.castled bin/linux_amd64/castled: $(CEPHD_BUILD_DIR)/linux_amd64/lib/libcephd.a -tools/glide: - @mkdir -p tools - @curl -sL https://github.com/Masterminds/glide/releases/download/v0.12.2/glide-v0.12.2-$(GOOS)-$(GOARCH).tar.gz | tar -xz -C tools - @mv tools/$(GOOS)-$(GOARCH)/glide tools/glide - @rm -r tools/$(GOOS)-$(GOARCH) +clean: cephd.clean.linux_amd64 + +build: bin/linux_amd64/castled + +check test: go.test.castled + +buildall: bin/linux_amd64/castled + +else +ifeq ($(GO_HOST_ARCH),arm64) + +$(eval $(call cephd-build,linux_arm64)) + +go.test.castled bin/linux_arm64/castled: $(CEPHD_BUILD_DIR)/linux_arm64/lib/libcephd.a + +clean: cephd.clean.linux_arm64 + +build: bin/linux_arm64/castled + +check test: go.test.castled + +buildall: bin/linux_arm64/castled + +endif # arm64 +endif # amd64 +endif # linux + +# ==================================================================================== +# Help + +.PHONY: help +help: + @echo 'Usage: make ... ' + @echo '' + @echo 'Targets:' + @echo ' build Build project.' + @echo ' buildall Build project for all platforms.' + @echo ' check Builds and runs tests on.' + @echo ' clean Remove binaries.' + @echo ' cleanall Remove binaries for all platforms.' + @echo ' fmt Check formatting of go sources.' + @echo ' help Show this help info.' + @echo ' vendor Installs vendor dependencies.' + @echo ' vet Runs lint checks on go sources.' + @echo '' + @echo 'Options:' + @echo '' + @echo ' CCACHE Set to 1 to enabled ccache, 0 to disable.' + @echo ' The default is 0.' + @echo ' DEBUG Set to 1 to disable stripping the binaries of.' + @echo ' debug information. The default is 0.' + @echo ' PIE Set to 1 to build build a position independent' + @echo ' executable. Can not be combined with static.' + @echo ' The default is 0.' + @echo ' STATIC Set to 1 for static build, 0 for dynamic.' + @echo ' The default is 1.' + @echo ' VERSION The version information compiled into binaries.' + @echo ' The default is obtained from git.' + @echo ' V Set to 1 enable verbose build. Default is 0.' diff --git a/build/build-container/Dockerfile b/build/build-container/Dockerfile deleted file mode 100644 index 67503c3f0..000000000 --- a/build/build-container/Dockerfile +++ /dev/null @@ -1,59 +0,0 @@ -FROM ubuntu:xenial -MAINTAINER Bassam Tabbara - -# git version 2.6.6+ through 2.8.3 had a bug with submodules -# see https://github.com/git/git/blob/master/Documentation/RelNotes/2.8.3.txt#L33 -# this is why we are adding a newer verion of git than is available on xenial - -RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E1DF1F24 && \ - echo "deb http://ppa.launchpad.net/git-core/ppa/ubuntu xenial main" > /etc/apt/sources.list.d/git.list && \ - apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -yq \ - build-essential \ - ca-certificates \ - ccache \ - cmake \ - curl \ - cython \ - cython3 \ - git \ - golang-1.6 \ - libaio-dev \ - libatomic-ops-dev \ - libblkid-dev \ - libboost-date-time-dev \ - libboost-dev \ - libboost-iostreams-dev \ - libboost-program-options-dev \ - libboost-random-dev \ - libboost-regex-dev \ - libboost-system-dev \ - libboost-thread-dev \ - libcrypto++-dev \ - libcurl4-openssl-dev \ - libexpat1-dev \ - libfcgi-dev \ - libgoogle-perftools-dev \ - libjemalloc-dev \ - libkeyutils-dev \ - libldap2-dev \ - libleveldb-dev \ - libsnappy-dev \ - libssl-dev \ - libudev-dev \ - mercurial \ - python-sphinx \ - python-dev \ - python3-all-dev \ - xfslibs-dev \ - yasm \ - --no-install-recommends && \ - apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* - -ENV GOPATH /go -ENV PATH $GOPATH/bin:/usr/lib/go-1.6/bin:$PATH -RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH" - -VOLUME $GOPATH/src/github.com/quantum/castle -WORKDIR $GOPATH/src/github.com/quantum/castle - -ENTRYPOINT [ "/bin/bash", "-c" ] diff --git a/build/cephd.mk b/build/cephd.mk new file mode 100644 index 000000000..e6b11103c --- /dev/null +++ b/build/cephd.mk @@ -0,0 +1,85 @@ +# ==================================================================================== +# Makefile helper functions for building embedded ceph + +# TODO: support for profiling in ceph +# TODO: support for tracing in ceph +# TODO: jemalloc and static linking are currently broken due to https://github.com/jemalloc/jemalloc/issues/442 +# TODO: remove leveldb from ceph +# TODO: rocksdb build_version always building +# TODO: rocksdb should not build from source tree + +# ==================================================================================== +# Configuration + +CEPHD_ALLOCATOR ?= tcmalloc +CEPHD_CCACHE ?= 1 +CEPHD_DEBUG ?= 0 +CEPHD_DIR ?= ceph +CEPHD_BUILD_DIR ?= ceph/build + +# ==================================================================================== +# Configuration + +CEPHD_CMAKE += \ + -DWITH_EMBEDDED=ON \ + -DALLOCATOR=$(CEPHD_ALLOCATOR) \ + -DWITH_FUSE=OFF \ + -DWITH_NSS=OFF \ + -DUSE_CRYPTOPP=ON \ + -DWITH_LEVELDB=OFF \ + -DWITH_XFS=OFF \ + -DWITH_OPENLDAP=OFF \ + -DWITH_MANPAGE=OFF \ + -DWITH_PROFILER=OFF \ + -DCMAKE_TARGET_MESSAGES=OFF \ + +ifeq ($(CEPHD_CCACHE),1) +CEPHD_CMAKE += -DWITH_CCACHE=ON +endif + +# poor man's dependencies. this is a notriously hard problem with recursive make. the +# compromise here is good enough. For release builds we always build clean so this +# not be an issue. For developer build, the worst that can happen is we miss a changed +# file and libcephd.a does not build. rm libcephd.a or touching ceph/build/*/Makefile +# would fix that. +CEPHD_SOURCES := $(shell find ceph/src -name .git -prune -o -type f | sed 's/ /\\ /g') + +# ==================================================================================== +# Targets + +# build cephd for the specified platform +# $1 platform name (for example castlectl) +define cephd-build + +$(CEPHD_BUILD_DIR)/$(1)/Makefile: + @mkdir -p $$(@D) + @echo "$(CEPHD_CMAKE)" > $$(@D)/cephd.cmake.new + @if test ! -f $$(@D)/cephd.cmake || ! diff $$(@D)/cephd.cmake.new $$(@D)/cephd.cmake > /dev/null; then \ + echo "====== Configuring cephd $(1)"; \ + cd $$(@D) && cmake $(CEPHD_CMAKE) -DCMAKE_TOOLCHAIN_FILE=../../../build/cross-build/toolchain.$(1).cmake ../..; \ + echo "$(CEPHD_CMAKE)" > cephd.cmake; \ + fi + @rm $$(@D)/cephd.cmake.new + +$(CEPHD_BUILD_DIR)/$(1)/lib/libcephd.a: $(CEPHD_BUILD_DIR)/$(1)/Makefile $(CEPHD_SOURCES) + @echo "====== Building cephd $(1)" + @cd $(CEPHD_BUILD_DIR)/$(1) && $$(MAKE) cephd +ifeq ($(CEPHD_DEBUG),0) + @strip -pS $$@ +endif + +.PHONY: cephd.clean.$(1) +cephd.clean.$(1): cephd.clean.rocksdb + @echo "====== Running make clean on cephd $(1)" + @ [ -d $(CEPHD_BUILD_DIR)/$(1) ] && cd $(CEPHD_BUILD_DIR)/$(1) && $(MAKE) clean +endef + +# BUGBUG: rocksdb builds out of the source tree which means arm64 and amd64 +# binaries will overwrite each other +.PHONY: cephd.clean.rocksdb +cephd.clean.rocksdb: + @ [ -d $(CEPHD_BUILD_DIR)/$(1) ] && cd ceph/src/rocksdb && $(MAKE) clean > /dev/null + +.PHONY: cephd.clean +cephd.cleanall: cephd.clean.rocksdb + @rm -rf $(CEPHD_BUILD_DIR) diff --git a/build/cross-build/Dockerfile.in b/build/cross-build/Dockerfile.in new file mode 100644 index 000000000..d42b8fd25 --- /dev/null +++ b/build/cross-build/Dockerfile.in @@ -0,0 +1,57 @@ +FROM debian:stretch +MAINTAINER Bassam Tabbara + +RUN \ + dpkg --add-architecture ==ARCH== && \ + apt-get update && \ + DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \ + \ + ca-certificates \ + ccache \ + cmake \ + curl \ + git \ + golang \ + mercurial \ + python \ + python-dev \ + python3 \ + python3-all-dev \ + cython \ + cython3 \ + runit \ + sudo \ + yasm \ + ==BUILD== \ + \ + libaio-dev:==ARCH== \ + libatomic-ops-dev:==ARCH== \ + libblkid-dev:==ARCH== \ + libboost-date-time-dev:==ARCH== \ + libboost-dev:==ARCH== \ + libboost-iostreams-dev:==ARCH== \ + libboost-program-options-dev:==ARCH== \ + libboost-random-dev:==ARCH== \ + libboost-regex-dev:==ARCH== \ + libboost-system-dev:==ARCH== \ + libboost-thread-dev:==ARCH== \ + libcrypto++-dev:==ARCH== \ + libcurl4-openssl-dev:==ARCH== \ + libexpat1-dev:==ARCH== \ + libfcgi-dev:==ARCH== \ + libgoogle-perftools-dev:==ARCH== \ + libjemalloc-dev:==ARCH== \ + libkeyutils-dev:==ARCH== \ + libldap2-dev:==ARCH== \ + libleveldb-dev:==ARCH== \ + libsnappy-dev:==ARCH== \ + libssl-dev:==ARCH== \ + libudev-dev:==ARCH== \ + \ + && \ + apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* + +WORKDIR /build +COPY entrypoint.sh /build + +ENTRYPOINT [ "/build/entrypoint.sh" ] diff --git a/build/cross-build/Makefile b/build/cross-build/Makefile new file mode 100644 index 000000000..818bae0f8 --- /dev/null +++ b/build/cross-build/Makefile @@ -0,0 +1,21 @@ +.PHONY: all buildall publishall +all: buildall + +buildall: build-amd64 build-arm64 +build-%: Dockerfile.in entrypoint.sh + $(eval ARCH := $*) + $(eval TMPDIR := $(shell mktemp -d)) + @echo building cross-build container for $(ARCH) + @sed 's/==ARCH==/$(ARCH)/g' Dockerfile.in > $(TMPDIR)/Dockerfile + @if [ $(ARCH) = "amd64" ]; then \ + sed -i 's/==BUILD==/build-essential/g' $(TMPDIR)/Dockerfile; \ + else \ + sed -i 's/==BUILD==/crossbuild-essential-$(ARCH)/g' $(TMPDIR)/Dockerfile; \ + fi + @cp entrypoint.sh $(TMPDIR)/ + @cd $(TMPDIR) && docker build -t quantum/castle-build-$* . + @rm -fr $(TMPDIR) + +publishall: publish-amd64 publish-arm64 +publish-%: build-% + docker push quantum/castle-build-$* diff --git a/build/cross-build/entrypoint.sh b/build/cross-build/entrypoint.sh new file mode 100755 index 000000000..200b48830 --- /dev/null +++ b/build/cross-build/entrypoint.sh @@ -0,0 +1,19 @@ +#!/bin/bash -e + +# 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 + groupadd -o -g $BUILDER_GID $BUILDER_GROUP 2> /dev/null + useradd -o -m -g $BUILDER_GID -u $BUILDER_UID $BUILDER_USER 2> /dev/null + echo "$BUILDER_USER ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers + export HOME=/home/${BUILDER_USER} + chown -R $BUILDER_UID:$BUILDER_GID $HOME/* + cd ${HOME}/castle + echo "export PATH=$PATH" > $HOME/.bash_profile + exec chpst -u :$BUILDER_UID:$BUILDER_GID "$@" +else + exec "$@" +fi diff --git a/build/cross-build/toolchain.linux_amd64.cmake b/build/cross-build/toolchain.linux_amd64.cmake new file mode 100644 index 000000000..e69de29bb diff --git a/build/cross-build/toolchain.linux_arm64.cmake b/build/cross-build/toolchain.linux_arm64.cmake new file mode 100644 index 000000000..413293e26 --- /dev/null +++ b/build/cross-build/toolchain.linux_arm64.cmake @@ -0,0 +1,13 @@ +set(CMAKE_SYSTEM_NAME Linux) +set(CMAKE_SYSTEM_VERSION 1) +set(CMAKE_SYSTEM_PROCESSOR arm64) + +set(cross_triple "aarch64-linux-gnu") + +set(CMAKE_C_COMPILER /usr/bin/${cross_triple}-gcc) +set(CMAKE_CXX_COMPILER /usr/bin/${cross_triple}-g++) + +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_FIND_ROOT_PATH_MODE_PACKAGE ONLY) diff --git a/build/golang.mk b/build/golang.mk new file mode 100644 index 000000000..c9b86bc32 --- /dev/null +++ b/build/golang.mk @@ -0,0 +1,181 @@ +# ==================================================================================== +# Makefile helper funcions for golang + +# TODO: check golang version +# TODO: can we strip -s all binaries to reduce size? + +# ==================================================================================== +# Configuration + +ifeq ($(GO_ORG),) +$(error the variable $$GO_ORG must be set prior to including golang.mk) +endif + +ifeq ($(GO_PROJ),) +$(error the variable $$GO_PROJ must be set prior to including golang.mk) +endif + +GO_SOURCE_DIRS ?= ./cmd ./pkg + +GO_BIN_DIR ?= bin +GO_WORK_DIR ?= .gowork +GO_TOOLS_DIR := tools +GO_VENDOR_DIR := vendor + +GO_OS ?= $(shell go env GOOS) +GO_ARCH ?= $(shell go env GOARCH) +GO_HOST_OS = $(shell go env GOHOSTOS) +GO_HOST_ARCH = $(shell go env GOHOSTARCH) + +# TODO: check version +GO_MIN_VERSION ?= 1.6 + +# ==================================================================================== +# Internal variables + +GO_SOURCES := $(shell find $(GO_SOURCE_DIRS) -name '*.go' | sed 's/ /\\ /g') + +GO_PATH ?= $(GO_WORK_DIR)/.gopath +GO_REPO= $(GO_ORG)/$(GO_PROJ) +GO_ORG_PATH = $(GO_PATH)/src/$(GO_ORG) +GO_REPO_PATH = $(GO_ORG_PATH)/$(GO_PROJ) +GO_VERSION := $(shell go version 2>&1 | awk -Fgo '{ print $3 }' | awk '{ print $1 }') + +export GOPATH=$(CURDIR)/$(GO_PATH) +export GOOS=$(GO_OS) +export GOARCH=$(GO_ARCH) + +# ==================================================================================== +# Targets + +$(GO_WORK_DIR): + @mkdir -p $(GO_WORK_DIR) + +$(GO_REPO_PATH): | $(GO_WORK_DIR) + @mkdir -p $(GO_ORG_PATH) + @ln -s ../../../../.. $(GO_REPO_PATH) + +$(GO_TOOLS_DIR): + @mkdir -p $(GO_TOOLS_DIR) + +# generate a list of dependent packages and sources +# $1 target name (for example castlectl) +# $2 the root package of the project (for example cmd/castlectl) +define golang-deps +$(GO_WORK_DIR)/$(1).gosrcs.mk: $(GO_SOURCES) | $(GO_WORK_DIR) $(GO_REPO_PATH) + @go list -f '{{.ImportPath}}{{printf "\n"}}{{range $$$$d := .Deps}}{{$$$$d}}{{printf "\n"}}{{end}}' $(2) | grep $(GO_REPO) | grep --invert-match vendor > $$@.tmp + @echo "$(1).gopkgs := \\" > $$@ + @cat $$@.tmp | tr '\n' ' ' >> $$@ + @echo "" >> $$@ + @echo "$(1).gosrcs := \\" >> $$@ + @go list -f '{{range $$$$f := .GoFiles}}{{$$$$.Dir}}/{{$$$$f}} {{end}}\' $$$$(cat $$@.tmp) >>$$@ + @go list -f '{{range $$$$f := .CgoFiles}}{{$$$$.Dir}}/{{$$$$f}} {{end}}\' $$$$(cat $$@.tmp) >>$$@ + @go list -f '{{range $$$$f := .TestGoFiles}}{{$$$$.Dir}}/{{$$$$f}} {{end}}\' $$$$(cat $$@.tmp) >>$$@ + @echo "" >> $$@ + @rm $$@.tmp + +-include $(GO_WORK_DIR)/$(1).gosrcs.mk +endef + +# generate targets to build a go project +# $1 target name (for example castlectl) +# $2 the root package of the project (for example github.com/quantum/castle/cmd/castlectl) +# $3 optional args that will be passed to go build +# $4 support platforms +define golang-build +.PRECIOUS: $(GO_BIN_DIR)/%/$(1) +$(GO_BIN_DIR)/%/$(1): $$($(1).gosrcs) $(GO_WORK_DIR)/vendor | $(GO_WORK_DIR) $(GO_REPO_PATH) + @echo "====== Building $(1) for $$*" + @GOOS=$$(word 1, $$(subst _, ,$$*)) GOARCH=$$(word 2, $$(subst _, ,$$*)) go build $(3) -o $$@ $(2) + +.PHONY: go.build.$(1) +go.build.$(1): $(GO_BIN_DIR)/$(GO_HOST_OS)_$(GO_HOST_ARCH)/$(1) ; +endef + +# generate targets to test a go project +# $1 target name (for example castlectl) +# $2 optional args that will be passed to go test +define golang-test +.PHONY: go.test.$(1) +go.test.$(1): $$($(1).gosrcs) $(GO_WORK_DIR)/vendor | $(GO_WORK_DIR) $(GO_REPO_PATH) + @echo "====== Running go test on target $(1)" + @GOOS=$(GOHOSTOS) GOARCH=$(GOHOSTARCH) go test $(2) $$($(1).gopkgs) +endef + +# generate targets to vet a go project +# $1 target name (for example castlectl) +# $2 optional args that will be passed to go vet +define golang-vet +$(GO_WORK_DIR)/$(1).vet: $$($(1).gosrcs) | $(GO_WORK_DIR) $(GO_REPO_PATH) + @echo "====== Running go vet on target $(1)" + @GOOS=$(GOHOSTOS) GOARCH=$(GOHOSTARCH) go vet $(2) $$($(1).gopkgs) + @touch $$@ +.PHONY: go.vet.$(1) +go.vet.$(1): $(GO_WORK_DIR)/$(1).vet +endef + +# generate targets to fmt a go project +# $1 target name (for example castlectl) +# $2 optional args that will be passed to go fmt +define golang-fmt +$(GO_WORK_DIR)/$(1).fmt: $$($(1).gosrcs) | $(GO_WORK_DIR) $(GO_REPO_PATH) + @echo "====== Running go fmt on target $(1)" + @GOOS=$(GOHOSTOS) GOARCH=$(GOHOSTARCH) go fmt $(2) $$($(1).gopkgs) + @touch $$@ +.PHONY: go.fmt.$(1) +go.fmt.$(1): $(GO_WORK_DIR)/$(1).fmt +endef + +# generate targets to clean a go project +# $1 target name (for example castlectl) +# $2 optional args that will be passed to go clean +define golang-clean +.PHONY: go.clean.$(1) +go.clean.$(1): | $(GO_WORK_DIR) $(GO_REPO_PATH) + @echo "====== Running go clean on target $(1)" + @GOOS=$(GOHOSTOS) GOARCH=$(GOHOSTARCH) go clean $(2) $$($(1).gopkgs) +endef + +# generate targets for a go project +# $1 target name (for example castlectl) +# $2 the root package of the project (for example cmd/castlectl) +# $3 optional args that will be passed to go tools +define golang-project +$(call golang-deps,$1,$(GO_REPO)/$2) +$(call golang-build,$1,$(GO_REPO)/$2,$3) +$(call golang-vet,$1,$3) +$(call golang-fmt,$1) +$(call golang-test,$1,$3) +$(call golang-clean,$1,$3) +endef + +.PHONY: vendor +vendor $(GO_WORK_DIR)/vendor: $(GO_TOOLS_DIR)/glide | $(GO_WORK_DIR) $(GO_REPO_PATH) + @echo "====== Installing vendor packages" + @$(GO_TOOLS_DIR)/glide install + @touch $@ + +$(GO_TOOLS_DIR)/glide: | $(GO_TOOLS_DIR) + @echo "====== Installing glide" + @curl -sL https://github.com/Masterminds/glide/releases/download/v0.12.2/glide-v0.12.2-$(GO_HOST_OS)-$(GO_HOST_ARCH).tar.gz | tar -xz -C $(GO_TOOLS_DIR) + @mv $(GO_TOOLS_DIR)/$(GO_HOST_OS)-$(GO_HOST_ARCH)/glide $(GO_TOOLS_DIR)/glide + @rm -r $(GO_TOOLS_DIR)/$(GO_HOST_OS)-$(GO_HOST_ARCH) + +go.cleanall: + @rm -rf $(GO_TOOLS_DIR) $(GO_VENDOR_DIR) $(GO_BIN_DIR) $(GO_REPO_PATH) $(GO_WORK_DIR) + +cleanall: go.cleanall + +# go supports cross compiling +ifneq ($(GOOS),linux) +ifeq ($(GOARCH),amd64) +CC=x86_64-linux-gnu-gcc +CXX=x86_64-linux-gnu-g++ +STRIP=x86_64-linux-gnu-strip +endif +ifeq ($(GOARCH),arm64) +CC=aarch64-linux-gnu-gcc +CXX=aarch64-linux-gnu-g++ +STRIP=aarch64-linux-gnu-strip +endif +endif # linux diff --git a/build/publish-build-container.sh b/build/publish-build-container.sh deleted file mode 100755 index 58e375abc..000000000 --- a/build/publish-build-container.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -e - -scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" - -cd $scriptdir/build-container -docker build -t quantum/castle-build . -docker push quantum/castle-build diff --git a/build/run-build.sh b/build/run-build.sh deleted file mode 100755 index da33c2284..000000000 --- a/build/run-build.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash -e - -scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" - -# git version 2.6.6+ through 2.8.3 had a bug with submodules -# see https://github.com/git/git/blob/master/Documentation/RelNotes/2.8.3.txt#L33 -function ver { - printf "%d%03d%03d%03d" $(echo "$1" | tr '.' ' ') -} -gitversion=$(git --version | cut -d" " -f3) -if (( $(ver ${gitversion}) > $(ver 2.6.6) && $(ver ${gitversion}) < $(ver 2.8.3) )); then - echo WARN: your running git version ${gitversion} which has a bug realted to relative - echo WARN: submodule paths. Please consider upgrading to 2.8.3 or later -fi - -docker run \ - -it \ - -e CCACHE_DIR=/root/.ccache \ - -v ${HOME}/.netrc:/root/.netrc \ - -v ${HOME}/.ccache:/root/.ccache \ - -v ${scriptdir}/..:/go/src/github.com/quantum/castle \ - quantum/castle-build \ - "$*" diff --git a/build/run-cross-build b/build/run-cross-build new file mode 100755 index 000000000..ee49ed4ed --- /dev/null +++ b/build/run-cross-build @@ -0,0 +1,41 @@ +#!/bin/bash -e + +scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +# git version 2.6.6+ through 2.8.3 had a bug with submodules +# see https://github.com/git/git/blob/master/Documentation/RelNotes/2.8.3.txt#L33 +function ver { + printf "%d%03d%03d%03d" $(echo "$1" | tr '.' ' ') +} +gitversion=$(git --version | cut -d" " -f3) +if (( $(ver ${gitversion}) > $(ver 2.6.6) && $(ver ${gitversion}) < $(ver 2.8.3) )); then + echo WARN: your running git version ${gitversion} which has a bug realted to relative + echo WARN: submodule paths. Please consider upgrading to 2.8.3 or later +fi + +if [ -z $DOCKER_HOST ]; then + USER_ARGS="-e BUILDER_UID=$( id -u ) -e BUILDER_GID=$( id -g ) -e BUILDER_USER=$( id -un ) -e BUILDER_GROUP=$( id -gn )" + BUILDER_HOME=/home/$( id -un ) +else + BUILDER_HOME=/root +fi + +if [ -n ${CCACHE_DIR+x} ]; then + CCACHE_ARGS="-e CCACHE_DIR=${BUILDER_HOME}/.ccache -v ${CCACHE_DIR}:${BUILDER_HOME}/.ccache" +fi + +[[ -f ${HOME}/.netrc ]] && NETRC_ARGS="-v ${HOME}/.netrc:${BUILDER_HOME}/.netrc" + +tty -s && TTY_ARGS=-ti || TTY_ARGS= + +arch=$1 +shift + +docker run \ + ${TTY_ARGS} \ + ${CCACHE_ARGS} \ + ${NETRC_ARGS} \ + ${USER_ARGS} \ + -v ${scriptdir}/..:${BUILDER_HOME}/castle \ + quantum/castle-build-${arch} \ + "$@" diff --git a/circle.yml b/circle.yml index f099d1159..c40abeaef 100644 --- a/circle.yml +++ b/circle.yml @@ -1,6 +1,7 @@ machine: environment: NPROCS: $(( ($(grep -c ^processor /proc/cpuinfo) + 1) / 2 )) + CCACHE_DIR: ${HOME}/.ccache services: - docker @@ -19,7 +20,7 @@ dependencies: override: - docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASS - docker pull quantum/castle-build - - ./build/run-build.sh make -j$NPROCS check + - build/run-cross-build amd64 make -j$NPROCS check test: override: diff --git a/pkg/cephmgr/cephd/cephd.go b/pkg/cephmgr/cephd/cephd.go index 616d64e32..120fbeb2f 100644 --- a/pkg/cephmgr/cephd/cephd.go +++ b/pkg/cephmgr/cephd/cephd.go @@ -1,14 +1,16 @@ package cephd // #cgo CFLAGS: -I${SRCDIR}/../../../ceph/src/include +// #cgo linux,amd64 LDFLAGS: -L${SRCDIR}/../../../ceph/build/linux_amd64/lib +// #cgo linux,arm64 LDFLAGS: -L${SRCDIR}/../../../ceph/build/linux_arm64/lib // #cgo jemalloc,static LDFLAGS: -ljemalloc // #cgo tcmalloc,static LDFLAGS: -ltcmalloc_minimal // #cgo jemalloc,dynamic LDFLAGS: -Wl,-Bstatic -ljemalloc -Wl,-Bdynamic // #cgo tcmalloc,dynamic LDFLAGS: -Wl,-Bstatic -ltcmalloc_minimal -Wl,-Bdynamic // #cgo jemalloc tcmalloc CFLAGS: -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free // #cgo jemalloc tcmalloc CXXFLAGS: -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free -// #cgo static LDFLAGS: -L${SRCDIR}/../../../ceph/build/lib -lcephd -lboost_system -lboost_thread -lboost_iostreams -lboost_random -lz -lsnappy -lcrypto++ -lleveldb -laio -lblkid -luuid -lm -ldl -lresolv -// #cgo dynamic LDFLAGS: -L${SRCDIR}/../../../ceph/build/lib -Wl,-Bstatic -lcephd -lboost_system -lboost_thread -lboost_iostreams -lboost_random -lz -lsnappy -lcrypto++ -lleveldb -laio -lblkid -luuid -Wl,-Bdynamic -ldl -lm -lresolv +// #cgo static LDFLAGS: -lcephd -lboost_system -lboost_thread -lboost_iostreams -lboost_random -lblkid -lz -lsnappy -lcrypto++ -lleveldb -laio -luuid -lm -ldl -lresolv +// #cgo dynamic LDFLAGS: -Wl,-Bstatic -lcephd -lboost_system -lboost_thread -lboost_iostreams -lboost_random -lblkid -lz -lsnappy -lcrypto++ -lleveldb -laio -luuid -Wl,-Bdynamic -ldl -lm -lresolv // #include // #include // #include