@@ -200,9 +200,10 @@ func main() {
200200
201201func doInstall (cmdline []string ) {
202202 var (
203- dlgo = flag .Bool ("dlgo" , false , "Download Go and build with it" )
204- arch = flag .String ("arch" , "" , "Architecture to cross build for" )
205- cc = flag .String ("cc" , "" , "C compiler to cross build with" )
203+ dlgo = flag .Bool ("dlgo" , false , "Download Go and build with it" )
204+ arch = flag .String ("arch" , "" , "Architecture to cross build for" )
205+ cc = flag .String ("cc" , "" , "C compiler to cross build with" )
206+ staticlink = flag .Bool ("static" , false , "Create statically-linked executable" )
206207 )
207208 flag .CommandLine .Parse (cmdline )
208209
@@ -213,9 +214,12 @@ func doInstall(cmdline []string) {
213214 tc .Root = build .DownloadGo (csdb , dlgoVersion )
214215 }
215216
217+ // Disable CLI markdown doc generation in release builds.
218+ buildTags := []string {"urfave_cli_no_docs" }
219+
216220 // Configure the build.
217221 env := build .Env ()
218- gobuild := tc .Go ("build" , buildFlags (env )... )
222+ gobuild := tc .Go ("build" , buildFlags (env , * staticlink , buildTags )... )
219223
220224 // arm64 CI builders are memory-constrained and can't handle concurrent builds,
221225 // better disable it. This check isn't the best, it should probably
@@ -224,9 +228,6 @@ func doInstall(cmdline []string) {
224228 gobuild .Args = append (gobuild .Args , "-p" , "1" )
225229 }
226230
227- // Disable CLI markdown doc generation in release builds.
228- gobuild .Args = append (gobuild .Args , "-tags" , "urfave_cli_no_docs" )
229-
230231 // We use -trimpath to avoid leaking local paths into the built executables.
231232 gobuild .Args = append (gobuild .Args , "-trimpath" )
232233
@@ -251,7 +252,7 @@ func doInstall(cmdline []string) {
251252}
252253
253254// buildFlags returns the go tool flags for building.
254- func buildFlags (env build.Environment ) (flags []string ) {
255+ func buildFlags (env build.Environment , staticLinking bool , buildTags [] string ) (flags []string ) {
255256 var ld []string
256257 if env .Commit != "" {
257258 ld = append (ld , "-X" , "main.gitCommit=" + env .Commit )
@@ -262,14 +263,24 @@ func buildFlags(env build.Environment) (flags []string) {
262263 if runtime .GOOS == "darwin" {
263264 ld = append (ld , "-s" )
264265 }
265- // Enforce the stacksize to 8M, which is the case on most platforms apart from
266- // alpine Linux.
267266 if runtime .GOOS == "linux" {
268- ld = append (ld , "-extldflags" , "-Wl,-z,stack-size=0x800000" )
267+ // Enforce the stacksize to 8M, which is the case on most platforms apart from
268+ // alpine Linux.
269+ extld := []string {"-Wl,-z,stack-size=0x800000" }
270+ if staticLinking {
271+ extld = append (extld , "-static" )
272+ // Under static linking, use of certain glibc features must be
273+ // disabled to avoid shared library dependencies.
274+ buildTags = append (buildTags , "osusergo" , "netgo" )
275+ }
276+ ld = append (ld , "-extldflags" , "'" + strings .Join (extld , " " )+ "'" )
269277 }
270278 if len (ld ) > 0 {
271279 flags = append (flags , "-ldflags" , strings .Join (ld , " " ))
272280 }
281+ if len (buildTags ) > 0 {
282+ flags = append (flags , "-tags" , strings .Join (buildTags , "," ))
283+ }
273284 return flags
274285}
275286
0 commit comments