Skip to content

Commit

Permalink
Add defines for assembly build (#3528)
Browse files Browse the repository at this point in the history
* Add defines for assembly build

Fixes #1894 by adding GOOS_
GOARCH_ and GOOS_GOARCH_ defines for building assembly.

* Return a copy of args

* Use build.Default variables for os and arch

Add a compilation test that uses another architecture with conditional
inclusion of an assembly function. This mimics a real library that fails
to build under rules_go.

---------

Co-authored-by: Patrick Scott <patrick.scott@observeinc.com>
  • Loading branch information
2 people authored and linzhp committed Apr 20, 2023
1 parent 797bd41 commit 6dfed3d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
11 changes: 9 additions & 2 deletions go/tools/builders/asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package main

import (
"go/build"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -24,6 +25,12 @@ import (
"strings"
)

var ASM_DEFINES = []string{
"-D", "GOOS_" + build.Default.GOOS,
"-D", "GOARCH_" + build.Default.GOARCH,
"-D", "GOOS_GOARCH_" + build.Default.GOOS + "_" + build.Default.GOARCH,
}

// buildSymabisFile generates a file from assembly files that is consumed
// by the compiler. This is only needed in go1.12+ when there is at least one
// .s file. If the symabis file is not needed, no file will be generated,
Expand Down Expand Up @@ -87,8 +94,7 @@ func buildSymabisFile(goenv *env, sFiles, hFiles []fileInfo, asmhdr string) (str
seenHdrDirs[hdrDir] = true
}
}
// TODO(#1894): define GOOS_goos, GOARCH_goarch, both here and in the
// GoAsm action.
asmargs = append(asmargs, ASM_DEFINES...)
asmargs = append(asmargs, "-gensymabis", "-o", symabisName, "--")
for _, sFile := range sFiles {
asmargs = append(asmargs, sFile.filename)
Expand All @@ -107,6 +113,7 @@ func asmFile(goenv *env, srcPath, packagePath string, asmFlags []string, outPath
if packagePath != "" && isGo119OrHigher() {
args = append(args, "-p", packagePath)
}
args = append(args, ASM_DEFINES...)
args = append(args, "-trimpath", ".")
args = append(args, "-o", outPath)
args = append(args, "--", srcPath)
Expand Down
8 changes: 8 additions & 0 deletions tests/core/cross/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ go_binary(
deps = [":platform_lib"],
)

go_binary(
name = "asm_cross",
srcs = ["asm.s", "main.go"],
goarch = "386",
goos = "linux",
deps = [":platform_lib"],
)

go_binary(
name = "native_bin",
srcs = ["main.go"],
Expand Down
15 changes: 15 additions & 0 deletions tests/core/cross/asm.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Example assembly copied from https://github.com/rpccloud/goid

#include "go_asm.h"
#include "textflag.h"

#ifdef GOARCH_386
#define get_tls(r) MOVL TLS, r
#define g(r) 0(r)(TLS*1)
#endif

TEXT ·getg(SB), NOSPLIT, $0-4
get_tls(CX)
MOVL g(CX), AX
MOVL AX, ret+0(FP)
RET

0 comments on commit 6dfed3d

Please sign in to comment.