Skip to content

Commit

Permalink
support for dynamic linking and PIE
Browse files Browse the repository at this point in the history
  • Loading branch information
Bassam Tabbara committed Aug 8, 2016
1 parent 7632264 commit 41bf52a
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ tools/
vendor/

.vscode/
.glide/
69 changes: 69 additions & 0 deletions Documentation/build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Building

Castle is golang binary that uses cgo to interface with embedded ceph. The ceph
project is a submodule of castle and is built from the top level Makefile.

## Static binary

This is the easiest option and produced static binaries for castled and tools. These
binaries can easily be used inside minimal or scratch containers, or run on minimal
linux distributions like CoreOS.

```
make STATIC=1 build
```

## Dyanmic binary

If you dont want to distribute a static binary, a dynamically linked binary is
supported. The approach we take is to link most of the glibc binaries dynamically
and the rest of the libraries continue to be linked statically. Ceph has a lot
of dependencies and we take this approach to simplify the distribution.

```
make STATIC=0 build
```

## Hardedened binary (PIE)

You can build a Position Independent executable as follows:

```
make STATIC=0 PIE=1 build
```

## Switching Allocators

Using a different memory allocator can impact the overall performance of the system.
Three allocators are currently supported: jemalloc, tcmalloc and libc. To specify
an allocator during the build run the following:

```
make ALLOCATOR=jemalloc build
```

## Debug Builds

To build a binary with debug symbols run the following:

```
make DEBUG=1 build
```

Note the binary will be significantly larger in size.

## Verbose Builds

To turn on verbose build output run the following

```
make DEBUG=1 build
```

## Parallel Builds

You can speed up the build significantly by passing the -j flag to make as follows:

```
make -j4 build
```
30 changes: 25 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ GOARCH = $(shell go env GOARCH)
# 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
# TODO: can we strip -s all binaries?

# Can be used for additional go build flags
BUILDFLAGS ?=
LDFLAGS ?=
TAGS ?=

# Additional flags to use when calling cmake on ceph
CEPHD_CMAKE += \
Expand All @@ -24,11 +26,26 @@ CEPHD_CMAKE += \
-DWITH_MANPAGE=OFF \
-DWITH_PROFILER=OFF

# set to 1 for a completely static build
# 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)
LDFLAGS += -extldflags "-static"
BUILDFLAGS += -installsuffix cgo
LDFLAGS += -extldflags "-static"
TAGS += static
else
TAGS += dynamic
endif

# build a position independent executable. This implies dynamic linking
# since statically-linked PIE is not supported by the linker/glibc
PIE ?= 0
ifeq ($(PIE),1)
ifeq ($(STATIC),1)
$(error PIE only supported with dynamic linking. Set STATIC=0.)
endif
BUILDFLAGS += -buildmode=pie
TAGS += pie
endif

# Can be jemalloc, tcmalloc or glibc
Expand Down Expand Up @@ -57,6 +74,9 @@ V ?= 0
ifeq ($(V),1)
LDFLAGS += -v
BUILDFLAGS += -x
MFLAGS += -w
else
MFLAGS += -s
endif

# set the version number.
Expand All @@ -70,16 +90,16 @@ all: build test

.PHONY: build
build: vendor ceph
mkdir -p ceph/build
@mkdir -p ceph/build
@echo "##### configuring ceph"
cd ceph/build && cmake $(CEPHD_CMAKE) ..
@echo "##### building ceph"
cd ceph/build && $(MAKE) $(MFLAGS) cephd
ifeq ($(DEBUG),0)
echo "##### stripping libcephd.a"
@echo "##### stripping libcephd.a"
strip -S ceph/build/lib/libcephd.a
endif
echo "##### building castled"
@echo "##### building castled"
go build $(BUILDFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)' -o bin/castled ./cmd/castled

ceph:
Expand Down
2 changes: 1 addition & 1 deletion ceph
Submodule ceph updated from f1f191 to 56131a
9 changes: 6 additions & 3 deletions pkg/cephd/cephd.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package cephd

// #cgo CFLAGS: -I${SRCDIR}/../../ceph/src/include
// #cgo jemalloc LDFLAGS: -ljemalloc
// #cgo tcmalloc LDFLAGS: -ltcmalloc_minimal
// #cgo LDFLAGS: -L${SRCDIR}/../../ceph/build/lib -lcephd -lm -ldl -lboost_system -lboost_thread -lboost_iostreams -lboost_random -lz -lsnappy -lcrypto++ -lresolv -lleveldb -laio -lblkid -luuid
// #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
// #include <errno.h>
// #include <stdlib.h>
// #include <string.h>
Expand Down

0 comments on commit 41bf52a

Please sign in to comment.