From e9516b7c13c39b69de74beb2aeda7509f1757fb1 Mon Sep 17 00:00:00 2001 From: David German Date: Mon, 9 Jan 2017 15:25:58 -0500 Subject: [PATCH] Add compiler-specific warnings in Bazel. --- WORKSPACE | 4 ++ tools/BUILD | 25 +++++++++++ tools/CROSSTOOL | 97 +++++++++++++++++++++++++++++++++++++++- tools/amend_crosstool.sh | 7 +++ tools/bazel.rc | 17 +++++++ tools/bullet.BUILD | 11 ++++- tools/gtest.BUILD | 5 ++- tools/lcm.BUILD | 1 + tools/toolchain.bzl | 22 +++++++++ 9 files changed, 186 insertions(+), 3 deletions(-) create mode 100755 tools/amend_crosstool.sh create mode 100644 tools/toolchain.bzl diff --git a/WORKSPACE b/WORKSPACE index ecdd64307cb7..a5b1e28da08d 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -130,3 +130,7 @@ soft_failure_binary_repository( name = "drake_visualizer", local_path = __workspace_dir__ + "/build/install/bin/drake-visualizer", ) + + +load("//tools:toolchain.bzl", "drake_toolchain") +drake_toolchain() diff --git a/tools/BUILD b/tools/BUILD index 9705190438b6..34bfc86acb4e 100644 --- a/tools/BUILD +++ b/tools/BUILD @@ -21,15 +21,40 @@ alias( cc_toolchain_suite( name = "default-toolchain", toolchains = { + "k8|compiler": "cc-compiler-gcc-linux", "k8|clang-3.9": "cc-compiler-clang-3.9-linux", }, ) +sh_binary( + name = "amend_crosstool", + srcs = ["amend_crosstool.sh"], +) + filegroup( name = "empty", srcs = [], ) +filegroup( + name = "gcc_wrapper", + srcs = ["gcc_wrapper.sh"], +) + +cc_toolchain( + name = "cc-compiler-gcc-linux", + all_files = ":gcc_wrapper", + compiler_files = ":gcc_wrapper", + cpu = "k8", + dwp_files = ":empty", + dynamic_runtime_libs = [":empty"], + linker_files = ":gcc_wrapper", + objcopy_files = ":empty", + static_runtime_libs = [":empty"], + strip_files = ":empty", + supports_param_files = 0, +) + cc_toolchain( name = "cc-compiler-clang-3.9-linux", all_files = ":empty", diff --git a/tools/CROSSTOOL b/tools/CROSSTOOL index d02c4c26cbe6..6c0b27663a09 100644 --- a/tools/CROSSTOOL +++ b/tools/CROSSTOOL @@ -3,9 +3,101 @@ minor_version: "" default_target_cpu: "same_as_host" default_toolchain { cpu: "k8" - toolchain_identifier: "clang-3.9-linux" + toolchain_identifier: "gcc-linux" +} + +# System GCC on Linux +# Supported versions: 4.9 on Trusty, 5.4 on Xenial +toolchain { + toolchain_identifier: "gcc-linux" + abi_libc_version: "local" + abi_version: "local" + builtin_sysroot: "" + compiler: "compiler" + compiler_flag: "-U_FORTIFY_SOURCE" + compiler_flag: "-D_FORTIFY_SOURCE=1" + compiler_flag: "-fstack-protector" + compiler_flag: "-Wall" + compiler_flag: "-Wl,-z,-relro,-z,now" + compiler_flag: "-B/usr/bin" + compiler_flag: "-B/usr/bin" + compiler_flag: "-Wunused-but-set-parameter" + compiler_flag: "-Wno-free-nonheap-object" + compiler_flag: "-fno-canonical-system-headers" + compiler_flag: "-fno-omit-frame-pointer" + cxx_builtin_include_directory: "/usr/include/c++/4.9" + cxx_builtin_include_directory: "/usr/include/x86_64-linux-gnu/c++/4.9" + cxx_builtin_include_directory: "/usr/include/c++/4.9/backward" + cxx_builtin_include_directory: "/usr/lib/gcc/x86_64-linux-gnu/4.9/include" + cxx_builtin_include_directory: "/usr/local/include" + cxx_builtin_include_directory: "/usr/lib/gcc/x86_64-linux-gnu/4.9/include-fixed" + cxx_builtin_include_directory: "/usr/include/x86_64-linux-gnu" + cxx_builtin_include_directory: "/usr/include" + cxx_flag: "-std=c++1y" + host_system_name: "local" + linker_flag: "-lstdc++" + linker_flag: "-lm" + linker_flag: "-Wl,-no-as-needed" + linker_flag: "-B/usr/bin" + linker_flag: "-B/usr/bin" + linker_flag: "-pass-exit-codes" + linker_flag: "-Wl,--build-id=md5" + linker_flag: "-Wl,--hash-style=gnu" + needsPic: true + objcopy_embed_flag: "-I" + objcopy_embed_flag: "binary" + supports_fission: false + supports_gold_linker: false + supports_incremental_linker: false + supports_interface_shared_objects: false + supports_normalizing_ar: false + supports_start_end_lib: false + target_cpu: "k8" + target_libc: "local" + target_system_name: "local" + unfiltered_cxx_flag: "-fno-canonical-system-headers" + unfiltered_cxx_flag: "-Wno-builtin-macro-redefined" + unfiltered_cxx_flag: "-D__DATE__=\"redacted\"" + unfiltered_cxx_flag: "-D__TIMESTAMP__=\"redacted\"" + unfiltered_cxx_flag: "-D__TIME__=\"redacted\"" + tool_path {name: "ar" path: "/usr/bin/ar" } + tool_path {name: "cpp" path: "/usr/bin/cpp" } + tool_path {name: "dwp" path: "/usr/bin/dwp" } + tool_path {name: "gcc" path: "/usr/bin/gcc-4.9" } + tool_path {name: "gcov" path: "/usr/bin/gcov" } + tool_path {name: "ld" path: "/usr/bin/ld" } + tool_path {name: "nm" path: "/usr/bin/nm" } + tool_path {name: "objcopy" path: "/usr/bin/objcopy" } + tool_path {name: "objdump" path: "/usr/bin/objdump" } + tool_path {name: "strip" path: "/usr/bin/strip" } + + compilation_mode_flags { + mode: DBG + compiler_flag: "-g" + } + compilation_mode_flags { + mode: OPT + compiler_flag: "-g0" + compiler_flag: "-O2" + compiler_flag: "-DNDEBUG" + compiler_flag: "-ffunction-sections" + compiler_flag: "-fdata-sections" + linker_flag: "-Wl,--gc-sections" + } + linking_mode_flags { mode: DYNAMIC } + + # Upgrade a bunch of warnings to errors. + cxx_flag: "-Werror=all" + cxx_flag: "-Werror=extra" + cxx_flag: "-Werror=ignored-qualifiers" + cxx_flag: "-Werror=overloaded-virtual" + cxx_flag: "-Werror=return-local-addr" + cxx_flag: "-Wno-unused-parameter" + cxx_flag: "-Wno-missing-field-initializers" } + +# Clang 3.9 on Linux toolchain { abi_version: "local" abi_libc_version: "local" @@ -48,6 +140,9 @@ toolchain { compiler_flag: "-fno-omit-frame-pointer" compiler_flag: "-O2" } + + compiler_flag: "-B/usr/bin/" + linker_flag: "-lstdc++" linker_flag: "-lm" linker_flag: "-B/usr/bin/" diff --git a/tools/amend_crosstool.sh b/tools/amend_crosstool.sh new file mode 100755 index 000000000000..1a9527355673 --- /dev/null +++ b/tools/amend_crosstool.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +if grep clang "$1"; then + +else + +fi \ No newline at end of file diff --git a/tools/bazel.rc b/tools/bazel.rc index 984f9046bf6c..3aa3313451a1 100644 --- a/tools/bazel.rc +++ b/tools/bazel.rc @@ -4,8 +4,25 @@ build -c opt # Default build options. build --strip=never +build --crosstool_top=//external:drake_toolchain + +# Compilation flags that apply on all supported compilers. build --copt -Werror=all build --cxxopt -std=c++1y +build --cxxopt -Werror=ignored-qualifiers +build --cxxopt -Werror=overloaded-virtual + +# Compilation flags that apply only on clang. +build --cxxopt -Werror=shadow +build --cxxopt -Werror=inconsistent-missing-override +build --cxxopt -Werror=sign-compare +build --cxxopt -Werror=return-stack-address + +# Compilation flags that apply only on GCC. +build --cxxopt -Werror=extra +build --cxxopt -Werror=return-local-addr +build --cxxopt -Wno-unused-parameter +build --cxxopt -Wno-missing-field-initializers # Default test options. test --test_output=errors diff --git a/tools/bullet.BUILD b/tools/bullet.BUILD index 50fbf36b650a..cd3733bcf724 100644 --- a/tools/bullet.BUILD +++ b/tools/bullet.BUILD @@ -1,5 +1,14 @@ # -*- python -*- + +# These options are used when building Bullet C/C++ code. +# They do not flow downstream to Bullet-using libraries. +BULLET_COPTS = [ + "-Wno-all", + "-Wno-extra", + "-Wno-overloaded-virtual", +] + # Note that this is only a portion of Bullet. cc_library( name = "lib", @@ -11,7 +20,7 @@ cc_library( "src/BulletCollision/**/*.h", "src/LinearMath/**/*.h", ]) + ["src/btBulletCollisionCommon.h"], - copts = ["-Wno-all"], + copts = BULLET_COPTS, defines = ["BT_USE_DOUBLE_PRECISION"], includes = ["src"], visibility = ["//visibility:public"], diff --git a/tools/gtest.BUILD b/tools/gtest.BUILD index 4c9ce1eedb8a..f2cf1985cf69 100644 --- a/tools/gtest.BUILD +++ b/tools/gtest.BUILD @@ -10,7 +10,10 @@ cc_library( "include/**/*.h", "src/*.h", ]), - copts = ["-Wno-unused-const-variable"], + copts = [ + "-Wno-unused-const-variable", + "-Wno-missing-field-initializers", + ], defines = [ "GTEST_DONT_DEFINE_FAIL=1", "GTEST_DONT_DEFINE_SUCCEED=1", diff --git a/tools/lcm.BUILD b/tools/lcm.BUILD index c1747330ca8d..0c7bb34aef7c 100644 --- a/tools/lcm.BUILD +++ b/tools/lcm.BUILD @@ -21,6 +21,7 @@ LCM_COPTS = [ "-Wno-all", "-Wno-deprecated-declarations", "-Wno-format-zero-length", + "-Wno-sign-compare", "-std=gnu11", ] diff --git a/tools/toolchain.bzl b/tools/toolchain.bzl new file mode 100644 index 000000000000..4840149a79f0 --- /dev/null +++ b/tools/toolchain.bzl @@ -0,0 +1,22 @@ +# -*- python -*- +# This is a Bazel repository_rule for auto-configuring the C++ toolchain. +# https://www.bazel.io/versions/master/docs/skylark/repository_rules.html +# It tweaks the default auto-configuration to add extra compiler flags. +# https://github.com/bazelbuild/bazel/blob/master/tools/cpp/cc_configure.bzl + +def _drake_toolchain_impl(repository_ctx): + print("in _drake_toolchain_impl") + repository_ctx.file("scratch", content="foo") + repository_ctx.symlink(Label("@local_config_cc//:BUILD"), "BUILD") + repository_ctx.symlink(Label("@local_config_cc//:CROSSTOOL"), "CROSSTOOL.generic") + repository_ctx.symlink(Label("//tools:amend_crosstool"), "amend_crosstool.sh") + repository_ctx.execute(["bash", "amend_crosstool.sh", "CROSSTOOL.generic"]) + +drake_toolchain_autoconf = repository_rule( + local = True, + implementation = _drake_toolchain_impl, +) + +def drake_toolchain(): + drake_toolchain_autoconf(name="local_config_drake") + native.bind(name="drake_toolchain", actual="@local_config_drake//:toolchain")