Skip to content

Commit

Permalink
build: add CONFIG_LTO to enable link-time optimization
Browse files Browse the repository at this point in the history
Change-Id: I9546d715b8d2ca3ebf46183bdbaa58e8aa921d95
Signed-off-by: Daniel Verkamp <[email protected]>
Signed-off-by: Ben Walker <[email protected]>
  • Loading branch information
danielverkamp committed May 10, 2017
1 parent 6a8cd33 commit 6d6d116
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ CONFIG.local
.project
.cproject
.settings
mk/cc.mk
3 changes: 3 additions & 0 deletions CONFIG
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ CONFIG_DEBUG?=n
# Treat warnings as errors (fail the build on any warning).
CONFIG_WERROR?=n

# Build with link-time optimization.
CONFIG_LTO?=n

# Build with code coverage instrumentation.
CONFIG_COVERAGE?=n

Expand Down
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,23 @@ include $(SPDK_ROOT_DIR)/mk/spdk.common.mk

DIRS-y += lib test examples app

.PHONY: all clean $(DIRS-y) config.h CONFIG.local
.PHONY: all clean $(DIRS-y) config.h CONFIG.local mk/cc.mk

all: $(DIRS-y)
clean: $(DIRS-y)
$(Q)rm -f mk/cc.mk
$(Q)rm -f config.h

app: lib
test: lib
examples: lib

$(DIRS-y): config.h
$(DIRS-y): mk/cc.mk config.h

mk/cc.mk:
$(Q)scripts/detect_cc.sh --cc=$(CC) --cxx=$(CXX) --lto=$(CONFIG_LTO) > $@.tmp; \
cmp -s $@.tmp $@ || mv $@.tmp $@ ; \
rm -f $@.tmp

config.h: CONFIG CONFIG.local scripts/genconfig.py
$(Q)python scripts/genconfig.py $(MAKEFLAGS) > $@.tmp; \
Expand Down
10 changes: 10 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function usage()
echo " --enable-asan Enable address sanitizer"
echo " --enable-ubsan Enable undefined behavior sanitizer"
echo " --enable-coverage Enable code coverage tracking"
echo " --enable-lto Enable link-time optimization"
echo " --with-env=path Use an alternate environment implementation"
echo ""
echo "Specifying Dependencies:"
Expand Down Expand Up @@ -67,6 +68,12 @@ for i in "$@"; do
--disable-coverage)
CONFIG_COVERAGE=n
;;
--enable-lto)
CONFIG_LTO=y
;;
--disable-lto)
CONFIG_LTO=n
;;
--enable-werror)
CONFIG_WERROR=y
;;
Expand Down Expand Up @@ -139,6 +146,9 @@ fi
if [ -n "$CONFIG_COVERAGE" ]; then
echo "CONFIG_COVERAGE?=$CONFIG_COVERAGE" >> CONFIG.local
fi
if [ -n "$CONFIG_LTO" ]; then
echo "CONFIG_LTO?=$CONFIG_LTO" >> CONFIG.local
fi
if [ -n "$CONFIG_ASAN" ]; then
echo "CONFIG_ASAN?=$CONFIG_ASAN" >> CONFIG.local
fi
Expand Down
9 changes: 8 additions & 1 deletion mk/spdk.common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
-include $(SPDK_ROOT_DIR)/CONFIG.local
include $(SPDK_ROOT_DIR)/CONFIG

-include $(SPDK_ROOT_DIR)/mk/cc.mk

C_OPT ?= -fno-omit-frame-pointer
ifneq ($(V),1)
Q ?= @
Expand All @@ -54,6 +56,11 @@ ifeq ($(CONFIG_WERROR), y)
COMMON_CFLAGS += -Werror
endif

ifeq ($(CONFIG_LTO),y)
COMMON_CFLAGS += -flto
LDFLAGS += -flto
endif

COMMON_CFLAGS += -Wformat -Wformat-security

COMMON_CFLAGS += -D_GNU_SOURCE
Expand Down Expand Up @@ -149,7 +156,7 @@ LINK_CXX=\
LIB_C=\
$(Q)echo " LIB $(notdir $@)"; \
rm -f $@; \
ar crDs $@ $(OBJS)
$(CCAR) crDs $@ $(OBJS)

# Clean up generated files listed as arguments plus a default list
CLEAN_C=\
Expand Down
80 changes: 80 additions & 0 deletions scripts/detect_cc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env bash

set -e

function err()
{
echo "$@" >&2
}

function usage()
{
err "Detect compiler and linker versions, generate mk/cc.mk"
err ""
err "Usage: ./detect_cc.sh [OPTION]..."
err ""
err "Defaults for the options are specified in brackets."
err ""
err "General:"
err " -h, --help Display this help and exit"
err " --cc=path C compiler to use"
err " --cxx=path C++ compiler to use"
err " --lto=[y|n] Attempt to configure for LTO"

}

CC=cc
CXX=c++
LTO=n

for i in "$@"; do
case "$i" in
-h|--help)
usage
exit 0
;;
--cc=*)
CC="${i#*=}"
;;
--cxx=*)
CXX="${i#*=}"
;;
--lto=*)
LTO="${i#*=}"
;;
--)
break
;;
*)
err "Unrecognized option $i"
usage
exit 1
esac
done

CC_TYPE=$($CC -v 2>&1 | grep version | awk '{print $1}')
CXX_TYPE=$($CXX -v 2>&1 | grep version | awk '{print $1}')
LD_TYPE=$(ld -v 2>&1 | awk '{print $2}')

if [ "$CC_TYPE" != "$CXX_TYPE" ]; then
err "C compiler is $CC_TYPE but C++ compiler is $CXX_TYPE"
err "This may result in errors"
exit 1
fi

CCAR="ar"
if [ "$LTO" = "y" ]; then
if [ "$CC_TYPE" = "clang" ]; then
if [ "$LD_TYPE" != "gold" ]; then
err "Using LTO with clang requires the gold linker."
exit 1
fi
CCAR="llvm-ar"
else
CCAR="gcc-ar"
fi
fi

echo "CC=$CC"
echo "CXX=$CXX"
echo "CCAR=$CCAR"

0 comments on commit 6d6d116

Please sign in to comment.