Rust: bazel packaging

This commit is contained in:
Paolo Tranquilli
2024-08-30 13:05:46 +02:00
parent 3da6cee6ef
commit 4f0fe1ce3a
28 changed files with 207 additions and 28 deletions

View File

@@ -63,6 +63,20 @@ r.from_cargo(
)
use_repo(r, ruby_deps = "rd")
rsp = use_extension(
"@rules_rust//crate_universe:extension.bzl",
"crate",
isolate = True,
)
rsp.from_cargo(
name = "rs_deps",
cargo_lockfile = "//rust/extractor:Cargo.lock",
manifests = [
"//rust/extractor:Cargo.toml",
],
)
use_repo(rsp, rust_deps = "rs_deps")
dotnet = use_extension("@rules_dotnet//dotnet:extensions.bzl", "dotnet")
dotnet.toolchain(dotnet_version = "8.0.101")
use_repo(dotnet, "dotnet_toolchains")

View File

@@ -26,7 +26,7 @@ def _get_type(t: str) -> str:
def _get_field(cls: schema.Class, p: schema.Property) -> rust.Field:
table_name = None
table_name = inflection.tableize(cls.name)
if not p.is_single:
table_name = f"{cls.name}_{p.name}"
if p.is_predicate:
@@ -47,11 +47,12 @@ def _get_field(cls: schema.Class, p: schema.Property) -> rust.Field:
def _get_properties(
cls: schema.Class, lookup: dict[str, schema.Class]
) -> typing.Iterable[schema.Property]:
cls: schema.Class, lookup: dict[str, schema.Class],
) -> typing.Iterable[tuple[schema.Class, schema.Property]]:
for b in cls.bases:
yield from _get_properties(lookup[b], lookup)
yield from cls.properties
for p in cls.properties:
yield cls, p
class Processor:
@@ -63,8 +64,8 @@ class Processor:
return rust.Class(
name=name,
fields=[
_get_field(cls, p)
for p in _get_properties(cls, self._classmap)
_get_field(c, p)
for c, p in _get_properties(cls, self._classmap)
if "rust_skip" not in p.pragmas and not p.synth
],
table_name=inflection.tableize(cls.name),

View File

@@ -78,7 +78,7 @@ def get_field_override(field: str):
class Field:
field_name: str
base_type: str
table_name: str = None
table_name: str
is_optional: bool = False
is_repeated: bool = False
is_unordered: bool = False
@@ -121,8 +121,12 @@ class Class:
fields: list[Field] = dataclasses.field(default_factory=list)
@property
def single_fields(self):
return [f for f in self.fields if f.is_single]
def single_field_entries(self):
ret = {self.table_name: []}
for f in self.fields:
if f.is_single:
ret.setdefault(f.table_name, []).append(f)
return [{"table_name": k, "fields": v} for k, v in ret.items()]
@dataclasses.dataclass

View File

@@ -18,7 +18,9 @@ impl TrapEntry for {{name}} {
}
fn emit<W: Write>(self, id: TrapLabel, out: &mut W) -> std::io::Result<()> {
write!(out, "{{table_name}}({id}{{#single_fields}}, {}{{/single_fields}})\n"{{#single_fields}}, {{#emitter}}self.{{field_name}}{{/emitter}}{{/single_fields}})?;
{{#single_field_entries}}
write!(out, "{{table_name}}({id}{{#fields}}, {}{{/fields}})\n"{{#fields}}, {{#emitter}}self.{{field_name}}{{/emitter}}{{/fields}})?;
{{/single_field_entries}}
{{#fields}}
{{#is_predicate}}
if self.{{field_name}} {

5
rust/.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,5 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/

6
rust/.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
</project>

8
rust/.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/rust.iml" filepath="$PROJECT_DIR$/.idea/rust.iml" />
</modules>
</component>
</project>

13
rust/.idea/rust.iml generated Normal file
View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="EMPTY_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/extractor/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/extractor/target" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
rust/.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

View File

@@ -1,4 +1,55 @@
exports_files([
"codegen.conf",
"schema.py",
])
load("@rules_pkg//pkg:mappings.bzl", "pkg_filegroup")
load(
"//misc/bazel:pkg.bzl",
"codeql_pack",
"codeql_pkg_files",
)
package(default_visibility = ["//rust:__subpackages__"])
filegroup(
name = "schema",
srcs = ["schema.py"],
)
filegroup(
name = "schema-includes",
srcs = glob(["*.dbscheme"]),
)
filegroup(
name = "codegen-conf",
srcs = ["codegen.conf"],
)
codeql_pkg_files(
name = "tools-arch",
exes = ["//rust/extractor"],
prefix = "{CODEQL_PLATFORM}",
)
pkg_filegroup(
name = "tools",
srcs = [
":tools-arch",
"//rust/tools",
],
prefix = "tools",
)
codeql_pkg_files(
name = "root-files",
srcs = [
"codeql-extractor.yml",
"ql/lib/rust.dbscheme",
"ql/lib/rust.dbscheme.stats",
],
)
codeql_pack(
name = "rust",
srcs = [
":root-files",
":tools",
],
)

View File

@@ -4,6 +4,6 @@
--ql-output=ql/lib/codeql/rust/generated
--ql-stub-output=ql/lib/codeql/rust/elements
--ql-test-output=ql/test/extractor-tests/generated
--rust-output=src/generated
--rust-output=extractor/src/generated
--generated-registry=.generated.list
--script-name=codegen

View File

@@ -5,11 +5,11 @@ native_binary(
src = "//misc/codegen",
out = "codegen",
args = [
"--configuration-file=$(location //rust:codegen.conf)",
"--configuration-file=$(location //rust:codegen-conf)",
],
data = [
"//rust:codegen.conf",
"//rust:schema.py",
"//rust:codegen-conf",
"//rust:schema",
],
visibility = ["//rust:__subpackages__"],
)

15
rust/codeql-extractor.yml Normal file
View File

@@ -0,0 +1,15 @@
name: "rust"
display_name: "Rust"
version: 0.1.0
column_kind: "utf8"
build_modes:
- none
github_api_languages:
- Rust
scc_languages:
- Rust
file_types:
- name: rust
display_name: Rust files
extensions:
- .rs

View File

@@ -0,0 +1,15 @@
load("@rust_deps//:defs.bzl", "aliases", "all_crate_deps")
load("//misc/bazel:rust.bzl", "codeql_rust_binary")
codeql_rust_binary(
name = "extractor",
srcs = glob(["src/**/*.rs"]),
aliases = aliases(),
proc_macro_deps = all_crate_deps(
proc_macro = True,
),
visibility = ["//rust:__subpackages__"],
deps = all_crate_deps(
normal = True,
),
)

View File

@@ -1566,8 +1566,7 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]]
name = "rustc_apfloat"
version = "0.2.1+llvm-462a31f5a5ab"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "886d94c63c812a8037c4faca2607453a0fa4cf82f734665266876b022244543f"
source = "git+https://github.com/redsun82/rustc_apfloat.git?rev=096d585100636bc2e9f09d7eefec38c5b334d47b#096d585100636bc2e9f09d7eefec38c5b334d47b"
dependencies = [
"bitflags 1.3.2",
"smallvec",

View File

@@ -21,3 +21,8 @@ serde = "1.0.209"
serde_with = "3.9.0"
stderrlog = "0.6.0"
triomphe = "0.1.13"
[patch.crates-io]
# patch for build script bug preventing bazel build
# see https://github.com/rust-lang/rustc_apfloat/pull/17
rustc_apfloat = { git = "https://github.com/redsun82/rustc_apfloat.git", rev = "096d585100636bc2e9f09d7eefec38c5b334d47b" }

View File

@@ -19,7 +19,7 @@ impl Archiver {
let mut dest = self.root.clone();
dest.push(path::key(source));
let parent = dest.parent().unwrap();
if fs::exists(&dest)? {
if fs::metadata(&dest).is_ok() {
return Ok(())
}
fs::create_dir_all(parent)?;

View File

@@ -15,7 +15,8 @@ impl TrapEntry for DbFile {
}
fn emit<W: Write>(self, id: TrapLabel, out: &mut W) -> std::io::Result<()> {
write!(out, "db_files({id}, {})\n", quoted(&self.name))?;
write!(out, "db_files({id})\n")?;
write!(out, "files({id}, {})\n", quoted(&self.name))?;
Ok(())
}
}
@@ -36,7 +37,8 @@ impl TrapEntry for DbLocation {
}
fn emit<W: Write>(self, id: TrapLabel, out: &mut W) -> std::io::Result<()> {
write!(out, "db_locations({id}, {}, {}, {}, {}, {})\n", self.file, self.start_line, self.start_column, self.end_line, self.end_column)?;
write!(out, "db_locations({id})\n")?;
write!(out, "locations({id}, {}, {}, {}, {}, {})\n", self.file, self.start_line, self.start_column, self.end_line, self.end_column)?;
Ok(())
}
}
@@ -56,7 +58,7 @@ impl TrapEntry for Function {
fn emit<W: Write>(self, id: TrapLabel, out: &mut W) -> std::io::Result<()> {
write!(out, "functions({id}, {})\n", quoted(&self.name))?;
if let Some(ref v) = &self.location {
write!(out, "function_locations({id}, {})\n", v)?;
write!(out, "locatable_locations({id}, {})\n", v)?;
}
Ok(())
}
@@ -77,7 +79,7 @@ impl TrapEntry for Module {
fn emit<W: Write>(self, id: TrapLabel, out: &mut W) -> std::io::Result<()> {
write!(out, "modules({id})\n")?;
if let Some(ref v) = &self.location {
write!(out, "module_locations({id}, {})\n", v)?;
write!(out, "locatable_locations({id}, {})\n", v)?;
}
for (i, &ref v) in self.declarations.iter().enumerate() {
write!(out, "module_declarations({id}, {}, {})\n", i, v)?;

View File

@@ -1,5 +1,3 @@
#![feature(path_add_extension)]
use std::fs;
use std::path::{PathBuf, Path};
use ra_ap_project_model::CargoConfig;

View File

@@ -1,3 +1,4 @@
use std::ffi::OsString;
use std::fmt::{Debug, Display, Formatter};
use std::fs::File;
use std::io::Write;
@@ -158,7 +159,11 @@ impl TrapFileProvider {
pub fn create(&self, category: &str, key: &Path) -> std::io::Result<TrapFile> {
let mut path = PathBuf::from(category);
path.push(path::key(key));
path.add_extension("trap");
path.set_extension(path.extension().map(|e| {
let mut o : OsString = e.to_owned();
o.push(".trap");
o
}).unwrap_or("trap".into()));
let trap_name = String::from(path.to_string_lossy());
debug!("creating trap file {}", trap_name);
path = self.trap_dir.join(path);

View File

@@ -0,0 +1,4 @@
<dbstats>
<typesizes />
<stats />
</dbstats>

9
rust/tools/BUILD.bazel Normal file
View File

@@ -0,0 +1,9 @@
load("//misc/bazel:pkg.bzl", "codeql_pkg_files")
codeql_pkg_files(
name = "tools",
exes = [
"index.sh",
],
visibility = ["//rust:__pkg__"],
)

17
rust/tools/index.sh Executable file
View File

@@ -0,0 +1,17 @@
#!/bin/bash
# TODO move this to rust code
inputs=($(find -name Cargo.toml))
if [ "${#inputs}" -eq 0 ]; then
inputs=($(find -name rust-project.json))
if [ "${#inputs}" -eq 0 ]; then
inputs=($(find -name '*.rs'))
if [ "${#inputs}" -eq 0 ]; then
echo "no source files found" >&2
exit 1
fi
fi
fi
exec "$CODEQL_EXTRACTOR_RUST_ROOT/tools/$CODEQL_PLATFORM/extractor" "${inputs[@]}"