libcap: generate cap_names.h at build time.

Test: treehugger
Change-Id: I4a9d1b878f1d1074ef28587a76adb54646b0bfc4
diff --git a/Android.bp b/Android.bp
index eb03cd8..3fb5c4e 100644
--- a/Android.bp
+++ b/Android.bp
@@ -1,22 +1,6 @@
-// Copyright (C) 2015 The Android Open Source Project
-//
-// 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.
-
 cc_defaults {
     name: "libcap_defaults",
     cflags: [
-        "-Wall",
-        "-Werror",
         "-Wno-unused-parameter",
         "-Wno-unused-result",
         "-Wno-tautological-compare",
@@ -41,6 +25,7 @@
         "libcap/cap_proc.c",
         "libcap/cap_text.c",
     ],
+    generated_headers: ["cap_names.h"],
 
     local_include_dirs: ["libcap/include"],
     export_include_dirs: ["libcap/include"],
@@ -66,11 +51,8 @@
 
 cc_binary {
     name: "getcap",
-
-    srcs: ["progs/getcap.c"],
-
     defaults: ["libcap_defaults"],
-
+    srcs: ["progs/getcap.c"],
     static_libs: [
         "libcap",
     ],
@@ -78,12 +60,45 @@
 
 cc_binary {
     name: "setcap",
-
-    srcs: ["progs/setcap.c"],
-
     defaults: ["libcap_defaults"],
-
+    srcs: ["progs/setcap.c"],
     static_libs: [
         "libcap",
     ],
 }
+
+//
+// Generate cap_names.list.h from the <linux/capability.h> header (using awk).
+//
+
+genrule {
+    name: "cap_names.list.h",
+    srcs: ["libcap/include/uapi/linux/capability.h"],
+    out: ["cap_names.list.h"],
+    tool_files: [":generate_cap_names_list.awk"],
+    cmd: "awk -f $(location :generate_cap_names_list.awk) $(in) > $(out)",
+}
+
+filegroup {
+    name: "generate_cap_names_list.awk",
+    srcs: ["generate_cap_names_list.awk"],
+}
+
+//
+// Generate cap_names.h from cap_names.list.h (using _makenames).
+//
+
+genrule {
+    name: "cap_names.h",
+    out: ["cap_names.h"],
+    tools: [":_makenames"],
+    cmd: "$(location :_makenames) > $(out)",
+}
+
+cc_binary {
+    name: "_makenames",
+    defaults: ["libcap_defaults"],
+    srcs: ["libcap/_makenames.c"],
+    generated_headers: ["cap_names.list.h"],
+    host_supported: true,
+}