Skip to content

Commit

Permalink
asm: Pass package path with -p in Go 1.19+ (#3231)
Browse files Browse the repository at this point in the history
This is required to produce linkable objects as of Go 1.19.
  • Loading branch information
fmeum authored Jul 13, 2022
1 parent df02d01 commit cde7d7b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
30 changes: 28 additions & 2 deletions go/tools/builders/asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -60,7 +61,10 @@ func asm(args []string) error {
}

// Build source with the assembler.
return asmFile(goenv, source, asmFlags, outPath)
// Note: As of Go 1.19, this would require a valid package path to work.
// But since this functionality is only used by the deprecated action API,
// we won't fix this.
return asmFile(goenv, source, "", asmFlags, outPath)
}

// buildSymabisFile generates a file from assembly files that is consumed
Expand Down Expand Up @@ -137,12 +141,34 @@ func buildSymabisFile(goenv *env, sFiles, hFiles []fileInfo, asmhdr string) (str
return symabisName, err
}

func asmFile(goenv *env, srcPath string, asmFlags []string, outPath string) error {
func asmFile(goenv *env, srcPath, packagePath string, asmFlags []string, outPath string) error {
args := goenv.goTool("asm")
args = append(args, asmFlags...)
// The package path has to be specified as of Go 1.19 or the resulting
// object will be unlinkable, but the -p flag is also only available
// since Go 1.19.
if packagePath != "" && isGo119OrHigher() {
args = append(args, "-p", packagePath)
}
args = append(args, "-trimpath", ".")
args = append(args, "-o", outPath)
args = append(args, "--", srcPath)
absArgs(args, []string{"-I", "-o", "-trimpath"})
return goenv.runCommand(args)
}

var goMinorVersionRegexp = regexp.MustCompile(`^go1\.(\d+)`)

func isGo119OrHigher() bool {
match := goMinorVersionRegexp.FindStringSubmatch(runtime.Version())
if match == nil {
// Developer version or something with an unparseable version string,
// assume Go 1.19 or higher.
return true
}
minorVersion, err := strconv.Atoi(match[1])
if err != nil {
return true
}
return minorVersion >= 19
}
2 changes: 1 addition & 1 deletion go/tools/builders/compilepkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ func compileArchive(
}
for i, sSrc := range srcs.sSrcs {
obj := filepath.Join(workDir, fmt.Sprintf("s%d.o", i))
if err := asmFile(goenv, sSrc.filename, asmFlags, obj); err != nil {
if err := asmFile(goenv, sSrc.filename, packagePath, asmFlags, obj); err != nil {
return err
}
objFiles = append(objFiles, obj)
Expand Down

0 comments on commit cde7d7b

Please sign in to comment.