|
| 1 | +""" |
| 2 | +# Usage overview |
| 3 | +Building the extractor can be done with bazel. If building from the internal repository, it is recommended to use |
| 4 | +`tools/bazel` from there. |
| 5 | +
|
| 6 | +A specific kotlin extractor variant can be built with |
| 7 | +``` |
| 8 | +bazel build @codeql//java/kotlin-extractor:codeql-extractor-kotlin-<variant>-<version> |
| 9 | +``` |
| 10 | +where `<variant>` is either `standalone` or `embeddable`, and `<version>` is one of the supported versions. |
| 11 | +``` |
| 12 | +bazel build @codeql//java/kotlin-extractor |
| 13 | +``` |
| 14 | +will build a default variant: |
| 15 | +* standalone, unless `CODEQL_KOTLIN_SINGLE_VERSION_EMBEDDABLE` is set to true, in which case it will go for embeddable |
| 16 | +* the version will be taken as the last supported version less than the version of the currently installed `kotlinc` |
| 17 | + * if `CODEQL_KOTLIN_SINGLE_VERSION` is set, that will be used instead |
| 18 | + * if `kotlinc` is not installed, `1.9.20-Beta` will be used |
| 19 | +
|
| 20 | +If `kotlinc` is updated, bazel won't be aware of it and will therefore keep the same default version. Possible workarounds for that: |
| 21 | +* `bazel clean` |
| 22 | +* `bazel fetch --force @codeql//java/kotlin-extractor` |
| 23 | +* `bazel fetch --force @codeql_kotlin_defaults//:all` (only from `codeql`) |
| 24 | +
|
| 25 | +If building from the `codeql` repository, `@codeql` can be skipped. |
| 26 | +""" |
| 27 | + |
| 28 | +# This file is used in the `@codeql_kotlin_embeddable` external repo, which means we need to |
| 29 | +# reference explicitly @codeql |
| 30 | +load( |
| 31 | + "@codeql//java/kotlin-extractor:versions.bzl", |
| 32 | + "VERSIONS", |
| 33 | + "get_compatilibity_sources", |
| 34 | + "get_language_version", |
| 35 | + "version_less", |
| 36 | +) |
| 37 | +load("@rules_kotlin//kotlin:core.bzl", "kt_javac_options", "kt_kotlinc_options") |
| 38 | +load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library") |
| 39 | + |
| 40 | +package(default_visibility = ["//java/kotlin-extractor:__subpackages__"]) |
| 41 | + |
| 42 | +_for_embeddable = repo_name().endswith("codeql_kotlin_embeddable") |
| 43 | + |
| 44 | +_common_extractor_name_prefix = "codeql-extractor-kotlin" |
| 45 | + |
| 46 | +_extractor_name_prefix = "%s-%s" % ( |
| 47 | + _common_extractor_name_prefix, |
| 48 | + "embeddable" if _for_embeddable else "standalone", |
| 49 | +) |
| 50 | + |
| 51 | +py_binary( |
| 52 | + name = "generate_dbscheme", |
| 53 | + srcs = ["generate_dbscheme.py"], |
| 54 | +) |
| 55 | + |
| 56 | +_resources = [ |
| 57 | + ( |
| 58 | + r, |
| 59 | + r[len("src/main/resources/"):], |
| 60 | + ) |
| 61 | + for r in glob(["src/main/resources/**"]) |
| 62 | +] |
| 63 | + |
| 64 | +kt_javac_options( |
| 65 | + name = "javac-options", |
| 66 | + release = "8", |
| 67 | +) |
| 68 | + |
| 69 | +[ |
| 70 | + ( |
| 71 | + kt_kotlinc_options( |
| 72 | + name = "kotlinc-options-%s" % v, |
| 73 | + include_stdlibs = "none", |
| 74 | + jvm_target = "1.8", |
| 75 | + language_version = get_language_version(v), |
| 76 | + warn = "error", |
| 77 | + x_optin = [ |
| 78 | + "kotlin.RequiresOptIn", |
| 79 | + "org.jetbrains.kotlin.ir.symbols.%s" % |
| 80 | + ("IrSymbolInternals" if version_less(v, "2.0.0") else "UnsafeDuringIrConstructionAPI"), |
| 81 | + ], |
| 82 | + x_suppress_version_warnings = True, |
| 83 | + ), |
| 84 | + # * extractor.name is different for each version, so we need to put it in different output dirs |
| 85 | + # * in order to put it in `resources`, we need to define `resource_strip_prefix` to strip this version |
| 86 | + # * `resource_strip_prefix` is unique per jar, so we must also put other resources under the same version prefix |
| 87 | + genrule( |
| 88 | + name = "resources-%s" % v, |
| 89 | + srcs = [src for src, _ in _resources], |
| 90 | + outs = [ |
| 91 | + "%s/com/github/codeql/extractor.name" % v, |
| 92 | + ] + [ |
| 93 | + "%s/%s" % (v, target) |
| 94 | + for _, target in _resources |
| 95 | + ], |
| 96 | + cmd = "\n".join([ |
| 97 | + "echo %s-%s > $(RULEDIR)/%s/com/github/codeql/extractor.name" % (_extractor_name_prefix, v, v), |
| 98 | + ] + [ |
| 99 | + "cp $(execpath %s) $(RULEDIR)/%s/%s" % (source, v, target) |
| 100 | + for source, target in _resources |
| 101 | + ]), |
| 102 | + ), |
| 103 | + kt_jvm_library( |
| 104 | + name = "%s-%s" % (_extractor_name_prefix, v), |
| 105 | + srcs = |
| 106 | + ["@codeql//java/kotlin-extractor:generated-dbscheme"] + |
| 107 | + glob( |
| 108 | + [ |
| 109 | + "src/**/*.kt", |
| 110 | + "src/**/*.java", |
| 111 | + ], |
| 112 | + exclude = ["src/main/kotlin/utils/versions/**"], |
| 113 | + ) + get_compatilibity_sources(v, "src/main/kotlin/utils/versions"), |
| 114 | + javac_opts = ":javac-options", |
| 115 | + kotlinc_opts = ":kotlinc-options-%s" % v, |
| 116 | + module_name = "codeql-kotlin-extractor", |
| 117 | + # resource_strip_prefix is very nit-picky: the following makes it work from |
| 118 | + # `codeql`, `@codeql_kotlin_embeddable` and `semmle-code` |
| 119 | + resource_strip_prefix = ( |
| 120 | + ("../%s/" % repo_name() if repo_name() else "") + |
| 121 | + ("%s/" % package_name() if package_name() else "") + |
| 122 | + v |
| 123 | + ), |
| 124 | + resources = [ |
| 125 | + ":resources-%s" % v, |
| 126 | + ], |
| 127 | + visibility = ["//visibility:public"], |
| 128 | + deps = [ |
| 129 | + "@kotlin-compiler%s-%s" % ( |
| 130 | + "-embeddable" if _for_embeddable else "", |
| 131 | + v, |
| 132 | + ), |
| 133 | + "@kotlin-stdlib-%s" % v, |
| 134 | + ], |
| 135 | + ), |
| 136 | + # if in main repository, alias the embeddable versions from the modified @codeql_kotlin_embeddable repo |
| 137 | + alias( |
| 138 | + name = "%s-embeddable-%s" % (_common_extractor_name_prefix, v), |
| 139 | + actual = "@codeql_kotlin_embeddable//:%s-embeddable-%s" % (_common_extractor_name_prefix, v), |
| 140 | + visibility = ["//visibility:public"], |
| 141 | + ) if not _for_embeddable else None, |
| 142 | + ) |
| 143 | + for v in VERSIONS |
| 144 | +] |
| 145 | + |
| 146 | +( |
| 147 | + genrule( |
| 148 | + name = "generated-dbscheme", |
| 149 | + srcs = ["@codeql//java:dbscheme"], |
| 150 | + outs = ["KotlinExtractorDbScheme.kt"], |
| 151 | + cmd = "$(execpath :generate_dbscheme) $< $@", |
| 152 | + tools = [":generate_dbscheme"], |
| 153 | + visibility = ["@codeql_kotlin_embeddable//:__pkg__"], |
| 154 | + ), |
| 155 | + [ |
| 156 | + alias( |
| 157 | + name = n, |
| 158 | + actual = "//java/kotlin-extractor/defaults:%s" % n, |
| 159 | + visibility = ["//visibility:public"], |
| 160 | + ) |
| 161 | + for n in ( |
| 162 | + "%s-standalone" % _common_extractor_name_prefix, |
| 163 | + "%s-embeddable" % _common_extractor_name_prefix, |
| 164 | + _common_extractor_name_prefix, |
| 165 | + ) |
| 166 | + ], |
| 167 | + alias( |
| 168 | + name = "kotlin-extractor", |
| 169 | + actual = _common_extractor_name_prefix, |
| 170 | + visibility = ["//visibility:public"], |
| 171 | + ), |
| 172 | + filegroup( |
| 173 | + name = "many", |
| 174 | + srcs = ["%s-%s-%s" % ( |
| 175 | + _common_extractor_name_prefix, |
| 176 | + variant, |
| 177 | + version, |
| 178 | + ) for variant in ("standalone", "embeddable") for version in VERSIONS], |
| 179 | + visibility = ["//visibility:public"], |
| 180 | + ), |
| 181 | + sh_binary( |
| 182 | + name = "print-default-version", |
| 183 | + srcs = ["//java/kotlin-extractor/defaults:default-version-printer"], |
| 184 | + ), |
| 185 | +) if not _for_embeddable else None |
0 commit comments