Builder/main.go

354 lines
12 KiB
Go
Raw Normal View History

2024-09-09 01:25:07 +02:00
package main
import (
2024-09-10 23:02:25 +02:00
"flag"
"fmt"
"log"
"os"
"path/filepath"
"runtime" // for detecting system architecture and platform
2024-12-08 14:34:31 +01:00
"spitfire-builder/spitfire"
2024-09-10 23:02:25 +02:00
"time"
//"errors"
2024-09-09 01:25:07 +02:00
)
var (
2024-09-10 23:02:25 +02:00
// Define all flags as package-level variables
all bool
buildFlag bool
clean bool
2024-12-16 21:24:35 +01:00
fullClean bool
update bool
prePatch bool
postPatch bool
skipPatchUpdate bool
run bool
compress bool
target string
version string
component string
arch string
release string
platform string
upload bool
uploadPath string
2025-01-18 11:48:30 +01:00
sourceRepo = "https://hg.mozilla.org/releases/mozilla-release"
patchesRepo = "https://weforge.xyz/Spitfire/Patcher.git"
url = "https://spitfirebrowser.xyz/"
licence = "AGPL-3.0"
name = "Spitfire"
2024-12-23 11:24:42 +01:00
packageName = "spitfire-browser"
maintainer = "Internet Addict"
initialDir string
2024-12-29 00:58:19 +01:00
skipDeps bool
2024-09-09 01:25:07 +02:00
)
func init() {
2024-09-10 23:02:25 +02:00
flag.StringVar(&target, "t", "", "Target location format: component-arch-release-platform")
flag.BoolVar(&compress, "c", false, "Compress the build directory into a tar.gz file before uploading")
flag.StringVar(&version, "v", "", "Specify version for the package. For nightly, use current date if not specified.")
flag.StringVar(&component, "component", "browser", "Component name (default: browser)")
flag.StringVar(&arch, "arch", runtime.GOARCH, "Architecture (default: system architecture)")
flag.StringVar(&release, "release", "nightly", "Release type (default: nightly)")
flag.StringVar(&platform, "platform", runtime.GOOS, "Platform (default: system platform)")
flag.BoolVar(&all, "a", false, "Perform all steps (build, clean, update)")
flag.BoolVar(&buildFlag, "b", false, "Build Spitfire")
2024-12-16 21:24:35 +01:00
flag.BoolVar(&clean, "clean", false, "Revert uncommitted changes without removing build artifacts")
flag.BoolVar(&fullClean, "full-clean", false, "Perform a full clean by removing all build artifacts (clobber)")
2024-09-10 23:02:25 +02:00
flag.BoolVar(&update, "u", false, "Update Mozilla repository")
flag.BoolVar(&prePatch, "pre-patch", false, "Apply pre-build patches")
flag.BoolVar(&postPatch, "post-patch", false, "Apply post-build patches")
flag.BoolVar(&skipPatchUpdate, "skip-patch-update", false, "Skip updating the patches repository")
2024-09-10 23:02:25 +02:00
flag.BoolVar(&run, "r", false, "Run the project after build")
flag.BoolVar(&upload, "upload", false, "Upload the compressed build file to SourceForge")
flag.StringVar(&uploadPath, "upload-path", "", "Path to the file to upload if no build present")
2024-12-29 00:58:19 +01:00
flag.BoolVar(&skipDeps, "skip-deps", false, "Skip checking for required system dependencies")
2024-09-10 23:02:25 +02:00
flag.Bool("h", false, "Display help message")
2024-09-09 01:25:07 +02:00
}
func printHelp() {
2024-09-10 23:09:24 +02:00
fmt.Println("Usage: go run . -p=<path-to-build> -t=<target> [-c|--compress] [-v|--version=<version>] [-component=<component>] [-arch=<architecture>] [-release=<release>] [-platform=<platform>]")
2024-09-10 23:02:25 +02:00
flag.PrintDefaults()
fmt.Println("Example: go run . --upload -c -a")
2024-09-10 23:02:25 +02:00
os.Exit(0)
2024-09-09 01:25:07 +02:00
}
func main() {
2024-09-10 23:02:25 +02:00
flag.Parse()
if flag.Lookup("h").Value.(flag.Getter).Get().(bool) {
printHelp()
}
2024-12-29 00:58:19 +01:00
if !skipDeps {
2025-01-11 23:11:11 +01:00
if err := spitfire.CheckDependencies(); err != nil {
2024-12-29 00:58:19 +01:00
log.Fatalf("System check failed: %v", err)
}
2024-12-08 09:05:39 +01:00
}
2024-09-10 23:02:25 +02:00
if version == "" && release == "nightly" {
version = time.Now().Format("2006.01.02")
2024-09-10 23:02:25 +02:00
}
// Save the initial directory
var err error
initialDir, err = os.Getwd()
if err != nil {
log.Fatalf("Failed to get current working directory: %v", err)
2024-09-10 23:02:25 +02:00
}
var buildDir string // Store the resolved build directory here
2024-09-10 23:02:25 +02:00
// Perform build if necessary
2024-12-11 17:12:00 +01:00
if all || buildFlag || prePatch || postPatch || clean || update || run {
buildDir = BuildProcess()
if buildDir == "" {
log.Fatalf("Build process completed, but no build directory was found.")
}
fmt.Printf("Build directory from process: %s\n", buildDir)
2024-09-10 23:02:25 +02:00
}
// Resolve build directory for compression/upload
2024-09-10 23:02:25 +02:00
if compress || upload {
if buildDir == "" { // Resolve dynamically if no build was performed
fmt.Println("No build directory detected during build process. Resolving dynamically...")
sourcePath, err := spitfire.ResolvePath("./mozilla-release")
if err != nil {
log.Fatalf("Error resolving source path: %v", err)
}
fmt.Printf("Resolved source path: %s\n", sourcePath)
2025-01-11 23:11:11 +01:00
buildDir, err = spitfire.ResolveBuildDir(sourcePath)
if err != nil {
log.Fatalf("Error resolving build directory dynamically: %v", err)
}
2025-01-11 23:11:11 +01:00
}
if buildDir == "" {
log.Fatalf("No build directory found for compression or upload.")
2025-01-11 23:11:11 +01:00
}
fmt.Printf("Resolved build directory for compress/upload: %s\n", buildDir)
2025-01-11 23:11:11 +01:00
PackageAndUploadProcess(buildDir)
2024-09-10 23:02:25 +02:00
}
spitfire.PrintErrors()
2024-09-09 01:25:07 +02:00
}
2024-12-08 14:34:31 +01:00
// Update the BuildProcess function to pass patchesRepo
func BuildProcess() string {
2025-01-18 11:48:30 +01:00
sourcePath, err := spitfire.ResolvePath("./mozilla-release")
2024-09-10 23:02:25 +02:00
if err != nil {
log.Fatalf("Error resolving source path: %v", err)
}
var buildDir string
2024-09-10 23:02:25 +02:00
if all {
spitfire.DownloadSource(sourcePath, sourceRepo)
spitfire.DiscardChanges(sourcePath)
2024-12-16 21:24:35 +01:00
spitfire.CleanBuild(sourcePath, fullClean)
2024-09-10 23:02:25 +02:00
spitfire.UpdateRepo(sourcePath)
if err := spitfire.ApplyPatches(sourcePath, patchesRepo, "pre-compile-patches", filepath.Join(sourcePath, "patcher"), skipPatchUpdate); err != nil {
2024-12-08 14:34:31 +01:00
log.Fatalf("Error during patch application: %v", err)
}
2024-09-10 23:02:25 +02:00
spitfire.Configure(sourcePath)
spitfire.Build(sourcePath)
2024-12-29 08:54:45 +01:00
// Detect the build directory dynamically
buildDir, err = spitfire.ResolveBuildDir(sourcePath)
2024-12-29 08:54:45 +01:00
if err != nil {
log.Fatalf("Error resolving build directory: %v", err)
}
if err := spitfire.ApplyPatches(buildDir, patchesRepo, "post-compile-patches", filepath.Join(sourcePath, "patcher"), skipPatchUpdate); err != nil {
2024-12-11 16:45:34 +01:00
log.Fatalf("Error during patch application: %v", err)
}
2024-09-10 23:02:25 +02:00
if run {
spitfire.RunProject(sourcePath)
}
fmt.Println("Spitfire build completed successfully.")
2024-12-13 14:31:28 +01:00
} else if buildFlag {
if prePatch {
if err := spitfire.ApplyPatches(sourcePath, patchesRepo, "pre-compile-patches", filepath.Join(sourcePath, "patcher"), skipPatchUpdate); err != nil {
log.Fatalf("Error during patch application: %v", err)
}
}
spitfire.Configure(sourcePath)
spitfire.Build(sourcePath)
2024-12-29 08:54:45 +01:00
// Detect the build directory dynamically
buildDir, err = spitfire.ResolveBuildDir(sourcePath)
2024-12-29 08:54:45 +01:00
if err != nil {
log.Fatalf("Error resolving build directory: %v", err)
}
2024-12-13 14:31:28 +01:00
if postPatch {
if err := spitfire.ApplyPatches(buildDir, patchesRepo, "post-compile-patches", filepath.Join(sourcePath, "patcher"), skipPatchUpdate); err != nil {
log.Fatalf("Error during patch application: %v", err)
}
}
if run {
spitfire.RunProject(sourcePath)
}
fmt.Println("Spitfire build completed successfully.")
2024-09-10 23:02:25 +02:00
} else if clean {
2024-12-16 21:24:35 +01:00
spitfire.CleanBuild(sourcePath, fullClean)
2024-09-10 23:02:25 +02:00
fmt.Println("Cleaned Firefox build.")
} else if update {
spitfire.DownloadSource(sourcePath, sourceRepo)
spitfire.UpdateRepo(sourcePath)
fmt.Println("Mozilla repository updated.")
} else if prePatch {
2024-09-10 23:02:25 +02:00
spitfire.DownloadSource(sourcePath, sourceRepo)
if err := spitfire.ApplyPatches(sourcePath, patchesRepo, "pre-compile-patches", filepath.Join(sourcePath, "patcher"), skipPatchUpdate); err != nil {
2024-12-08 14:34:31 +01:00
log.Fatalf("Error during patch application: %v", err)
}
2024-09-10 23:02:25 +02:00
fmt.Println("Patches updated.")
} else if postPatch {
2024-12-29 08:54:45 +01:00
// Detect the build directory dynamically
buildDir, err = spitfire.ResolveBuildDir(sourcePath)
2024-12-29 08:54:45 +01:00
if err != nil {
log.Fatalf("Error resolving build directory: %v", err)
}
if _, err := os.Stat(buildDir); err == nil {
if err := spitfire.ApplyPatches(buildDir, patchesRepo, "post-compile-patches", filepath.Join(sourcePath, "patcher"), skipPatchUpdate); err != nil {
log.Fatalf("Error during patch application: %v", err)
}
}
2024-09-10 23:02:25 +02:00
if run {
spitfire.RunProject(sourcePath)
}
2024-12-11 17:12:00 +01:00
} else if run {
spitfire.RunProject(sourcePath)
2024-09-10 23:02:25 +02:00
}
return buildDir
2024-09-09 01:25:07 +02:00
}
// PackageAndUploadProcess handles compressing, packaging, and uploading the build to SourceForge.
2025-01-11 23:11:11 +01:00
func PackageAndUploadProcess(buildDir string) {
2024-09-10 23:02:25 +02:00
// Restore working directory before performing SourceForge operations
restoreWorkingDirectory()
2025-01-11 23:11:11 +01:00
pathToUse := buildDir
2024-09-10 23:02:25 +02:00
if upload && uploadPath != "" {
pathToUse = uploadPath
}
if pathToUse == "" {
log.Fatalf("Error: no valid build or upload path provided.")
}
2024-12-23 11:24:42 +01:00
// Calculate & display uncompressed size
2024-09-10 23:02:25 +02:00
uncompressedSize, err := spitfire.GetDirectorySize(pathToUse)
if err != nil {
log.Fatalf("Failed to calculate uncompressed size: %v", err)
}
fmt.Printf("Uncompressed directory size: %s\n", spitfire.BytesToHumanReadable(uncompressedSize))
2024-09-10 23:02:25 +02:00
2024-12-23 11:24:42 +01:00
// Create the tar.gz name, e.g. "browser-amd64-nightly-linux.tar.gz"
outputCompressedFile := filepath.Join(".", fmt.Sprintf(
"%s-%s-%s-%s.tar.gz",
component, arch, release, platform,
))
// Compress if requested
2024-09-10 23:02:25 +02:00
if compress {
err := spitfire.CompressDirectory(pathToUse, outputCompressedFile)
if err != nil {
log.Fatalf("Failed to compress build directory: %v", err)
}
fmt.Printf("Build directory compressed to: %s\n", outputCompressedFile)
}
2024-12-23 11:24:42 +01:00
// Compressed size
2024-09-10 23:02:25 +02:00
compressedSize, err := spitfire.GetFileSize(outputCompressedFile)
if err != nil {
log.Fatalf("Failed to get compressed file size: %v", err)
}
fmt.Printf("Compressed file size: %s\n", spitfire.BytesToHumanReadable(compressedSize))
2024-12-23 11:24:42 +01:00
// Show compression ratio & efficiency
compressionRatio, efficiency := spitfire.CalculateCompressionEfficiency(uncompressedSize, compressedSize)
fmt.Printf("Compression ratio: %.2f:1\n", compressionRatio)
fmt.Printf("Compression efficiency: %.2f%%\n", efficiency)
2024-09-10 23:02:25 +02:00
2025-01-11 23:11:11 +01:00
// Display compressed directory path
fmt.Printf("Compressed dir: %s\n", pathToUse)
2025-01-11 23:11:11 +01:00
2024-12-23 11:24:42 +01:00
// If not uploading, we're done
if !upload {
return
}
2024-09-10 23:02:25 +02:00
2024-12-23 11:24:42 +01:00
// Load SourceForge config
config, err := spitfire.LoadConfig()
if err != nil {
log.Fatalf("Failed to load SourceForge config: %v", err)
}
2024-09-10 23:02:25 +02:00
2024-12-23 11:24:42 +01:00
// Check tarball existence
if _, err := os.Stat(outputCompressedFile); err != nil {
log.Fatalf("No compressed file found to upload: %v", err)
}
2024-09-10 23:02:25 +02:00
2024-12-23 11:24:42 +01:00
// The subdirectory path in the SF project
// e.g. "browser/amd64/nightly/2024.12.23"
uploadDir := fmt.Sprintf("%s/%s/%s/%s", component, arch, release, version)
2024-09-10 23:02:25 +02:00
2024-12-23 11:24:42 +01:00
// 1) Upload the file to SourceForge once
err = spitfire.Upload(config, outputCompressedFile,
"/home/frs/project/spitfire-browser/"+uploadDir+"/")
if err != nil {
log.Fatalf("Failed to upload compressed file: %v", err)
}
fmt.Println("Compressed file uploaded successfully.")
2024-09-10 23:02:25 +02:00
2024-12-23 11:24:42 +01:00
// 2) Download existing APPINDEX or create new
err = spitfire.DownloadAPPINDEX(config, "/home/frs/project/spitfire-browser/")
if err != nil {
fmt.Println("Failed to download APPINDEX. A new one will be created and uploaded.")
}
// 3) Update the APPINDEX
err = spitfire.PackageAPPINDEX(
packageName, // e.g. "spitfire-browser"
release, // e.g. "nightly"
version, // e.g. "2024.12.23"
arch,
fmt.Sprintf("%d", compressedSize),
fmt.Sprintf("%d", uncompressedSize),
name, // e.g. "Spitfire"
url,
licence,
component,
maintainer,
"", // dependencies
platform,
uploadDir,
)
if err != nil {
log.Fatalf("Failed to update APPINDEX: %v", err)
}
fmt.Println("APPINDEX updated successfully.")
// 4) Clean
if err := spitfire.CleanAppIndex(); err != nil {
log.Fatalf("Failed to clean APPINDEX: %v", err)
}
// 5) Upload the updated APPINDEX
err = spitfire.UploadAPPINDEX(config, "/home/frs/project/spitfire-browser/")
if err != nil {
log.Fatalf("Failed to upload updated APPINDEX: %v", err)
2024-09-10 23:02:25 +02:00
}
2024-12-23 11:24:42 +01:00
fmt.Println("APPINDEX uploaded successfully.")
2024-09-09 01:25:07 +02:00
}
// restoreWorkingDirectory restores the initial working directory after any operation that might change it.
func restoreWorkingDirectory() {
2024-09-10 23:02:25 +02:00
err := os.Chdir(initialDir)
if err != nil {
log.Fatalf("Failed to restore the working directory: %v", err)
}
fmt.Printf("Restored working directory to: %s\n", initialDir)
}