Builder/spitfire/checks.go
2025-01-18 11:48:30 +01:00

86 lines
2.6 KiB
Go

package spitfire
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
// CheckDependencies ensures that all required tools and dependencies for the build are installed.
func CheckDependencies() error {
// Common dependencies
dependencies := map[string]string{
"git": "https://git-scm.com/download/win", // Git
"python": "https://www.python.org/downloads/", // Python
"pip3": "https://pip.pypa.io/en/stable/installing/", // Pip3
"mercurial": "https://www.mercurial-scm.org/", // Mercurial (hg)
}
// Add platform-specific dependencies
if runtime.GOOS == "windows" || !isMsys2() {
mozBuildPath := os.Getenv("MOZILLABUILD")
if mozBuildPath == "" {
mozBuildPath = "C:\\mozilla-build" // Default path for MozillaBuild
}
if !dirExists(mozBuildPath) {
dependencies["mozbuild"] = "https://ftp.mozilla.org/pub/mozilla/libraries/win32/MozillaBuildSetup-Latest.exe"
}
} else if runtime.GOOS != "windows" || !isMsys2() {
dependencies["rsync"] = "https://rsync.samba.org/download.html" // rsync for non-Windows platforms
}
// Check for missing tools
missingTools := []string{}
for tool, downloadLink := range dependencies {
if !isCommandAvailable(tool) {
missingTools = append(missingTools, fmt.Sprintf("%s (Download: %s)", tool, downloadLink))
}
}
// Special check for `mach` in the local source directory
machPath := filepath.Join("mozilla-release", "mach")
if !fileExists(machPath) {
missingTools = append(missingTools, "mach (ensure you are in the mozilla-release directory)")
}
// Report missing tools if any
if len(missingTools) > 0 {
fmt.Println("The following tools are missing and are required for the build:")
for _, tool := range missingTools {
fmt.Println(" - " + tool)
}
return fmt.Errorf("missing required tools")
}
fmt.Println("All required system dependencies are installed.")
return nil
}
// isCommandAvailable checks if a command/tool is available on the system.
func isCommandAvailable(command string) bool {
_, err := exec.LookPath(command)
return err == nil
}
// fileExists checks if a file exists at the given path.
func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
// dirExists checks if a directory exists at the given path.
func dirExists(path string) bool {
info, err := os.Stat(path)
if os.IsNotExist(err) {
return false
}
return info.IsDir()
}
// isMsys2 detects if the environment is MSYS2 by checking the MSYSTEM environment variable.
func isMsys2() bool {
return strings.Contains(strings.ToLower(os.Getenv("MSYSTEM")), "msys")
}