From 306eea5e59c982c25b6f932b45dee4504e28bed3 Mon Sep 17 00:00:00 2001
From: Eric <muffgaga@gmx.de>
Date: Tue, 18 Oct 2016 16:46:42 +0200
Subject: [PATCH] Support for bazel (#2023)

* Add package bazel

* Add support for bazel-based builds
---
 lib/spack/spack/cmd/create.py                 |  11 +-
 .../builtin/packages/bazel/cc_configure.patch |  28 ++++
 .../packages/bazel/fix_env_handling.patch     | 119 ++++++++++++++++
 .../repos/builtin/packages/bazel/link.patch   | 133 ++++++++++++++++++
 .../repos/builtin/packages/bazel/package.py   |  89 ++++++++++++
 5 files changed, 379 insertions(+), 1 deletion(-)
 create mode 100644 var/spack/repos/builtin/packages/bazel/cc_configure.patch
 create mode 100644 var/spack/repos/builtin/packages/bazel/fix_env_handling.patch
 create mode 100644 var/spack/repos/builtin/packages/bazel/link.patch
 create mode 100644 var/spack/repos/builtin/packages/bazel/package.py

diff --git a/lib/spack/spack/cmd/create.py b/lib/spack/spack/cmd/create.py
index aa7c42def0..741a320ea7 100644
--- a/lib/spack/spack/cmd/create.py
+++ b/lib/spack/spack/cmd/create.py
@@ -116,6 +116,10 @@ def install(self, spec, prefix):
     # FIXME: Add additional dependencies if required.
     depends_on('scons', type='build')""",
 
+    'bazel': """\
+    # FIXME: Add additional dependencies if required.
+    depends_on('bazel', type='build')""",
+
     'python': """\
     extends('python')
 
@@ -164,6 +168,10 @@ def install(self, spec, prefix):
         scons('prefix={0}'.format(prefix))
         scons('install')""",
 
+    'bazel': """\
+        # FIXME: Add logic to build and install here.
+        bazel()""",
+
     'python': """\
         # FIXME: Add logic to build and install here.
         setup_py('install', '--prefix={0}'.format(prefix))""",
@@ -238,7 +246,8 @@ def __call__(self, stage, url):
             (r'/CMakeLists.txt$', 'cmake'),
             (r'/SConstruct$',     'scons'),
             (r'/setup.py$',       'python'),
-            (r'/NAMESPACE$',      'R')
+            (r'/NAMESPACE$',      'R'),
+            (r'/WORKSPACE$',      'bazel')
         ]
 
         # Peek inside the compressed file.
diff --git a/var/spack/repos/builtin/packages/bazel/cc_configure.patch b/var/spack/repos/builtin/packages/bazel/cc_configure.patch
new file mode 100644
index 0000000000..3e108c3b5e
--- /dev/null
+++ b/var/spack/repos/builtin/packages/bazel/cc_configure.patch
@@ -0,0 +1,28 @@
+--- bazel-0.3.1/tools/cpp/cc_configure.bzl	2016-10-13 14:00:32.118358387 +0200
++++ bazel-0.3.1/tools/cpp/cc_configure.bzl	2016-10-13 13:52:45.342610147 +0200
+@@ -173,8 +173,23 @@
+   else:
+     inc_dirs = result.stderr[index1 + 1:index2].strip()
+ 
+-  return [repository_ctx.path(_cxx_inc_convert(p))
+-          for p in inc_dirs.split("\n")]
++  default_inc_directories = [
++    repository_ctx.path(_cxx_inc_convert(p))
++    for p in inc_dirs.split("\n")
++  ]
++
++  env = repository_ctx.os.environ
++  if "SPACK_DEPENDENCIES" in env:
++    for dep in env["SPACK_DEPENDENCIES"].split(":"):
++      path = dep + "/include"
++      # path = repository_ctx.os.path.join(dep, "include")
++      # if not repository_ctx.os.path.exists(path):
++      #   continue
++      default_inc_directories.append(
++        repository_ctx.path(_cxx_inc_convert(path))
++      )
++
++  return default_inc_directories
+ 
+ def _add_option_if_supported(repository_ctx, cc, option):
+   """Checks that `option` is supported by the C compiler."""
diff --git a/var/spack/repos/builtin/packages/bazel/fix_env_handling.patch b/var/spack/repos/builtin/packages/bazel/fix_env_handling.patch
new file mode 100644
index 0000000000..9be9f97d69
--- /dev/null
+++ b/var/spack/repos/builtin/packages/bazel/fix_env_handling.patch
@@ -0,0 +1,119 @@
+diff -pu bazel-0.3.1/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelConfiguration.java bazel-0.3.1/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelConfiguration.java 
+--- bazel-0.3.1/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelConfiguration.java 2016-09-14 11:56:01.565756979 +0200
++++ bazel-0.3.1/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelConfiguration.java	2016-09-14 12:04:13.292839801 +0200
+@@ -92,5 +92,115 @@ public class BazelConfiguration extends
+     if (tmpdir != null) {
+       builder.put("TMPDIR", tmpdir);
+     }
++
++    String spack_prefix = System.getenv("SPACK_PREFIX");
++    if (spack_prefix != null) {
++      builder.put("SPACK_PREFIX", spack_prefix);
++    }
++
++    String spack_env_path = System.getenv("SPACK_ENV_PATH");
++    if (spack_env_path != null) {
++      builder.put("SPACK_ENV_PATH", spack_env_path);
++    }
++
++    String spack_debug_log_dir = System.getenv("SPACK_DEBUG_LOG_DIR");
++    if (spack_debug_log_dir != null) {
++      builder.put("SPACK_DEBUG_LOG_DIR", spack_debug_log_dir);
++    }
++
++    String spack_compiler_spec = System.getenv("SPACK_COMPILER_SPEC");
++    if (spack_compiler_spec != null) {
++      builder.put("SPACK_COMPILER_SPEC", spack_compiler_spec);
++    }
++
++    String spack_cc_rpath_arg = System.getenv("SPACK_CC_RPATH_ARG");
++    if (spack_cc_rpath_arg != null) {
++      builder.put("SPACK_CC_RPATH_ARG", spack_cc_rpath_arg);
++    }
++
++    String spack_cxx_rpath_arg = System.getenv("SPACK_CXX_RPATH_ARG");
++    if (spack_cxx_rpath_arg != null) {
++      builder.put("SPACK_CXX_RPATH_ARG", spack_cxx_rpath_arg);
++    }
++
++    String spack_f77_rpath_arg = System.getenv("SPACK_F77_RPATH_ARG");
++    if (spack_f77_rpath_arg != null) {
++      builder.put("SPACK_F77_RPATH_ARG", spack_f77_rpath_arg);
++    }
++
++    String spack_fc_rpath_arg = System.getenv("SPACK_FC_RPATH_ARG");
++    if (spack_fc_rpath_arg != null) {
++      builder.put("SPACK_FC_RPATH_ARG", spack_fc_rpath_arg);
++    }
++
++    String spack_short_spec = System.getenv("SPACK_SHORT_SPEC");
++    if (spack_short_spec != null) {
++      builder.put("SPACK_SHORT_SPEC", spack_short_spec);
++    }
++
++    String spack_cc = System.getenv("SPACK_CC");
++    if (spack_cc != null) {
++      builder.put("SPACK_CC", spack_cc);
++    }
++
++    String spack_cxx = System.getenv("SPACK_CXX");
++    if (spack_cxx != null) {
++      builder.put("SPACK_CXX", spack_cxx);
++    }
++
++    String spack_f77 = System.getenv("SPACK_F77");
++    if (spack_f77 != null) {
++      builder.put("SPACK_F77", spack_f77);
++    }
++
++    String spack_fc = System.getenv("SPACK_FC");
++    if (spack_fc != null) {
++      builder.put("SPACK_FC", spack_fc);
++    }
++
++    String spack_cflags = System.getenv("SPACK_CFLAGS");
++    if (spack_cflags != null) {
++      builder.put("SPACK_CFLAGS", spack_cflags);
++    }
++
++    String spack_cxxflags = System.getenv("SPACK_CXXFLAGS");
++    if (spack_cxxflags != null) {
++      builder.put("SPACK_CXXFLAGS", spack_cxxflags);
++    }
++
++    String spack_fcflags = System.getenv("SPACK_FCFLAGS");
++    if (spack_fcflags != null) {
++      builder.put("SPACK_FCFLAGS", spack_fcflags);
++    }
++
++    String spack_fflags = System.getenv("SPACK_FFLAGS");
++    if (spack_fflags != null) {
++      builder.put("SPACK_FFLAGS", spack_fflags);
++    }
++
++    String spack_ldflags = System.getenv("SPACK_LDFLAGS");
++    if (spack_ldflags != null) {
++      builder.put("SPACK_LDFLAGS", spack_ldflags);
++    }
++
++    String spack_ldlibs = System.getenv("SPACK_LDLIBS");
++    if (spack_ldlibs != null) {
++      builder.put("SPACK_LDLIBS", spack_ldlibs);
++    }
++
++    String spack_debug = System.getenv("SPACK_DEBUG");
++    if (spack_debug != null) {
++      builder.put("SPACK_DEBUG", spack_debug);
++    }
++
++    String spack_test_command = System.getenv("SPACK_TEST_COMMAND");
++    if (spack_test_command != null) {
++      builder.put("SPACK_TEST_COMMAND", spack_test_command);
++    }
++
++    String spack_dependencies = System.getenv("SPACK_DEPENDENCIES");
++    if (spack_dependencies != null) {
++      builder.put("SPACK_DEPENDENCIES", spack_dependencies);
++    }
+   }
+ }
diff --git a/var/spack/repos/builtin/packages/bazel/link.patch b/var/spack/repos/builtin/packages/bazel/link.patch
new file mode 100644
index 0000000000..69d100f62c
--- /dev/null
+++ b/var/spack/repos/builtin/packages/bazel/link.patch
@@ -0,0 +1,133 @@
+--- bazel-0.3.1/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkAction.java	2016-07-29 10:22:16.000000000 +0200
++++ bazel-0.3.1/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkAction.java	2016-10-13 15:21:35.036617890 +0200
+@@ -214,6 +214,130 @@
+               .getParentDirectory()
+               .getPathString());
+     }
++
++    String path = System.getenv("PATH");
++    result.put("PATH", path == null ? "/bin:/usr/bin" : path);
++
++    String ldLibraryPath = System.getenv("LD_LIBRARY_PATH");
++    if (ldLibraryPath != null) {
++      result.put("LD_LIBRARY_PATH", ldLibraryPath);
++    }
++
++    String tmpdir = System.getenv("TMPDIR");
++    if (tmpdir != null) {
++      result.put("TMPDIR", tmpdir);
++    }
++
++    String spack_prefix = System.getenv("SPACK_PREFIX");
++    if (spack_prefix != null) {
++      result.put("SPACK_PREFIX", spack_prefix);
++    }
++
++    String spack_env_path = System.getenv("SPACK_ENV_PATH");
++    if (spack_env_path != null) {
++      result.put("SPACK_ENV_PATH", spack_env_path);
++    }
++
++    String spack_debug_log_dir = System.getenv("SPACK_DEBUG_LOG_DIR");
++    if (spack_debug_log_dir != null) {
++      result.put("SPACK_DEBUG_LOG_DIR", spack_debug_log_dir);
++    }
++
++    String spack_compiler_spec = System.getenv("SPACK_COMPILER_SPEC");
++    if (spack_compiler_spec != null) {
++      result.put("SPACK_COMPILER_SPEC", spack_compiler_spec);
++    }
++
++    String spack_cc_rpath_arg = System.getenv("SPACK_CC_RPATH_ARG");
++    if (spack_cc_rpath_arg != null) {
++      result.put("SPACK_CC_RPATH_ARG", spack_cc_rpath_arg);
++    }
++
++    String spack_cxx_rpath_arg = System.getenv("SPACK_CXX_RPATH_ARG");
++    if (spack_cxx_rpath_arg != null) {
++      result.put("SPACK_CXX_RPATH_ARG", spack_cxx_rpath_arg);
++    }
++
++    String spack_f77_rpath_arg = System.getenv("SPACK_F77_RPATH_ARG");
++    if (spack_f77_rpath_arg != null) {
++      result.put("SPACK_F77_RPATH_ARG", spack_f77_rpath_arg);
++    }
++
++    String spack_fc_rpath_arg = System.getenv("SPACK_FC_RPATH_ARG");
++    if (spack_fc_rpath_arg != null) {
++      result.put("SPACK_FC_RPATH_ARG", spack_fc_rpath_arg);
++    }
++
++    String spack_short_spec = System.getenv("SPACK_SHORT_SPEC");
++    if (spack_short_spec != null) {
++      result.put("SPACK_SHORT_SPEC", spack_short_spec);
++    }
++
++    String spack_cc = System.getenv("SPACK_CC");
++    if (spack_cc != null) {
++      result.put("SPACK_CC", spack_cc);
++    }
++
++    String spack_cxx = System.getenv("SPACK_CXX");
++    if (spack_cxx != null) {
++      result.put("SPACK_CXX", spack_cxx);
++    }
++
++    String spack_f77 = System.getenv("SPACK_F77");
++    if (spack_f77 != null) {
++      result.put("SPACK_F77", spack_f77);
++    }
++
++    String spack_fc = System.getenv("SPACK_FC");
++    if (spack_fc != null) {
++      result.put("SPACK_FC", spack_fc);
++    }
++
++    String spack_cflags = System.getenv("SPACK_CFLAGS");
++    if (spack_cflags != null) {
++      result.put("SPACK_CFLAGS", spack_cflags);
++    }
++
++    String spack_cxxflags = System.getenv("SPACK_CXXFLAGS");
++    if (spack_cxxflags != null) {
++      result.put("SPACK_CXXFLAGS", spack_cxxflags);
++    }
++
++    String spack_fcflags = System.getenv("SPACK_FCFLAGS");
++    if (spack_fcflags != null) {
++      result.put("SPACK_FCFLAGS", spack_fcflags);
++    }
++
++    String spack_fflags = System.getenv("SPACK_FFLAGS");
++    if (spack_fflags != null) {
++      result.put("SPACK_FFLAGS", spack_fflags);
++    }
++
++    String spack_ldflags = System.getenv("SPACK_LDFLAGS");
++    if (spack_ldflags != null) {
++      result.put("SPACK_LDFLAGS", spack_ldflags);
++    }
++
++    String spack_ldlibs = System.getenv("SPACK_LDLIBS");
++    if (spack_ldlibs != null) {
++      result.put("SPACK_LDLIBS", spack_ldlibs);
++    }
++
++    String spack_debug = System.getenv("SPACK_DEBUG");
++    if (spack_debug != null) {
++      result.put("SPACK_DEBUG", spack_debug);
++    }
++
++    String spack_test_command = System.getenv("SPACK_TEST_COMMAND");
++    if (spack_test_command != null) {
++      result.put("SPACK_TEST_COMMAND", spack_test_command);
++    }
++
++    String spack_dependencies = System.getenv("SPACK_DEPENDENCIES");
++    if (spack_dependencies != null) {
++      result.put("SPACK_DEPENDENCIES", spack_dependencies);
++    }
++
+     return result.build();
+   }
+ 
diff --git a/var/spack/repos/builtin/packages/bazel/package.py b/var/spack/repos/builtin/packages/bazel/package.py
new file mode 100644
index 0000000000..94ac73cbd2
--- /dev/null
+++ b/var/spack/repos/builtin/packages/bazel/package.py
@@ -0,0 +1,89 @@
+##############################################################################
+# Copyright (c) 2013-2016, Lawrence Livermore National Security, LLC.
+# Produced at the Lawrence Livermore National Laboratory.
+#
+# This file is part of Spack.
+# Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
+# LLNL-CODE-647188
+#
+# For details, see https://github.com/llnl/spack
+# Please also see the LICENSE file for our notice and the LGPL.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License (as
+# published by the Free Software Foundation) version 2.1, February 1999.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
+# conditions of the GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+##############################################################################
+from spack import *
+from multiprocessing import cpu_count
+from spack.util.environment import env_flag
+from spack.build_environment import SPACK_NO_PARALLEL_MAKE
+
+
+class Bazel(Package):
+    """Bazel is Google's own build tool"""
+
+    homepage = "https://www.bazel.io"
+    url = "https://github.com/bazelbuild/bazel/archive/0.3.1.tar.gz"
+
+    version('0.3.1', '5c959467484a7fc7dd2e5e4a1e8e866b')
+    version('0.3.0', '33a2cb457d28e1bee9282134769b9283')
+    version('0.2.3', '393a491d690e43caaba88005efe6da91')
+    version('0.2.2b', '75081804f073cbd194da1a07b16cba5f')
+    version('0.2.2', '644bc4ea7f429d835e74f255dc1054e6')
+
+    depends_on('jdk@8:')
+    patch('fix_env_handling.patch')
+    patch('link.patch')
+    patch('cc_configure.patch')
+
+    def install(self, spec, prefix):
+        bash = which('bash')
+        bash('-c', './compile.sh')
+        mkdir(prefix.bin)
+        install('output/bazel', prefix.bin)
+
+    def setup_dependent_package(self, module, dep_spec):
+        class BazelExecutable(Executable):
+            """Special callable executable object for bazel so the user can
+               specify parallel or not on a per-invocation basis.  Using
+               'parallel' as a kwarg will override whatever the package's
+               global setting is, so you can either default to true or false
+               and override particular calls.
+
+               Note that if the SPACK_NO_PARALLEL_MAKE env var is set it
+               overrides everything.
+            """
+
+            def __init__(self, name, command, jobs):
+                super(BazelExecutable, self).__init__(name)
+                self.bazel_command = command
+                self.jobs = jobs
+
+            def __call__(self, *args, **kwargs):
+                disable = env_flag(SPACK_NO_PARALLEL_MAKE)
+                parallel = ((not disable) and
+                            kwargs.get('parallel', self.jobs > 1))
+
+                jobs = "--jobs=1"
+                if parallel:
+                    jobs = "--jobs=%d" % self.jobs
+
+                args = (self.bazel_command,) + (jobs,) + args
+
+                return super(BazelExecutable, self).__call__(*args, **kwargs)
+
+        jobs = cpu_count()
+        if not dep_spec.package.parallel:
+            jobs = 1
+        elif dep_spec.package.make_jobs:
+            jobs = dep_spec.package.make_jobs
+        module.bazel = BazelExecutable('bazel', 'build', jobs)
-- 
GitLab