Builder/spitfire/checks.go

91 lines
2.6 KiB
Go
Raw Normal View History

2024-09-13 12:47:30 +02:00
package spitfire
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
2025-01-11 23:11:11 +01:00
"strings"
2024-09-13 12:47:30 +02:00
)
2025-01-11 23:11:11 +01:00
// CheckDependencies ensures that all required tools and dependencies for the build are installed.
func CheckDependencies() error {
// Common dependencies
dependencies := map[string]string{
2025-01-18 20:41:00 +01:00
"git": "https://git-scm.com/downloads", // Git
"python": "https://www.python.org/downloads/", // Python
"pip3": "https://pip.pypa.io/en/stable/installing/", // Pip3
"hg": "https://www.mercurial-scm.org/", // Mercurial (hg)
2024-09-13 12:47:30 +02:00
}
2025-01-11 23:11:11 +01:00
// Add platform-specific dependencies
2025-01-18 20:41:00 +01:00
if runtime.GOOS == "windows" {
2024-09-13 12:47:30 +02:00
mozBuildPath := os.Getenv("MOZILLABUILD")
if mozBuildPath == "" {
2025-01-18 20:41:00 +01:00
mozBuildPath = "C:\\mozilla-build" // Default path for MozillaBuild on Windows
2024-09-13 12:47:30 +02:00
}
if !dirExists(mozBuildPath) {
2025-01-11 23:11:11 +01:00
dependencies["mozbuild"] = "https://ftp.mozilla.org/pub/mozilla/libraries/win32/MozillaBuildSetup-Latest.exe"
2024-09-13 12:47:30 +02:00
}
2025-01-18 20:41:00 +01:00
} else {
2025-01-11 23:11:11 +01:00
dependencies["rsync"] = "https://rsync.samba.org/download.html" // rsync for non-Windows platforms
2024-09-13 12:47:30 +02:00
}
2025-01-11 23:11:11 +01:00
// Check for missing tools
2024-09-13 12:47:30 +02:00
missingTools := []string{}
2025-01-11 23:11:11 +01:00
for tool, downloadLink := range dependencies {
2024-09-13 12:47:30 +02:00
if !isCommandAvailable(tool) {
missingTools = append(missingTools, fmt.Sprintf("%s (Download: %s)", tool, downloadLink))
}
}
2025-01-18 20:41:00 +01:00
// Check for `mach` script in the source directory
2025-01-18 11:48:30 +01:00
machPath := filepath.Join("mozilla-release", "mach")
2024-09-13 12:47:30 +02:00
if !fileExists(machPath) {
2025-01-18 20:41:00 +01:00
missingTools = append(missingTools, "mach (ensure you are in the mozilla-release directory and it is built)")
2024-09-13 12:47:30 +02:00
}
2025-01-11 23:11:11 +01:00
// Report missing tools if any
2024-09-13 12:47:30 +02:00
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 {
2025-01-18 20:41:00 +01:00
path, err := exec.LookPath(command)
if err != nil {
return false
}
fmt.Printf("Command '%s' found at path '%s'\n", command, path)
return true
2024-09-13 12:47:30 +02:00
}
// 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()
}
2025-01-11 23:11:11 +01:00
// 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")
}