Add a --symlinks argument to soong_zip

Add a --symlinks argument that defaults to true to soong_zip.
Passing --symlinks=false will cause it to follow symlinks instead
of storing them in the zip file.

Bug: 112843624
Test: glob_test.go
Test: m checkbuild
Change-Id: I4deb98daa9d4ba9f94e3d7670c117fe00381d2ba
diff --git a/zip/cmd/main.go b/zip/cmd/main.go
index f49105a..c4e1196 100644
--- a/zip/cmd/main.go
+++ b/zip/cmd/main.go
@@ -186,6 +186,8 @@
 	emulateJar := flags.Bool("jar", false, "modify the resultant .zip to emulate the output of 'jar'")
 	writeIfChanged := flags.Bool("write_if_changed", false, "only update resultant .zip if it has changed")
 
+	symlinks := flags.Bool("symlinks", true, "store symbolic links in zip instead of following them")
+
 	parallelJobs := flags.Int("parallel", runtime.NumCPU(), "number of parallel threads to use")
 	cpuProfile := flags.String("cpuprofile", "", "write cpu profile to file")
 	traceFile := flags.String("trace", "", "write trace to file")
@@ -216,9 +218,10 @@
 		NumParallelJobs:          *parallelJobs,
 		NonDeflatedFiles:         nonDeflatedFiles,
 		WriteIfChanged:           *writeIfChanged,
+		StoreSymlinks:            *symlinks,
 	})
 	if err != nil {
-		fmt.Fprintln(os.Stderr, err.Error())
+		fmt.Fprintln(os.Stderr, "error:", err.Error())
 		os.Exit(1)
 	}
 }
diff --git a/zip/zip.go b/zip/zip.go
index 4a02531..d9645b8 100644
--- a/zip/zip.go
+++ b/zip/zip.go
@@ -107,6 +107,7 @@
 
 	compressorPool sync.Pool
 	compLevel      int
+	followSymlinks pathtools.ShouldFollowSymlinks
 }
 
 type zipEntry struct {
@@ -132,6 +133,7 @@
 	NumParallelJobs          int
 	NonDeflatedFiles         map[string]bool
 	WriteIfChanged           bool
+	StoreSymlinks            bool
 }
 
 const NOQUOTE = '\x00'
@@ -212,12 +214,16 @@
 		args.AddDirectoryEntriesToZip = true
 	}
 
+	// Have Glob follow symlinks if they are not being stored as symlinks in the zip file.
+	followSymlinks := pathtools.ShouldFollowSymlinks(!args.StoreSymlinks)
+
 	w := &ZipWriter{
-		time:         jar.DefaultTime,
-		createdDirs:  make(map[string]string),
-		createdFiles: make(map[string]string),
-		directories:  args.AddDirectoryEntriesToZip,
-		compLevel:    args.CompressionLevel,
+		time:           jar.DefaultTime,
+		createdDirs:    make(map[string]string),
+		createdFiles:   make(map[string]string),
+		directories:    args.AddDirectoryEntriesToZip,
+		compLevel:      args.CompressionLevel,
+		followSymlinks: followSymlinks,
 	}
 	pathMappings := []pathMapping{}
 
@@ -226,14 +232,14 @@
 	for _, fa := range args.FileArgs {
 		var srcs []string
 		for _, s := range fa.SourceFiles {
-			globbed, _, err := pathtools.Glob(s, nil, pathtools.DontFollowSymlinks)
+			globbed, _, err := pathtools.Glob(s, nil, followSymlinks)
 			if err != nil {
 				return err
 			}
 			srcs = append(srcs, globbed...)
 		}
 		if fa.GlobDir != "" {
-			globbed, _, err := pathtools.Glob(filepath.Join(fa.GlobDir, "**/*"), nil, pathtools.DontFollowSymlinks)
+			globbed, _, err := pathtools.Glob(filepath.Join(fa.GlobDir, "**/*"), nil, followSymlinks)
 			if err != nil {
 				return err
 			}
@@ -472,7 +478,15 @@
 	var fileSize int64
 	var executable bool
 
-	if s, err := os.Lstat(src); err != nil {
+	var s os.FileInfo
+	var err error
+	if z.followSymlinks {
+		s, err = os.Stat(src)
+	} else {
+		s, err = os.Lstat(src)
+	}
+
+	if err != nil {
 		return err
 	} else if s.IsDir() {
 		if z.directories {