Skip to content

Commit

Permalink
dev: add support for dev generate cgo
Browse files Browse the repository at this point in the history
The contents of these files here is basically cargo-culted from the
`Makefile`. I have verified that I can get a build working via the
following steps:

    ./dev generate go cgo
    ./dev go -- build ./pkg/cmd/cockroach-short

We don't exactly mirror the previous contents of the `zcgo_flags.go`
files that `make` would create. One difference is that we don't populate
the `zcgo_flags` files in `go-libedit` to configure how the C sources
are built and linked, so we get the default behavior, which is fine, but
means the `go build`-built binary and the Bazel-built binary will be
using a different compiled archive (maybe compiled with different flags/
a different configuration/etc.) for the `libedit` sources.

With `make`, we address this by putting a `zcgo_flags_extra.go` file in
the sources for `go-libedit` right in `vendor`. Post-Bazel we have no
reason for `vendor` and have no plans to keep it in the long-term so
this is not really suitable. I'm punting on this for now -- the default
behavior will probably be "good enough" for most people.

Closes #77170.

Release note: None
  • Loading branch information
rickystewart committed Apr 6, 2022
1 parent f69b88f commit 0ea8283
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,9 @@ native-tag := $(subst -,_,$(TARGET_TRIPLE))$(if $(use-stdmalloc),_stdmalloc)
# encounters a given native tag or when the build signature changes (see
# build/defs.mk.sig). These tags are unset when building with the Go toolchain
# directly, so these files are only compiled when building with Make.
#
# NB: If you update the zcgo_flags.go generation below, make sure to make the
# corresponding changes to `dev generate cgo`.
CGO_PKGS := \
pkg/cli \
pkg/cli/clisqlshell \
Expand Down
2 changes: 1 addition & 1 deletion dev
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -euo pipefail

# Bump this counter to force rebuilding `dev` on all machines.
DEV_VERSION=27
DEV_VERSION=28

THIS_DIR=$(cd "$(dirname "$0")" && pwd)
BINARY_DIR=$THIS_DIR/bin/dev-versions
Expand Down
74 changes: 73 additions & 1 deletion pkg/cmd/dev/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"text/template"

"github.com/spf13/cobra"
)
Expand All @@ -29,13 +31,21 @@ func makeGenerateCmd(runE func(cmd *cobra.Command, args []string) error) *cobra.
Use: "generate [target..]",
Aliases: []string{"gen"},
Short: `Generate the specified files`,
Long: `Generate the specified files.`,
Long: `Generate the specified files.
` + "`dev generate bazel`" + ` updates BUILD.bazel files and other Bazel files like DEPS.bzl.
Use the --mirror option if vendoring a new dependency that needs to be mirrored.
` + "`dev generate go`" + ` populates the workspace with generated code.
` + "`dev generate docs`" + ` updates generated documentation.
` + "`dev generate go+docs`" + ` does the same as ` + "`dev generate go docs`" + `, but slightly faster.
` + "`dev generate cgo`" + ` populates the workspace with a few zcgo_flags.go files that can prepare non-Bazel build systems to link in our C dependencies.
` + "`dev generate protobuf`" + ` generates a subset of the code that ` + "`dev generate go`" + ` does, specifically the .pb.go files.`,
Example: `
dev generate
dev generate bazel
dev generate docs
dev generate go
dev generate protobuf
dev generate cgo
dev generate go+docs
`,
Args: cobra.MinimumNArgs(0),
Expand All @@ -52,6 +62,7 @@ func makeGenerateCmd(runE func(cmd *cobra.Command, args []string) error) *cobra.
func (d *dev) generate(cmd *cobra.Command, targets []string) error {
var generatorTargetMapping = map[string]func(cmd *cobra.Command) error{
"bazel": d.generateBazel,
"cgo": d.generateCgo,
"docs": d.generateDocs,
"go": d.generateGo,
"protobuf": d.generateProtobuf,
Expand Down Expand Up @@ -149,3 +160,64 @@ func (d *dev) generateRedactSafe(ctx context.Context) error {
filepath.Join(workspace, "docs", "generated", "redact_safe.md"), string(output),
)
}

func (d *dev) generateCgo(cmd *cobra.Command) error {
ctx := cmd.Context()
args := []string{"build", "//c-deps:libjemalloc", "//c-deps:libproj", "//c-deps:libgeos"}
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
}
bazelBin, err := d.getBazelBin(ctx)
if err != nil {
return err
}
workspace, err := d.getWorkspace(ctx)
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))
cppFlags := fmt.Sprintf("-I%s", filepath.Join(bazelBin, "c-deps/libjemalloc/include"))
ldFlags := fmt.Sprintf("-L%s -L%s", filepath.Join(bazelBin, "c-deps/libjemalloc/lib"), filepath.Join(bazelBin, "c-deps/libproj/lib"))
if runtime.GOOS == "linux" {
cppFlags += fmt.Sprintf(" -I%s", filepath.Join(bazelBin, "c-deps/libkrb5/include"))
ldFlags += fmt.Sprintf(" -L%s", filepath.Join(bazelBin, "c-deps/libkrb5/lib"))
}

cgoPkgs := []string{
"pkg/cli",
"pkg/cli/clisqlshell",
"pkg/server/status",
"pkg/ccl/gssapiccl",
"pkg/geo/geoproj",
}

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
}
}

return nil
}

0 comments on commit 0ea8283

Please sign in to comment.