diff --git a/.bazelrc b/.bazelrc new file mode 100644 index 000000000000..cd6226d01710 --- /dev/null +++ b/.bazelrc @@ -0,0 +1,3 @@ +build --copt="-std=c++17" + +try-import %workspace%/local.bazelrc diff --git a/.bazelversion b/.bazelversion new file mode 100644 index 000000000000..0062ac971805 --- /dev/null +++ b/.bazelversion @@ -0,0 +1 @@ +5.0.0 diff --git a/.gitignore b/.gitignore index b87ee01c3876..33ef1770c959 100644 --- a/.gitignore +++ b/.gitignore @@ -31,5 +31,8 @@ csharp/extractor/Semmle.Extraction.CSharp.Driver/Properties/launchSettings.json # Compiled class file *.class -# links create by bazel +# links created by bazel /bazel-* + +# CLion project files +/.clwb diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index c9357d0f4480..9459b276ff39 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -1,2 +1,12 @@ # Please notice that any bazel targets and definitions in this repository are currently experimental # and for internal use only. + +workspace(name = "ql") + +load("//misc/bazel:workspace.bzl", "ql_workspace") + +ql_workspace() + +load("//misc/bazel:workspace_deps.bzl", "ql_workspace_deps") + +ql_workspace_deps() diff --git a/defs.bzl b/defs.bzl new file mode 100644 index 000000000000..d6748d831761 --- /dev/null +++ b/defs.bzl @@ -0,0 +1,5 @@ +codeql_platform = select({ + "@platforms//os:linux": "linux64", + "@platforms//os:macos": "osx64", + "@platforms//os:windows": "win64", +}) diff --git a/misc/bazel/BUILD.bazel b/misc/bazel/BUILD.bazel new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/misc/bazel/workspace.bzl b/misc/bazel/workspace.bzl new file mode 100644 index 000000000000..b306363d7c09 --- /dev/null +++ b/misc/bazel/workspace.bzl @@ -0,0 +1,23 @@ +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") + +def ql_workspace(): + 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 000000000000..a082b4b1cb7e --- /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/.clang-format b/swift/.clang-format new file mode 100644 index 000000000000..96d2febc1c2c --- /dev/null +++ b/swift/.clang-format @@ -0,0 +1,7 @@ +BasedOnStyle: Chromium +ColumnLimit: 100 +IndentWidth: 2 +SortIncludes: false +AllowShortIfStatementsOnASingleLine: WithoutElse +AlwaysBreakBeforeMultilineStrings: false +Standard: c++17 diff --git a/swift/.codeqlmanifest.json b/swift/.codeqlmanifest.json new file mode 100644 index 000000000000..3b809abfd3b0 --- /dev/null +++ b/swift/.codeqlmanifest.json @@ -0,0 +1,7 @@ +{ + "provide": [ + "ql/lib/qlpack.yml", + "ql/test/qlpack.yml", + "extractor-pack/codeql-extractor.yml" + ] +} diff --git a/swift/.gitignore b/swift/.gitignore new file mode 100644 index 000000000000..52d331603031 --- /dev/null +++ b/swift/.gitignore @@ -0,0 +1 @@ +extractor-pack diff --git a/swift/BUILD.bazel b/swift/BUILD.bazel new file mode 100644 index 000000000000..fd5e5b46de41 --- /dev/null +++ b/swift/BUILD.bazel @@ -0,0 +1,67 @@ +load("@rules_pkg//:mappings.bzl", "pkg_attributes", "pkg_filegroup", "pkg_files") +load("@rules_pkg//:install.bzl", "pkg_install") +load("//:defs.bzl", "codeql_platform") + +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 = "_create_extractor_pack", + srcs = ["//swift:extractor-pack"], +) + +py_binary( + name = "create-extractor-pack", + srcs = ["tools/create_extractor_pack.py"], + main = "tools/create_extractor_pack.py", + deps = [":_create_extractor_pack"], +) diff --git a/swift/README.md b/swift/README.md new file mode 100644 index 000000000000..ef805a8273ba --- /dev/null +++ b/swift/README.md @@ -0,0 +1,9 @@ +## Warning + +The Swift codeql package is an experimental and unsupported work in progress. + +## Usage + +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.json`. diff --git a/swift/codeql-extractor.yml b/swift/codeql-extractor.yml new file mode 100644 index 000000000000..94f464fd9b58 --- /dev/null +++ b/swift/codeql-extractor.yml @@ -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 diff --git a/swift/extractor/BUILD.bazel b/swift/extractor/BUILD.bazel new file mode 100644 index 000000000000..5a87a0b2c150 --- /dev/null +++ b/swift/extractor/BUILD.bazel @@ -0,0 +1,10 @@ +cc_binary( + name = "extractor", + srcs = ["main.cpp"], + target_compatible_with = select({ + "@platforms//os:linux": [], + "@platforms//os:macos": [], + "//conditions:default": ["@platforms//:incompatible"], + }), + visibility = ["//swift:__pkg__"], +) diff --git a/swift/extractor/main.cpp b/swift/extractor/main.cpp new file mode 100644 index 000000000000..949b3fb246bf --- /dev/null +++ b/swift/extractor/main.cpp @@ -0,0 +1,14 @@ +#include +#include +#include + +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; +} diff --git a/swift/ql/lib/qlpack.yml b/swift/ql/lib/qlpack.yml new file mode 100644 index 000000000000..5a6a80d2a594 --- /dev/null +++ b/swift/ql/lib/qlpack.yml @@ -0,0 +1,5 @@ +name: codeql/swift-all +version: 0.0.0 +dbscheme: swift.dbscheme +extractor: swift +library: true diff --git a/swift/ql/lib/swift.dbscheme b/swift/ql/lib/swift.dbscheme new file mode 100644 index 000000000000..3022d88cce04 --- /dev/null +++ b/swift/ql/lib/swift.dbscheme @@ -0,0 +1,7 @@ +sourceLocationPrefix( + string prefix: string ref +); + +answer_to_life_the_universe_and_everything( + int answer: int ref +) diff --git a/swift/ql/lib/swift.dbscheme.stats b/swift/ql/lib/swift.dbscheme.stats new file mode 100644 index 000000000000..9995467e33e5 --- /dev/null +++ b/swift/ql/lib/swift.dbscheme.stats @@ -0,0 +1,4 @@ + + + + diff --git a/swift/ql/test/answer.expected b/swift/ql/test/answer.expected new file mode 100644 index 000000000000..862807c972cc --- /dev/null +++ b/swift/ql/test/answer.expected @@ -0,0 +1 @@ +| 42 | diff --git a/swift/ql/test/answer.ql b/swift/ql/test/answer.ql new file mode 100644 index 000000000000..e8c6ceb7858f --- /dev/null +++ b/swift/ql/test/answer.ql @@ -0,0 +1,3 @@ +from int answer +where answer_to_life_the_universe_and_everything(answer) +select answer diff --git a/swift/ql/test/qlpack.yml b/swift/ql/test/qlpack.yml new file mode 100644 index 000000000000..de89e5c9ef81 --- /dev/null +++ b/swift/ql/test/qlpack.yml @@ -0,0 +1,6 @@ +name: codeql-swift-tests +version: 0.0.0 +dependencies: + codeql/swift-all: "*" +tests: . +extractor: swift diff --git a/swift/tools/create_extractor_pack.py b/swift/tools/create_extractor_pack.py new file mode 100644 index 000000000000..ce86a00ea2d4 --- /dev/null +++ b/swift/tools/create_extractor_pack.py @@ -0,0 +1,16 @@ +import os +import pathlib +import shutil +import sys +from swift._create_extractor_pack_install_script import main + +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 / 'swift' / 'extractor-pack' +shutil.rmtree(dest_dir, ignore_errors=True) +os.environ['DESTDIR'] = str(dest_dir) +main(sys.argv) diff --git a/swift/tools/qltest.sh b/swift/tools/qltest.sh new file mode 100755 index 000000000000..1e9b098aa0e9 --- /dev/null +++ b/swift/tools/qltest.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +mkdir -p "$CODEQL_EXTRACTOR_SWIFT_TRAP_DIR" + +exec "$CODEQL_EXTRACTOR_SWIFT_ROOT/tools/$CODEQL_PLATFORM/extractor"