From 0d962e30c403569c50bce6878058696ea819d608 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Tue, 8 Oct 2019 00:44:08 +0200 Subject: [PATCH] build system: move flag generation to Rakefile for several features Features include debuging, profiling, optimizations & sanitizers. The flags don't depend on something that's actually tested, but only on which features are enabled and which compiler & compiler version is used. Therefore doing it in Ruby instead of sh is easier. --- Rakefile | 37 ++++++++++------ ac/debugging_profiling.m4 | 91 +++++++++++++++++++++++---------------- build-config.in | 13 +++--- 3 files changed, 84 insertions(+), 57 deletions(-) diff --git a/Rakefile b/Rakefile index 9b4f77798..23448b851 100644 --- a/Rakefile +++ b/Rakefile @@ -124,21 +124,20 @@ def setup_globals $libmtxcommon_as_dll = $building_for[:windows] && %r{shared}i.match(c(:host)) - stack_protector = "" - stack_protector = " -fstack-protector" if is_gcc? && !check_compiler_version("gcc", "4.9.0") - stack_protector = " -fstack-protector-strong" if check_compiler_version("gcc", "4.9.0") || check_compiler_version("clang", "3.5.0") - cflags_common = "-Wall -Wno-comment -Wfatal-errors #{c(:WLOGICAL_OP)} #{c(:WNO_MISMATCHED_TAGS)} #{c(:WNO_SELF_ASSIGN)} #{c(:QUNUSED_ARGUMENTS)}" cflags_common += " #{c(:WNO_INCONSISTENT_MISSING_OVERRIDE)} #{c(:WNO_POTENTIALLY_EVALUATED_EXPRESSION)}" cflags_common += " #{c(:OPTIMIZATION_CFLAGS)} -D_FILE_OFFSET_BITS=64" cflags_common += " -DMTX_LOCALE_DIR=\\\"#{c(:localedir)}\\\" -DMTX_PKG_DATA_DIR=\\\"#{c(:pkgdatadir)}\\\" -DMTX_DOC_DIR=\\\"#{c(:docdir)}\\\"" - cflags_common += stack_protector - cflags_common += " -fsanitize=undefined" if c?(:UBSAN) - cflags_common += " -fsanitize=address -fno-omit-frame-pointer" if c?(:ADDRSAN) + cflags_common += determine_stack_protector_flags + cflags_common += determine_optimization_cflags + cflags_common += " -g3 -DDEBUG" if c?(:USE_DEBUG) + cflags_common += " -pg" if c?(:USE_PROFILING) + cflags_common += " -fsanitize=undefined" if c?(:USE_UBSAN) + cflags_common += " -fsanitize=address -fno-omit-frame-pointer" if c?(:USE_ADDRSAN) cflags_common += " -Ilib/libebml -Ilib/libmatroska" if c?(:EBML_MATROSKA_INTERNAL) cflags_common += " -Ilib/nlohmann-json/include" if c?(:NLOHMANN_JSON_INTERNAL) cflags_common += " -Ilib/fmt/include" if c?(:FMT_INTERNAL) - cflags_common += " #{c(:MATROSKA_CFLAGS)} #{c(:EBML_CFLAGS)} #{c(:PUGIXML_CFLAGS)} #{c(:CMARK_CFLAGS)} #{c(:EXTRA_CFLAGS)} #{c(:DEBUG_CFLAGS)} #{c(:PROFILING_CFLAGS)} #{c(:USER_CPPFLAGS)}" + cflags_common += " #{c(:MATROSKA_CFLAGS)} #{c(:EBML_CFLAGS)} #{c(:PUGIXML_CFLAGS)} #{c(:CMARK_CFLAGS)} #{c(:EXTRA_CFLAGS)} #{c(:USER_CPPFLAGS)}" cflags_common += " -mno-ms-bitfields -DWINVER=0x0601 -D_WIN32_WINNT=0x0601 " if $building_for[:windows] # 0x0601 = Windows 7/Server 2008 R2 cflags_common += " -march=i686" if $building_for[:windows] && /i686/.match(c(:host)) cflags_common += " -fPIC " if c?(:USE_QT) && !$building_for[:windows] @@ -161,15 +160,16 @@ def setup_globals cxxflags += " #{c(:QT_CFLAGS)} #{c(:BOOST_CPPFLAGS)} #{c(:USER_CXXFLAGS)}" ldflags = "" - ldflags += stack_protector + ldflags += determine_stack_protector_flags + ldflags += " -pg" if c?(:USE_PROFILING) ldflags += " -fuse-ld=lld" if is_clang? && !c(:LLVM_LLD).empty? ldflags += " -Llib/libebml/src -Llib/libmatroska/src" if c?(:EBML_MATROSKA_INTERNAL) ldflags += " -Llib/fmt/src" if c?(:FMT_INTERNAL) - ldflags += " #{c(:EXTRA_LDFLAGS)} #{c(:PROFILING_LIBS)} #{c(:USER_LDFLAGS)} #{c(:LDFLAGS_RPATHS)} #{c(:BOOST_LDFLAGS)}" + ldflags += " #{c(:EXTRA_LDFLAGS)} #{c(:USER_LDFLAGS)} #{c(:LDFLAGS_RPATHS)} #{c(:BOOST_LDFLAGS)}" ldflags += " -Wl,--dynamicbase,--nxcompat" if $building_for[:windows] ldflags += " -L#{c(:DRMINGW_PATH)}/lib" if c?(:USE_DRMINGW) && $building_for[:windows] - ldflags += " -fsanitize=undefined" if c?(:UBSAN) - ldflags += " -fsanitize=address -fno-omit-frame-pointer" if c?(:ADDRSAN) + ldflags += " -fsanitize=undefined" if c?(:USE_UBSAN) + ldflags += " -fsanitize=address -fno-omit-frame-pointer" if c?(:USE_ADDRSAN) ldflags += " -headerpad_max_install_names" if $building_for[:macos] windres = "" @@ -218,6 +218,19 @@ def setup_compiler_specifics end end +def determine_optimization_cflags + return "" if !c?(:USE_OPTIMIZATION) + return " -O1" if is_clang? && !check_compiler_version("clang", "3.8.0") # LLVM bug 11962 + return " -O2 -fno-ipa-icf" if $building_for[:windows] && check_compiler_version("gcc", "5.1.0") && !check_compiler_version("gcc", "7.2.0") + return " -O3" +end + +def determine_stack_protector_flags + return " -fstack-protector" if is_gcc? && !check_compiler_version("gcc", "4.9.0") + return " -fstack-protector-strong" if check_compiler_version("gcc", "4.9.0") || check_compiler_version("clang", "3.5.0") + return "" +end + def generate_helper_files return unless c?(:EBML_MATROSKA_INTERNAL) diff --git a/ac/debugging_profiling.m4 b/ac/debugging_profiling.m4 index df84fd7d1..670cc0faf 100644 --- a/ac/debugging_profiling.m4 +++ b/ac/debugging_profiling.m4 @@ -1,5 +1,5 @@ dnl -dnl Debugging, profiling and optimization options +dnl Debugging dnl AC_ARG_ENABLE([debug], @@ -7,11 +7,39 @@ AC_ARG_ENABLE([debug], [], [enable_debug=no]) +if test x"$enable_debug" = xyes ; then + opt_features_yes="$opt_features_yes\n * debugging information" +else + opt_features_no="$opt_features_no\n * debugging information" +fi + +USE_DEBUG=$enable_debug +AC_SUBST(USE_DEBUG) + + +dnl +dnl Profiling +dnl + AC_ARG_ENABLE([profiling], AC_HELP_STRING([--enable-profiling],[compile with profiling information (no)]), [], [enable_profiling=no]) +if test x"$enable_profiling" = xyes ; then + opt_features_yes="$opt_features_yes\n * profiling support" +else + opt_features_no="$opt_features_no\n * profiling support" +fi + +USE_PROFILING=$enable_profiling +AC_SUBST(USE_PROFILING) + + +dnl +dnl Optimization +dnl + AC_ARG_ENABLE([optimization], AC_HELP_STRING([--enable-optimization],[compile with optimization: -O3 (yes)]), [], @@ -21,67 +49,56 @@ AC_ARG_ENABLE([optimization], enable_optimization=yes fi]) -DEBUG_CFLAGS="" -OPTIMIZATION_CFLAGS="" -PROFILING_CFLAGS="" -PROFILING_LIBS="" - -if test x"$enable_debug" = xyes ; then - DEBUG_CFLAGS="-g -DDEBUG" - opt_features_yes="$opt_features_yes\n * debugging information" -else - opt_features_no="$opt_features_no\n * debugging information" -fi - if test x"$enable_optimization" = xyes; then if test $COMPILER_TYPE = clang && ! check_version 3.8.0 $COMPILER_VERSION; then - opt_features_no="$opt_features_no\n * full optimization: due to bug 11962 in LLVM/clang only -O1 will be used for optimization" - OPTIMIZATION_CFLAGS="-O1" + opt_features_yes="$opt_features_yes\n * partial optimizations: due to bug 11962 in LLVM/clang only -O1 will be used for optimization" elif test "x$ac_cv_mingw32" = "xyes" -a "x$MINGW_PROCESSOR_ARCH" = "xx86" && check_version 5.1.0 $COMPILER_VERSION && ! check_version 7.2.0 $COMPILER_VERSION; then - OPTIMIZATION_CFLAGS="-O2 -fno-ipa-icf" - opt_features_no="$opt_features_no\n * full optimization: due to an issue in mingw g++ >= 5.1.0 and < 7.2.0 full optimization cannot be used" + opt_features_yes="$opt_features_yes\n * partial optimizations: due to an issue in mingw g++ >= 5.1.0 and < 7.2.0 full optimization cannot be used" else - OPTIMIZATION_CFLAGS="-O3" - fi + opt_features_yes="$opt_features_yes\n * compiler optimizations" - opt_features_yes="$opt_features_yes\n * compiler optimizations ($OPTIMIZATION_CFLAGS)" + fi else opt_features_no="$opt_features_no\n * compiler optimizations" fi -if test x"$enable_profiling" = xyes ; then - PROFILING_CFLAGS="-pg" - PROFILING_LIBS="-pg" - opt_features_yes="$opt_features_yes\n * profiling support" -else - opt_features_no="$opt_features_no\n * profiling support" -fi +USE_OPTIMIZATION=$enable_optimization +AC_SUBST(USE_OPTIMIZATION) + + +dnl +dnl Address sanitizer +dnl AC_ARG_ENABLE([addrsan], AC_HELP_STRING([--enable-addrsan],[compile with address sanitization turned on (no)]), - [ADDRSAN=$enable_addrsan],[ADDRSAN=no]) + [],[enable_addrsan=no]) -if test x"$ADDRSAN" = xyes ; then +if test x"$enable_addrsan" = xyes ; then opt_features_yes="$opt_features_yes\n * development technique 'address sanitizer'" else opt_features_no="$opt_features_no\n * development technique 'address sanitizer'" fi +USE_ADDRSAN=$enable_addrsan +AC_SUBST(USE_ADDRSAN) + + +dnl +dnl Undefined behavior sanitizer +dnl + AC_ARG_ENABLE([ubsan], AC_HELP_STRING([--enable-ubsan],[compile with sanitization for undefined behavior turned on (no)]), - [UBSAN=$enable_ubsan],[UBSAN=no]) + [USE_UBSAN=$enable_ubsan],[USE_UBSAN=no]) -if test x"$UBSAN" = xyes ; then +if test x"$USE_UBSAN" = xyes ; then opt_features_yes="$opt_features_yes\n * development technique 'undefined behavior sanitizer'" else opt_features_no="$opt_features_no\n * development technique 'undefined behavior sanitizer'" fi -AC_SUBST(DEBUG_CFLAGS) -AC_SUBST(PROFILING_CFLAGS) -AC_SUBST(PROFILING_LIBS) -AC_SUBST(OPTIMIZATION_CFLAGS) -AC_SUBST(ADDRSAN) -AC_SUBST(UBSAN) +USE_UBSAN=$enable_ubsan +AC_SUBST(USE_UBSAN) diff --git a/build-config.in b/build-config.in index 413b53571..ef475ff0a 100644 --- a/build-config.in +++ b/build-config.in @@ -85,7 +85,6 @@ CMARK_CFLAGS = @CMARK_CFLAGS@ CMARK_LIBS = @CMARK_LIBS@ COMPILER_TYPE = @COMPILER_TYPE@ COMPILER_VERSION = @COMPILER_VERSION@ -DEBUG_CFLAGS = @DEBUG_CFLAGS@ DOCBOOK_ROOT = @DOCBOOK_ROOT@ DRMINGW_PATH = @DRMINGW_PATH@ EBML_MATROSKA_INTERNAL = @EBML_MATROSKA_INTERNAL@ @@ -112,14 +111,11 @@ MINGW = @MINGW@ NLOHMANN_JSON_INTERNAL = @NLOHMANN_JSON_INTERNAL@ OGG_LIBS = @OGG_LIBS@ LINK_STATICALLY=@LINK_STATICALLY@ -OPTIMIZATION_CFLAGS = @OPTIMIZATION_CFLAGS@ PO4A = @PO4A@ PO4A_TRANSLATE = @PO4A_TRANSLATE@ PO4A_FLAGS = @PO4A_FLAGS@ PO4A_TRANSLATE_FLAGS = @PO4A_TRANSLATE_FLAGS@ PO4A_WORKS = @PO4A_WORKS@ -PROFILING_CFLAGS = @PROFILING_CFLAGS@ -PROFILING_LIBS = @PROFILING_LIBS@ PUGIXML_CFLAGS = @PUGIXML_CFLAGS@ PUGIXML_INTERNAL = @PUGIXML_INTERNAL@ PUGIXML_LIBS = @PUGIXML_LIBS@ @@ -140,13 +136,14 @@ ZLIB_LIBS = @ZLIB_LIBS@ # Which additional stuff to compile USE_DRMINGW = @USE_DRMINGW@ USE_QT = @USE_QT@ +USE_DEBUG = @USE_DEBUG@ +USE_PROFILING = @USE_PROFILING@ +USE_OPTIMIZATION = @USE_OPTIMIZATION@ +USE_ADDRSAN = @USE_ADDRSAN@ +USE_UBSAN = @USE_UBSAN@ APPIMAGE_BUILD = @APPIMAGE_BUILD@ BUILD_TOOLS = @BUILD_TOOLS@ BUILD_COMPILATION_DATABASE = no TRANSLATIONS = @TRANSLATIONS@ MANPAGES_TRANSLATIONS = @MANPAGES_TRANSLATIONS@ - -# Debugging aids: compiler sanitization features -ADDRSAN = @ADDRSAN@ -UBSAN = @UBSAN@