使用方式:

    1. $ gf build -h
    2. USAGE
    3. gf build FILE [OPTION]
    4. ARGUMENT
    5. FILE building file path.
    6. OPTION
    7. -n, --name output binary name
    8. -v, --version output binary version
    9. -a, --arch output binary architecture, multiple arch separated with ','
    10. -s, --system output binary system, multiple os separated with ','
    11. -o, --output output binary path, used when building single binary file
    12. -p, --path output binary directory path, default is './bin'
    13. -e, --extra extra custom "go build" options
    14. -m, --mod like "-mod" option of "go build", use "-m none" to disable go module
    15. -c, --cgo enable or disable cgo feature, it's disabled in default
    16. --pack pack specified folder into packed/data.go before building.
    17. --swagger auto parse and pack swagger into packed/swagger.go before building.
    18. EXAMPLES
    19. gf build main.go
    20. gf build main.go --swagger
    21. gf build main.go --pack public,template
    22. gf build main.go --cgo
    23. gf build main.go -m none
    24. gf build main.go -n my-app -a all -s all
    25. gf build main.go -n my-app -a amd64,386 -s linux -p .
    26. gf build main.go -n my-app -v 1.0 -a amd64,386 -s linux,windows,darwin -p ./docker/bin
    27. DESCRIPTION
    28. The "build" command is most commonly used command, which is designed as a powerful wrapper for
    29. "go build" command for convenience cross-compiling usage.
    30. It provides much more features for building binary:
    31. 1. Cross-Compiling for many platforms and architectures.
    32. 2. Configuration file support for compiling.
    33. 3. Build-In Variables.
    34. PLATFORMS
    35. darwin amd64
    36. freebsd 386,amd64,arm
    37. linux 386,amd64,arm,arm64,ppc64,ppc64le,mips,mipsle,mips64,mips64le
    38. netbsd 386,amd64,arm
    39. openbsd 386,amd64,arm
    40. windows 386,amd64

    仅限于交叉编译使用到GF框架的项目,支持绝大部分常见系统的直接交叉编译。并且支持配置文件管理编译选项、嵌入编译时变量。使用gf build的项目将会默认嵌入以下变量(参考gf -v):

    • 当前Go版本。
    • 当前GF版本。
    • 当前编译时间。
    • 当前Git Commit(如果存在)。

    编译配置文件选项示例:

    编译的配置默认读取的是默认配置文件config.toml,我们也可以通过命令行参数-gf.gcfg.file=build.toml来修改默认读取的配置文件,关于如何修改配置管理组件默认的配置目录及文件请参考章节:配置管理

    1. [gfcli]
    2. [gfcli.build]
    3. name = "gf"
    4. arch = "all"
    5. system = "all"
    6. mod = "none"
    7. cgo = 0
    8. pack = ""
    9. version = "v1.0.0"
    10. output = "./bin"
    11. extra = ""

    配置选项的释义同命令行同名选项。

    名称必须默认值含义示例
    name与程序入口go文件同名生成的可执行文件名称。如果是windows平台,那么默认会加上.exe后缀gf
    arch当前系统架构编译架构,多个以,号分隔,如果是all表示编译所有支持架构386,amd64,arm
    system当前系统平台编译平台,多个以,号分隔,如果是all表示编译所有支持平台linux,darwin,windows
    path./bin编译可执行文件存储的目录地址./bin
    mod
    go build -mod编译选项,不常用none
    cgo0是否开启CGO,默认是关闭的。如果开启,那么交叉编译可能会有问题。0
    pack
    需要打包的目录,多个以,号分隔,生成到packed/data.gopublic,template
    version
    程序版本,如果指定版本信息,那么程序生成的路径中会多一层以版本名称的目录v1.0.0
    output
    输出的可执行文件路径,当该参数指定时,namepath参数失效,常用于编译单个可执行文件./bin/gf.exe
    extra
    额外自定义的编译参数,会直接传递给go build命令
    varmap
    自定义的内置变量键值对
    1. [gfcli]
    2. [gfcli.build]
    3. name = gf
    4. arch = all
    5. system = all
    6. mod = none
    7. cgo = 0
    8. [gfcli.build.varmap]
    9. k1 = v1
    10. k2 = v2

    编译时的内置变量可以在运行时通过gbuildgbuild (构建信息获取) 获取。

    使用示例:

    1. $ gf build
    2. 2020-12-31 00:35:25.562 start building...
    3. 2020-12-31 00:35:25.562 go build -o ./bin/darwin_amd64/gf main.go
    4. 2020-12-31 00:35:28.381 go build -o ./bin/freebsd_386/gf main.go
    5. 2020-12-31 00:35:30.650 go build -o ./bin/freebsd_amd64/gf main.go
    6. 2020-12-31 00:35:32.957 go build -o ./bin/freebsd_arm/gf main.go
    7. 2020-12-31 00:35:35.824 go build -o ./bin/linux_386/gf main.go
    8. 2020-12-31 00:35:38.082 go build -o ./bin/linux_amd64/gf main.go
    9. 2020-12-31 00:35:41.076 go build -o ./bin/linux_arm/gf main.go
    10. 2020-12-31 00:35:44.369 go build -o ./bin/linux_arm64/gf main.go
    11. 2020-12-31 00:35:47.352 go build -o ./bin/linux_ppc64/gf main.go
    12. 2020-12-31 00:35:50.293 go build -o ./bin/linux_ppc64le/gf main.go
    13. 2020-12-31 00:35:53.166 go build -o ./bin/linux_mips/gf main.go
    14. 2020-12-31 00:35:55.840 go build -o ./bin/linux_mipsle/gf main.go
    15. 2020-12-31 00:35:58.423 go build -o ./bin/linux_mips64/gf main.go
    16. 2020-12-31 00:36:01.062 go build -o ./bin/linux_mips64le/gf main.go
    17. 2020-12-31 00:36:03.502 go build -o ./bin/netbsd_386/gf main.go
    18. 2020-12-31 00:36:06.280 go build -o ./bin/netbsd_amd64/gf main.go
    19. 2020-12-31 00:36:09.332 go build -o ./bin/netbsd_arm/gf main.go
    20. 2020-12-31 00:36:11.811 go build -o ./bin/openbsd_386/gf main.go
    21. 2020-12-31 00:36:14.140 go build -o ./bin/openbsd_amd64/gf main.go
    22. 2020-12-31 00:36:17.859 go build -o ./bin/openbsd_arm/gf main.go
    23. 2020-12-31 00:36:20.327 go build -o ./bin/windows_386/gf.exe main.go
    24. 2020-12-31 00:36:22.994 go build -o ./bin/windows_amd64/gf.exe main.go
    25. 2020-12-31 00:36:25.795 done!