Check if custom clang version and release version is set before building libfuzzer.

Test: `make -j48 LLVM_PREBUILTS_VERSION=clang-stable LLVM_RELEASE_VERSION=5.0`

Change-Id: I8ddf353f8ec3ec98d2526379e2f20f4b177f7b28
(cherry picked from commit 6bf99fee15091f213a253ce9f07ba5f06750cf78)
diff --git a/Android.bp b/Android.bp
index d9f1783..9981e4d 100644
--- a/Android.bp
+++ b/Android.bp
@@ -14,33 +14,13 @@
 // limitations under the License.
 //
 
-clang_version = "3859424"
-clang_libfuzzer_dir = "clang-" + clang_version + "/lib64/clang/4.0/lib/linux/"
-
-cc_prebuilt_library_static {
+libfuzzer_prebuilt_library_static {
     name: "libFuzzer",
-    export_include_dirs: ["clang-" + clang_version + "/prebuilt_include/llvm/lib/Fuzzer/"],
-    target: {
-        android_arm: {
-            srcs: [clang_libfuzzer_dir + "arm/libFuzzer.a"],
-        },
-        android_arm64: {
-            srcs: [clang_libfuzzer_dir + "aarch64/libFuzzer.a"],
-        },
-        android_mips: {
-            srcs: [clang_libfuzzer_dir + "mips/libFuzzer.a"],
-        },
-        android_mips64: {
-            srcs: [clang_libfuzzer_dir + "mips64/libFuzzer.a"],
-        },
-        android_x86: {
-            srcs: [clang_libfuzzer_dir + "i386/libFuzzer.a"],
-        },
-        android_x86_64: {
-            srcs: [clang_libfuzzer_dir + "x86_64/libFuzzer.a"],
-        },
-    },
     sanitize: {
         never: true,
     },
 }
+
+subdirs = [
+    "soong",
+]
diff --git a/soong/Android.bp b/soong/Android.bp
new file mode 100644
index 0000000..0bb87a8
--- /dev/null
+++ b/soong/Android.bp
@@ -0,0 +1,30 @@
+//
+// Copyright (C) 2017 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.
+//
+
+bootstrap_go_package {
+    name: "soong-libfuzzer",
+    pkgPath: "android/soong/prebuilts/clang/host/linux-x86",
+    deps: [
+        "blueprint",
+        "soong-android",
+        "soong-cc",
+        "soong-cc-config"
+    ],
+    srcs: [
+        "libfuzzer.go",
+    ],
+    pluginFor: ["soong_build"],
+}
diff --git a/soong/libfuzzer.go b/soong/libfuzzer.go
new file mode 100644
index 0000000..a0ce14c
--- /dev/null
+++ b/soong/libfuzzer.go
@@ -0,0 +1,89 @@
+//
+// Copyright (C) 2017 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.
+//
+
+package libfuzzer
+
+import (
+	"path"
+
+	"github.com/google/blueprint"
+
+	"android/soong/android"
+	"android/soong/cc"
+	"android/soong/cc/config"
+)
+
+// This module is used to generate libfuzzer static libraries. When
+// LLVM_PREBUILTS_VERSION and LLVM_RELEASE_VERSION are set, the library will
+// generated from the given path.
+
+func init() {
+	android.RegisterModuleType("libfuzzer_prebuilt_library_static",
+		libfuzzerPrebuiltLibraryStaticFactory)
+}
+
+func libfuzzerPrebuiltLibraryStatic(ctx android.LoadHookContext) {
+	// Because of b/38393317, changing clang base dir is not allowed.
+	clangDir := path.Join(
+		"./",
+		ctx.AConfig().GetenvWithDefault("LLVM_PREBUILTS_VERSION", config.ClangDefaultVersion),
+	)
+	headerDir := path.Join(clangDir, "prebuilt_include", "llvm", "lib", "Fuzzer")
+	releaseVersion := ctx.AConfig().GetenvWithDefault("LLVM_RELEASE_VERSION",
+		config.ClangDefaultShortVersion)
+	libDir := path.Join(clangDir, "lib64", "clang", releaseVersion, "lib", "linux")
+
+	type props struct {
+		Export_include_dirs []string
+		Target              struct {
+			Android_arm struct {
+				Srcs []string
+			}
+			Android_arm64 struct {
+				Srcs []string
+			}
+			Android_mips struct {
+				Srcs []string
+			}
+			Android_mips64 struct {
+				Srcs []string
+			}
+			Android_x86 struct {
+				Srcs []string
+			}
+			Android_x86_64 struct {
+				Srcs []string
+			}
+		}
+	}
+
+	p := &props{}
+
+	p.Export_include_dirs = []string{headerDir}
+	p.Target.Android_arm.Srcs = []string{path.Join(libDir, "arm/libFuzzer.a")}
+	p.Target.Android_arm64.Srcs = []string{path.Join(libDir, "aarch64/libFuzzer.a")}
+	p.Target.Android_mips.Srcs = []string{path.Join(libDir, "mips/libFuzzer.a")}
+	p.Target.Android_mips64.Srcs = []string{path.Join(libDir, "mips64/libFuzzer.a")}
+	p.Target.Android_x86.Srcs = []string{path.Join(libDir, "i386/libFuzzer.a")}
+	p.Target.Android_x86_64.Srcs = []string{path.Join(libDir, "x86_64/libFuzzer.a")}
+	ctx.AppendProperties(p)
+}
+
+func libfuzzerPrebuiltLibraryStaticFactory() (blueprint.Module, []interface{}) {
+	module, _ := cc.NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
+	android.AddLoadHook(module, libfuzzerPrebuiltLibraryStatic)
+	return module.Init()
+}