Add script and genrule to build RPM package.

This only builds a development package, further work (and probably a full pkg_rpm rule) will be needed to make a releasable RPM.

Closes #2276.

--
Change-Id: If307c6143334957a127c7c2fe897009a08e88d5c
Reviewed-on: https://github.com/bazelbuild/bazel/pull/2276
PiperOrigin-RevId: 143450212
MOS_MIGRATED_REVID=143450212
diff --git a/scripts/BUILD b/scripts/BUILD
index 106e4a3..decf702 100644
--- a/scripts/BUILD
+++ b/scripts/BUILD
@@ -20,7 +20,7 @@
         "//src:bazel",
         "//tools/defaults:jdk",
     ],
-    visibility = ["//scripts/packages:__pkg__"],
+    visibility = ["//scripts/packages:__subpackages__"],
 )
 
 sh_test(
diff --git a/scripts/packages/BUILD b/scripts/packages/BUILD
index 2e52021..955ddab 100644
--- a/scripts/packages/BUILD
+++ b/scripts/packages/BUILD
@@ -1,7 +1,14 @@
+package(default_visibility = ["//scripts/packages:__subpackages__"])
+
 filegroup(
     name = "srcs",
-    srcs = glob(["**"]),
-    visibility = ["//scripts:__pkg__"],
+    srcs = glob(["**"]) + [
+        "//scripts/packages/fedora:srcs",
+    ],
+    visibility = [
+        "//scripts:__pkg__",
+        "//scripts/packages:__subpackages__",
+    ],
 )
 
 filegroup(
diff --git a/scripts/packages/fedora/BUILD b/scripts/packages/fedora/BUILD
new file mode 100644
index 0000000..36d8690
--- /dev/null
+++ b/scripts/packages/fedora/BUILD
@@ -0,0 +1,30 @@
+package(default_visibility = ["//scripts/packages:__subpackages__"])
+
+filegroup(
+    name = "srcs",
+    srcs = glob(["**"]),
+)
+
+genrule(
+    name = "bazel-fedora",
+    srcs = [
+        "bazel.spec",
+        "//scripts/packages:bazel",
+        "//scripts/packages:bazel-real",
+        "//scripts/packages:debian/bazel.bazelrc",
+        "//scripts:bash_completion",
+    ],
+    outs = [
+        "bazel-fedora.rpm",
+    ],
+    cmd = """
+      $(location :build_rpm.sh) \
+        $(location :bazel.spec) \
+        $(location bazel-fedora.rpm) \
+        $(location //scripts/packages:bazel) \
+        $(location //scripts/packages:bazel-real) \
+        $(location //scripts/packages:debian/bazel.bazelrc) \
+        $(location //scripts:bash_completion)
+    """,
+    tools = ["build_rpm.sh"],
+)
diff --git a/scripts/packages/fedora/bazel.spec b/scripts/packages/fedora/bazel.spec
new file mode 100644
index 0000000..0d69e61
--- /dev/null
+++ b/scripts/packages/fedora/bazel.spec
@@ -0,0 +1,43 @@
+Name: bazel
+Version: devel
+Release: 1
+Summary: Correct, reproducible, and fast builds for everyone.
+URL: https://bazel.build
+License: Apache License, v2.0
+
+Source0: bazel
+Source1: bazel-real
+Source2: bazel.bazelrc
+Source3: bazel-complete.bash
+
+Requires: java-1.8.0-openjdk-headless
+
+%description
+Bazel is a build tool that builds code quickly and reliably. It is used to build the majority of Google's software, and thus it has been designed to handle build problems present in Google's development environment.
+
+%global _enable_debug_package 0
+%global debug_package %{nil}
+%global __os_install_post /usr/lib/rpm/brp-compress %{nil}
+
+%prep
+
+%build
+
+%install
+export DONT_STRIP=1
+mkdir -p %{buildroot}%{_bindir}/
+install -m 755 bazel %{buildroot}%{_bindir}/bazel
+install -m 755 bazel-real %{buildroot}%{_bindir}/bazel-real
+mkdir -p %{buildroot}%{_sysconfdir}/
+install -m 644 bazel.bazelrc %{buildroot}%{_sysconfdir}/bazel.bazelrc
+mkdir -p %{buildroot}%{_sysconfdir}/bash_completion.d
+install -m 644 bazel-complete.bash %{buildroot}%{_sysconfdir}/bash_completion.d/bazel
+
+%files
+%{_bindir}/bazel
+%{_bindir}/bazel-real
+%{_sysconfdir}/bazel.bazelrc
+%{_sysconfdir}/bash_completion.d/bazel
+
+%changelog
+# TODO: Include changelog.
diff --git a/scripts/packages/fedora/build_rpm.sh b/scripts/packages/fedora/build_rpm.sh
new file mode 100755
index 0000000..525c858
--- /dev/null
+++ b/scripts/packages/fedora/build_rpm.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+# Copyright 2016 The Bazel Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+set -eu
+
+# Build a RPM archive from the Bazel sources.
+
+# Usage: build_rpm.sh spec_file dest_file [other files]
+
+spec_file=$1
+shift
+dest_file=$1
+shift
+
+echo "Building ${dest_file} from ${spec_file}."
+WORK_DIR="${PWD}/bazel-fedora"
+
+# Copy needed sources.
+rm -rf ${WORK_DIR}
+mkdir -p ${WORK_DIR}/SOURCES
+mkdir -p ${WORK_DIR}/BUILD
+cp $spec_file ${WORK_DIR}
+for i in "$@"; do
+  cp $i $WORK_DIR/BUILD
+done
+
+# Build the RPM.
+rpmbuild \
+  --define "_topdir ${WORK_DIR}" \
+  --define "_tmppath /tmp" \
+  -bb ${spec_file} > rpmbuild.log 2>&1
+if [ $? -ne 0 ]; then
+  err=$?
+  echo "Error in rpmbuild:"
+  cat rpmbuild.log
+  exit $err
+fi
+out_file=$(grep '^Wrote:' rpmbuild.log | cut -d ' ' -f 2)
+
+# Copy output back to the destination.
+cp $out_file $dest_file
+echo "Created $dest_file"
+