diff --git a/.bazelversion b/.bazelversion new file mode 100644 index 00000000000..0062ac97180 --- /dev/null +++ b/.bazelversion @@ -0,0 +1 @@ +5.0.0 diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 4d485742b28..b427ab37b93 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -3,30 +3,10 @@ workspace(name = "ql") -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - -http_archive( - name = "rules_pkg", - sha256 = "62eeb544ff1ef41d786e329e1536c1d541bb9bcad27ae984d57f18f314018e66", - urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.6.0/rules_pkg-0.6.0.tar.gz", - "https://github.com/bazelbuild/rules_pkg/releases/download/0.6.0/rules_pkg-0.6.0.tar.gz", - ], -) - -load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies") - -rules_pkg_dependencies() - -http_archive( - name = "platforms", - sha256 = "460caee0fa583b908c622913334ec3c1b842572b9c23cf0d3da0c2543a1a157d", - urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.3/platforms-0.0.3.tar.gz", - "https://github.com/bazelbuild/platforms/releases/download/0.0.3/platforms-0.0.3.tar.gz", - ], -) - load("@ql//misc/bazel:workspace.bzl", "ql_workspace") ql_workspace() + +load("@ql//misc/bazel:workspace_deps.bzl", "ql_workspace_deps") + +ql_workspace_deps() diff --git a/misc/bazel/BUILD.toolchain.tpl b/misc/bazel/BUILD.toolchain.tpl new file mode 100644 index 00000000000..849508908d8 --- /dev/null +++ b/misc/bazel/BUILD.toolchain.tpl @@ -0,0 +1,12 @@ +load("@//misc/bazel:toolchain.bzl", "codeql_cli_toolchain") + +codeql_cli_toolchain( + name = "codeql-cli", + path = "{codeql_cli_path}", +) + +toolchain( + name = "codeql-cli-toolchain", + toolchain = ":codeql-cli", + toolchain_type = "@//:toolchain_type", +) diff --git a/misc/bazel/test.bzl b/misc/bazel/test.bzl new file mode 100644 index 00000000000..24fe7dbe396 --- /dev/null +++ b/misc/bazel/test.bzl @@ -0,0 +1,38 @@ +def _test_script_impl(ctx): + output = ctx.actions.declare_file("%s.py" % ctx.label.name) + codeql_cli_path = ctx.toolchains["//:toolchain_type"].codeql_cli.path + ctx.actions.expand_template( + template = ctx.file._template, + output = output, + substitutions = { + "{codeql_cli_path}": codeql_cli_path, + "{test_sources}": str([f.path for f in ctx.files.srcs]), + }, + ) + return DefaultInfo( + files = depset([output]), + ) + +_test_script = rule( + implementation = _test_script_impl, + attrs = { + "srcs": attr.label_list(allow_files = True), + "_template": attr.label(default = "//misc/bazel:test.template.py", allow_single_file = True), + }, + toolchains = ["//:toolchain_type"], +) + +def codeql_test(*, name, srcs, deps): + srcs = native.glob(["test/**/*.ql", "test/**/*.qlref"]) + data = srcs + deps + script = name + "-script" + _test_script( + name = script, + srcs = srcs, + ) + native.py_test( + name = name, + main = script + ".py", + srcs = [script], + data = data, + ) diff --git a/misc/bazel/test.template.py b/misc/bazel/test.template.py new file mode 100644 index 00000000000..2430afb04b0 --- /dev/null +++ b/misc/bazel/test.template.py @@ -0,0 +1,3 @@ +import os + +os.execl("{codeql_cli_path}", "test", "run", "--check-databases", "--", *{test_sources}) diff --git a/misc/bazel/workspace.bzl b/misc/bazel/workspace.bzl index b6f9bacd201..1b52c486925 100644 --- a/misc/bazel/workspace.bzl +++ b/misc/bazel/workspace.bzl @@ -1,3 +1,6 @@ +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") + def _ql_utils_impl(repository_ctx): root = repository_ctx.path(Label("//:WORKSPACE.bazel")).realpath.dirname repository_ctx.file("BUILD.bazel") @@ -12,4 +15,24 @@ _ql_utils = repository_rule( ) def ql_workspace(): - _ql_utils(name = "utils") + _ql_utils(name = "ql_utils") + + maybe( + repo_rule = http_archive, + name = "rules_pkg", + sha256 = "62eeb544ff1ef41d786e329e1536c1d541bb9bcad27ae984d57f18f314018e66", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.6.0/rules_pkg-0.6.0.tar.gz", + "https://github.com/bazelbuild/rules_pkg/releases/download/0.6.0/rules_pkg-0.6.0.tar.gz", + ], + ) + + maybe( + repo_rule = http_archive, + name = "platforms", + sha256 = "460caee0fa583b908c622913334ec3c1b842572b9c23cf0d3da0c2543a1a157d", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.3/platforms-0.0.3.tar.gz", + "https://github.com/bazelbuild/platforms/releases/download/0.0.3/platforms-0.0.3.tar.gz", + ], + ) diff --git a/misc/bazel/workspace_deps.bzl b/misc/bazel/workspace_deps.bzl new file mode 100644 index 00000000000..a082b4b1cb7 --- /dev/null +++ b/misc/bazel/workspace_deps.bzl @@ -0,0 +1,4 @@ +load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies") + +def ql_workspace_deps(): + rules_pkg_dependencies() diff --git a/swift/.codeqlmanifest.json b/swift/.codeqlmanifest.json index 4e5e2c13cb2..3b809abfd3b 100644 --- a/swift/.codeqlmanifest.json +++ b/swift/.codeqlmanifest.json @@ -2,6 +2,6 @@ "provide": [ "ql/lib/qlpack.yml", "ql/test/qlpack.yml", - "extractor_pack/codeql-extractor.yml" + "extractor-pack/codeql-extractor.yml" ] } diff --git a/swift/.gitignore b/swift/.gitignore index b6256eaf747..52d33160303 100644 --- a/swift/.gitignore +++ b/swift/.gitignore @@ -1 +1 @@ -extractor_pack +extractor-pack diff --git a/swift/BUILD.bazel b/swift/BUILD.bazel index 7219f98388c..134a32d8603 100644 --- a/swift/BUILD.bazel +++ b/swift/BUILD.bazel @@ -1,5 +1,7 @@ load("@rules_pkg//:mappings.bzl", "pkg_attributes", "pkg_filegroup", "pkg_files") +load("@rules_pkg//:install.bzl", "pkg_install") load("@ql//:defs.bzl", "codeql_platform") +load("@ql_utils//:source_dir.bzl", "current_source_dir") pkg_files( name = "dbscheme", @@ -52,3 +54,12 @@ pkg_filegroup( ], visibility = ["//visibility:public"], ) + +pkg_install( + name = "create-extractor-pack", + srcs = ["//swift:extractor-pack"], + args = [ + "--destdir", + current_source_dir() + "/extractor-pack", + ], +) diff --git a/swift/README.md b/swift/README.md index e34138d0616..d2b56d1a6f3 100644 --- a/swift/README.md +++ b/swift/README.md @@ -4,5 +4,6 @@ The Swift codeql package is an experimental and unsupported work in progress. ## Usage -Run `bazel run //swift/install`, which will install `swift/extractor_pack`. Using `--search-path=swift` will then pick -up the Swift extractor. +Run `bazel run //swift:create-extractor-pack`, which will install `swift/extractor-pack`. +Using `--search-path=swift/extractor-pack` will then pick up the Swift extractor. You can also use +`--search-path=swift`, as the extractor pack is mentioned in `swift/.codeqlmanifest`. diff --git a/swift/install/BUILD.bazel b/swift/install/BUILD.bazel deleted file mode 100644 index 5064a265eec..00000000000 --- a/swift/install/BUILD.bazel +++ /dev/null @@ -1,11 +0,0 @@ -load("@utils//:source_dir.bzl", "current_source_dir") -load("@rules_pkg//:install.bzl", "pkg_install") - -pkg_install( - name = "install", - srcs = ["//swift:extractor-pack"], - args = [ - "--destdir", - current_source_dir() + "/../extractor_pack", - ], -)