[tintd] Simplify installation

* Remove the CMake TINT_INSTALL_TINTD flag: it doesn't install, just use TINT_BUILD_TINTD.
* Make the default of './tools/run tintd install' copy instead of symlink.
* Enable TINT_BUILD_TINTD with setup-build.

Bug: tint:2127
Change-Id: I6a5b439225445cabe6d6bf5f20fe959c559728ac
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/180484
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Ben Clayton <bclayton@google.com>
Reviewed-by: David Neto <dneto@google.com>
diff --git a/tools/src/cmd/tintd/install/install.go b/tools/src/cmd/tintd/install/install.go
index fadf860..687683c 100644
--- a/tools/src/cmd/tintd/install/install.go
+++ b/tools/src/cmd/tintd/install/install.go
@@ -26,7 +26,6 @@
 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 // Package install installs the tintd vscode extension for local development
-// via a symlink to the build directory
 package install
 
 import (
@@ -49,6 +48,7 @@
 
 type Cmd struct {
 	flags struct {
+		symlink  bool
 		buildDir string
 		npmPath  string
 	}
@@ -59,12 +59,13 @@
 }
 
 func (Cmd) Desc() string {
-	return `install installs the tintd vscode extension for local development via a symlink to the build directory`
+	return `install installs the tintd vscode extension for local development`
 }
 
 func (c *Cmd) RegisterFlags(ctx context.Context, _ any) ([]string, error) {
 	dawnRoot := fileutils.DawnRoot()
 	npmPath, _ := exec.LookPath("npm")
+	flag.BoolVar(&c.flags.symlink, "symlink", false, "create a symlink from the vscode extension directory to the build directory")
 	flag.StringVar(&c.flags.buildDir, "build", filepath.Join(dawnRoot, "out", "active"), "the output build directory")
 	flag.StringVar(&c.flags.npmPath, "npm", npmPath, "path to npm")
 
@@ -102,7 +103,6 @@
 		return fmt.Errorf("could not parse '%v': %v", packageJSONPath, err)
 	}
 
-	// Symlink to vscode extensions directory
 	home, err := os.UserHomeDir()
 	if err != nil {
 		return fmt.Errorf("failed to obtain home directory: %w", err)
@@ -115,8 +115,16 @@
 	vscodeTintdDir := filepath.Join(vscodeBaseExtsDir, fmt.Sprintf("google.%v-%v", pkg.Name, pkg.Version))
 	os.RemoveAll(vscodeTintdDir)
 
-	if err := os.Symlink(pkgDir, vscodeTintdDir); err != nil {
-		return fmt.Errorf("failed to create symlink '%v' <- '%v': %w", pkgDir, vscodeTintdDir, err)
+	if c.flags.symlink {
+		// Symlink the vscode extensions directory to the build directory
+		if err := os.Symlink(pkgDir, vscodeTintdDir); err != nil {
+			return fmt.Errorf("failed to create symlink '%v' <- '%v': %w", pkgDir, vscodeTintdDir, err)
+		}
+	} else {
+		// Copy the build directory to vscode extensions directory
+		if err := fileutils.CopyDir(vscodeTintdDir, pkgDir); err != nil {
+			return fmt.Errorf("failed to copy '%v' to '%v': %w", pkgDir, vscodeTintdDir, err)
+		}
 	}
 
 	return nil
diff --git a/tools/src/fileutils/copy.go b/tools/src/fileutils/copy.go
new file mode 100644
index 0000000..0170c4f
--- /dev/null
+++ b/tools/src/fileutils/copy.go
@@ -0,0 +1,106 @@
+// Copyright 2024 The Dawn & Tint Authors
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+//
+// 2. 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.
+//
+// 3. Neither the name of the copyright holder 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 BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER 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 USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+package fileutils
+
+import (
+	"fmt"
+	"io"
+	"os"
+	"path/filepath"
+)
+
+// CopyFile copies the file from 'src' to 'dst' replacing the existing file at 'dst' if it already
+// exists.
+func CopyFile(dst, src string) error {
+	if !IsFile(src) {
+		return fmt.Errorf("'%v' is not a file", src)
+	}
+
+	dstDir := filepath.Dir(dst)
+	if !IsDir(dstDir) {
+		if err := os.MkdirAll(dstDir, 0777); err != nil {
+			return err
+		}
+	}
+
+	s, err := os.Open(src)
+	if err != nil {
+		return err
+	}
+	defer s.Close()
+
+	info, err := s.Stat()
+	if err != nil {
+		return err
+	}
+
+	d, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666|info.Mode()&0777)
+	if err != nil {
+		return err
+	}
+	defer d.Close()
+
+	_, err = io.Copy(d, s)
+	return err
+}
+
+// CopyDir copies the directory and all its content from 'src' to 'dst' replacing the existing
+// directory at 'dst' if it already exists.
+func CopyDir(dst, src string) error {
+	if !IsDir(src) {
+		return fmt.Errorf("'%v' is not a directory", src)
+	}
+	if IsFile(dst) {
+		return fmt.Errorf("'%v' is a file", dst)
+	}
+
+	if IsDir(dst) {
+		if err := os.RemoveAll(dst); err != nil {
+			return err
+		}
+	}
+
+	return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
+		if err != nil {
+			return err
+		}
+
+		rel, err := filepath.Rel(src, path)
+		if err != nil {
+			return err
+		}
+
+		if !info.IsDir() {
+			if err := CopyFile(filepath.Join(dst, rel), path); err != nil {
+				return err
+			}
+		}
+
+		return nil
+	})
+}