aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLogan Chien <tzuhsiang.chien@gmail.com>2014-06-05 17:11:59 +0800
committerAndrew Hsieh <andrewhsieh@google.com>2015-03-24 11:54:50 -0700
commitad3ce39f69116a52fbc3113ec6fbe3b4893208c3 (patch)
tree7eae32708700ba0dabab016b94ba9aa78d972f8b
parent1699c89ab85349e65053dc5dfc6cf4209b0d8fba (diff)
downloadllvm-ad3ce39f69116a52fbc3113ec6fbe3b4893208c3.tar.gz
[ndk][conf] Add enable-shrink-binary-size to reduce size.
Add an option --enable-shrink-binary-size to reduce the cross- compiled LLVM binaries size. This patch will pass -fdata-sections and -ffunction-sections to $(CXX) and --gc-sections to the $(LD) when --enable-shrink-binary-size is enabled. This option is disabled by default; otherwise, the LLVM tools might not be able to load plugins (eg. LLVMPolly.so) This is ported from release_33 and release_34 branch, and squashing following patches: [1bb7be3] (release_33) WenHan Gu <Wenhan.gu@mediatek.com> Shrink binary sizes when cross-compiling. [6230037] (release_33) Ray Donnelly <mingw.android@gmail.com> Fixes for "Shrink binary sizes when cross-compiling." [43153c9] (release_33) Ray Donnelly <mingw.android@gmail.com> Fix config comment about -Wl,--gc-sections. [d44086b] (release_34) Lai Wei-Chih <Robert.Lai@mediatek.com> Add an option --enable-shrink-binary-size to configure.
-rw-r--r--Makefile4
-rw-r--r--autoconf/configure.ac35
-rw-r--r--autoconf/m4/link_options.m420
-rwxr-xr-xconfigure196
-rw-r--r--tools/Makefile4
-rw-r--r--tools/llvm-shlib/Makefile16
6 files changed, 270 insertions, 5 deletions
diff --git a/Makefile b/Makefile
index 69532da2347..a111af4c041 100644
--- a/Makefile
+++ b/Makefile
@@ -294,3 +294,7 @@ else # Building "Apple-style."
# Look for the string "Apple-style" in utils/buildit/build_llvm.
include $(shell find . -name GNUmakefile) # Building "Apple-style."
endif # Building "Apple-style."
+
+ifeq ($(LLVM_CROSS_COMPILING),1)
+ DIRS := $(filter-out unittests, $(DIRS))
+endif
diff --git a/autoconf/configure.ac b/autoconf/configure.ac
index dfcb9f787bb..dc1324dd458 100644
--- a/autoconf/configure.ac
+++ b/autoconf/configure.ac
@@ -1051,6 +1051,16 @@ AC_SUBST(LLVM_ENUM_ASM_PRINTERS)
AC_SUBST(LLVM_ENUM_ASM_PARSERS)
AC_SUBST(LLVM_ENUM_DISASSEMBLERS)
+dnl --enable-shrink-binary-size : check whether they want to shrink LLVM toolchain size:
+AC_ARG_ENABLE(shrink-binary-size, AS_HELP_STRING(
+ --enable-shrink-binary-size,[Shrink LLVM toolchain size (default is NO)]),,enableval="no")
+
+if test ${enableval} = "no" ; then
+ AC_SUBST(ENABLE_SHRINK_BINARY_SIZE,[0])
+else
+ AC_SUBST(ENABLE_SHRINK_BINARY_SIZE,[1])
+fi
+
dnl Override the option to use for optimized builds.
AC_ARG_WITH(optimize-option,
AS_HELP_STRING([--with-optimize-option],
@@ -1066,6 +1076,11 @@ case "$withval" in
esac ;;
*) optimize_option="$withval" ;;
esac
+
+if test "$ENABLE_SHRINK_BINARY_SIZE" = "1"; then
+ optimize_option="$optimize_option -Os"
+fi
+
AC_SUBST(OPTIMIZE_OPTION,$optimize_option)
AC_MSG_RESULT([$optimize_option])
@@ -1078,6 +1093,17 @@ case "$withval" in
default) EXTRA_OPTIONS= ;;
*) EXTRA_OPTIONS=$withval ;;
esac
+
+dnl Compiler flags for binary size reduction
+AC_MSG_CHECKING([binary size reduction compiler flags])
+CXX_FLAG_CHECK(DATA_SECTIONS, [-fdata-sections])
+CXX_FLAG_CHECK(FUNCTION_SECTIONS, [-ffunction-sections])
+CXX_FLAG_CHECK(VISIBILITY_INLINES_HIDDEN, [-fvisibility-inlines-hidden])
+
+if test "$ENABLE_SHRINK_BINARY_SIZE" = "1"; then
+ EXTRA_OPTIONS="$EXTRA_OPTIONS $DATA_SECTIONS $FUNCTION_SECTIONS $VISIBILITY_INLINES_HIDDEN"
+fi
+
AC_SUBST(EXTRA_OPTIONS,$EXTRA_OPTIONS)
dnl Specify extra linker build options
@@ -1089,6 +1115,15 @@ case "$withval" in
default) EXTRA_LD_OPTIONS= ;;
*) EXTRA_LD_OPTIONS=$withval ;;
esac
+
+dnl Linker flags for binary size reduction (cooperative with -ffunction-sections, -fdata-sections)
+AC_MSG_CHECKING([binary size reduction linker flags])
+AC_LINK_USE_GC_SECTIONS([GC_SECTIONS])
+
+if test "$ENABLE_SHRINK_BINARY_SIZE" = "1" ; then
+ EXTRA_LD_OPTIONS="$EXTRA_LD_OPTIONS $GC_SECTIONS"
+fi
+
AC_SUBST(EXTRA_LD_OPTIONS,$EXTRA_LD_OPTIONS)
dnl Allow specific bindings to be specified for building (or not)
diff --git a/autoconf/m4/link_options.m4 b/autoconf/m4/link_options.m4
index abf6596f7c6..d816f5ee3a4 100644
--- a/autoconf/m4/link_options.m4
+++ b/autoconf/m4/link_options.m4
@@ -20,6 +20,26 @@ AC_DEFUN([AC_LINK_GET_VERSION],
])
#
+# Determine if the system can handle the --gc-sections option being passed to the linker.
+#
+# This macro is specific to LLVM.
+#
+AC_DEFUN([AC_LINK_USE_GC_SECTIONS],
+[AC_CACHE_CHECK([for compiler -Wl,--gc-sections option],[llvm_cv_link_use_gc_sections],
+[ AC_LANG_PUSH([C])
+ oldcflags="$CFLAGS"
+ CFLAGS="$CFLAGS -Wl,--gc-sections"
+ AC_LINK_IFELSE([AC_LANG_PROGRAM([[]],[[]])],
+ [llvm_cv_link_use_gc_sections=yes],[llvm_cv_link_use_gc_sections=no])
+ CFLAGS="$oldcflags"
+ AC_LANG_POP([C])
+])
+if test "$llvm_cv_link_use_gc_sections" = yes ; then
+ AC_SUBST($1,[-Wl,--gc-sections])
+ fi
+])
+
+#
# Determine if the system can handle the -R option being passed to the linker.
#
# This macro is specific to LLVM.
diff --git a/configure b/configure
index c1bcda1c961..a2d1025df6d 100755
--- a/configure
+++ b/configure
@@ -718,8 +718,13 @@ LLVM_ENUM_TARGETS
LLVM_ENUM_ASM_PRINTERS
LLVM_ENUM_ASM_PARSERS
LLVM_ENUM_DISASSEMBLERS
+ENABLE_SHRINK_BINARY_SIZE
OPTIMIZE_OPTION
+DATA_SECTIONS
+FUNCTION_SECTIONS
+VISIBILITY_INLINES_HIDDEN
EXTRA_OPTIONS
+GC_SECTIONS
EXTRA_LD_OPTIONS
CLANG_SRC_ROOT
BINUTILS_INCDIR
@@ -1440,6 +1445,8 @@ Optional Features:
--enable-experimental-targets
Build experimental host targets: disable or
target1,target2,... (default=disable)
+ --enable-shrink-binary-size
+ Shrink LLVM toolchain size (default is NO)
--enable-bindings Build specific language bindings:
all,auto,none,{binding-name} (default=auto)
--enable-terminfo Query the terminfo database if available (default is
@@ -5467,6 +5474,22 @@ done
+# Check whether --enable-shrink-binary-size was given.
+if test "${enable_shrink_binary_size+set}" = set; then
+ enableval=$enable_shrink_binary_size;
+else
+ enableval="no"
+fi
+
+
+if test ${enableval} = "no" ; then
+ ENABLE_SHRINK_BINARY_SIZE=0
+
+else
+ ENABLE_SHRINK_BINARY_SIZE=1
+
+fi
+
# Check whether --with-optimize-option was given.
if test "${with_optimize_option+set}" = set; then
@@ -5486,6 +5509,11 @@ case "$withval" in
esac ;;
*) optimize_option="$withval" ;;
esac
+
+if test "$ENABLE_SHRINK_BINARY_SIZE" = "1"; then
+ optimize_option="$optimize_option -Os"
+fi
+
OPTIMIZE_OPTION=$optimize_option
{ echo "$as_me:$LINENO: result: $optimize_option" >&5
@@ -5503,6 +5531,20 @@ case "$withval" in
default) EXTRA_OPTIONS= ;;
*) EXTRA_OPTIONS=$withval ;;
esac
+
+{ echo "$as_me:$LINENO: checking binary size reduction compiler flags" >&5
+echo $ECHO_N "checking binary size reduction compiler flags... $ECHO_C" >&6; }
+DATA_SECTIONS=`$CXX -Werror -fdata-sections -fsyntax-only -xc /dev/null 2>/dev/null && echo -fdata-sections`
+
+FUNCTION_SECTIONS=`$CXX -Werror -ffunction-sections -fsyntax-only -xc /dev/null 2>/dev/null && echo -ffunction-sections`
+
+VISIBILITY_INLINES_HIDDEN=`$CXX -Werror -fvisibility-inlines-hidden -fsyntax-only -xc /dev/null 2>/dev/null && echo -fvisibility-inlines-hidden`
+
+
+if test "$ENABLE_SHRINK_BINARY_SIZE" = "1"; then
+ EXTRA_OPTIONS="$EXTRA_OPTIONS $DATA_SECTIONS $FUNCTION_SECTIONS $VISIBILITY_INLINES_HIDDEN"
+fi
+
EXTRA_OPTIONS=$EXTRA_OPTIONS
@@ -5518,6 +5560,103 @@ case "$withval" in
default) EXTRA_LD_OPTIONS= ;;
*) EXTRA_LD_OPTIONS=$withval ;;
esac
+
+{ echo "$as_me:$LINENO: checking binary size reduction linker flags" >&5
+echo $ECHO_N "checking binary size reduction linker flags... $ECHO_C" >&6; }
+
+{ echo "$as_me:$LINENO: checking for compiler -Wl,--gc-sections option" >&5
+echo $ECHO_N "checking for compiler -Wl,--gc-sections option... $ECHO_C" >&6; }
+if test "${llvm_cv_link_use_gc_sections+set}" = set; then
+ echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+ ac_ext=c
+ac_cpp='$CPP $CPPFLAGS'
+ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_c_compiler_gnu
+
+ oldcflags="$CFLAGS"
+ CFLAGS="$CFLAGS -Wl,--gc-sections"
+ cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h. */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h. */
+
+int
+main ()
+{
+
+ ;
+ return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext conftest$ac_exeext
+if { (ac_try="$ac_link"
+case "(($ac_try" in
+ *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
+ *) ac_try_echo=$ac_try;;
+esac
+eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
+ (eval "$ac_link") 2>conftest.er1
+ ac_status=$?
+ grep -v '^ *+' conftest.er1 >conftest.err
+ rm -f conftest.er1
+ cat conftest.err >&5
+ echo "$as_me:$LINENO: \$? = $ac_status" >&5
+ (exit $ac_status); } &&
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
+ { (case "(($ac_try" in
+ *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
+ *) ac_try_echo=$ac_try;;
+esac
+eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
+ (eval "$ac_try") 2>&5
+ ac_status=$?
+ echo "$as_me:$LINENO: \$? = $ac_status" >&5
+ (exit $ac_status); }; } &&
+ { ac_try='test -s conftest$ac_exeext'
+ { (case "(($ac_try" in
+ *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
+ *) ac_try_echo=$ac_try;;
+esac
+eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
+ (eval "$ac_try") 2>&5
+ ac_status=$?
+ echo "$as_me:$LINENO: \$? = $ac_status" >&5
+ (exit $ac_status); }; }; then
+ llvm_cv_link_use_gc_sections=yes
+else
+ echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+ llvm_cv_link_use_gc_sections=no
+fi
+
+rm -f core conftest.err conftest.$ac_objext \
+ conftest$ac_exeext conftest.$ac_ext
+ CFLAGS="$oldcflags"
+ ac_ext=c
+ac_cpp='$CPP $CPPFLAGS'
+ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_c_compiler_gnu
+
+
+fi
+{ echo "$as_me:$LINENO: result: $llvm_cv_link_use_gc_sections" >&5
+echo "${ECHO_T}$llvm_cv_link_use_gc_sections" >&6; }
+if test "$llvm_cv_link_use_gc_sections" = yes ; then
+ GC_SECTIONS=-Wl,--gc-sections
+
+ fi
+
+
+if test "$ENABLE_SHRINK_BINARY_SIZE" = "1" ; then
+ EXTRA_LD_OPTIONS="$EXTRA_LD_OPTIONS $GC_SECTIONS"
+fi
+
EXTRA_LD_OPTIONS=$EXTRA_LD_OPTIONS
@@ -7035,7 +7174,6 @@ _ACEOF
-
{ echo "$as_me:$LINENO: checking for compiler -Wl,-R<path> option" >&5
echo $ECHO_N "checking for compiler -Wl,-R<path> option... $ECHO_C" >&6; }
if test "${llvm_cv_link_use_r+set}" = set; then
@@ -19317,8 +19455,13 @@ LLVM_ENUM_TARGETS!$LLVM_ENUM_TARGETS$ac_delim
LLVM_ENUM_ASM_PRINTERS!$LLVM_ENUM_ASM_PRINTERS$ac_delim
LLVM_ENUM_ASM_PARSERS!$LLVM_ENUM_ASM_PARSERS$ac_delim
LLVM_ENUM_DISASSEMBLERS!$LLVM_ENUM_DISASSEMBLERS$ac_delim
+ENABLE_SHRINK_BINARY_SIZE!$ENABLE_SHRINK_BINARY_SIZE$ac_delim
OPTIMIZE_OPTION!$OPTIMIZE_OPTION$ac_delim
+DATA_SECTIONS!$DATA_SECTIONS$ac_delim
+FUNCTION_SECTIONS!$FUNCTION_SECTIONS$ac_delim
+VISIBILITY_INLINES_HIDDEN!$VISIBILITY_INLINES_HIDDEN$ac_delim
EXTRA_OPTIONS!$EXTRA_OPTIONS$ac_delim
+GC_SECTIONS!$GC_SECTIONS$ac_delim
EXTRA_LD_OPTIONS!$EXTRA_LD_OPTIONS$ac_delim
CLANG_SRC_ROOT!$CLANG_SRC_ROOT$ac_delim
BINUTILS_INCDIR!$BINUTILS_INCDIR$ac_delim
@@ -19390,13 +19533,54 @@ HAVE_OCAML_OUNIT!$HAVE_OCAML_OUNIT$ac_delim
OCAML_LIBDIR!$OCAML_LIBDIR$ac_delim
ENABLE_VISIBILITY_INLINES_HIDDEN!$ENABLE_VISIBILITY_INLINES_HIDDEN$ac_delim
RPATH!$RPATH$ac_delim
+_ACEOF
+
+ if test `sed -n "s/.*$ac_delim\$/X/p" conf$$subs.sed | grep -c X` = 97; then
+ break
+ elif $ac_last_try; then
+ { { echo "$as_me:$LINENO: error: could not make $CONFIG_STATUS" >&5
+echo "$as_me: error: could not make $CONFIG_STATUS" >&2;}
+ { (exit 1); exit 1; }; }
+ else
+ ac_delim="$ac_delim!$ac_delim _$ac_delim!! "
+ fi
+done
+
+ac_eof=`sed -n '/^CEOF[0-9]*$/s/CEOF/0/p' conf$$subs.sed`
+if test -n "$ac_eof"; then
+ ac_eof=`echo "$ac_eof" | sort -nru | sed 1q`
+ ac_eof=`expr $ac_eof + 1`
+fi
+
+cat >>$CONFIG_STATUS <<_ACEOF
+cat >"\$tmp/subs-2.sed" <<\CEOF$ac_eof
+/@[a-zA-Z_][a-zA-Z_0-9]*@/!b
+_ACEOF
+sed '
+s/[,\\&]/\\&/g; s/@/@|#_!!_#|/g
+s/^/s,@/; s/!/@,|#_!!_#|/
+:n
+t n
+s/'"$ac_delim"'$/,g/; t
+s/$/\\/; p
+N; s/^.*\n//; s/[,\\&]/\\&/g; s/@/@|#_!!_#|/g; b n
+' >>$CONFIG_STATUS <conf$$subs.sed
+rm -f conf$$subs.sed
+cat >>$CONFIG_STATUS <<_ACEOF
+CEOF$ac_eof
+_ACEOF
+
+
+ac_delim='%!_!# '
+for ac_last_try in false false false false false :; do
+ cat >conf$$subs.sed <<_ACEOF
RDYNAMIC!$RDYNAMIC$ac_delim
program_prefix!$program_prefix$ac_delim
LIBOBJS!$LIBOBJS$ac_delim
LTLIBOBJS!$LTLIBOBJS$ac_delim
_ACEOF
- if test `sed -n "s/.*$ac_delim\$/X/p" conf$$subs.sed | grep -c X` = 96; then
+ if test `sed -n "s/.*$ac_delim\$/X/p" conf$$subs.sed | grep -c X` = 4; then
break
elif $ac_last_try; then
{ { echo "$as_me:$LINENO: error: could not make $CONFIG_STATUS" >&5
@@ -19414,8 +19598,8 @@ if test -n "$ac_eof"; then
fi
cat >>$CONFIG_STATUS <<_ACEOF
-cat >"\$tmp/subs-2.sed" <<\CEOF$ac_eof
-/@[a-zA-Z_][a-zA-Z_0-9]*@/!b
+cat >"\$tmp/subs-3.sed" <<\CEOF$ac_eof
+/@[a-zA-Z_][a-zA-Z_0-9]*@/!b end
_ACEOF
sed '
s/[,\\&]/\\&/g; s/@/@|#_!!_#|/g
@@ -19428,6 +19612,8 @@ N; s/^.*\n//; s/[,\\&]/\\&/g; s/@/@|#_!!_#|/g; b n
' >>$CONFIG_STATUS <conf$$subs.sed
rm -f conf$$subs.sed
cat >>$CONFIG_STATUS <<_ACEOF
+:end
+s/|#_!!_#|//g
CEOF$ac_eof
_ACEOF
@@ -19675,7 +19861,7 @@ s&@abs_builddir@&$ac_abs_builddir&;t t
s&@abs_top_builddir@&$ac_abs_top_builddir&;t t
s&@INSTALL@&$ac_INSTALL&;t t
$ac_datarootdir_hack
-" $ac_file_inputs | sed -f "$tmp/subs-1.sed" | sed -f "$tmp/subs-2.sed" | sed 's/|#_!!_#|//g' >$tmp/out
+" $ac_file_inputs | sed -f "$tmp/subs-1.sed" | sed -f "$tmp/subs-2.sed" | sed -f "$tmp/subs-3.sed" >$tmp/out
test -z "$ac_datarootdir_hack$ac_datarootdir_seen" &&
{ ac_out=`sed -n '/\${datarootdir}/p' "$tmp/out"`; test -n "$ac_out"; } &&
diff --git a/tools/Makefile b/tools/Makefile
index f2ef54e5b4b..c7f56d7a630 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -41,6 +41,10 @@ ifeq ($(USE_INTEL_JITEVENTS), 1)
PARALLEL_DIRS += llvm-jitlistener
endif
+ifeq ($(LLVM_CROSS_COMPILING),1)
+ PARALLEL_DIRS := llc le32-none-ndk-translate
+endif
+
# Let users override the set of tools to build from the command line.
ifdef ONLY_TOOLS
OPTIONAL_PARALLEL_DIRS :=
diff --git a/tools/llvm-shlib/Makefile b/tools/llvm-shlib/Makefile
index 19077a3858a..7d02ce7b616 100644
--- a/tools/llvm-shlib/Makefile
+++ b/tools/llvm-shlib/Makefile
@@ -38,6 +38,22 @@ SharedLibraries := $(wildcard $(LibDir)/libLLVM*$(SHLIBEXT))
ExcludeFromLibLlvm := $(basename $(SharedLibraries)).a %/libLLVMTableGen.a
IncludeInLibLlvm := $(filter-out $(ExcludeFromLibLlvm), $(Archives))
LLVMLibsOptions := $(IncludeInLibLlvm:$(LibDir)/lib%.a=-l%)
+ifeq ($(LLVM_CROSS_COMPILING),1)
+ # Don't link these for saving binary size about 1MB
+ LLVMLibsOptions := $(filter-out -lLLVMArchive, $(LLVMLibsOptions))
+ LLVMLibsOptions := $(filter-out -lLLVMDebugInfo, $(LLVMLibsOptions))
+ LLVMLibsOptions := $(filter-out -lLLVMExecutionEngine, $(LLVMLibsOptions))
+ LLVMLibsOptions := $(filter-out -lLLVMInstrumentation, $(LLVMLibsOptions))
+ LLVMLibsOptions := $(filter-out -lLLVMInterpreter, $(LLVMLibsOptions))
+ LLVMLibsOptions := $(filter-out -lLLVMipo, $(LLVMLibsOptions))
+ LLVMLibsOptions := $(filter-out -lLLVMJIT, $(LLVMLibsOptions))
+ LLVMLibsOptions := $(filter-out -lLLVMLinker, $(LLVMLibsOptions))
+ LLVMLibsOptions := $(filter-out -lLLVMMCDisassembler, $(LLVMLibsOptions))
+ LLVMLibsOptions := $(filter-out -lLLVMMCJIT, $(LLVMLibsOptions))
+ LLVMLibsOptions := $(filter-out -lLLVMRuntimeDyld, $(LLVMLibsOptions))
+ LLVMLibsOptions := $(filter-out -lLLVMVectorize, $(LLVMLibsOptions))
+ LLVMLibsOptions := $(filter-out -lLLVMWrap, $(LLVMLibsOptions))
+endif
LLVMLibsPaths := $(IncludeInLibLlvm)
$(LibName.SO): $(LLVMLibsPaths)