From 4c62eaf7e56998149081ad9634ca6cb4b3d880a5 Mon Sep 17 00:00:00 2001 From: Diego Becciolini Date: Tue, 30 Jun 2020 17:55:45 +0100 Subject: [PATCH] feedback and tweaks --- upgrade/upgrade.go | 47 +++++++++++++++++++++++++++++++++----- upgrade/upgrade_unix.go | 30 +++--------------------- upgrade/upgrade_windows.go | 22 +++--------------- 3 files changed, 47 insertions(+), 52 deletions(-) diff --git a/upgrade/upgrade.go b/upgrade/upgrade.go index 2c4a92d..919e186 100644 --- a/upgrade/upgrade.go +++ b/upgrade/upgrade.go @@ -10,12 +10,14 @@ import ( "crypto/sha1" "encoding/hex" "errors" + "flag" "fmt" "io" "io/ioutil" "log" "net/http" "os" + "os/exec" "path/filepath" "strings" "time" @@ -23,9 +25,18 @@ import ( "github.com/PuerkitoBio/goquery" ) -const buildFlags = "-DSQLITE_ENABLE_UPDATE_DELETE_LIMIT=1" +var ( + cFlags = "-DSQLITE_ENABLE_UPDATE_DELETE_LIMIT=1" + cleanup = true +) func main() { + flag.StringVar(&shellPath, "shell", shellPath, "path to shell executable") + flag.StringVar(&makePath, "make", makePath, "path to make executable") + flag.StringVar(&cFlags, "cflags", cFlags, "sqlite CFLAGS") + flag.BoolVar(&cleanup, "cleanup", cleanup, "cleanup source") + flag.Parse() + err := func() error { fmt.Println("Go-SQLite3 Upgrade Tool") @@ -46,7 +57,7 @@ func main() { // Extract Source baseDir, err := extractZip(source) - if baseDir != "" && !filepath.IsAbs(baseDir) { + if cleanup && baseDir != "" && !filepath.IsAbs(baseDir) { defer func() { fmt.Println("Cleaning up source: deleting", baseDir) os.RemoveAll(baseDir) @@ -57,9 +68,9 @@ func main() { } fmt.Println("Extracted sqlite source to", baseDir) - // Build amalgamation files (OS-specific) - fmt.Printf("Starting to generate amalgamation with build flags: %s\n", buildFlags) - if err := buildAmalgamation(baseDir, buildFlags); err != nil { + // Build amalgamation files + fmt.Printf("Starting to generate amalgamation with CFLAGS: %s\n", cFlags) + if err := buildAmalgamation(baseDir, cFlags); err != nil { return fmt.Errorf("failed to build amalgamation: %v", err) } fmt.Println("SQLite3 amalgamation built") @@ -73,7 +84,7 @@ func main() { return nil }() if err != nil { - log.Fatal("Returned with error:", err) + log.Fatalln("Returned with error:", err) } } @@ -255,3 +266,27 @@ func patchSource(baseDir, src, dst string, extensions ...string) error { return nil } + +func buildAmalgamation(baseDir, buildFlags string) error { + configureScript := "./configure" + if cFlags != "" { + configureScript += fmt.Sprintf(" CFLAGS=%q", cFlags) + } + cmd := exec.Command(shellPath, "-c", configureScript) + cmd.Dir = baseDir + out, err := cmd.CombinedOutput() + if err != nil { + return fmt.Errorf("configure failed: %v\n\n%s", err, out) + } + fmt.Println("Ran configure successfully") + + cmd = exec.Command(makePath, "sqlite3.c") + cmd.Dir = baseDir + out, err = cmd.CombinedOutput() + if err != nil { + return fmt.Errorf("make failed: %v\n\n%s", err, out) + } + fmt.Println("Ran make successfully") + + return nil +} diff --git a/upgrade/upgrade_unix.go b/upgrade/upgrade_unix.go index 61ad3bc..2296719 100644 --- a/upgrade/upgrade_unix.go +++ b/upgrade/upgrade_unix.go @@ -4,31 +4,7 @@ package main -import ( - "fmt" - "os/exec" +var ( + shellPath = "bash" + makePath = "make" ) - -func buildAmalgamation(baseDir, buildFlags string) error { - args := []string{"configure"} - if buildFlags != "" { - args = append(args, "CFLAGS="+buildFlags) - } - cmd := exec.Command("sh", args...) - cmd.Dir = baseDir - out, err := cmd.CombinedOutput() - if err != nil { - return fmt.Errorf("configure failed: %v\n\n%s", err, out) - } - fmt.Println("Ran configure successfully") - - cmd = exec.Command("make", "sqlite3.c") - cmd.Dir = baseDir - out, err = cmd.CombinedOutput() - if err != nil { - return fmt.Errorf("make failed: %v\n\n%s", err, out) - } - fmt.Println("Ran make successfully") - - return nil -} diff --git a/upgrade/upgrade_windows.go b/upgrade/upgrade_windows.go index 7f926f8..b4c498b 100644 --- a/upgrade/upgrade_windows.go +++ b/upgrade/upgrade_windows.go @@ -3,23 +3,7 @@ package main -import ( - "fmt" - "os/exec" +var ( + shellPath = `c:\msys64\usr\bin\bash.exe` + makePath = `c:\msys64\usr\bin\make.exe` ) - -func buildAmalgamation(baseDir, buildFlags string) error { - args := []string{"/f", "Makefile.msc", "sqlite3.c"} - if buildFlags != "" { - args = append(args, "OPTS="+buildFlags) - } - cmd := exec.Command("nmake", args...) - cmd.Dir = baseDir - out, err := cmd.CombinedOutput() - if err != nil { - return fmt.Errorf("nmake failed: %v\n\n%s", err, out) - } - fmt.Println("Ran nmake successfully") - - return nil -}