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

Go: add bazel build #16317

Merged
merged 47 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
393f6b7
Go: add gazelle-generated `BUILD` files
redsun82 Apr 24, 2024
4ca8faa
Go: introduce universal binaries on macOS
redsun82 Apr 25, 2024
7d9a68b
Go: wrap gazelle to regenerate from scratch and add header
redsun82 Apr 26, 2024
3ad9c02
Go: remove `go_sdk.host`
redsun82 Apr 26, 2024
925a2cc
Go: create whole extractor pack with bazel
redsun82 Apr 26, 2024
19b2e56
Go: group BUILD and dbscheme generation target
redsun82 Apr 26, 2024
bfa189e
Go: use a dbscheme generated during the build in `extractor-pack`
redsun82 Apr 26, 2024
146d84b
Go: rework makefile
redsun82 Apr 26, 2024
c8b0224
Merge branch 'main' into redsun82/go
redsun82 Apr 26, 2024
d98ccdf
Go: update workflow
redsun82 Apr 26, 2024
0f387ee
Go: add vendor update to `//go:gen`
redsun82 Apr 26, 2024
86d6b8e
Go: put back go setup
redsun82 Apr 26, 2024
d66494d
Go: update `go-tests-other-os.yml`
redsun82 Apr 26, 2024
b0758fd
Go: workaround for gazelle on macOS
redsun82 Apr 29, 2024
0dfd336
Go: fix `//go:gen` on windows
redsun82 Apr 29, 2024
6ec223c
Go: small cleanup in `Makefile`
redsun82 Apr 29, 2024
2f6dd2a
Go: refactor workflows with shared action
redsun82 Apr 29, 2024
f0f6c22
Go: fix regex in action for macOS
redsun82 Apr 29, 2024
1f78882
Go: make windows checks happy
redsun82 Apr 29, 2024
15bb846
Go: add workaround for extractor pack windows installer
redsun82 Apr 29, 2024
e7886d0
Bazel: add empty registry override
redsun82 Apr 29, 2024
cb85a75
Bazel: patch `apple_support` to avoid registering its toolchain
redsun82 Apr 29, 2024
2590d8a
Merge branch 'main' into redsun82/go
redsun82 Apr 30, 2024
608791f
Bazel/Go: use native cross compilation for fat binaries
redsun82 May 2, 2024
94212d1
Bazel/Go: remove `apple_support`
redsun82 May 2, 2024
a8d3226
Merge branch 'main' into redsun82/go
redsun82 May 2, 2024
12b9b80
Go: revert changes to `make` and CI to postpone them in a separate PR
redsun82 May 2, 2024
520a2c9
Merge branch 'main' into redsun82/go
redsun82 May 2, 2024
9055d95
Go: remove unused action (will be re-added later)
redsun82 May 2, 2024
ca2d94b
Fix go pattern in `.pre-commit-config.yaml`
redsun82 May 2, 2024
318d954
Go: make `//go:gen` not clear by default, and clean on `--force`
redsun82 May 2, 2024
4ae82ac
Go: add explanatory comment to `extractor` `BUILD` file
redsun82 May 2, 2024
0bc6934
Go: rename `pkg_files` to something less confusing
redsun82 May 2, 2024
abcd916
Go: write test go runtime version in a specific file
redsun82 May 2, 2024
1aafc37
Revert "Go: write test go runtime version in a specific file"
redsun82 May 2, 2024
76067cb
Go: skip `X:nocoverageredesign` printing by autobuilder built with bazel
redsun82 May 2, 2024
81dea9f
Merge branch 'main' into redsun82/go
redsun82 May 2, 2024
00baccb
Go: autoformat
redsun82 May 2, 2024
31c427e
Bazel/Go: add more explanation in `gen.py`
redsun82 May 3, 2024
8f0b884
Bazel/Go: be more specific in `go/extractor/BUILD.bazel` comments
redsun82 May 3, 2024
2132c7b
Bazel/Go: make `@codeql//go:gen` runnable from internal repo
redsun82 May 3, 2024
471303b
Bazel/Go: remove unneeded comment
redsun82 May 3, 2024
17990da
Update go/extractor/BUILD.bazel
redsun82 May 3, 2024
77128de
Bazel/Go: make installer work from internal repo and on windows
redsun82 May 3, 2024
cba4ba0
Merge branch 'main' into redsun82/go
redsun82 May 6, 2024
5b184c1
Bazel/Go: add some comments
redsun82 May 6, 2024
73df4fa
Go: fix Windows installation
redsun82 May 6, 2024
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
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ repos:
entry: bazel run //:buildifier
pass_filenames: false

- id: go-gen
name: Check checked in generated files in go
files: ^go/.*
language: system
entry: bazel run //go:gen
pass_filenames: false

- id: codeql-format
name: Fix QL file formatting
files: \.qll?$
Expand Down
7 changes: 6 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ local_path_override(

# see https://registry.bazel.build/ for a list of available packages

bazel_dep(name = "platforms", version = "0.0.8")
bazel_dep(name = "platforms", version = "0.0.9")
bazel_dep(name = "rules_go", version = "0.47.0")
bazel_dep(name = "rules_pkg", version = "0.10.1")
bazel_dep(name = "rules_nodejs", version = "6.0.3")
bazel_dep(name = "rules_python", version = "0.31.0")
bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "abseil-cpp", version = "20240116.0", repo_name = "absl")
bazel_dep(name = "nlohmann_json", version = "3.11.3", repo_name = "json")
bazel_dep(name = "fmt", version = "10.0.0")
bazel_dep(name = "gazelle", version = "0.36.0")

bazel_dep(name = "buildifier_prebuilt", version = "6.4.0", dev_dependency = True)

Expand Down Expand Up @@ -52,6 +54,9 @@ node.toolchain(
)
use_repo(node, "nodejs", "nodejs_toolchains")

go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(version = "1.22.2") # default
Copy link
Member

@mbg mbg May 2, 2024

Choose a reason for hiding this comment

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

Note: I assume this now replaces the GO_VERSION environment variables in the workflows, so we should update the playbook accordingly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

indeed, I will open a PR for that!

criemen marked this conversation as resolved.
Show resolved Hide resolved

register_toolchains(
"@nodejs_toolchains//:all",
)
119 changes: 119 additions & 0 deletions go/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
load("@bazel_skylib//rules:native_binary.bzl", "native_binary")
load("@gazelle//:def.bzl", "gazelle")
load("@rules_pkg//pkg:install.bzl", "pkg_install")
load("@rules_pkg//pkg:mappings.bzl", "pkg_attributes", "pkg_filegroup", "pkg_files")
load("@rules_pkg//pkg:zip.bzl", "pkg_zip")
load("//:defs.bzl", "codeql_platform")

gazelle(
name = "gazelle",
extra_args = ["go/extractor"],
)

_gen_binaries = [
"@rules_go//go",
":gazelle",
"//go/extractor/cli/go-gen-dbscheme",
]

py_binary(
name = "gen",
srcs = ["gen.py"],
args = ["$(rlocationpath %s)" % bin for bin in _gen_binaries],
data = _gen_binaries,
deps = ["@rules_python//python/runfiles"],
)

# this is an internal copy of the dbscheme to be used by extractor-pack
redsun82 marked this conversation as resolved.
Show resolved Hide resolved
# this allows the extractor-pack target to be independent and up-to-date with respect to
# having run //go:gen to update the checked in files
genrule(
name = "dbscheme",
outs = ["go.dbscheme"],
cmd = "$(execpath //go/extractor/cli/go-gen-dbscheme) $@",
tools = ["//go/extractor/cli/go-gen-dbscheme"],
)

pkg_files(
name = "resources",
srcs = [
"LICENSE",
"codeql-extractor.yml",
"ql/lib/go.dbscheme.stats",
":dbscheme",
],
)

pkg_filegroup(
name = "extractor-pack-generic",
srcs = [
":resources",
"//go/codeql-tools",
"//go/downgrades",
"//go/extractor:tokenizer",
],
visibility = ["//visibility:public"],
)

pkg_files(
name = "extractor-pack-arch",
srcs = [
"//go/extractor/cli/go-autobuilder",
"//go/extractor/cli/go-bootstrap",
"//go/extractor/cli/go-build-runner",
"//go/extractor/cli/go-extractor",
"//go/extractor/cli/go-gen-dbscheme",
"//go/extractor/cli/go-tokenizer",
],
attributes = pkg_attributes(mode = "0755"),
criemen marked this conversation as resolved.
Show resolved Hide resolved
prefix = "tools/" + codeql_platform,
visibility = ["//visibility:public"],
)

pkg_filegroup(
name = "extractor-pack",
srcs = [
":extractor-pack-arch",
":extractor-pack-generic",
],
visibility = ["//visibility:public"],
)

pkg_install(
name = "_extractor-pack-installer",
srcs = [":extractor-pack"],
)

# rules_pkg installer is currently broken on Windows
# see https://github.com/bazelbuild/rules_pkg/issues/387
# for now, work around it using an archive
pkg_zip(
criemen marked this conversation as resolved.
Show resolved Hide resolved
name = "_extractor-pack-zip",
srcs = [":extractor-pack"],
)

alias(
name = "_create-extractor-pack-arg",
actual = select({
"@platforms//os:windows": ":_extractor-pack-zip",
"//conditions:default": ":_extractor-pack-installer",
}),
)

py_binary(
name = "create-extractor-pack",
srcs = ["create_extractor_pack.py"],
args = ["$(rlocationpath :_create-extractor-pack-arg)"],
data = [":_create-extractor-pack-arg"],
main = "create_extractor_pack.py",
deps = ["@rules_python//python/runfiles"],
)

native_binary(
name = "gen-dbscheme",
src = "//go/extractor/cli/go-gen-dbscheme",
out = "go-gen-dbscheme",
args = [
"$$BUILD_WORKSPACE_DIRECTORY/go/ql/lib/go.dbscheme",
criemen marked this conversation as resolved.
Show resolved Hide resolved
],
)
28 changes: 28 additions & 0 deletions go/codeql-tools/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
load("@rules_pkg//pkg:mappings.bzl", "pkg_attributes", "pkg_filegroup", "pkg_files")

pkg_files(
name = "sh-files",
srcs = glob(["*.sh"]),
mbg marked this conversation as resolved.
Show resolved Hide resolved
attributes = pkg_attributes(mode = "0755"),
criemen marked this conversation as resolved.
Show resolved Hide resolved
)

pkg_files(
name = "non-sh-files",
srcs = glob(
["*"],
exclude = [
"*.sh",
"BUILD.bazel",
],
),
)

pkg_filegroup(
name = "codeql-tools",
srcs = [
":non-sh-files",
":sh-files",
],
prefix = "tools",
visibility = ["//go:__pkg__"],
)
26 changes: 26 additions & 0 deletions go/create_extractor_pack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os
import pathlib
import shutil
import sys
import subprocess
import zipfile
from python.runfiles import runfiles

try:
workspace_dir = pathlib.Path(os.environ['BUILD_WORKSPACE_DIRECTORY'])
except KeyError:
print("this should be run with bazel run", file=sys.stderr)
sys.exit(1)

dest_dir = workspace_dir / 'go' / 'build' / 'codeql-extractor-go'
installer_or_zip = pathlib.Path(runfiles.Create().Rlocation(sys.argv[1]))

shutil.rmtree(dest_dir, ignore_errors=True)

if installer_or_zip.suffix == '.zip':
dest_dir.mkdir()
with zipfile.ZipFile(installer_or_zip) as pack:
pack.extractall(dest_dir)
else:
os.environ['DESTDIR'] = str(dest_dir)
subprocess.check_call([installer_or_zip])
12 changes: 12 additions & 0 deletions go/downgrades/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")

pkg_files(
name = "downgrades",
srcs = glob(
["**"],
exclude = ["BUILD.bazel"],
),
prefix = "downgrades",
strip_prefix = strip_prefix.from_pkg(),
visibility = ["//go:__pkg__"],
)
1 change: 1 addition & 0 deletions go/extractor/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*/**/BUILD.bazel linguist-generated=true
56 changes: 56 additions & 0 deletions go/extractor/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
load("@rules_go//go:def.bzl", "go_library")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")

# gazelle:prefix github.com/github/codeql-go/extractor
# gazelle:map_kind go_binary codeql_go_binary //go:rules.bzl

# following target is kept up to date by `bazel run //go:gen`, do not edit directly
redsun82 marked this conversation as resolved.
Show resolved Hide resolved
go_library(
name = "extractor",
srcs = [
"extractor.go",
"gomodextractor.go",
"semaphore.go",
],
importpath = "github.com/github/codeql-go/extractor",
visibility = ["//visibility:public"],
deps = [
"//go/extractor/dbscheme",
"//go/extractor/diagnostics",
"//go/extractor/srcarchive",
"//go/extractor/trap",
"//go/extractor/util",
"//go/extractor/vendor/golang.org/x/mod/modfile",
"//go/extractor/vendor/golang.org/x/tools/go/packages",
],
)
redsun82 marked this conversation as resolved.
Show resolved Hide resolved

java_library(
criemen marked this conversation as resolved.
Show resolved Hide resolved
name = "tokenizer-deps",
srcs = [
"net/sourceforge/pmd/cpd/AbstractLanguage.java",
"net/sourceforge/pmd/cpd/SourceCode.java",
"net/sourceforge/pmd/cpd/TokenEntry.java",
"net/sourceforge/pmd/cpd/Tokenizer.java",
],
)

java_library(
name = "tokenizer-jar",
srcs = [
"net/sourceforge/pmd/cpd/GoLanguage.java",
"opencsv/CSVParser.java",
"opencsv/CSVReader.java",
],
deps = [":tokenizer-deps"],
)

pkg_files(
name = "tokenizer",
srcs = [":tokenizer-jar"],
prefix = "tools",
renames = {
":tokenizer-jar": "tokenizer.jar",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did you check that this JAR has all the class files? I'd expect you need to build a deploy jar here, as otherwise the tokenizer-deps library class files will not make it into tokenizer.jar. If our tests haven't uncovered this yet, we need to make sure this JAR is functional at all (see discussion above about JVM versions), and ideally add a test for this, so we don't accidentally break this functionality during bazel upgrades.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, I checked that exactly the same files end up in the extractor pack. By the way, I'm no java expert so I don't know what this all entails, but the split between tokenizer-deps and tokenizer-jar is precisely intended to reproduce what was being done on the pack previously in the Makefile:

codeql/go/Makefile

Lines 101 to 106 in 880262d

tools/net/sourceforge/pmd/cpd/GoLanguage.class: extractor/net/sourceforge/pmd/cpd/GoLanguage.java
javac -cp extractor -d tools $<
rm tools/net/sourceforge/pmd/cpd/AbstractLanguage.class
rm tools/net/sourceforge/pmd/cpd/SourceCode.class
rm tools/net/sourceforge/pmd/cpd/TokenEntry.class
rm tools/net/sourceforge/pmd/cpd/Tokenizer.class

You can see we are actively rming the files in tokenizer-deps, so it seems we actually didn't want a deploy jar here? Maybe @mbg has more details here?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I haven't dealt with this part of the codebase before. @smowton or @owen-mc may know more.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oo okay, in that case this needs to be documented - the way it's currently written in the BUILD.bazel file it looks like a bug, not intended behavior. It is a correct reproduction of the behavior from the Makefile though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll add a comment then!

},
visibility = ["//go:__pkg__"],
)
26 changes: 26 additions & 0 deletions go/extractor/autobuilder/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions go/extractor/cli/go-autobuilder/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion go/extractor/cli/go-autobuilder/go-autobuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,9 @@ func extract(workspace project.GoWorkspace) bool {

// Build the project and run the extractor.
func installDependenciesAndBuild() {
log.Printf("Autobuilder was built with %s, environment has %s\n", runtime.Version(), toolchain.GetEnvGoVersion())
// do not print experiments the autobuilder was built with if any, only the version
version := strings.SplitN(runtime.Version(), " ", 2)[0]
log.Printf("Autobuilder was built with %s, environment has %s\n", version, toolchain.GetEnvGoVersion())

srcdir := getSourceDir()

Expand Down
17 changes: 17 additions & 0 deletions go/extractor/cli/go-bootstrap/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions go/extractor/cli/go-build-runner/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading