Merge "Only write CMakeLists.txt for normal variants"
diff --git a/cc/binary.go b/cc/binary.go
index 00fda06..04b912a 100644
--- a/cc/binary.go
+++ b/cc/binary.go
@@ -318,6 +318,10 @@
 	builderFlags := flagsToBuilderFlags(flags)
 
 	if binary.stripper.needsStrip(ctx) {
+		// b/80093681, GNU strip/objcopy bug.
+		// Use llvm-{strip,objcopy} when clang lld is used.
+		builderFlags.stripUseLlvmStrip =
+			flags.Clang && binary.baseLinker.useClangLld(ctx)
 		strippedOutputFile := outputFile
 		outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
 		binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
diff --git a/cc/builder.go b/cc/builder.go
index dd1fc05..51d3195 100644
--- a/cc/builder.go
+++ b/cc/builder.go
@@ -117,7 +117,7 @@
 		blueprint.RuleParams{
 			Depfile:     "${out}.d",
 			Deps:        blueprint.DepsGCC,
-			Command:     "CROSS_COMPILE=$crossCompile XZ=$xzCmd $stripPath ${args} -i ${in} -o ${out} -d ${out}.d",
+			Command:     "CROSS_COMPILE=$crossCompile XZ=$xzCmd CLANG_BIN=${config.ClangBin} $stripPath ${args} -i ${in} -o ${out} -d ${out}.d",
 			CommandDeps: []string{"$stripPath", "$xzCmd"},
 		},
 		"args", "crossCompile")
@@ -266,6 +266,7 @@
 	stripKeepSymbols       bool
 	stripKeepMiniDebugInfo bool
 	stripAddGnuDebuglink   bool
+	stripUseLlvmStrip      bool
 }
 
 type Objects struct {
@@ -725,11 +726,14 @@
 	baseName, exportedHeaderFlags string, isVndkExt bool) android.OptionalPath {
 
 	outputFile := android.PathForModuleOut(ctx, baseName+".abidiff")
-
+	libName := strings.TrimSuffix(baseName, filepath.Ext(baseName))
 	localAbiCheckAllowFlags := append([]string(nil), abiCheckAllowFlags...)
 	if exportedHeaderFlags == "" {
 		localAbiCheckAllowFlags = append(localAbiCheckAllowFlags, "-advice-only")
 	}
+	if inList(libName, llndkLibraries) {
+		localAbiCheckAllowFlags = append(localAbiCheckAllowFlags, "-consider-opaque-types-different")
+	}
 	if isVndkExt {
 		localAbiCheckAllowFlags = append(localAbiCheckAllowFlags, "-allow-extensions")
 	}
@@ -742,7 +746,7 @@
 		Implicit:    referenceDump,
 		Args: map[string]string{
 			"referenceDump": referenceDump.String(),
-			"libName":       baseName[0:(len(baseName) - len(filepath.Ext(baseName)))],
+			"libName":       libName,
 			"arch":          ctx.Arch().ArchType.Name,
 			"allowFlags":    strings.Join(localAbiCheckAllowFlags, " "),
 		},
@@ -822,6 +826,9 @@
 	if flags.stripKeepSymbols {
 		args += " --keep-symbols"
 	}
+	if flags.stripUseLlvmStrip {
+		args += " --use-llvm-strip"
+	}
 
 	ctx.Build(pctx, android.BuildParams{
 		Rule:        strip,
diff --git a/cc/config/global.go b/cc/config/global.go
index c734c2e..dee7640 100644
--- a/cc/config/global.go
+++ b/cc/config/global.go
@@ -89,6 +89,9 @@
 
 	deviceGlobalLldflags = append(ClangFilterUnknownLldflags(deviceGlobalLdflags),
 		[]string{
+			// TODO(b/109657296): needs --no-rosegment until Android
+			// stack unwinder can handle the read-only segment.
+			"-Wl,--no-rosegment",
 			"-Wl,--pack-dyn-relocs=android",
 			"-fuse-ld=lld",
 		}...)
diff --git a/cc/library.go b/cc/library.go
index c83e89a..3bc1001 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -593,6 +593,10 @@
 	}
 
 	if library.stripper.needsStrip(ctx) {
+		// b/80093681, GNU strip/objcopy bug.
+		// Use llvm-{strip,objcopy} when clang lld is used.
+		builderFlags.stripUseLlvmStrip =
+			flags.Clang && library.baseLinker.useClangLld(ctx)
 		strippedOutputFile := outputFile
 		outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
 		library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
diff --git a/cc/makevars.go b/cc/makevars.go
index d036bb6..8b72dbb 100644
--- a/cc/makevars.go
+++ b/cc/makevars.go
@@ -72,6 +72,8 @@
 	ctx.Strict("CLANG_CXX", "${config.ClangBin}/clang++")
 	ctx.Strict("LLVM_AS", "${config.ClangBin}/llvm-as")
 	ctx.Strict("LLVM_LINK", "${config.ClangBin}/llvm-link")
+	ctx.Strict("LLVM_OBJCOPY", "${config.ClangBin}/llvm-objcopy")
+	ctx.Strict("LLVM_STRIP", "${config.ClangBin}/llvm-strip")
 	ctx.Strict("PATH_TO_CLANG_TIDY", "${config.ClangBin}/clang-tidy")
 	ctx.StrictSorted("CLANG_CONFIG_UNKNOWN_CFLAGS", strings.Join(config.ClangUnknownCflags, " "))
 
diff --git a/cc/vndk_prebuilt.go b/cc/vndk_prebuilt.go
index 9c9545d..849bb3f 100644
--- a/cc/vndk_prebuilt.go
+++ b/cc/vndk_prebuilt.go
@@ -21,7 +21,8 @@
 )
 
 var (
-	vndkSuffix = ".vndk."
+	vndkSuffix     = ".vndk."
+	binder32Suffix = ".binder32"
 )
 
 // Creates vndk prebuilts that include the VNDK version.
@@ -53,6 +54,10 @@
 	// Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64_ab')
 	Target_arch *string
 
+	// If the prebuilt snapshot lib is built with 32 bit binder, this must be set to true.
+	// The lib with 64 bit binder does not need to set this property.
+	Binder32bit *bool
+
 	// Prebuilt files for each arch.
 	Srcs []string `android:"arch_variant"`
 }
@@ -67,10 +72,14 @@
 }
 
 func (p *vndkPrebuiltLibraryDecorator) NameSuffix() string {
+	suffix := p.version()
 	if p.arch() != "" {
-		return vndkSuffix + p.version() + "." + p.arch()
+		suffix += "." + p.arch()
 	}
-	return vndkSuffix + p.version()
+	if Bool(p.properties.Binder32bit) {
+		suffix += binder32Suffix
+	}
+	return vndkSuffix + suffix
 }
 
 func (p *vndkPrebuiltLibraryDecorator) version() string {
@@ -81,6 +90,13 @@
 	return String(p.properties.Target_arch)
 }
 
+func (p *vndkPrebuiltLibraryDecorator) binderBit() string {
+	if Bool(p.properties.Binder32bit) {
+		return "32"
+	}
+	return "64"
+}
+
 func (p *vndkPrebuiltLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
 	p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix())
 	return p.libraryDecorator.linkerFlags(ctx, flags)
@@ -114,6 +130,9 @@
 	if len(arches) == 0 || arches[0].ArchType.String() != p.arch() {
 		return
 	}
+	if ctx.DeviceConfig().BinderBitness() != p.binderBit() {
+		return
+	}
 	if p.shared() {
 		if ctx.isVndkSp() {
 			p.baseInstaller.subDir = "vndk-sp-" + p.version()
diff --git a/java/androidmk.go b/java/androidmk.go
index 0840198..ab9ceeb 100644
--- a/java/androidmk.go
+++ b/java/androidmk.go
@@ -333,6 +333,9 @@
 				if ddoc.apiFile != nil {
 					fmt.Fprintln(w, apiFilePrefix+"API_FILE := ", ddoc.apiFile.String())
 				}
+				if ddoc.dexApiFile != nil {
+					fmt.Fprintln(w, apiFilePrefix+"DEX_API_FILE := ", ddoc.dexApiFile.String())
+				}
 				if ddoc.privateApiFile != nil {
 					fmt.Fprintln(w, apiFilePrefix+"PRIVATE_API_FILE := ", ddoc.privateApiFile.String())
 				}
diff --git a/java/droiddoc.go b/java/droiddoc.go
index b641041..ca99e76 100644
--- a/java/droiddoc.go
+++ b/java/droiddoc.go
@@ -203,6 +203,9 @@
 	// the generated public API filename by Doclava.
 	Api_filename *string
 
+	// the generated public Dex API filename by Doclava.
+	Dex_api_filename *string
+
 	// the generated private API filename by Doclava.
 	Private_api_filename *string
 
@@ -266,6 +269,7 @@
 
 	properties        DroiddocProperties
 	apiFile           android.WritablePath
+	dexApiFile        android.WritablePath
 	privateApiFile    android.WritablePath
 	privateDexApiFile android.WritablePath
 	removedApiFile    android.WritablePath
@@ -753,6 +757,12 @@
 		implicitOutputs = append(implicitOutputs, d.privateApiFile)
 	}
 
+	if String(d.properties.Dex_api_filename) != "" {
+		d.dexApiFile = android.PathForModuleOut(ctx, String(d.properties.Dex_api_filename))
+		args = args + " -dexApi " + d.dexApiFile.String()
+		implicitOutputs = append(implicitOutputs, d.dexApiFile)
+	}
+
 	if String(d.properties.Private_dex_api_filename) != "" {
 		d.privateDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Private_dex_api_filename))
 		args = args + " -privateDexApi " + d.privateDexApiFile.String()
diff --git a/scripts/strip.sh b/scripts/strip.sh
index 318c7ad..432582f 100755
--- a/scripts/strip.sh
+++ b/scripts/strip.sh
@@ -17,15 +17,17 @@
 # Script to handle the various ways soong may need to strip binaries
 # Inputs:
 #  Environment:
+#   CLANG_BIN: path to the clang bin directory
 #   CROSS_COMPILE: prefix added to readelf, objcopy tools
 #   XZ: path to the xz binary
 #  Arguments:
 #   -i ${file}: input file (required)
 #   -o ${file}: output file (required)
 #   -d ${file}: deps file (required)
-#   --keep-symbols
-#   --keep-mini-debug-info
 #   --add-gnu-debuglink
+#   --keep-mini-debug-info
+#   --keep-symbols
+#   --use-llvm-strip
 
 OPTSTRING=d:i:o:-:
 
@@ -33,25 +35,52 @@
     cat <<EOF
 Usage: strip.sh [options] -i in-file -o out-file -d deps-file
 Options:
-        --keep-symbols          Keep symbols in out-file
-        --keep-mini-debug-info  Keep compressed debug info in out-file
         --add-gnu-debuglink     Add a gnu-debuglink section to out-file
+        --keep-mini-debug-info  Keep compressed debug info in out-file
+        --keep-symbols          Keep symbols in out-file
+        --use-llvm-strip        Use llvm-{strip,objcopy} instead of strip/objcopy
 EOF
     exit 1
 }
 
+# With --use-llvm-strip, GNU strip is replaced with llvm-strip to work around
+# old GNU strip bug on lld output files, b/80093681.
+# Similary, calls to objcopy are replaced with llvm-objcopy,
+# with some exceptions.
+
 do_strip() {
-    "${CROSS_COMPILE}strip" --strip-all "${infile}" -o "${outfile}.tmp"
+    # ${CROSS_COMPILE}strip --strip-all does not strip .ARM.attributes,
+    # so we tell llvm-strip to keep it too.
+    if [ ! -z "${use_llvm_strip}" ]; then
+        "${CLANG_BIN}/llvm-strip" --strip-all -keep=.ARM.attributes "${infile}" "${outfile}.tmp"
+    else
+        "${CROSS_COMPILE}strip" --strip-all "${infile}" -o "${outfile}.tmp"
+    fi
 }
 
 do_strip_keep_symbols() {
-    "${CROSS_COMPILE}objcopy" "${infile}" "${outfile}.tmp" \
-	`"${CROSS_COMPILE}readelf" -S "${infile}" | awk '/.debug_/ {print "-R " $2}' | xargs`
+    # Maybe we should replace this objcopy with llvm-objcopy, but
+    # we have not found a use case that is broken by objcopy yet.
+    REMOVE_SECTIONS=`"${CROSS_COMPILE}readelf" -S "${infile}" | awk '/.debug_/ {print "--remove-section " $2}' | xargs`
+    if [ ! -z "${use_llvm_strip}" ]; then
+        "${CROSS_COMPILE}objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS}
+    else
+        "${CLANG_BIN}/llvm-objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS}
+    fi
 }
 
 do_strip_keep_mini_debug_info() {
     rm -f "${outfile}.dynsyms" "${outfile}.funcsyms" "${outfile}.keep_symbols" "${outfile}.debug" "${outfile}.mini_debuginfo" "${outfile}.mini_debuginfo.xz"
-    if "${CROSS_COMPILE}strip" --strip-all -R .comment "${infile}" -o "${outfile}.tmp"; then
+    if [ ! -z "${use_llvm_strip}" ]; then
+        "${CLANG_BIN}/llvm-strip" --strip-all -keep=.ARM.attributes -remove-section=.comment "${infile}" "${outfile}.tmp"
+    else
+        "${CROSS_COMPILE}strip" --strip-all -R .comment "${infile}" -o "${outfile}.tmp"
+    fi
+    if [ "$?" == "0" ]; then
+        # Current prebult llvm-objcopy does not support the following flags:
+        #    --only-keep-debug --rename-section --keep-symbols
+        # For the following use cases, ${CROSS_COMPILE}objcopy does fine with lld linked files,
+        # except the --add-section flag.
         "${CROSS_COMPILE}objcopy" --only-keep-debug "${infile}" "${outfile}.debug"
         "${CROSS_COMPILE}nm" -D "${infile}" --format=posix --defined-only | awk '{ print $$1 }' | sort >"${outfile}.dynsyms"
         "${CROSS_COMPILE}nm" "${infile}" --format=posix --defined-only | awk '{ if ($$2 == "T" || $$2 == "t" || $$2 == "D") print $$1 }' | sort > "${outfile}.funcsyms"
@@ -61,14 +90,22 @@
         "${CROSS_COMPILE}objcopy" -S --remove-section .gdb_index --remove-section .comment --keep-symbols="${outfile}.keep_symbols" "${outfile}.mini_debuginfo"
         "${CROSS_COMPILE}objcopy" --rename-section saved_debug_frame=.debug_frame "${outfile}.mini_debuginfo"
         "${XZ}" "${outfile}.mini_debuginfo"
-        "${CROSS_COMPILE}objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp"
+        if [ ! -z "${use_llvm_strip}" ]; then
+            "${CLANG_BIN}/llvm-objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp"
+        else
+            "${CROSS_COMPILE}objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp"
+        fi
     else
         cp -f "${infile}" "${outfile}.tmp"
     fi
 }
 
 do_add_gnu_debuglink() {
-    "${CROSS_COMPILE}objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp"
+    if [ ! -z "${use_llvm_strip}" ]; then
+        "${CLANG_BIN}/llvm-objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp"
+    else
+        "${CROSS_COMPILE}objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp"
+    fi
 }
 
 while getopts $OPTSTRING opt; do
@@ -78,9 +115,10 @@
 	o) outfile="${OPTARG}" ;;
 	-)
 	    case "${OPTARG}" in
-		keep-symbols) keep_symbols=true ;;
-		keep-mini-debug-info) keep_mini_debug_info=true ;;
 		add-gnu-debuglink) add_gnu_debuglink=true ;;
+		keep-mini-debug-info) keep_mini_debug_info=true ;;
+		keep-symbols) keep_symbols=true ;;
+		use-llvm-strip) use_llvm_strip=true ;;
 		*) echo "Unknown option --${OPTARG}"; usage ;;
 	    esac;;
 	?) usage ;;
@@ -130,12 +168,18 @@
 rm -f "${outfile}"
 mv "${outfile}.tmp" "${outfile}"
 
+if [ ! -z "${use_llvm_strip}" ]; then
+  USED_STRIP_OBJCOPY="${CLANG_BIN}/llvm-strip ${CLANG_BIN}/llvm-objcopy"
+else
+  USED_STRIP_OBJCOPY="${CROSS_COMPILE}strip"
+fi
+
 cat <<EOF > "${depsfile}"
 ${outfile}: \
   ${infile} \
   ${CROSS_COMPILE}nm \
   ${CROSS_COMPILE}objcopy \
   ${CROSS_COMPILE}readelf \
-  ${CROSS_COMPILE}strip
+  ${USED_STRIP_OBJCOPY}
 
 EOF