-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Extract proguard logic to new rules #29641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
katre
wants to merge
5
commits into
bazelbuild:master
Choose a base branch
from
katre:push-syywlltpvxtx
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+201
−41
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e5593aa
Extract proguard logic to new rules.
katre 6689348
Use `ctx.action.args`
katre 3ef14b9
Combine proguard and timestamp fixes into a single wrapper python
katre c5a28f8
Fix stderr output
katre e1b868d
Attempt to fix runfiles lookup for windows
katre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| load("@rules_java//java:defs.bzl", "java_binary") | ||
| load("@rules_python//python:py_binary.bzl", "py_binary") | ||
|
|
||
| licenses(["notice"]) # Apache 2.0 | ||
|
|
||
| filegroup( | ||
| name = "srcs", | ||
| srcs = glob(["**"]), | ||
| visibility = ["//tools:__subpackages__"], | ||
| ) | ||
|
|
||
| exports_files( | ||
| ["proguard.bzl"], | ||
| visibility = ["//visibility:public"], | ||
| ) | ||
|
|
||
| # Utility used to run proguard and fix timestamps. | ||
| py_binary( | ||
| name = "wrapper_private", | ||
| srcs = ["wrapper.py"], | ||
| data = [ | ||
| ":proguard_bin", | ||
| ], | ||
| main = "wrapper.py", | ||
| visibility = ["//visibility:private"], | ||
| deps = [ | ||
| "@rules_python//python/runfiles", | ||
| ], | ||
| ) | ||
|
|
||
| java_binary( | ||
| name = "proguard_bin", | ||
| jvm_flags = [ | ||
| # Prevent ProGuard from calling out to the internet through log4j. | ||
| "-Dlog4j.rootLogger=OFF", | ||
| ], | ||
| main_class = "proguard.ProGuard", | ||
| runtime_deps = ["@maven//:com_guardsquare_proguard_base"], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| """Apply proguard rules to a JAR file.""" | ||
|
|
||
| def _proguard_jar_impl(ctx): | ||
| inputs = ctx.files.srcs + ctx.files.deps + [ctx.file.proguard_spec] | ||
| output = ctx.outputs.out | ||
|
|
||
| args = ctx.actions.args() | ||
| args.add_joined("--srcs", ctx.files.srcs, join_with = ",") | ||
| args.add_joined("--deps", ctx.files.deps, join_with = ",") | ||
| args.add("--proguard_spec", ctx.file.proguard_spec) | ||
| args.add("--output", output) | ||
| args.add("--timestamp", "1980-01-01 00:00:00") | ||
|
|
||
| ctx.actions.run( | ||
| inputs = inputs, | ||
| mnemonic = "ProguardJar", | ||
| outputs = [output], | ||
| executable = ctx.executable._wrapper, | ||
| arguments = [args], | ||
| ) | ||
|
|
||
| return DefaultInfo(files = depset([output])) | ||
|
|
||
| proguard_jar = rule( | ||
| implementation = _proguard_jar_impl, | ||
| attrs = { | ||
| "srcs": attr.label_list(), | ||
| "deps": attr.label_list(), | ||
| "proguard_spec": attr.label(allow_single_file = True), | ||
| "out": attr.output(), | ||
| "_wrapper": attr.label( | ||
| cfg = "exec", | ||
| default = ":wrapper_private", | ||
| executable = True, | ||
| ), | ||
| }, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| # Copyright 2026 The Bazel Authors. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import argparse | ||
| import datetime | ||
| import os | ||
| import platform | ||
| import subprocess | ||
| import tempfile | ||
| import zipfile | ||
|
|
||
| from python.runfiles import runfiles | ||
|
|
||
| PROGUARD_BIN_PATH = "_main/tools/build_defs/proguard/proguard_bin" | ||
|
|
||
| def lookup_binary(path): | ||
| if platform.system() == "Windows": | ||
| path = path + ".exe" | ||
| rf = runfiles.Create() | ||
| binary = rf.Rlocation(path) | ||
| if not binary: | ||
| raise Exception(f"Runfiles failed to resolve {path}") | ||
| elif not os.path.exists(binary): | ||
| raise Exception(f"Runfiles resolved {path} to {binary} but the file does not exist") | ||
| return binary | ||
|
|
||
| def apply_proguard(srcs, deps, proguard_spec, output_jar): | ||
|
|
||
| proguard_binary = lookup_binary(PROGUARD_BIN_PATH) | ||
|
|
||
| command = [ | ||
| proguard_binary, | ||
| "-injars", srcs, | ||
| "-libraryjars", deps, | ||
| "-outjars", output_jar, | ||
| "@" + proguard_spec | ||
| ] | ||
|
|
||
| #print("Running proguard: %s" % " ".join(command)) | ||
| p = subprocess.run(command, capture_output=True) | ||
|
|
||
| if p.returncode != 0: | ||
| message = f"Proguard failed ({p.returncode})" | ||
| stdout = p.stdout.decode() | ||
| if stdout: | ||
| message += f"\n stdout:\n{stdout}" | ||
| stderr = p.stderr.decode() | ||
| if stderr: | ||
| message += f"\n stderr:\n{stderr}" | ||
| raise Exception(message) | ||
|
|
||
| def reset_timestamps(input, output, timestamp): | ||
| #print("Resetting timestamps in %s to %s, writing to %s" % (input, timestamp, output)) | ||
|
|
||
| with zipfile.ZipFile(input, mode="r") as src: | ||
| with zipfile.ZipFile(output, mode="w") as dest: | ||
| for info in src.infolist(): | ||
| #print(f"Filename: {info.filename}") | ||
| #print(f" Modified: {datetime.datetime(*info.date_time)}") | ||
| data = src.read(info) | ||
| info.date_time = timestamp.timetuple()[:6] | ||
| dest.writestr(info, data) | ||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser( | ||
| description="Resets timestamps in ZIP files", fromfile_prefix_chars="@" | ||
| ) | ||
| parser.add_argument( | ||
| "--srcs", | ||
| required=True, | ||
| help="Input jar files, mandatory." | ||
| ) | ||
| parser.add_argument( | ||
| "--deps", | ||
| default=[], | ||
| help="Library jar files, optional." | ||
| ) | ||
| parser.add_argument( | ||
| "--proguard_spec", | ||
| required=True, | ||
| help="Proguard spec file, mandatory." | ||
| ) | ||
| parser.add_argument( | ||
| "--output", | ||
| required=True, | ||
| help="The output file, mandatory." | ||
| ) | ||
| parser.add_argument( | ||
| "--timestamp", | ||
| default = "1980-01-01 00:00:00", | ||
| type=datetime.datetime.fromisoformat, | ||
| help = "The timestamp (in ISO format) to set all files to.", | ||
| ) | ||
| opts = parser.parse_args() | ||
|
|
||
| with tempfile.TemporaryDirectory() as wdir: | ||
| output_jar = os.path.join(wdir, "stripped.jar") | ||
| apply_proguard(opts.srcs, opts.deps, opts.proguard_spec, output_jar) | ||
| reset_timestamps(output_jar, opts.output, opts.timestamp) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The path will likely end with
.exeon Windows. In any case it's better to pass this in from location expansion of$(rlocationpath ...)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed this in a dumb way, but it works.
I couldn't get the
rlocationpathsolution to work: anyargson thepy_binaryseem to be swallowed up by the actualctx.actions.argscreated in the action.