2015-10-17 19:32:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2015-10-17 22:50:51 +00:00
|
|
|
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/tools/git"
|
2015-10-17 19:32:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-05-25 19:51:44 +00:00
|
|
|
flagTargetDir = flag.String("dir", "", "Directory to put generated files.")
|
2015-12-27 22:27:39 +00:00
|
|
|
flagTargetOS = flag.String("os", runtime.GOOS, "Target OS of this build.")
|
|
|
|
flagTargetArch = flag.String("arch", runtime.GOARCH, "Target CPU arch of this build.")
|
|
|
|
flagArchive = flag.Bool("zip", false, "Whether to make an archive of files or not.")
|
|
|
|
flagMetadataFile = flag.String("metadata", "metadata.txt", "File to store metadata info of released packages.")
|
2015-11-04 20:20:06 +00:00
|
|
|
|
|
|
|
binPath string
|
2015-10-17 19:32:48 +00:00
|
|
|
)
|
|
|
|
|
2015-10-17 22:50:51 +00:00
|
|
|
func createTargetDirectory(version string, goOS GoOS, goArch GoArch) (string, error) {
|
2016-05-25 19:51:44 +00:00
|
|
|
var targetDir string
|
|
|
|
if len(*flagTargetDir) > 0 {
|
|
|
|
targetDir = *flagTargetDir
|
|
|
|
} else {
|
|
|
|
suffix := getSuffix(goOS, goArch)
|
2015-10-17 22:50:51 +00:00
|
|
|
|
2016-05-25 19:51:44 +00:00
|
|
|
targetDir = filepath.Join(binPath, "v2ray-"+version+suffix)
|
|
|
|
if version != "custom" {
|
|
|
|
os.RemoveAll(targetDir)
|
|
|
|
}
|
2015-10-17 19:32:48 +00:00
|
|
|
}
|
2016-05-25 19:51:44 +00:00
|
|
|
|
2015-10-17 19:32:48 +00:00
|
|
|
err := os.MkdirAll(targetDir, os.ModeDir|0777)
|
|
|
|
return targetDir, err
|
|
|
|
}
|
|
|
|
|
2015-10-17 22:50:51 +00:00
|
|
|
func getTargetFile(goOS GoOS) string {
|
|
|
|
suffix := ""
|
2015-10-18 09:51:21 +00:00
|
|
|
if goOS == Windows {
|
2015-10-17 19:32:48 +00:00
|
|
|
suffix += ".exe"
|
|
|
|
}
|
|
|
|
return "v2ray" + suffix
|
|
|
|
}
|
|
|
|
|
2015-11-04 20:20:06 +00:00
|
|
|
func getBinPath() string {
|
|
|
|
GOPATH := os.Getenv("GOPATH")
|
|
|
|
return filepath.Join(GOPATH, "bin")
|
|
|
|
}
|
|
|
|
|
2015-10-17 19:32:48 +00:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
2015-11-04 20:20:06 +00:00
|
|
|
binPath = getBinPath()
|
2015-12-27 22:27:39 +00:00
|
|
|
build(*flagTargetOS, *flagTargetArch, *flagArchive, "", *flagMetadataFile)
|
2015-11-04 20:20:06 +00:00
|
|
|
}
|
2015-10-17 19:32:48 +00:00
|
|
|
|
2015-12-27 22:27:39 +00:00
|
|
|
func build(targetOS, targetArch string, archive bool, version string, metadataFile string) {
|
2015-11-04 20:20:06 +00:00
|
|
|
v2rayOS := parseOS(targetOS)
|
|
|
|
v2rayArch := parseArch(targetArch)
|
2015-10-17 19:32:48 +00:00
|
|
|
|
2015-11-04 20:20:06 +00:00
|
|
|
if len(version) == 0 {
|
|
|
|
v, err := git.RepoVersionHead()
|
|
|
|
if v == git.VersionUndefined {
|
|
|
|
v = "custom"
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Unable to detect V2Ray version: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
2015-11-04 20:52:48 +00:00
|
|
|
version = v
|
2015-10-17 19:32:48 +00:00
|
|
|
}
|
|
|
|
fmt.Printf("Building V2Ray (%s) for %s %s\n", version, v2rayOS, v2rayArch)
|
|
|
|
|
|
|
|
targetDir, err := createTargetDirectory(version, v2rayOS, v2rayArch)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Unable to create directory " + targetDir + ": " + err.Error())
|
|
|
|
}
|
|
|
|
|
2015-10-17 22:50:51 +00:00
|
|
|
targetFile := getTargetFile(v2rayOS)
|
|
|
|
err = buildV2Ray(filepath.Join(targetDir, targetFile), version, v2rayOS, v2rayArch)
|
2015-10-17 19:32:48 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Unable to build V2Ray: " + err.Error())
|
|
|
|
}
|
|
|
|
|
2015-10-17 22:50:51 +00:00
|
|
|
err = copyConfigFiles(targetDir, v2rayOS)
|
2015-10-17 19:32:48 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Unable to copy config files: " + err.Error())
|
|
|
|
}
|
2015-10-18 09:42:00 +00:00
|
|
|
|
2015-11-04 20:20:06 +00:00
|
|
|
if archive {
|
2015-10-18 09:42:00 +00:00
|
|
|
err := os.Chdir(binPath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Unable to switch to directory (%s): %v\n", binPath, err)
|
|
|
|
}
|
|
|
|
suffix := getSuffix(v2rayOS, v2rayArch)
|
|
|
|
zipFile := "v2ray" + suffix + ".zip"
|
|
|
|
root := filepath.Base(targetDir)
|
|
|
|
err = zipFolder(root, zipFile)
|
|
|
|
if err != nil {
|
2015-12-27 22:27:39 +00:00
|
|
|
fmt.Printf("Unable to create archive (%s): %v\n", zipFile, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
metadataWriter, err := os.OpenFile(filepath.Join(binPath, metadataFile), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Unable to create metadata file (%s): %v\n", metadataFile, err)
|
|
|
|
}
|
|
|
|
defer metadataWriter.Close()
|
|
|
|
|
|
|
|
err = CalcMetadata(zipFile, metadataWriter)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to calculate metadata for file (%s): %v", zipFile, err)
|
2015-10-18 09:42:00 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-17 19:32:48 +00:00
|
|
|
}
|