Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev,generate-cgo: externalize cgo files gen into a seperate binary #103140

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@
/pkg/cmd/generate-spatial-ref-sys/ @cockroachdb/spatial
/pkg/cmd/generate-bazel-extra/ @cockroachdb/dev-inf
/pkg/cmd/generate-staticcheck/ @cockroachdb/dev-inf
/pkg/cmd/generate-cgo/ @cockroachdb/dev-inf
/pkg/cmd/geoviz/ @cockroachdb/spatial
/pkg/cmd/github-post/ @cockroachdb/test-eng
/pkg/cmd/github-pull-request-make/ @cockroachdb/dev-inf
Expand Down
6 changes: 3 additions & 3 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5059,10 +5059,10 @@ def go_deps():
name = "com_github_jordanlewis_gcassert",
build_file_proto_mode = "disable_global",
importpath = "github.com/jordanlewis/gcassert",
sha256 = "4e6d2be78096ee4158d921af5d388f76bbf6d2638cee052ca628ba70da911704",
strip_prefix = "github.com/jordanlewis/[email protected]20221027203946-81f097ad35a0",
sha256 = "3919384e0288d9ce93da816ef227aec2c61b978eb761bd45a7fded957f792dcf",
strip_prefix = "github.com/jordanlewis/[email protected]20230505190637-fed79d91cd5f",
urls = [
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/jordanlewis/gcassert/com_github_jordanlewis_gcassert-v0.0.0-20221027203946-81f097ad35a0.zip",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/jordanlewis/gcassert/com_github_jordanlewis_gcassert-v0.0.0-20230505190637-fed79d91cd5f.zip",
],
)
go_repository(
Expand Down
2 changes: 1 addition & 1 deletion build/bazelutil/distdir_files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ DISTDIR_FILES = {
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/joho/godotenv/com_github_joho_godotenv-v1.3.0.zip": "acef5a394fbd1193f52d0d19690b0bfe82728d18dd3bf67730dc5031c22d563f",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/jonboulle/clockwork/com_github_jonboulle_clockwork-v0.1.0.zip": "930d355d1ced60a668bcbca6154bb5671120ba11a34119505d1c0677f7bbbf97",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/jordan-wright/email/com_github_jordan_wright_email-v4.0.1-0.20210109023952-943e75fe5223+incompatible.zip": "6d35fa83ea02cfacd0e1ba9c9061381b963215cef84c8bf83ad5944cb304c390",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/jordanlewis/gcassert/com_github_jordanlewis_gcassert-v0.0.0-20221027203946-81f097ad35a0.zip": "4e6d2be78096ee4158d921af5d388f76bbf6d2638cee052ca628ba70da911704",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/jordanlewis/gcassert/com_github_jordanlewis_gcassert-v0.0.0-20230505190637-fed79d91cd5f.zip": "3919384e0288d9ce93da816ef227aec2c61b978eb761bd45a7fded957f792dcf",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/josharian/intern/com_github_josharian_intern-v1.0.0.zip": "5679bfd11c14adccdb45bd1a0f9cf4b445b95caeed6fb507ba96ecced11c248d",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/jpillora/backoff/com_github_jpillora_backoff-v1.0.0.zip": "f856692c725143c49b9cceabfbca8bc93d3dbde84a0aaa53fb26ed3774c220cc",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/json-iterator/go/com_github_json_iterator_go-v1.1.12.zip": "d001ea57081afd0e378467c8f4a9b6a51259996bb8bb763f78107eaf12f99501",
Expand Down
5 changes: 5 additions & 0 deletions build/teamcity/cockroach/ci/tests/gcassert_impl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ set -xeuo pipefail

bazel build @com_github_jordanlewis_gcassert//cmd/gcassert:gcassert --config=ci
bazel run //pkg/gen:code
WORKSPACE=$(bazel info workspace --color=no)
BAZEL_BIN=$(bazel info bazel-bin --color=no)
bazel run //pkg/gen:code
bazel run //pkg/cmd/generate-cgo:generate-cgo -- --workspace $WORKSPACE --bazel-bin $BAZEL_BIN
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Can just use $PWD or pwd instead of bazel info workspace.

GODIR=$(dirname $(bazel run @go_sdk//:bin/go --run_under=realpath))
echo "##teamcity[testStarted name='GcAssert' captureStandardOutput='true']"
exit_status=0
Expand All @@ -12,4 +16,5 @@ if [ "$exit_status" -ne 0 ]; then
echo "##teamcity[testFailed name='GcAssert']"
fi
echo "##teamcity[testFinished name='GcAssert']"
cp /tmp/gcassert* $PWD/artifacts

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ require (
github.com/jackc/pgx/v5 v5.3.1
github.com/jaegertracing/jaeger v1.18.1
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible
github.com/jordanlewis/gcassert v0.0.0-20221027203946-81f097ad35a0
github.com/jordanlewis/gcassert v0.0.0-20230505190637-fed79d91cd5f
github.com/kevinburke/go-bindata v3.13.0+incompatible
github.com/kisielk/errcheck v1.6.1-0.20210625163953-8ddee489636a
github.com/kisielk/gotool v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1438,8 +1438,8 @@ github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqx
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible h1:jdpOPRN1zP63Td1hDQbZW73xKmzDvZHzVdNYxhnTMDA=
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible/go.mod h1:1c7szIrayyPPB/987hsnvNzLushdWf4o/79s3P08L8A=
github.com/jordanlewis/gcassert v0.0.0-20221027203946-81f097ad35a0 h1:y9VRjxb2hZlTElaaUH50QkZnPKrvQR5NhPza66vh+9w=
github.com/jordanlewis/gcassert v0.0.0-20221027203946-81f097ad35a0/go.mod h1:FsDIOYX6N9TFyHD602Yu/Zp04+KySTH00okDq+9IPqo=
github.com/jordanlewis/gcassert v0.0.0-20230505190637-fed79d91cd5f h1:GoV80Ggs5ZeF6uDv/lYDGJAv2Wz3Mp/brqRbjvEm6mI=
github.com/jordanlewis/gcassert v0.0.0-20230505190637-fed79d91cd5f/go.mod h1:FsDIOYX6N9TFyHD602Yu/Zp04+KySTH00okDq+9IPqo=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
Expand Down
3 changes: 3 additions & 0 deletions pkg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,8 @@ GO_TARGETS = [
"//pkg/cmd/generate-bazel-extra:generate-bazel-extra_test",
"//pkg/cmd/generate-binary:generate-binary",
"//pkg/cmd/generate-binary:generate-binary_lib",
"//pkg/cmd/generate-cgo:generate-cgo",
"//pkg/cmd/generate-cgo:generate-cgo_lib",
"//pkg/cmd/generate-distdir:generate-distdir",
"//pkg/cmd/generate-distdir:generate-distdir_lib",
"//pkg/cmd/generate-logictest:generate-logictest",
Expand Down Expand Up @@ -2597,6 +2599,7 @@ GET_X_DATA_TARGETS = [
"//pkg/cmd/generate-acceptance-tests:get_x_data",
"//pkg/cmd/generate-bazel-extra:get_x_data",
"//pkg/cmd/generate-binary:get_x_data",
"//pkg/cmd/generate-cgo:get_x_data",
"//pkg/cmd/generate-distdir:get_x_data",
"//pkg/cmd/generate-logictest:get_x_data",
"//pkg/cmd/generate-metadata-tables:get_x_data",
Expand Down
81 changes: 9 additions & 72 deletions pkg/cmd/dev/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"text/template"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -272,14 +270,6 @@ func (d *dev) generateTarget(ctx context.Context, target string) error {

func (d *dev) generateCgo(cmd *cobra.Command) error {
ctx := cmd.Context()
args := []string{"build", "//build/bazelutil:test_force_build_cdeps", "//c-deps:libjemalloc", "//c-deps:libproj"}
if runtime.GOOS == "linux" {
args = append(args, "//c-deps:libkrb5")
}
logCommand("bazel", args...)
if err := d.exec.CommandContextInheritingStdStreams(ctx, "bazel", args...); err != nil {
return err
}
workspace, err := d.getWorkspace(ctx)
if err != nil {
return err
Expand All @@ -288,70 +278,17 @@ func (d *dev) generateCgo(cmd *cobra.Command) error {
if err != nil {
return err
}

const cgoTmpl = `// GENERATED FILE DO NOT EDIT

package {{ .Package }}

// #cgo CPPFLAGS: {{ .CPPFlags }}
// #cgo LDFLAGS: {{ .LDFlags }}
import "C"
`

tpl := template.Must(template.New("source").Parse(cgoTmpl))
archived, err := d.getArchivedCdepString(bazelBin)
if err != nil {
return err
}
// Figure out where to find the c-deps libraries.
var jemallocDir, projDir, krbDir string
if archived != "" {
execRoot, err := d.getExecutionRoot(ctx)
if err != nil {
return err
}
jemallocDir = filepath.Join(execRoot, "external", fmt.Sprintf("archived_cdep_libjemalloc_%s", archived))
projDir = filepath.Join(execRoot, "external", fmt.Sprintf("archived_cdep_libproj_%s", archived))
if runtime.GOOS == "linux" {
krbDir = filepath.Join(execRoot, "external", fmt.Sprintf("archived_cdep_libkrb5_%s", archived))
}
} else {
jemallocDir = filepath.Join(bazelBin, "c-deps/libjemalloc_foreign")
projDir = filepath.Join(bazelBin, "c-deps/libproj_foreign")
if runtime.GOOS == "linux" {
krbDir = filepath.Join(bazelBin, "c-deps/libkrb5_foreign")
}
}
cppFlags := fmt.Sprintf("-I%s", filepath.Join(jemallocDir, "include"))
ldFlags := fmt.Sprintf("-L%s -L%s", filepath.Join(jemallocDir, "lib"), filepath.Join(projDir, "lib"))
if krbDir != "" {
cppFlags += fmt.Sprintf(" -I%s", filepath.Join(krbDir, "include"))
ldFlags += fmt.Sprintf(" -L%s", filepath.Join(krbDir, "lib"))
}

cgoPkgs := []string{
"pkg/cli",
"pkg/cli/clisqlshell",
"pkg/server/status",
"pkg/ccl/gssapiccl",
"pkg/geo/geoproj",
args := []string{
"run",
"//pkg/cmd/generate-cgo:generate-cgo",
"--",
"--workspace", workspace,
"--bazel-bin", bazelBin,
}

for _, cgoPkg := range cgoPkgs {
out, err := os.Create(filepath.Join(workspace, cgoPkg, "zcgo_flags.go"))
if err != nil {
return err
}
err = tpl.Execute(out, struct {
Package string
CPPFlags string
LDFlags string
}{Package: filepath.Base(cgoPkg), CPPFlags: cppFlags, LDFlags: ldFlags})
if err != nil {
return err
}
logCommand("bazel", args...)
if err := d.exec.CommandContextInheritingStdStreams(ctx, "bazel", args...); err != nil {
return fmt.Errorf("generating cgo: %w", err)
}

return nil
}

Expand Down
21 changes: 21 additions & 0 deletions pkg/cmd/generate-cgo/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
load("//build/bazelutil/unused_checker:unused.bzl", "get_x_data")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_library(
name = "generate-cgo_lib",
srcs = ["main.go"],
importpath = "github.com/cockroachdb/cockroach/pkg/cmd/generate-cgo",
visibility = ["//visibility:private"],
deps = [
"@com_github_alessio_shellescape//:shellescape",
"@com_github_cockroachdb_errors//:errors",
],
)

go_binary(
name = "generate-cgo",
embed = [":generate-cgo_lib"],
visibility = ["//visibility:public"],
)

get_x_data(name = "get_x_data")
Loading