Ticket #58138: go.patch

File go.patch, 7.8 KB (added by Ionic (Mihai Moldovan), 5 years ago)

Suggested build fix for older platforms. Maintainer, please review. I'll push it myself, probably within the next few days.

  • lang/go/Portfile

    commit e56b78e4af75caf3f77b6093384f494247b2077f
    Author: Mihai Moldovan <ionic@ionic.de>
    Date:   Thu Mar 21 15:52:42 2019 +0100
    
        lang/go: support older OS versions using legacysupport PG.
        
        Fixes: https://trac.macports.org/ticket/58138
        
        This adds a simple patch and changes the go build procedure to inject
        the legacy-support library into all compiled binaries, getting support
        for 10.9 and probably older OS versions.
        
        Users will have to do the same when compiling go programs, the new note
        details this process.
        
        A separate patch is needed to workaround a base bug which won't let
        ports define environment variables with a quote character in them. Once
        base is fixed, this will be made conditional and not used for newer base
        versions.
    
    diff --git a/lang/go/Portfile b/lang/go/Portfile
    index b4326c0cfd..4e28ed90fa 100644
    a b  
    22
    33PortSystem          1.0
    44
     5PortGroup           legacysupport 1.0
     6legacysupport.newest_darwin_requires_legacy 13
     7
    58name                go
    69epoch               2
    710version             1.12.1
    build.env GOROOT_BOOTSTRAP=${prefix}/lib/go-1.4 \ 
    6265                    GOROOT_FINAL=${GOROOT_FINAL} \
    6366                    CC=${configure.cc}
    6467
     68if {${os.platform} eq "darwin" && ${os.major} <= ${legacysupport.newest_darwin_requires_legacy}} {
     69    # The legacy support PG will not actually change anything in this port directly,
     70    # since go doesn't use the standard CFLAGS/CXXFLAGS.
     71    # We need to patch the build system and set up env variables manually.
     72
     73    patchfiles-append   dist-support-BOOT_GO_LDFLAGS.patch
     74
     75    # I'd like to use the following notation, but base has a bug and cannot include
     76    # quote characters in environment variable values.
     77    # Using a special patch and another set of environment variables to work around
     78    # that issue for now.
     79    # build.env-append  "GO_EXTLINK_ENABLED=1" \
     80    #                   "GO_LDFLAGS=\"-extldflags=${configure.ldflags}\"" \
     81    #                   "BOOT_GO_LDFLAGS=\"-extldflags=${configure.ldflags}\""
     82
     83    patchfiles-append   workaround-base-env-issue-GO_EXT_LDFLAGS.patch
     84    build.env-append    GO_EXTLINK_ENABLED="1" \
     85                        GO_EXT_LDFLAGS="${configure.ldflags}" \
     86                        BOOT_GO_EXT_LDFLAGS="${configure.ldflags}"
     87
     88    notes-append [subst {
     89                    go had to be specially patched and built to work on your platform.
     90
     91                    It likely won't work out of the box when building other projects,\
     92                    so make sure change your environment to use the following variables:
     93                      * GO_EXTLINK_ENABLED="1"
     94                    to always force go to use the external gcc or clang linker and
     95                      * GO_LDFLAGS="\\\"-extldflags=${configure.ldflags}\\\""
     96                    to force-link any binary against the legacy support library.\
     97                    Use exactly the quoting provided here, even if it may look odd,\
     98                    or compilation will fail.
     99
     100                    Failure to do so will leave you unable to create binaries that use\
     101                    features not natively available on your system, either directly\
     102                    or through a go core dependency.
     103    }]
     104}
     105
    65106use_parallel_build  no
    66107
    67108post-build {
  • new file lang/go/files/dist-support-BOOT_GO_LDFLAGS.patch

    diff --git a/lang/go/files/dist-support-BOOT_GO_LDFLAGS.patch b/lang/go/files/dist-support-BOOT_GO_LDFLAGS.patch
    new file mode 100644
    index 0000000000..f9032bb0ca
    - +  
     1--- src/cmd/dist/build.go.old   2019-03-14 20:43:37.000000000 +0100
     2+++ src/cmd/dist/build.go       2019-03-19 11:46:25.000000000 +0100
     3@@ -190,6 +190,7 @@ func xinit() {
     4        }
     5 
     6        gogcflags = os.Getenv("BOOT_GO_GCFLAGS")
     7+       goldflags = os.Getenv("BOOT_GO_LDFLAGS")
     8 
     9        cc, cxx := "gcc", "g++"
     10        if defaultclang {
     11@@ -646,7 +647,11 @@ func runInstall(dir string, ch chan stru
     12                if elem == "go" {
     13                        elem = "go_bootstrap"
     14                }
     15-               link = []string{pathf("%s/link", tooldir), "-o", pathf("%s/%s%s", tooldir, elem, exe)}
     16+               link = []string{pathf("%s/link", tooldir)}
     17+               if goldflags != "" {
     18+                       link = append(link, goldflags)
     19+               }
     20+               link = append (link, "-o", pathf("%s/%s%s", tooldir, elem, exe))
     21                targ = len(link) - 1
     22        }
     23        ttarg := mtime(link[targ])
     24@@ -1237,7 +1242,7 @@ func cmdbootstrap() {
     25        }
     26 
     27        gogcflags = os.Getenv("GO_GCFLAGS") // we were using $BOOT_GO_GCFLAGS until now
     28-       goldflags = os.Getenv("GO_LDFLAGS")
     29+       goldflags = os.Getenv("GO_LDFLAGS") // we were using $BOOT_GO_LDFLAGS until now
     30        goBootstrap := pathf("%s/go_bootstrap", tooldir)
     31        cmdGo := pathf("%s/go", gobin)
     32        if debug {
  • new file lang/go/files/workaround-base-env-issue-GO_EXT_LDFLAGS.patch

    diff --git a/lang/go/files/workaround-base-env-issue-GO_EXT_LDFLAGS.patch b/lang/go/files/workaround-base-env-issue-GO_EXT_LDFLAGS.patch
    new file mode 100644
    index 0000000000..ce1c64fca2
    - +  
     1--- src/cmd/dist/build.go.old   2019-03-19 14:24:21.000000000 +0100
     2+++ src/cmd/dist/build.go       2019-03-19 14:30:14.000000000 +0100
     3@@ -38,6 +38,7 @@ var (
     4        goextlinkenabled string
     5        gogcflags        string // For running built compiler
     6        goldflags        string
     7+       goextldflags     string // Flags passed to external linker, if enabled
     8        workdir          string
     9        tooldir          string
     10        oldgoos          string
     11@@ -191,6 +192,7 @@ func xinit() {
     12 
     13        gogcflags = os.Getenv("BOOT_GO_GCFLAGS")
     14        goldflags = os.Getenv("BOOT_GO_LDFLAGS")
     15+       goextldflags = os.Getenv("BOOT_GO_EXT_LDFLAGS")
     16 
     17        cc, cxx := "gcc", "g++"
     18        if defaultclang {
     19@@ -651,6 +653,9 @@ func runInstall(dir string, ch chan stru
     20                if goldflags != "" {
     21                        link = append(link, goldflags)
     22                }
     23+               if goextldflags != "" {
     24+                       link = append(link, "-extldflags=" + goextldflags)
     25+               }
     26                link = append (link, "-o", pathf("%s/%s%s", tooldir, elem, exe))
     27                targ = len(link) - 1
     28        }
     29@@ -1243,6 +1248,7 @@ func cmdbootstrap() {
     30 
     31        gogcflags = os.Getenv("GO_GCFLAGS") // we were using $BOOT_GO_GCFLAGS until now
     32        goldflags = os.Getenv("GO_LDFLAGS") // we were using $BOOT_GO_LDFLAGS until now
     33+       goextldflags = os.Getenv("GO_EXT_LDFLAGS") // we were using $BOOT_GO_EXT_LDFLAGS until now
     34        goBootstrap := pathf("%s/go_bootstrap", tooldir)
     35        cmdGo := pathf("%s/go", gobin)
     36        if debug {
     37@@ -1378,7 +1384,12 @@ func cmdbootstrap() {
     38 }
     39 
     40 func goInstall(goBinary string, args ...string) {
     41-       installCmd := []string{goBinary, "install", "-gcflags=all=" + gogcflags, "-ldflags=all=" + goldflags}
     42+       installCmd := []string{goBinary, "install", "-gcflags=all=" + gogcflags}
     43+       if goextldflags != "" {
     44+               installCmd = append(installCmd, "-ldflags=all=" + goldflags + " \"-extldflags=" + goextldflags + "\"")
     45+       } else {
     46+               installCmd = append(installCmd, "-ldflags=all=" + goldflags)
     47+       }
     48        if vflag > 0 {
     49                installCmd = append(installCmd, "-v")
     50        }
     51@@ -1392,12 +1403,15 @@ func goInstall(goBinary string, args ...
     52 }
     53 
     54 func checkNotStale(goBinary string, targets ...string) {
     55+       checkCmd := []string{goBinary, "list", "-gcflags=all=" + gogcflags}
     56+       if goextldflags != "" {
     57+               checkCmd = append(checkCmd, "-ldflags=all=" + goldflags + " \"-extldflags=" + goextldflags + "\"")
     58+       } else {
     59+               checkCmd = append(checkCmd, "-ldflags=all=" + goldflags)
     60+       }
     61+       checkCmd = append(checkCmd, "-f={{if .Stale}}\tSTALE {{.ImportPath}}: {{.StaleReason}}{{end}}")
     62        out := run(goroot, CheckExit,
     63-               append([]string{
     64-                       goBinary,
     65-                       "list", "-gcflags=all=" + gogcflags, "-ldflags=all=" + goldflags,
     66-                       "-f={{if .Stale}}\tSTALE {{.ImportPath}}: {{.StaleReason}}{{end}}",
     67-               }, targets...)...)
     68+               append(checkCmd, targets...)...)
     69        if strings.Contains(out, "\tSTALE ") {
     70                os.Setenv("GODEBUG", "gocachehash=1")
     71                for _, target := range []string{"runtime/internal/sys", "cmd/dist", "cmd/link"} {