Skip to content

Commit

Permalink
lang/go: support older OS versions using legacysupport PG.
Browse files Browse the repository at this point in the history
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. Since
base is fixed, this is conditional and not used for newer base versions.
  • Loading branch information
Ionic committed Mar 23, 2019
1 parent 906d0a3 commit abe46d0
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lang/go/Portfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

PortSystem 1.0

PortGroup legacysupport 1.0
legacysupport.newest_darwin_requires_legacy 13

name go
epoch 2
version 1.12.1
Expand Down Expand Up @@ -62,6 +65,42 @@ build.env GOROOT_BOOTSTRAP=${prefix}/lib/go-1.4 \
GOROOT_FINAL=${GOROOT_FINAL} \
CC=${configure.cc}

if {${os.platform} eq "darwin" && ${os.major} <= ${legacysupport.newest_darwin_requires_legacy}} {
# The legacy support PG will not actually change anything in this port directly,
# since go doesn't use the standard CFLAGS/CXXFLAGS.
# We need to patch the build system and set up env variables manually.

patchfiles-append dist-support-BOOT_GO_LDFLAGS.patch

if {[vercmp [macports_version] 2.5.99] >= 0} {
build.env-append "GO_EXTLINK_ENABLED=1" \
"GO_LDFLAGS=\"-extldflags=${configure.ldflags}\"" \
"BOOT_GO_LDFLAGS=-extldflags=${configure.ldflags}"
} else {
patchfiles-append workaround-base-env-issue-GO_EXT_LDFLAGS.patch
build.env-append GO_EXTLINK_ENABLED="1" \
GO_EXT_LDFLAGS="${configure.ldflags}" \
BOOT_GO_EXT_LDFLAGS="${configure.ldflags}"
}

notes-append [subst {
go had to be specially patched and built to work on your platform.

It likely won't work out of the box when building other projects,\
so make sure change your environment to use the following variables:
* GO_EXTLINK_ENABLED="1"
to always force go to use the external gcc or clang linker and
* GO_LDFLAGS="\\\"-extldflags=${configure.ldflags}\\\""
to force-link any binary against the legacy support library.\
Use exactly the quoting provided here, even if it may look odd,\
or compilation will fail.

Failure to do so will leave you unable to create binaries that use\
features not natively available on your system, either directly\
or through a go core dependency.
}]
}

use_parallel_build no

post-build {
Expand Down
32 changes: 32 additions & 0 deletions lang/go/files/dist-support-BOOT_GO_LDFLAGS.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--- src/cmd/dist/build.go.old 2019-03-14 20:43:37.000000000 +0100
+++ src/cmd/dist/build.go 2019-03-19 11:46:25.000000000 +0100
@@ -190,6 +190,7 @@ func xinit() {
}

gogcflags = os.Getenv("BOOT_GO_GCFLAGS")
+ goldflags = os.Getenv("BOOT_GO_LDFLAGS")

cc, cxx := "gcc", "g++"
if defaultclang {
@@ -646,7 +647,11 @@ func runInstall(dir string, ch chan stru
if elem == "go" {
elem = "go_bootstrap"
}
- link = []string{pathf("%s/link", tooldir), "-o", pathf("%s/%s%s", tooldir, elem, exe)}
+ link = []string{pathf("%s/link", tooldir)}
+ if goldflags != "" {
+ link = append(link, goldflags)
+ }
+ link = append (link, "-o", pathf("%s/%s%s", tooldir, elem, exe))
targ = len(link) - 1
}
ttarg := mtime(link[targ])
@@ -1237,7 +1242,7 @@ func cmdbootstrap() {
}

gogcflags = os.Getenv("GO_GCFLAGS") // we were using $BOOT_GO_GCFLAGS until now
- goldflags = os.Getenv("GO_LDFLAGS")
+ goldflags = os.Getenv("GO_LDFLAGS") // we were using $BOOT_GO_LDFLAGS until now
goBootstrap := pathf("%s/go_bootstrap", tooldir)
cmdGo := pathf("%s/go", gobin)
if debug {
71 changes: 71 additions & 0 deletions lang/go/files/workaround-base-env-issue-GO_EXT_LDFLAGS.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
--- src/cmd/dist/build.go.old 2019-03-19 14:24:21.000000000 +0100
+++ src/cmd/dist/build.go 2019-03-19 14:30:14.000000000 +0100
@@ -38,6 +38,7 @@ var (
goextlinkenabled string
gogcflags string // For running built compiler
goldflags string
+ goextldflags string // Flags passed to external linker, if enabled
workdir string
tooldir string
oldgoos string
@@ -191,6 +192,7 @@ func xinit() {

gogcflags = os.Getenv("BOOT_GO_GCFLAGS")
goldflags = os.Getenv("BOOT_GO_LDFLAGS")
+ goextldflags = os.Getenv("BOOT_GO_EXT_LDFLAGS")

cc, cxx := "gcc", "g++"
if defaultclang {
@@ -651,6 +653,9 @@ func runInstall(dir string, ch chan stru
if goldflags != "" {
link = append(link, goldflags)
}
+ if goextldflags != "" {
+ link = append(link, "-extldflags=" + goextldflags)
+ }
link = append (link, "-o", pathf("%s/%s%s", tooldir, elem, exe))
targ = len(link) - 1
}
@@ -1243,6 +1248,7 @@ func cmdbootstrap() {

gogcflags = os.Getenv("GO_GCFLAGS") // we were using $BOOT_GO_GCFLAGS until now
goldflags = os.Getenv("GO_LDFLAGS") // we were using $BOOT_GO_LDFLAGS until now
+ goextldflags = os.Getenv("GO_EXT_LDFLAGS") // we were using $BOOT_GO_EXT_LDFLAGS until now
goBootstrap := pathf("%s/go_bootstrap", tooldir)
cmdGo := pathf("%s/go", gobin)
if debug {
@@ -1378,7 +1384,12 @@ func cmdbootstrap() {
}

func goInstall(goBinary string, args ...string) {
- installCmd := []string{goBinary, "install", "-gcflags=all=" + gogcflags, "-ldflags=all=" + goldflags}
+ installCmd := []string{goBinary, "install", "-gcflags=all=" + gogcflags}
+ if goextldflags != "" {
+ installCmd = append(installCmd, "-ldflags=all=" + goldflags + " \"-extldflags=" + goextldflags + "\"")
+ } else {
+ installCmd = append(installCmd, "-ldflags=all=" + goldflags)
+ }
if vflag > 0 {
installCmd = append(installCmd, "-v")
}
@@ -1392,12 +1403,15 @@ func goInstall(goBinary string, args ...
}

func checkNotStale(goBinary string, targets ...string) {
+ checkCmd := []string{goBinary, "list", "-gcflags=all=" + gogcflags}
+ if goextldflags != "" {
+ checkCmd = append(checkCmd, "-ldflags=all=" + goldflags + " \"-extldflags=" + goextldflags + "\"")
+ } else {
+ checkCmd = append(checkCmd, "-ldflags=all=" + goldflags)
+ }
+ checkCmd = append(checkCmd, "-f={{if .Stale}}\tSTALE {{.ImportPath}}: {{.StaleReason}}{{end}}")
out := run(goroot, CheckExit,
- append([]string{
- goBinary,
- "list", "-gcflags=all=" + gogcflags, "-ldflags=all=" + goldflags,
- "-f={{if .Stale}}\tSTALE {{.ImportPath}}: {{.StaleReason}}{{end}}",
- }, targets...)...)
+ append(checkCmd, targets...)...)
if strings.Contains(out, "\tSTALE ") {
os.Setenv("GODEBUG", "gocachehash=1")
for _, target := range []string{"runtime/internal/sys", "cmd/dist", "cmd/link"} {

0 comments on commit abe46d0

Please sign in to comment.