windows build compatability

This commit is contained in:
partisan 2024-09-13 12:47:30 +02:00
parent 048bbc1e3e
commit 358cd7147b
3 changed files with 137 additions and 29 deletions

35
main.go
View file

@ -67,16 +67,11 @@ func printHelp() {
} }
func main() { func main() {
// // Check system dependencies
path, err3 := spitfire.ResolvePath("/mozilla-central") // err := spitfire.CheckSystemDependencies()
// Check and set the MOZILLABUILD environment variable globally // if err != nil {
err2 := spitfire.SetGlobalEnv("MOZILLABUILD", path, "user") // For user // log.Fatalf("System check failed: %v", err)
if err2 != nil { // }
log.Fatalf("Error setting global environment variable: %v", err2)
}
if err3 != nil {
log.Fatalf("Error setting global environment variable: %v", err3)
}
flag.Parse() flag.Parse()
@ -90,26 +85,26 @@ func main() {
} }
// Save the initial directory // Save the initial directory
var err error var err2 error
initialDir, err = os.Getwd() initialDir, err2 = os.Getwd()
if err != nil { if err2 != nil {
log.Fatalf("Failed to get current working directory: %v", err) log.Fatalf("Failed to get current working directory: %v", err2)
} }
fmt.Printf("Initial working directory: %s\n", initialDir) fmt.Printf("Initial working directory: %s\n", initialDir)
// Convert buildPath and uploadPath to absolute paths // Convert buildPath and uploadPath to absolute paths
if buildPath != "" { if buildPath != "" {
buildPath, err = spitfire.ResolvePath(buildPath) buildPath, err2 = spitfire.ResolvePath(buildPath)
if err != nil { if err2 != nil {
log.Fatalf("Failed to convert buildPath to absolute path: %v", err) log.Fatalf("Failed to convert buildPath to absolute path: %v", err2)
} }
fmt.Printf("Resolved buildPath: %s\n", buildPath) fmt.Printf("Resolved buildPath: %s\n", buildPath)
} }
if uploadPath != "" { if uploadPath != "" {
uploadPath, err = spitfire.ResolvePath(uploadPath) uploadPath, err2 = spitfire.ResolvePath(uploadPath)
if err != nil { if err2 != nil {
log.Fatalf("Failed to convert uploadPath to absolute path: %v", err) log.Fatalf("Failed to convert uploadPath to absolute path: %v", err2)
} }
fmt.Printf("Resolved uploadPath: %s\n", uploadPath) fmt.Printf("Resolved uploadPath: %s\n", uploadPath)
} }

View file

@ -37,6 +37,7 @@ func SetGlobalEnv(variable, value string, scope string) error {
// Run an external command like scp or rsync // Run an external command like scp or rsync
func runCommand(command string, args ...string) error { func runCommand(command string, args ...string) error {
fmt.Printf("Running command: %s %v\n", command, args)
cmd := exec.Command(command, args...) cmd := exec.Command(command, args...)
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
@ -83,10 +84,21 @@ func CleanBuild(sourcePath string) {
errors = append(errors, "Failed to navigate to source directory.") errors = append(errors, "Failed to navigate to source directory.")
return return
} }
// Use the appropriate mach command for Windows or Unix-like systems
var machCmd string
if runtime.GOOS == "windows" {
machCmd = ".\\mach"
} else {
machCmd = "./mach"
}
// Revert uncommitted changes
if err := runCommand("hg", "revert", "--all", "--no-backup"); err != nil { if err := runCommand("hg", "revert", "--all", "--no-backup"); err != nil {
errors = append(errors, "Failed to revert changes in Mozilla repository.") errors = append(errors, "Failed to revert changes in Mozilla repository.")
} }
if err := runCommand("./mach", "clobber"); err != nil { // Clean the build
if err := runCommand(machCmd, "clobber"); err != nil {
errors = append(errors, "Failed to clean build.") errors = append(errors, "Failed to clean build.")
} }
} }
@ -147,7 +159,7 @@ func UpdatePatches(patchesDir, patchesRepo, sourcePath string) {
fmt.Println("Copying files from patches directory to Firefox source directory...") fmt.Println("Copying files from patches directory to Firefox source directory...")
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
// Use robocopy for Windows // Use robocopy for Windows
if err := runCommand("robocopy", patchesDir, sourcePath, "*", "/E", "/XF", ".git"); err != nil { if err := runCommand("robocopy", patchesDir, sourcePath, "*", "/S", "/XF", ".git", "/XD", ".git"); err != nil {
errors = append(errors, "Failed to copy files (Windows robocopy).") errors = append(errors, "Failed to copy files (Windows robocopy).")
} }
} else { } else {
@ -165,7 +177,16 @@ func Configure(sourcePath string) {
errors = append(errors, "Failed to navigate to source directory.") errors = append(errors, "Failed to navigate to source directory.")
return return
} }
if err := runCommand("./mach", "configure"); err != nil {
// Use the appropriate mach command for Windows or Unix-like systems
var machCmd string
if runtime.GOOS == "windows" {
machCmd = ".\\mach"
} else {
machCmd = "./mach"
}
if err := runCommand(machCmd, "configure"); err != nil {
errors = append(errors, "Configuration failed.") errors = append(errors, "Configuration failed.")
} }
} }
@ -177,7 +198,16 @@ func Build(sourcePath string) {
errors = append(errors, "Failed to navigate to source directory.") errors = append(errors, "Failed to navigate to source directory.")
return return
} }
if err := runCommand("./mach", "build"); err != nil {
// Use the appropriate mach command for Windows or Unix-like systems
var machCmd string
if runtime.GOOS == "windows" {
machCmd = ".\\mach"
} else {
machCmd = "./mach"
}
if err := runCommand(machCmd, "build"); err != nil {
errors = append(errors, "Build failed.") errors = append(errors, "Build failed.")
} }
} }
@ -185,11 +215,16 @@ func Build(sourcePath string) {
// Function to run the project after build // Function to run the project after build
func RunProject(sourcePath string) { func RunProject(sourcePath string) {
fmt.Println("Running the project...") fmt.Println("Running the project...")
if err := os.Chdir(sourcePath); err != nil {
errors = append(errors, "Failed to navigate to source directory.") // Use the appropriate mach command for Windows or Unix-like systems
return var machCmd string
if runtime.GOOS == "windows" {
machCmd = ".\\mach"
} else {
machCmd = "./mach"
} }
if err := runCommand("./mach", "run"); err != nil {
if err := runCommand(machCmd, "run"); err != nil {
errors = append(errors, "Failed to run the project.") errors = append(errors, "Failed to run the project.")
} }
} }

78
spitfire/checks.go Normal file
View file

@ -0,0 +1,78 @@
package spitfire
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
)
// CheckSystemDependencies ensures that required tools for building are installed.
func CheckSystemDependencies() error {
requiredTools := 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
}
if runtime.GOOS == "windows" {
// Check for MozillaBuild installation
mozBuildPath := os.Getenv("MOZILLABUILD")
if mozBuildPath == "" {
mozBuildPath = "C:\\mozilla-build" // Default to standard MozillaBuild path
}
// Check if MozillaBuild exists at the specified location
if !dirExists(mozBuildPath) {
requiredTools["mozbuild"] = "https://ftp.mozilla.org/pub/mozilla/libraries/win32/MozillaBuildSetup-Latest.exe"
}
}
missingTools := []string{}
// Check for each required tool
for tool, downloadLink := range requiredTools {
if !isCommandAvailable(tool) {
missingTools = append(missingTools, fmt.Sprintf("%s (Download: %s)", tool, downloadLink))
}
}
// Special check for mach in the local source directory (mozilla-central)
machPath := filepath.Join("mozilla-central", "mach")
if !fileExists(machPath) {
missingTools = append(missingTools, fmt.Sprintf("mach (run from mozilla-central directory)"))
}
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()
}