Add Script to generate vendor hal make files

Author: Vinay Verma <vvinay@codeaurora.org>
Date:   Mon Feb 19 07:45:32 2018 +0530

    Add Script to generate vendor hal make files

    Script will generate blueprint files for vendor hals present
    under proprieatry and opensource projects.It will recursively
    search for all hal files and generate accordingly

    Change-Id: Id08c68d0d059a9bbbf0260350e2c3d5bd702d517

Author: Vinay Verma <vvinay@codeaurora.org>
Date:   Wed Mar 14 17:18:23 2018 +0530

    - Do not generate fingerprint bp files - Check for bp files and exit if found.

    Change-Id: Iae998facddd73d4ac24b4366dcd0f3bea119e6b7

Author: Vinay Verma <vvinay@codeaurora.org>
Date:   Tue Apr 17 10:40:33 2018 +0530

    Generate blueprint files for HAL's

    - Add condition to check if .bp exists
    - Generate .bp only if not present in build tree for a HAL.
    - Remove fingerprint hard-coding and checks for not generating
      bp files. It will be taken care by above point.

    Change-Id: I21501c768e732167f44887730e0a1dc38ada4140

Author: David Ng <dave@codeaurora.org>
Date:   Fri May 4 18:53:24 2018 -0700

    Re-run hidl-gen on updated vendor HALs

    Update vendor_hal makefile generation script to always re-run
    for previously compile-time generated Android.bp's - but update
    only if there are differences.

    hidl-gen is skipped in HAL directories where there's an existing
    Android.bp but are not previously compile-time generated.

    Compile-time generated Android.bp's are denoted by an
    .hidl-autogen alongside the Android.bp.

    Change-Id: I79f70d48a72c456649d975b450a4d69bb47e9e93

Author: David Ng <dave@codeaurora.org>
Date:   Fri May 11 19:09:13 2018 -0700

    Sanitize host tools used in vendor HAL hidl-gen wrapper

    Sanitize all host tools used by the vendor HAL hidl-gen wrapper.
    This is an attempt to standardize the host tool behaviors that
    may be impacted by variations in build-time environments.

    Change-Id: I2f6eb5af05a7f2a3f5ea82ee1780a94f305b2b18

Author: Vinay Verma <vvinay@codeaurora.org>
Date:   Sun Jun 24 19:53:06 2018 +0530

    Handle case when .bp generation is failed

    - Return exit command to calling function if .bp generation is
      failed.
    - Do not proceed with other HAL's if error is found.
    - Move creation of .hidl-gen file at earlier stage because failure
      in bp genration results in empty .bp file and script can run in
      wrong case of pre-existing .bp files in next run.

    Change-Id: I0f45326913fa1a4e3b6e289cf56a43b4382a9f7e

Author: Vinay Verma <vvinay@codeaurora.org>
Date:   Tue Jul 17 17:49:01 2018 +0530

    Create .hidl-autogen marker

    If Android.bp is already present then it is moved to .hidl-autogen
    and then back to Android.bp after checking diff. In this case we
    are left with no marker and in next iteration Android.bp is
    treated as committed file and hence not updated.

    Creating .hidl-autogen explicitly.

    Change-Id: I9c02dbba9a86cbdc4551d43a75b47d7d17c1b9d3

Author: Dileep Marchya <dmarchya@codeaurora.org>
Date:   Wed Jun 26 20:42:17 2019 +0530

    hidl-gen: Match exact string in hidl-gen before skipping.

    - Grep leads to a substring match and causes package
      names to get skipped incorrectly. Use -w option to
      find the exact match.

    CRs-Fixed: 2479486
    Change-Id: Ic1fbe72304177d396eab142eaf832b841b61da24

Change-Id: Ic574fbee9ce858ee102235e248a9bf494112848b
diff --git a/vendor_hal_makefile_generator.sh b/vendor_hal_makefile_generator.sh
new file mode 100755
index 0000000..7321b0a
--- /dev/null
+++ b/vendor_hal_makefile_generator.sh
@@ -0,0 +1,161 @@
+#!/bin/bash
+# Copyright (c) 2018, The Linux Foundation. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#     * Redistributions of source code must retain the above copyright
+#       notice, this list of conditions and the following disclaimer.
+#     * Redistributions in binary form must reproduce the above
+#       copyright notice, this list of conditions and the following
+#       disclaimer in the documentation and/or other materials provided
+#       with the distribution.
+#     * Neither the name of The Linux Foundation nor the names of its
+#       contributors may be used to endorse or promote products derived
+#       from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE US
+
+# This file is used to generate blueprint/makefiles for the vendor hals
+
+# Sanitize host tools
+LS=`which ls`
+LS=${LS:-ls}
+MV=`which mv`
+MV=${MV:-mv}
+CAT=`which cat`
+CAT=${CAT:-cat}
+CUT=`which cut`
+CUT=${CUT:-cut}
+REV=`which rev`
+REV=${REV:-rev}
+SED=`which sed`
+SED=${SED:-sed}
+DIFF=`which diff`
+DIFF=${DIFF:-diff}
+ECHO=`which echo`
+ECHO=${ECHO:-echo}
+FIND=`which find`
+FIND=${FIND:-find}
+GREP=`which grep`
+GREP=${GREP:-grep}
+SORT=`which sort`
+SORT=${SORT:-sort}
+TOUCH=`which touch`
+TOUCH=${TOUCH:-touch}
+
+function generate_make_files() {
+    local dir_path="$ANDROID_BUILD_TOP/$1"
+    pushd $dir_path > /dev/null
+
+    # Due to same package name in different folders we need to detect
+    # opensource case so that it can be handled.
+    local flag_opensource=false
+    if ${ECHO} "$dir_path" | ${GREP} "opensource" > /dev/null;then
+        flag_opensource=true
+    fi
+
+    # Search for all HAL files in given dir.
+    local halFilePaths=`${ECHO} $(${FIND} -iname "*.hal" | ${SORT})`
+
+    #Store package name in below array to create a unique set so that we trigger
+    #hidl-gen only once  for a given package.
+    package_collection=()
+
+    #Iterate over identified .hal Paths
+    local prev_path=""
+    for file in $halFilePaths; do
+        local hal_path=`${ECHO} "$file" | ${REV} | ${CUT} -d"/" -f2- | ${REV}`
+        if [ -e "$hal_path/Android.bp" ] && [ ! -e "$hal_path/.hidl-autogen" ]; then
+            if [ ! "$hal_path" = "$prev_path" ]; then
+                ${ECHO} "Skipping hidl-gen on $1/$hal_path as Android.bp is not compile-time generated"
+                prev_path="$hal_path"
+            fi
+            continue;
+        fi
+        prev_path="$hal_path"
+
+        # Find out package name from HAL file
+        local hal_package=`${ECHO} $(${CAT} $file | ${GREP} -E -m 1 "^package ") | ${CUT} -d' ' -f2`
+
+        # Get rid of extra delimter
+        hal_package=${hal_package%?}
+
+        #Check if we already executed hidl-gen for a given package
+        if ${ECHO} "${package_collection[@]}" | ${GREP} -w $hal_package > /dev/null; then
+            continue;
+        else
+            package_collection+=($hal_package)
+            local delimeter=`${ECHO} "$file" | ${CUT} -d'/' -f2`
+            local root=`${ECHO} "$hal_package" | ${SED} "s/$delimeter/#/g" | ${CUT} -d'#' -f1`
+            #Identify Package Root
+            root=${root%?}
+            #Create root arguments for hidl-command
+            local root_arguments="-r $root:$1 -r  $2"
+        fi
+
+        local hal_path2=$1;
+        #Handling for opensource HAL to solve package name conflict
+        if [ "$flag_opensource" = true ]; then
+            root="$root.$delimeter"
+            hal_path2="$hal_path2/$delimeter"
+        fi
+
+        local root_arguments="-r $root:$hal_path2 -r  $2"
+
+        update_check=0
+        if [ -e "$hal_path/Android.bp" ]; then
+            ${MV} $hal_path/Android.bp $hal_path/.hidl-autogen
+            update_check=1
+        fi
+        ${TOUCH} $hal_path/.hidl-autogen
+
+        ${ECHO} -n "Running hidl-gen on $hal_package: "
+        hidl-gen -Landroidbp $root_arguments $hal_package;
+        rc=$?; if [[ $rc != 0 ]]; then return $rc; fi
+
+        if [ "$update_check" -eq 1 ]; then
+            ${DIFF} -q $hal_path/Android.bp $hal_path/.hidl-autogen > /dev/null
+            if [ $? -eq 0 ]; then
+                ${ECHO} "no changes"
+                ${MV} $hal_path/.hidl-autogen $hal_path/Android.bp
+            else
+                ${ECHO} "updated"
+            fi
+        else
+            ${ECHO} "created"
+        fi
+        ${TOUCH} $hal_path/.hidl-autogen
+    done
+    popd > /dev/null
+}
+
+function start_script_for_interfaces {
+    #Find interfaces in workspace
+    local interfaces=$(${LS} -d ${ANDROID_BUILD_TOP}/vendor/mediatek/*/interfaces)
+
+    ${ECHO} "HIDL interfaces:  Scanning for changes..."
+    for interface in $interfaces; do
+        #generate interfaces
+        local relative_interface=${interface#${ANDROID_BUILD_TOP}/}
+        generate_make_files $relative_interface "android.hidl:system/libhidl/transport"
+        if [ $? -ne 0 ] ; then
+           ${ECHO} "HIDL interfaces: Update Failed"
+           return 1;
+        fi
+    done
+    ${ECHO} "HIDL interfaces:  Update complete."
+}
+
+#Start script for interfaces
+start_script_for_interfaces
+