Skip to content

Commit

Permalink
Swift: first skeleton extractor
Browse files Browse the repository at this point in the history
This adds a first dummy extractor for swift.

Running `bazel run //swift:install` will create an `extractor_pack`
directory in `swift`. From that moment providing `--search-path=swift`
will pick up the extractor.
  • Loading branch information
redsun82 committed Apr 12, 2022
1 parent a43f3a2 commit 95dbf2d
Show file tree
Hide file tree
Showing 20 changed files with 216 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build --copt="-std=c++17"

try-import %workspace%/local.bazelrc
Empty file added BUILD.bazel
Empty file.
43 changes: 43 additions & 0 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -1,2 +1,45 @@
# Please notice that any bazel targets and definitions in this repository are currently experimental
# and for internal use only.

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",
],
)

http_archive(
name = "bazel_skylib",
sha256 = "c6966ec828da198c5d9adbaa94c05e3a1c7f21bd012a0b29ba8ddbccb2c93b0d",
urls = [
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz",
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz",
],
)

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()

load("@ql//:defs.bzl", "ql_utils")

ql_utils(name = "utils")
6 changes: 3 additions & 3 deletions cpp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ load("@rules_pkg//:mappings.bzl", "pkg_filegroup")

alias(
name = "dbscheme",
actual = "//cpp/ql/lib:dbscheme",
actual = "@ql//cpp/ql/lib:dbscheme",
)

pkg_filegroup(
name = "db-files",
srcs = [
":dbscheme",
"//cpp/downgrades",
"//cpp/ql/lib:dbscheme-stats",
"@ql//cpp/downgrades",
"@ql//cpp/ql/lib:dbscheme-stats",
],
)
19 changes: 19 additions & 0 deletions defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
codeql_platform = select({
"@platforms//os:linux": "linux64",
"@platforms//os:macos": "osx64",
"@platforms//os:windows": "win64",
})

_paths_bzl = """
def source_dir():
return '%s/' + native.package_name()
"""

def _ql_utils_impl(repository_ctx):
root = repository_ctx.path(Label("@ql//:WORKSPACE.bazel")).realpath.dirname
repository_ctx.file("BUILD.bazel")
repository_ctx.file("paths.bzl", content = _paths_bzl % root)

ql_utils = repository_rule(
implementation = _ql_utils_impl,
)
7 changes: 7 additions & 0 deletions swift/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
BasedOnStyle: Chromium
ColumnLimit: 100
IndentWidth: 2
SortIncludes: false
AllowShortIfStatementsOnASingleLine: WithoutElse
AlwaysBreakBeforeMultilineStrings: false
Standard: c++17
7 changes: 7 additions & 0 deletions swift/.codeqlmanifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"provide": [
"ql/lib/qlpack.yml",
"ql/test/qlpack.yml",
"extractor_pack/codeql-extractor.yml"
]
}
1 change: 1 addition & 0 deletions swift/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extractor_pack
66 changes: 66 additions & 0 deletions swift/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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("@bazel_skylib//rules:native_binary.bzl", "native_binary")
load("@utils//:paths.bzl", "source_dir")

pkg_files(
name = "dbscheme",
srcs = [
"ql/lib/swift.dbscheme",
"ql/lib/swift.dbscheme.stats",
],
)

pkg_files(
name = "qltest",
srcs = ["tools/qltest.sh"],
attributes = pkg_attributes(mode = "0755"),
prefix = "tools",
)

pkg_files(
name = "manifest",
srcs = ["codeql-extractor.yml"],
)

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

pkg_files(
name = "extractor",
srcs = ["//swift/extractor"],
attributes = pkg_attributes(mode = "0755"),
prefix = "tools/" + codeql_platform,
)

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

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

pkg_install(
name = "install",
srcs = [":extractor-pack"],
args = [
"--destdir",
source_dir() + "/extractor_pack",
],
)
8 changes: 8 additions & 0 deletions swift/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Warning

The Swift codeql package is an experimental and unsupported work in progress.

## Usage

Run `bazel run //swift:install-extractor`, which will install `swift/extractor_pack`. Using `--search-path=swift` will
then pick up the Swift extractor.
10 changes: 10 additions & 0 deletions swift/codeql-extractor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: "swift"
display_name: "Swift"
version: 0.0.1
column_kind: "utf8"
legacy_qltest_extraction: true
file_types:
- name: swift
display_name: Swift files
extensions:
- .swift
5 changes: 5 additions & 0 deletions swift/extractor/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cc_binary(
name = "extractor",
srcs = ["main.cpp"],
visibility = ["//swift:__pkg__"],
)
14 changes: 14 additions & 0 deletions swift/extractor/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <fstream>
#include <iomanip>
#include <stdlib.h>

int main() {
if (auto trapDir = getenv("CODEQL_EXTRACTOR_SWIFT_TRAP_DIR")) {
std::string file = trapDir;
file += "/my_first.trap";
if (std::ofstream out{file}) {
out << "answer_to_life_the_universe_and_everything(42)\n";
}
}
return 0;
}
5 changes: 5 additions & 0 deletions swift/ql/lib/qlpack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: codeql/swift-all
version: 0.0.0
dbscheme: swift.dbscheme
extractor: swift
library: true
7 changes: 7 additions & 0 deletions swift/ql/lib/swift.dbscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
sourceLocationPrefix(
string prefix: string ref
);

answer_to_life_the_universe_and_everything(
int answer: int ref
)
4 changes: 4 additions & 0 deletions swift/ql/lib/swift.dbscheme.stats
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<dbstats>
<typesizes />
<stats />
</dbstats>
1 change: 1 addition & 0 deletions swift/ql/test/answer.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| 42 |
3 changes: 3 additions & 0 deletions swift/ql/test/answer.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from int answer
where answer_to_life_the_universe_and_everything(answer)
select answer
5 changes: 5 additions & 0 deletions swift/ql/test/qlpack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: codeql-swift-tests
version: 0.0.0
libraryPathDependencies:
- codeql/swift-all
extractor: swift
5 changes: 5 additions & 0 deletions swift/tools/qltest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

mkdir -p "$CODEQL_EXTRACTOR_SWIFT_TRAP_DIR"

exec "$CODEQL_EXTRACTOR_SWIFT_ROOT/tools/$CODEQL_PLATFORM/extractor"

0 comments on commit 95dbf2d

Please sign in to comment.