Bug 1309505 - Add warning flags to gyp build, r=franziskus
authorMartin Thomson <martin.thomson@gmail.com>
Fri, 21 Oct 2016 13:22:12 +1100
changeset 12750 a1d2234bed689864b7b0007e5d422f2aef89f453
parent 12749 e6ad34c4144f35d47eb8986a4094d866cb1093c8
child 12751 cbeabbf0fb44ae6341996a91d51d20ccf53b5ff0
push id1692
push usermartin.thomson@gmail.com
push dateMon, 24 Oct 2016 11:19:37 +0000
reviewersfranziskus
bugs1309505
Bug 1309505 - Add warning flags to gyp build, r=franziskus
automation/taskcluster/scripts/build_gyp.sh
build.sh
coreconf/config.gypi
coreconf/werror.py
--- a/automation/taskcluster/scripts/build_gyp.sh
+++ b/automation/taskcluster/scripts/build_gyp.sh
@@ -6,16 +6,13 @@ if [[ $(id -u) -eq 0 ]]; then
     # Drop privileges by re-running this script.
     exec su worker $0
 fi
 
 # Clone NSPR if needed.
 hg_clone https://hg.mozilla.org/projects/nspr nspr default
 
 # Build.
-cd nss && ./build.sh -g
-if [ $? != 0 ]; then
-    exit 1
-fi
+nss/build.sh -g -v
 
 # Package.
-cd .. && mkdir artifacts
+mkdir artifacts
 tar cvfjh artifacts/dist.tar.bz2 dist
--- a/build.sh
+++ b/build.sh
@@ -1,55 +1,70 @@
 #!/bin/bash
 # This script builds NSS with gyp and ninja.
 #
 # This build system is still under development.  It does not yet support all
 # the features or platforms that NSS supports.
 #
 # -c = clean before build
 # -g = force a rebuild of gyp (and NSPR, because why not)
+# -v = verbose build
 
 set -e
 
 CWD=$(cd $(dirname $0); pwd -P)
-OBJ_DIR=$(make platform)
+OBJ_DIR=$(cd "$CWD";make platform)
 DIST_DIR="$CWD/../dist/$OBJ_DIR"
 
 if [ -n "$CCC" ] && [ -z "$CXX" ]; then
     export CXX="$CCC"
 fi
 
+while [ $# -gt 0 ]; do
+    case $1 in
+        -c) CLEAN=1 ;;
+        -g) REBUILD_GYP=1 ;;
+        -v) VERBOSE=1 ;;
+    esac
+    shift
+done
+
 # -c = clean first
-if [ "$1" = "-c" ]; then
+if [ "$CLEAN" = 1 ]; then
     rm -rf "$CWD/out"
 fi
 
 if [ "$BUILD_OPT" = "1" ]; then
     TARGET=Release
 else
     TARGET=Debug
 fi
-TARGET_DIR="$CWD/out/$TARGET"
-if [ "$USE_64" != "1" ]; then
+if [ "$USE_64" == "1" ]; then
+    TARGET="${TARGET}_x64"
+else
     GYP_PARAMS="-Dtarget_arch=ia32"
 fi
+TARGET_DIR="$CWD/out/$TARGET"
 
 # These steps can take a while, so don't overdo them.
 # Force a redo with -g.
-if [ "$1" = "-g" -o ! -d "$TARGET_DIR" ]; then
+if [ "$REBUILD_GYP" = 1 -o ! -d "$TARGET_DIR" ]; then
     # Build NSPR.
-    make NSS_GYP=1 install_nspr
+    make -C "$CWD" NSS_GYP=1 install_nspr
 
     # Run gyp.
     PKG_CONFIG_PATH="$CWD/../nspr/$OBJ_DIR/config" $SCANBUILD \
-        gyp -f ninja $GYP_PARAMS --depth=. --generator-output="." nss.gyp
+        gyp -f ninja $GYP_PARAMS --depth="$CWD" --generator-output="." "$CWD/nss.gyp"
 fi
 
 # Run ninja.
 if which ninja >/dev/null 2>&1; then
     NINJA=ninja
 elif which ninja-build >/dev/null 2>&1; then
     NINJA=ninja-build
 else
     echo "Please install ninja" 1>&2
     exit 1
 fi
+if [ "$VERBOSE" = 1 ]; then
+    NINJA="$NINJA -v"
+fi
 $NINJA -C "$TARGET_DIR"
--- a/coreconf/config.gypi
+++ b/coreconf/config.gypi
@@ -81,25 +81,27 @@
     'nspr_include_dir%': '<(nspr_include_dir)',
     'nss_dist_obj_dir%': '<(nss_dist_obj_dir)',
     'nss_dist_dir%': '<(nss_dist_dir)',
     'use_system_sqlite%': '<(use_system_sqlite)',
     'sqlite_libs%': ['-lsqlite3'],
     'dll_prefix': '<(dll_prefix)',
     'dll_suffix': '<(dll_suffix)',
     'cc_is_clang%': '<(cc_is_clang)',
+    # Some defaults
     'disable_tests%': 0,
     'disable_chachapoly%': 0,
     'disable_dbm%': 0,
     'disable_libpkix%': 0,
-    'ssl_enable_zlib%': 1,
-    'use_asan%': 0,
+    'disable_werror%': 0,
     'mozilla_client%': 0,
     'moz_fold_libs%': 0,
     'moz_folded_library_name%': '',
+    'ssl_enable_zlib%': 1,
+    'use_asan%': 0,
   },
   'target_defaults': {
     # Settings specific to targets should go here.
     # This is mostly for linking to libraries.
     'variables': {
       'mapfile%': '',
     },
     'include_dirs': [
@@ -221,22 +223,19 @@
         },
         'xcode_settings': {
           'CLANG_CXX_LANGUAGE_STANDARD': 'c++0x',
           'OTHER_CFLAGS': [
             '-fPIC',
             '-fno-common',
             '-pipe',
           ],
-          # TODO:
-          # 'GCC_TREAT_WARNINGS_AS_ERRORS'
-          # 'WARNING_CFLAGS'
         },
         'conditions': [
-          ['OS=="linux" or OS=="android"', {
+          [ 'OS=="linux" or OS=="android"', {
             'defines': [
               'LINUX2_1',
               'LINUX',
               'linux',
               'HAVE_STRERROR',
               'XP_UNIX',
               '_REENTRANT',
             ],
@@ -257,16 +256,21 @@
                 'ldflags': ['-m32'],
               }],
               [ 'target_arch=="x64"', {
                 'cflags': ['-m64'],
                 'ldflags': ['-m64'],
               }],
             ],
           }],
+          [ 'disable_werror==0 and (OS=="linux" or OS=="mac")', {
+            'cflags': [
+              '<!@(<(python) <(DEPTH)/coreconf/werror.py)',
+            ],
+          }],
           [ 'OS=="android" and mozilla_client==0', {
             'defines': [
               'NO_SYSINFO',
               'NO_FORK_CHECK',
               'ANDROID',
             ],
           }],
           [ 'OS=="mac"', {
@@ -293,16 +297,25 @@
             'defines': [
               '_WINDOWS',
               'WIN95',
               '_CRT_SECURE_NO_WARNINGS',
               '_CRT_NONSTDC_NO_WARNINGS',
             ],
             'cflags': [
               '-W3',
+              '-w44267', # Disable C4267: conversion from 'size_t' to 'type', possible loss of data
+              '-w44244', # Disable C4244: conversion from 'type1' to 'type2', possible loss of data
+              '-w44018', # Disable C4018: 'expression' : signed/unsigned mismatch
+              '-w44312', # Disable C4312: 'type cast': conversion from 'type1' to 'type2' of greater size
+            ],
+            'conditions': [
+              [ 'disable_werror==0', {
+                'cflags': ['-WX']
+              }]
             ],
           }],
           [ 'disable_dbm==1', {
             'defines': [
               'NSS_DISABLE_DBM',
             ],
           }],
           [ 'disable_libpkix==1', {
new file mode 100644
--- /dev/null
+++ b/coreconf/werror.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python
+
+import os
+import subprocess
+
+def main():
+    cc = os.environ.get('CC', 'cc')
+    sink = open(os.devnull, 'wb')
+    cc_is_clang = 'clang' in subprocess.check_output([cc, '--version'], stderr=sink)
+
+    def warning_supported(warning):
+        return subprocess.call([cc, '-x', 'c', '-E', '-Werror',
+                                '-W%s' % warning, os.devnull], stdout=sink, stderr=sink) == 0
+    def can_enable():
+        # This would be a problem
+        if not warning_supported('all'):
+            return False
+
+        # If we aren't clang, make sure we have gcc 4.8 at least
+        if not cc_is_clang:
+            try:
+                v = subprocess.check_output([cc, '-dumpversion'], stderr=sink)
+                v = v.strip(' \r\n').split('.')
+                if v[0] < 4 or (v[0] == 4 and v[1] < 8):
+                    # gcc 4.8 minimum
+                    return False
+            except OSError:
+                return False
+        return True
+
+    if not can_enable():
+        print('-DNSS_NO_GCC48')
+        return
+
+    print('-Werror')
+    print('-Wall')
+
+    def set_warning(warning, contra=''):
+        if warning_supported(warning):
+            print('-W%s%s' % (contra, warning))
+
+    if cc_is_clang:
+        # clang is unable to handle glib's expansion of strcmp and similar for
+        # optimized builds, so disable the resulting errors.
+        # See https://llvm.org/bugs/show_bug.cgi?id=20144
+        for w in ['array-bounds', 'unevaluated-expression',
+                  'parentheses-equality']:
+            set_warning(w, 'no-')
+        print('-Qunused-arguments')
+
+    # set_warning('shadow') # Bug 1309068
+
+if __name__ == '__main__':
+    main()