Avoid python 2 and hardcoding the clang version in the icu4c

The clang keeps get updated, and the hardcoded version in the script
gets outdated quicly, and the script stopped working.

Avoid hardcoding the version, and so the script should keep working
even though clang is updated.

Also, print the clang version in the script.

Test: ./generate_ndk.py
Change-Id: Ia5cfd2db9af5a75f93cb54a78363076c973c8303
diff --git a/tools/icu4c_srcgen/generate_libandroidicu.py b/tools/icu4c_srcgen/generate_libandroidicu.py
index 25c5380..85ca889 100755
--- a/tools/icu4c_srcgen/generate_libandroidicu.py
+++ b/tools/icu4c_srcgen/generate_libandroidicu.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 #
 # Copyright (C) 2018 The Android Open Source Project
 #
@@ -88,13 +88,11 @@
     with open(android_path('external/icu/libandroidicu/static_shim/shim.cpp'),
               'w') as out_file:
         out_file.write(generate_shim(functions, includes, SYMBOL_SUFFIX,
-                                     'libandroidicu_shim.cpp.j2')
-                       .encode('utf8'))
+                                     'libandroidicu_shim.cpp.j2'))
 
     with open(android_path('external/icu/libandroidicu/libandroidicu.map.txt'),
               'w') as out_file:
-        out_file.write(generate_symbol_txt(functions, [], 'libandroidicu.map.txt.j2')
-                       .encode('utf8'))
+        out_file.write(generate_symbol_txt(functions, [], 'libandroidicu.map.txt.j2'))
 
     for path in parser.header_paths_to_copy:
         basename = os.path.basename(path)
@@ -102,5 +100,7 @@
 
     copy_header_only_files()
 
+    print("Done. See libandroidicu/ for the generated content.")
+
 if __name__ == '__main__':
     main()
diff --git a/tools/icu4c_srcgen/generate_ndk.py b/tools/icu4c_srcgen/generate_ndk.py
index 802d07e..42e6808 100755
--- a/tools/icu4c_srcgen/generate_ndk.py
+++ b/tools/icu4c_srcgen/generate_ndk.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 #
 # Copyright (C) 2018 The Android Open Source Project
 #
@@ -221,12 +221,10 @@
 
     with open(android_path('external/icu/libicu/src/shim.cpp'),
               'w') as out_file:
-        out_file.write(generate_shim(functions, includes, SYMBOL_SUFFIX, 'libicu_shim.cpp.j2')
-                       .encode('utf8'))
+        out_file.write(generate_shim(functions, includes, SYMBOL_SUFFIX, 'libicu_shim.cpp.j2'))
 
     with open(android_path('external/icu/libicu/libicu.map.txt'), 'w') as out_file:
-        out_file.write(generate_symbol_txt(functions, [], 'libicu.map.txt.j2')
-                       .encode('utf8'))
+        out_file.write(generate_symbol_txt(functions, [], 'libicu.map.txt.j2'))
 
     # Process the C headers and put them into the ndk folder.
     for src_path in parser.header_paths_to_copy:
@@ -245,5 +243,7 @@
     subprocess.check_call(
         [android_path('external/icu/tools/icu4c_srcgen/doc_patches/apply_patches.sh')])
 
+    print("Done. See the generated headers at libicu/ndk_headers/.")
+
 if __name__ == '__main__':
     main()
diff --git a/tools/icu4c_srcgen/genutil.py b/tools/icu4c_srcgen/genutil.py
index d652ac2..39b4eda 100644
--- a/tools/icu4c_srcgen/genutil.py
+++ b/tools/icu4c_srcgen/genutil.py
@@ -70,15 +70,38 @@
     return os.path.join(ANDROID_TOP, *args)
 
 
-# TODO: Include clang bindings in prebuilt package. http://b/119270767
-site.addsitedir(android_path('external/clang/bindings/python'))
-import clang.cindex  # pylint: disable=import-error,wrong-import-position
+def get_clang_path():
+    """Find the latest clang version and return the full path"""
+    base_path = android_path('prebuilts/clang/host/linux-x86/')
+    files = [f for f in os.listdir(base_path) if f.startswith('clang-r')]
+    # TODO: Don't use sort() because it assumes the same number of digits in the version name
+    files.sort(reverse=True)
+    selected = files[0]
+    print("Using clang version %s" % selected)
+    path = os.path.join(base_path, selected)
+    return path
 
-# TODO: Do not hardcode clang version. http://b/119270767
-CLANG_REVISION = 'r383902c'
-CLANG_LIB_VERSION = '11git'
-CLANG_HEADER_VERSION = '11.0.3'
-CLANG_PATH = android_path('prebuilts/clang/host/linux-x86/clang-%s' % CLANG_REVISION)
+
+def get_clang_lib_path(clang_path):
+    """Return the libclang.so path"""
+    base_path = os.path.join(clang_path, 'lib64')
+    files = [f for f in os.listdir(base_path) if f.startswith('libclang.so.')]
+    return os.path.join(base_path, files[0])
+
+
+def get_clang_header_dir(clang_path):
+    """Return the path to clang header directory"""
+    base_path = os.path.join(clang_path, 'lib64/clang/')
+    files = os.listdir(base_path)
+    return os.path.join(base_path, files[0], 'include/')
+
+
+CLANG_PATH = get_clang_path()
+CLANG_LIB_PATH = get_clang_lib_path(CLANG_PATH)
+CLANG_HEADER_PATH = get_clang_header_dir(CLANG_PATH)
+
+site.addsitedir(os.path.join(CLANG_PATH, 'lib64/python3/site-packages/'))
+import clang.cindex  # pylint: disable=import-error,wrong-import-position
 
 
 class Function:
@@ -170,9 +193,7 @@
         # Configures libclang to load in our environment
         # Set up LD_LIBRARY_PATH to include libclang.so, libLLVM.so, etc.  Note
         # that setting LD_LIBRARY_PATH with os.putenv() sometimes doesn't help.
-        # clang.cindex.Config.set_library_path(os.path.join(CLANG_PATH, 'lib64'))
-        clang.cindex.Config.set_library_file(
-            os.path.join(CLANG_PATH, 'lib64', 'libclang.so.%s' % CLANG_LIB_VERSION))
+        clang.cindex.Config.set_library_file(CLANG_LIB_PATH)
 
     def set_va_functions_mapping(self, mapping):
         """Set mapping from a variable argument function to an implementation.
@@ -232,8 +253,7 @@
         ]
 
         include_dirs = [
-            # TODO: Do not hardcode clang version. http://b/119270767
-            os.path.join(CLANG_PATH, 'lib64/clang/', CLANG_HEADER_VERSION, 'include/'),
+            CLANG_HEADER_PATH,
             android_path('bionic/libc/include'),
             android_path('external/icu/android_icu4c/include'),
             android_path('external/icu/icu4c/source/common'),