Power support



Closes #2139.

--
Reviewed-on: https://github.com/bazelbuild/bazel/pull/2139
PiperOrigin-RevId: 142570236
MOS_MIGRATED_REVID=142570236
diff --git a/scripts/bootstrap/buildenv.sh b/scripts/bootstrap/buildenv.sh
index dbd7d24..4e4d87e 100755
--- a/scripts/bootstrap/buildenv.sh
+++ b/scripts/bootstrap/buildenv.sh
@@ -54,6 +54,10 @@
   MACHINE_IS_Z='yes'
 fi
 
+if [ "${MACHINE_TYPE}" = 'ppc64' -o "${MACHINE_TYPE}" = 'ppc64le' ]; then
+  MACHINE_IS_64BIT='yes'
+fi
+
 # Extension for executables.
 EXE_EXT=""
 case "${PLATFORM}" in
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/DeployArchiveBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/java/DeployArchiveBuilder.java
index 2e1be9d..13bdf5d 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/DeployArchiveBuilder.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/DeployArchiveBuilder.java
@@ -258,7 +258,7 @@
         outputJar, javaStartClass, deployManifestLines, buildInfoArtifacts, classpathResources,
         runtimeClasspath, includeBuildData, compression, launcher);
 
-    List<String> jvmArgs = ImmutableList.of("-client", SINGLEJAR_MAX_MEMORY);
+    List<String> jvmArgs = ImmutableList.of(SINGLEJAR_MAX_MEMORY);
     ResourceSet resourceSet =
         ResourceSet.createWithRamCpuIo(/*memoryMb = */200.0, /*cpuUsage = */.2, /*ioUsage=*/.2);
 
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/mock/MOCK_CROSSTOOL b/src/test/java/com/google/devtools/build/lib/analysis/mock/MOCK_CROSSTOOL
index f0cfb1f..14e38ac 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/mock/MOCK_CROSSTOOL
+++ b/src/test/java/com/google/devtools/build/lib/analysis/mock/MOCK_CROSSTOOL
@@ -24,6 +24,133 @@
   cpu: "x64_windows"
   toolchain_identifier: "local_windows_msys64"
 }
+default_toolchain {
+  cpu: "ppc"
+  toolchain_identifier: "local_linux"
+}
+
+toolchain {
+  abi_version: "local"
+  abi_libc_version: "local"
+  builtin_sysroot: ""
+  compiler: "compiler"
+  host_system_name: "local"
+  needsPic: true
+  supports_gold_linker: false
+  supports_incremental_linker: false
+  supports_fission: false
+  supports_interface_shared_objects: false
+  supports_normalizing_ar: false
+  supports_start_end_lib: false
+  target_libc: "local"
+  target_cpu: "ppc"
+  target_system_name: "local"
+  toolchain_identifier: "local_linux"
+
+  tool_path { name: "ar" path: "/usr/bin/ar" }
+  tool_path { name: "compat-ld" path: "/usr/bin/ld" }
+  tool_path { name: "cpp" path: "/usr/bin/cpp" }
+  tool_path { name: "dwp" path: "/usr/bin/dwp" }
+  tool_path { name: "gcc" path: "/usr/bin/gcc" }
+  cxx_flag: "-std=c++0x"
+  linker_flag: "-lstdc++"
+  linker_flag: "-B/usr/bin/"
+
+  # TODO(bazel-team): In theory, the path here ought to exactly match the path
+  # used by gcc. That works because bazel currently doesn't track files at
+  # absolute locations and has no remote execution, yet. However, this will need
+  # to be fixed, maybe with auto-detection?
+  cxx_builtin_include_directory: "/usr/lib/gcc/"
+  cxx_builtin_include_directory: "/usr/local/include"
+  cxx_builtin_include_directory: "/usr/include"
+  tool_path { name: "gcov" path: "/usr/bin/gcov" }
+
+  # C(++) compiles invoke the compiler (as that is the one knowing where
+  # to find libraries), but we provide LD so other rules can invoke the linker.
+  tool_path { name: "ld" path: "/usr/bin/ld" }
+
+  tool_path { name: "nm" path: "/usr/bin/nm" }
+  tool_path { name: "objcopy" path: "/usr/bin/objcopy" }
+  objcopy_embed_flag: "-I"
+  objcopy_embed_flag: "binary"
+  tool_path { name: "objdump" path: "/usr/bin/objdump" }
+  tool_path { name: "strip" path: "/usr/bin/strip" }
+
+  # Anticipated future default.
+  unfiltered_cxx_flag: "-no-canonical-prefixes"
+  unfiltered_cxx_flag: "-fno-canonical-system-headers"
+
+  # Make C++ compilation deterministic. Use linkstamping instead of these
+  # compiler symbols.
+  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\""
+
+  # Security hardening on by default.
+  # Conservative choice; -D_FORTIFY_SOURCE=2 may be unsafe in some cases.
+  # We need to undef it before redefining it as some distributions now have
+  # it enabled by default.
+  compiler_flag: "-U_FORTIFY_SOURCE"
+  compiler_flag: "-D_FORTIFY_SOURCE=1"
+  compiler_flag: "-fstack-protector"
+  linker_flag: "-Wl,-z,relro,-z,now"
+
+  # Enable coloring even if there's no attached terminal. Bazel removes the
+  # escape sequences if --nocolor is specified. This isn't supported by gcc
+  # on Ubuntu 14.04.
+  # compiler_flag: "-fcolor-diagnostics"
+
+  # All warnings are enabled. Maybe enable -Werror as well?
+  compiler_flag: "-Wall"
+  # Enable a few more warnings that aren't part of -Wall.
+  compiler_flag: "-Wunused-but-set-parameter"
+  # But disable some that are problematic.
+  compiler_flag: "-Wno-free-nonheap-object" # has false positives
+
+  # Keep stack frames for debugging, even in opt mode.
+  compiler_flag: "-fno-omit-frame-pointer"
+
+  # Anticipated future default.
+  linker_flag: "-no-canonical-prefixes"
+  # Have gcc return the exit code from ld.
+  linker_flag: "-pass-exit-codes"
+  # Stamp the binary with a unique identifier.
+  linker_flag: "-Wl,--build-id=md5"
+  linker_flag: "-Wl,--hash-style=gnu"
+  # Gold linker only? Can we enable this by default?
+  # linker_flag: "-Wl,--warn-execstack"
+  # linker_flag: "-Wl,--detect-odr-violations"
+
+  compilation_mode_flags {
+    mode: DBG
+    # Enable debug symbols.
+    compiler_flag: "-g"
+  }
+  compilation_mode_flags {
+    mode: OPT
+
+    # No debug symbols.
+    # Maybe we should enable https://gcc.gnu.org/wiki/DebugFission for opt or
+    # even generally? However, that can't happen here, as it requires special
+    # handling in Bazel.
+    compiler_flag: "-g0"
+
+    # Conservative choice for -O
+    # -O3 can increase binary size and even slow down the resulting binaries.
+    # Profile first and / or use FDO if you need better performance than this.
+    compiler_flag: "-O2"
+
+    # Disable assertions
+    compiler_flag: "-DNDEBUG"
+
+    # Removal of unused code and data at link time (can this increase binary size in some cases?).
+    compiler_flag: "-ffunction-sections"
+    compiler_flag: "-fdata-sections"
+    linker_flag: "-Wl,--gc-sections"
+  }
+  linking_mode_flags { mode: DYNAMIC }
+}
 
 toolchain {
   toolchain_identifier: "k8"
diff --git a/src/test/java/com/google/devtools/build/lib/packages/util/BazelMockCcSupport.java b/src/test/java/com/google/devtools/build/lib/packages/util/BazelMockCcSupport.java
index 47708aa..dd2f474 100644
--- a/src/test/java/com/google/devtools/build/lib/packages/util/BazelMockCcSupport.java
+++ b/src/test/java/com/google/devtools/build/lib/packages/util/BazelMockCcSupport.java
@@ -40,7 +40,7 @@
   private BazelMockCcSupport() {}
 
   private static final ImmutableList<String> CROSSTOOL_ARCHS =
-      ImmutableList.of("piii", "k8", "armeabi-v7a");
+      ImmutableList.of("piii", "k8", "armeabi-v7a", "ppc");
 
   protected static void createBasePackage(MockToolsConfig config) throws IOException {
     config.create(
@@ -89,6 +89,7 @@
         "      'darwin|compiler': ':cc-compiler-darwin',",
         "      'armeabi-v7a|compiler': ':cc-compiler-armeabi-v7a',",
         "      'x64_windows|windows_msys64': ':cc-compiler-x64_windows',",
+        "      'ppc|compiler': ':cc-compiler-ppc',",
         "    })",
         "cc_toolchain(name = 'cc-compiler-k8', all_files = ':empty', compiler_files = ':empty',",
         "    cpu = 'local', dwp_files = ':empty', dynamic_runtime_libs = [':empty'], ",
@@ -96,6 +97,12 @@
         "    module_map = 'crosstool.cppmap', supports_header_parsing = 1,",
         "    objcopy_files = ':empty', static_runtime_libs = [':empty'], strip_files = ':empty',",
         ")",
+        "cc_toolchain(name = 'cc-compiler-ppc', all_files = ':empty', compiler_files = ':empty',",
+        "    cpu = 'ppc', dwp_files = ':empty', dynamic_runtime_libs = [':empty'], ",
+        "    linker_files = ':empty',",
+        "    module_map = 'crosstool.cppmap', supports_header_parsing = 1,",
+        "    objcopy_files = ':empty', static_runtime_libs = [':empty'], strip_files = ':empty',",
+        ")",
         "cc_toolchain(name = 'cc-compiler-piii', all_files = ':empty', compiler_files = ':empty',",
         "    cpu = 'local', dwp_files = ':empty', dynamic_runtime_libs = [':empty'], ",
         "    linker_files = ':empty',",
diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainSuiteTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainSuiteTest.java
index 21fb62c..f53cf57 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainSuiteTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainSuiteTest.java
@@ -40,6 +40,7 @@
         "       'k8|k8-compiler': ':k8-toolchain',",
         "       'darwin|darwin-compiler': ':darwin-toolchain',",
         "       'x64_windows|windows-compiler': ':windows-toolchain',",
+        "       'ppc|compiler': ':local_linux',",
         "    },",
         "    proto = \"\"\"",
         "major_version: 'v1'",
@@ -57,6 +58,10 @@
         "  cpu: 'x64_windows'",
         "  toolchain_identifier: 'windows-toolchain'",
         "}",
+        "default_toolchain {",
+        "  cpu: 'ppc'",
+        "  toolchain_identifier: 'local_linux'",
+        "}",
         "toolchain {",
         "  compiler: 'k8-compiler'",
         "  target_cpu: 'k8'",
@@ -123,6 +128,28 @@
         "  tool_path { name: 'objdump', path: 'windows/objdump' }",
         "  tool_path { name: 'strip', path: 'windows/strip' }",
         "}",
+        "toolchain {",
+        "  abi_version: 'local'",
+        "  abi_libc_version: 'local'",
+        "  builtin_sysroot: ''",
+        "  compiler: 'compiler'",
+        "  host_system_name: 'local'",
+        "  target_libc: 'local'",
+        "  target_cpu: 'ppc'",
+        "  target_system_name: 'local'",
+        "  toolchain_identifier: 'local_linux'",
+        "  tool_path { name: 'ar' path: '/usr/bin/ar' }",
+        "  tool_path { name: 'compat-ld' path: '/usr/bin/ld' }",
+        "  tool_path { name: 'cpp' path: '/usr/bin/cpp' }",
+        "  tool_path { name: 'dwp' path: '/usr/bin/dwp' }",
+        "  tool_path { name: 'gcc' path: '/usr/bin/gcc' }",
+        "  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' }",
+        "}",
         "\"\"\")",
         "cc_toolchain(",
         "    name = 'k8-toolchain',",
@@ -169,7 +196,22 @@
         "    static_runtime_libs = ['windows-static-runtime-libs'])",
         "filegroup(",
         "    name = 'windows-files',",
-        "    srcs = ['windows-marker', 'everything'])");
+        "    srcs = ['windows-marker', 'everything'])",
+        "cc_toolchain(",
+        "    name = 'local_linux',",
+        "    module_map = 'map',",
+        "    cpu = 'cpu',",
+        "    compiler_files = 'compile',",
+        "    dwp_files = 'dwp',",
+        "    linker_files = 'link',",
+        "    strip_files = ':strip',",
+        "    objcopy_files = 'objcopy',",
+        "    all_files = ':linux-files',",
+        "    dynamic_runtime_libs = ['linux-dynamic-runtime-libs'],",
+        "    static_runtime_libs = ['linux-static-runtime-libs'])",
+        "filegroup(",
+        "    name = 'linux-files',",
+        "    srcs = ['linux-marker', 'everything'])");
 
     scratch.file("a/BUILD",
         "genrule(name='a', srcs=[], outs=['ao'], tools=['//tools/defaults:crosstool'], cmd='x')");
@@ -177,10 +219,10 @@
     useConfiguration("--crosstool_top=//cc:suite", "--cpu=k8");
     Action action = getGeneratingAction(getConfiguredTarget("//a:a"), "a/ao");
     assertThat(ActionsTestUtil.baseArtifactNames(action.getInputs()))
-        .containsAllOf("k8-marker", "darwin-marker", "windows-marker");
+        .containsAllOf("k8-marker", "darwin-marker", "windows-marker", "linux-marker");
 
     NestedSet<Artifact> suiteFiles = getFilesToBuild(getConfiguredTarget("//cc:suite"));
     assertThat(ActionsTestUtil.baseArtifactNames(suiteFiles))
-        .containsAllOf("k8-marker", "darwin-marker", "windows-marker");
+        .containsAllOf("k8-marker", "darwin-marker", "windows-marker", "linux-marker");
   }
 }
diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CrosstoolConfigurationHelper.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CrosstoolConfigurationHelper.java
index cf858ac..ada901e 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CrosstoolConfigurationHelper.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CrosstoolConfigurationHelper.java
@@ -15,6 +15,7 @@
 package com.google.devtools.build.lib.rules.cpp;
 
 import com.google.devtools.build.lib.analysis.util.AnalysisMock;
+import com.google.devtools.build.lib.util.CPU;
 import com.google.devtools.build.lib.util.OS;
 import com.google.devtools.build.lib.vfs.FileSystemUtils;
 import com.google.devtools.build.lib.vfs.Path;
@@ -22,7 +23,6 @@
 import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.DefaultCpuToolchain;
 import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.ToolPath;
 import com.google.protobuf.TextFormat;
-
 import java.io.IOException;
 
 /**
@@ -59,6 +59,21 @@
   public static String defaultCpu() {
     if (OS.getCurrent() == OS.WINDOWS) {
       return "x64_windows";
+    } else if (OS.getCurrent() == OS.LINUX) {
+      switch (CPU.getCurrent()) {
+        case X86_32:
+          return "piii";
+        case X86_64:
+          return "k8";
+        case PPC:
+          return "ppc";
+        case ARM:
+          return "arm";
+        case S390X:
+          return "s390x";
+        default:
+          return "unknown";
+      }
     }
     return OS.getCurrent() == OS.DARWIN ? "darwin" : "k8";
   }
diff --git a/src/test/shell/bazel/bazel_toolchain_test.sh b/src/test/shell/bazel/bazel_toolchain_test.sh
index 67a5ac6..880c5a4 100755
--- a/src/test/shell/bazel/bazel_toolchain_test.sh
+++ b/src/test/shell/bazel/bazel_toolchain_test.sh
@@ -22,8 +22,8 @@
 source "${CURRENT_DIR}/../integration_test_setup.sh" \
   || { echo "integration_test_setup.sh not found!" >&2; exit 1; }
 
-if [ "${PLATFORM-}" = "darwin" ]; then
-  echo "Skipping test: linaro toolchain is not supported on darwin host."
+if ! [ "${PLATFORM-}" = "linux" && "${MACHINE_TYPE}" = "x86_64" ]; then
+  echo "Skipping test: linaro toolchain is not supported on this platform"
   exit 0
 fi
 
diff --git a/src/tools/singlejar/diag.h b/src/tools/singlejar/diag.h
index 9085e12..8f61b66 100644
--- a/src/tools/singlejar/diag.h
+++ b/src/tools/singlejar/diag.h
@@ -19,7 +19,7 @@
  * Various useful diagnostics functions from Linux err.h file, wrapped
  * for portability.
  */
-#if defined(__APPLE__) || defined(__linux)
+#if defined(__APPLE__) || defined(__linux__)
 #include <err.h>
 #define diag_err(...) err(__VA_ARGS__)
 #define diag_errx(...) errx(__VA_ARGS__)
diff --git a/src/tools/singlejar/mapped_file.h b/src/tools/singlejar/mapped_file.h
index 6338994..e840d96 100644
--- a/src/tools/singlejar/mapped_file.h
+++ b/src/tools/singlejar/mapped_file.h
@@ -35,7 +35,7 @@
  *
  * The implementation is 64-bit Linux or OSX specific.
  */
-#if !((defined(__linux) || defined(__APPLE__)) && __SIZEOF_POINTER__ == 8)
+#if !((defined(__linux__) || defined(__APPLE__)) && __SIZEOF_POINTER__ == 8)
 #error This code for 64 bit Unix.
 #endif
 
diff --git a/src/tools/singlejar/zip_headers.h b/src/tools/singlejar/zip_headers.h
index 226c7cb..8947fc6 100644
--- a/src/tools/singlejar/zip_headers.h
+++ b/src/tools/singlejar/zip_headers.h
@@ -24,7 +24,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-#if defined(__linux)
+#if defined(__linux__)
 #include <endian.h>
 #elif defined(__APPLE__)
 // Hopefully OSX will keep running solely on little endian CPUs, so:
diff --git a/third_party/BUILD b/third_party/BUILD
index 3fa3a31..d57f0df 100644
--- a/third_party/BUILD
+++ b/third_party/BUILD
@@ -582,3 +582,8 @@
     name = "s390x",
     values = {"host_cpu": "s390x"},
 )
+
+config_setting(
+    name = "ppc",
+    values = {"host_cpu": "ppc"},
+)
diff --git a/tools/cpp/BUILD b/tools/cpp/BUILD
index a3c8cbe..4e93a31 100644
--- a/tools/cpp/BUILD
+++ b/tools/cpp/BUILD
@@ -38,6 +38,7 @@
         "local|compiler": ":cc-compiler-local",
         "x64_windows|compiler": ":cc-compiler-x64_windows",
         "x64_windows_msvc|compiler": ":cc-compiler-x64_windows_msvc",
+        "ppc|compiler": ":cc-compiler-ppc",
     },
 )
 
@@ -56,6 +57,20 @@
 )
 
 cc_toolchain(
+    name = "cc-compiler-ppc",
+    all_files = ":empty",
+    compiler_files = ":empty",
+    cpu = "ppc",
+    dwp_files = ":empty",
+    dynamic_runtime_libs = [":empty"],
+    linker_files = ":empty",
+    objcopy_files = ":empty",
+    static_runtime_libs = [":empty"],
+    strip_files = ":empty",
+    supports_param_files = 1,
+)
+
+cc_toolchain(
     name = "cc-compiler-armeabi-v7a",
     all_files = ":empty",
     compiler_files = ":empty",
diff --git a/tools/cpp/CROSSTOOL b/tools/cpp/CROSSTOOL
index dd63b3e..0922cf3 100644
--- a/tools/cpp/CROSSTOOL
+++ b/tools/cpp/CROSSTOOL
@@ -39,6 +39,10 @@
   toolchain_identifier: "local_linux"
 }
 
+default_toolchain {
+  cpu: "ppc"
+  toolchain_identifier: "local_linux"
+}
 toolchain {
   abi_version: "armeabi-v7a"
   abi_libc_version: "armeabi-v7a"
diff --git a/tools/cpp/cc_configure.bzl b/tools/cpp/cc_configure.bzl
index 2c3b974..8d33a67 100644
--- a/tools/cpp/cc_configure.bzl
+++ b/tools/cpp/cc_configure.bzl
@@ -140,6 +140,8 @@
     return "x64_windows"
   # Use uname to figure out whether we are on x86_32 or x86_64
   result = repository_ctx.execute(["uname", "-m"])
+  if result.stdout.strip() in ["power", "ppc64le", "ppc"]:
+    return "ppc"
   return "k8" if result.stdout.strip() in ["amd64", "x86_64", "x64"] else "piii"