mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
Merge remote-tracking branch 'origin/main' into criemen/new-pkg
This commit is contained in:
@@ -42,7 +42,7 @@ endmacro()
|
||||
macro(generate_and_include)
|
||||
file(REMOVE "${BAZEL_WORKSPACE}/.bazel-cmake/BUILD.bazel")
|
||||
# use aquery to only get targets compatible with the current platform
|
||||
bazel_even_if_failing(aquery "kind(\"cc_test|cc_binary\", ${ARGN})" ${BAZEL_BUILD_OPTIONS} --output=jsonproto OUTPUT_VARIABLE BAZEL_AQUERY_RESULT)
|
||||
bazel_even_if_failing(aquery "kind(\"cc_test|cc_binary\",${ARGN})" ${BAZEL_BUILD_OPTIONS} --output=jsonproto OUTPUT_VARIABLE BAZEL_AQUERY_RESULT)
|
||||
string(JSON BAZEL_JSON_TARGETS GET "${BAZEL_AQUERY_RESULT}" targets)
|
||||
string(JSON LAST_IDX LENGTH "${BAZEL_JSON_TARGETS}")
|
||||
math(EXPR LAST_IDX "${LAST_IDX} - 1")
|
||||
|
||||
@@ -57,10 +57,18 @@ def git(*args, **kwargs):
|
||||
|
||||
|
||||
def get_endpoint():
|
||||
lfs_env = get_env(subprocess.check_output(["git", "lfs", "env"], text=True, cwd=source_dir))
|
||||
endpoint = next(v for k, v in lfs_env.items() if k.startswith('Endpoint'))
|
||||
lfs_env_items = iter(get_env(subprocess.check_output(["git", "lfs", "env"], text=True, cwd=source_dir)).items())
|
||||
endpoint = next(v for k, v in lfs_env_items if k.startswith('Endpoint'))
|
||||
endpoint, _, _ = endpoint.partition(' ')
|
||||
ssh_endpoint = lfs_env.get(" SSH")
|
||||
# only take the ssh endpoint if it follows directly after the first endpoint we found
|
||||
# in a situation like
|
||||
# Endpoint (a)=...
|
||||
# Endpoint (b)=...
|
||||
# SSH=...
|
||||
# we want to ignore the SSH endpoint, as it's not linked to the default (a) endpoint
|
||||
following_key, following_value = next(lfs_env_items, (None, None))
|
||||
ssh_endpoint = following_value if following_key == " SSH" else None
|
||||
|
||||
endpoint = Endpoint(endpoint, {
|
||||
"Content-Type": "application/vnd.git-lfs+json",
|
||||
"Accept": "application/vnd.git-lfs+json",
|
||||
|
||||
@@ -50,6 +50,7 @@ if opts.zip_manifest:
|
||||
for line in manifest:
|
||||
prefix, _, zip = line.partition(":")
|
||||
assert zip, f"missing prefix for {prefix}, you should use prefix:zip format"
|
||||
zip = zip.strip()
|
||||
dest = destdir / prefix
|
||||
dest.mkdir(parents=True, exist_ok=True)
|
||||
subprocess.run([ripunzip, "unzip-file", zip, "-d", dest], check=True)
|
||||
|
||||
@@ -4,6 +4,7 @@ cc_library(
|
||||
"zipmerge.cpp",
|
||||
],
|
||||
hdrs = ["zipmerge.h"],
|
||||
copts = ["-std=c++20"],
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
@@ -11,6 +12,7 @@ cc_binary(
|
||||
srcs = [
|
||||
"zipmerge_main.cpp",
|
||||
],
|
||||
copts = ["-std=c++20"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
":lib",
|
||||
@@ -21,6 +23,7 @@ cc_test(
|
||||
name = "test",
|
||||
size = "small",
|
||||
srcs = ["zipmerge_test.cpp"],
|
||||
copts = ["-std=c++20"],
|
||||
data = glob(["test-files/*"]),
|
||||
linkstatic = True, # required to build the test in the internal repo
|
||||
deps = [
|
||||
|
||||
38
misc/bazel/os.bzl
Normal file
38
misc/bazel/os.bzl
Normal file
@@ -0,0 +1,38 @@
|
||||
""" Os detection facilities. """
|
||||
|
||||
def os_select(
|
||||
ctx = None,
|
||||
*,
|
||||
linux = None,
|
||||
windows = None,
|
||||
macos = None,
|
||||
default = None):
|
||||
"""
|
||||
This can work both in a macro and a rule context to choose something based on the current OS.
|
||||
If used in a rule implementation, you need to pass `ctx` and add `OS_DETECTION_ATTRS` to the
|
||||
rule attributes.
|
||||
"""
|
||||
choices = {
|
||||
"linux": linux or default,
|
||||
"windows": windows or default,
|
||||
"macos": macos or default,
|
||||
}
|
||||
if not ctx:
|
||||
return select({
|
||||
"@platforms//os:%s" % os: v
|
||||
for os, v in choices.items()
|
||||
if v != None
|
||||
})
|
||||
|
||||
for os, v in choices.items():
|
||||
if ctx.target_platform_has_constraint(getattr(ctx.attr, "_%s_constraint" % os)[platform_common.ConstraintValueInfo]):
|
||||
if v == None:
|
||||
fail("%s not supported by %s" % (os, ctx.label))
|
||||
return v
|
||||
fail("Unknown OS detected")
|
||||
|
||||
OS_DETECTION_ATTRS = {
|
||||
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
|
||||
"_macos_constraint": attr.label(default = "@platforms//os:macos"),
|
||||
"_linux_constraint": attr.label(default = "@platforms//os:linux"),
|
||||
}
|
||||
@@ -7,6 +7,7 @@ load("@rules_pkg//pkg:mappings.bzl", "pkg_attributes", "pkg_filegroup", "pkg_fil
|
||||
load("@rules_pkg//pkg:pkg.bzl", "pkg_zip")
|
||||
load("@rules_pkg//pkg:providers.bzl", "PackageFilegroupInfo", "PackageFilesInfo")
|
||||
load("@rules_python//python:defs.bzl", "py_binary")
|
||||
load("//misc/bazel:os.bzl", "OS_DETECTION_ATTRS", "os_select")
|
||||
|
||||
def _make_internal(name):
|
||||
def internal(suffix = "internal", *args):
|
||||
@@ -15,11 +16,6 @@ def _make_internal(name):
|
||||
|
||||
return internal
|
||||
|
||||
_PLAT_DETECTION_ATTRS = {
|
||||
"_windows": attr.label(default = "@platforms//os:windows"),
|
||||
"_macos": attr.label(default = "@platforms//os:macos"),
|
||||
}
|
||||
|
||||
_PLAT_PLACEHOLDER = "{CODEQL_PLATFORM}"
|
||||
|
||||
def _expand_path(path, platform):
|
||||
@@ -28,28 +24,8 @@ def _expand_path(path, platform):
|
||||
return ("arch", path)
|
||||
return ("generic", path)
|
||||
|
||||
def _platform_select(
|
||||
ctx = None,
|
||||
*,
|
||||
linux,
|
||||
windows,
|
||||
macos):
|
||||
if ctx:
|
||||
if ctx.target_platform_has_constraint(ctx.attr._windows[platform_common.ConstraintValueInfo]):
|
||||
return windows
|
||||
elif ctx.target_platform_has_constraint(ctx.attr._macos[platform_common.ConstraintValueInfo]):
|
||||
return macos
|
||||
else:
|
||||
return linux
|
||||
else:
|
||||
return select({
|
||||
"@platforms//os:linux": linux,
|
||||
"@platforms//os:macos": macos,
|
||||
"@platforms//os:windows": windows,
|
||||
})
|
||||
|
||||
def _detect_platform(ctx = None):
|
||||
return _platform_select(ctx, linux = "linux64", macos = "osx64", windows = "win64")
|
||||
return os_select(ctx, linux = "linux64", macos = "osx64", windows = "win64")
|
||||
|
||||
def codeql_pkg_files(
|
||||
*,
|
||||
@@ -137,7 +113,7 @@ _extract_pkg_filegroup = rule(
|
||||
"src": attr.label(providers = [PackageFilegroupInfo, DefaultInfo]),
|
||||
"kind": attr.string(doc = "What part to extract", values = ["generic", "arch"]),
|
||||
"arch_overrides": attr.string_list(doc = "A list of files that should be included in the arch package regardless of the path"),
|
||||
} | _PLAT_DETECTION_ATTRS,
|
||||
} | OS_DETECTION_ATTRS,
|
||||
)
|
||||
|
||||
_ZipInfo = provider(fields = {"zips_to_prefixes": "mapping of zip files to prefixes"})
|
||||
@@ -187,7 +163,7 @@ _zip_info_filter = rule(
|
||||
attrs = {
|
||||
"srcs": attr.label_list(doc = "_ZipInfos to transform", providers = [_ZipInfo]),
|
||||
"kind": attr.string(doc = "Which zip kind to consider", values = ["generic", "arch"]),
|
||||
} | _PLAT_DETECTION_ATTRS,
|
||||
} | OS_DETECTION_ATTRS,
|
||||
)
|
||||
|
||||
def _imported_zips_manifest_impl(ctx):
|
||||
@@ -284,19 +260,19 @@ def codeql_pack(
|
||||
install_dest = "extractor-pack",
|
||||
compression_level = None,
|
||||
arch_overrides = None,
|
||||
prefix_override = None,
|
||||
zip_prefix = None,
|
||||
**kwargs):
|
||||
"""
|
||||
Define a codeql pack. This macro accepts `pkg_files`, `pkg_filegroup` or their `codeql_*` counterparts as `srcs`.
|
||||
`zips` is a map from prefixes to `.zip` files to import.
|
||||
`zips` is a map from `.zip` files to prefixes to import.
|
||||
* defines a `<name>-generic-zip` target creating a `<zip_filename>-generic.zip` archive with the generic bits,
|
||||
prefixed with `name`
|
||||
prefixed with `zip_prefix`
|
||||
* defines a `<name>-arch-zip` target creating a `<zip_filename>-<codeql_platform>.zip` archive with the
|
||||
arch-specific bits, prefixed with `name`
|
||||
arch-specific bits, prefixed with `zip_prefix`
|
||||
* defines a runnable `<name>-installer` target that will install the pack in `install_dest`, relative to where the
|
||||
rule is used. The install destination can be overridden appending `-- --destdir=...` to the `bazel run`
|
||||
invocation. This installation _does not_ prefix the contents with `name`.
|
||||
The prefix for the zip files can be overriden with `prefix_override`.
|
||||
invocation. This installation _does not_ prefix the contents with `zip_prefix`.
|
||||
The prefix for the zip files can be set with `zip_prefix`, it is `name` by default.
|
||||
|
||||
The distinction between arch-specific and generic contents is made based on whether the paths (including possible
|
||||
prefixes added by rules) contain the special `{CODEQL_PLATFORM}` placeholder, which in case it is present will also
|
||||
@@ -310,9 +286,8 @@ def codeql_pack(
|
||||
internal = _make_internal(name)
|
||||
zip_filename = zip_filename or name
|
||||
zips = zips or {}
|
||||
prefix = name
|
||||
if prefix_override != None:
|
||||
prefix = prefix_override
|
||||
if zip_prefix == None:
|
||||
zip_prefix = name
|
||||
pkg_filegroup(
|
||||
name = internal("all"),
|
||||
srcs = srcs,
|
||||
@@ -350,7 +325,7 @@ def codeql_pack(
|
||||
name = internal(kind, "zip"),
|
||||
srcs = [internal(kind, "zip-base"), internal(kind, "zip-info")],
|
||||
out = _get_zip_filename(name, kind),
|
||||
prefix = prefix,
|
||||
prefix = zip_prefix,
|
||||
visibility = visibility,
|
||||
)
|
||||
else:
|
||||
@@ -358,7 +333,7 @@ def codeql_pack(
|
||||
name = internal(kind, "zip"),
|
||||
srcs = [internal(kind)],
|
||||
visibility = visibility,
|
||||
package_dir = prefix,
|
||||
package_dir = zip_prefix,
|
||||
package_file_name = _get_zip_filename(name, kind),
|
||||
compression_level = compression_level,
|
||||
)
|
||||
@@ -382,14 +357,14 @@ def codeql_pack(
|
||||
)
|
||||
py_binary(
|
||||
name = internal("installer"),
|
||||
srcs = ["//misc/bazel/internal:install.py"],
|
||||
main = "//misc/bazel/internal:install.py",
|
||||
srcs = [Label("//misc/bazel/internal:install.py")],
|
||||
main = Label("//misc/bazel/internal:install.py"),
|
||||
data = [
|
||||
internal("build-file"),
|
||||
internal("script"),
|
||||
] + ([
|
||||
internal("zip-manifest"),
|
||||
"//misc/bazel/internal/ripunzip",
|
||||
Label("//misc/bazel/internal/ripunzip"),
|
||||
] if zips else []),
|
||||
deps = ["@rules_python//python/runfiles"],
|
||||
args = [
|
||||
@@ -398,7 +373,7 @@ def codeql_pack(
|
||||
"--destdir",
|
||||
install_dest,
|
||||
] + ([
|
||||
"--ripunzip=$(rlocationpath //misc/bazel/internal/ripunzip)",
|
||||
"--ripunzip=$(rlocationpath %s)" % Label("//misc/bazel/internal/ripunzip"),
|
||||
"--zip-manifest=$(rlocationpath %s)" % internal("zip-manifest"),
|
||||
] if zips else []),
|
||||
visibility = visibility,
|
||||
|
||||
Reference in New Issue
Block a user