Skip to content

Commit

Permalink
Don't use pngcrush on macOS (bazelbuild#2361)
Browse files Browse the repository at this point in the history
Xcode uses the `copypng` script to post-process PNGs on most platforms,
but not on macOS. This is enabled incorrectly on macOS, which can make
PNGs appear "corrupt" to some libraries on this platform as the use of
Foundation is not guaranteed. This fixes this by making the behavior
match that of `copypng`, replacing `pngcrush` in favor of a simple copy
(via a symlink action) on macOS.
  • Loading branch information
maxbelanger authored Jan 23, 2024
1 parent 3853ba1 commit ed2a4dd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
62 changes: 37 additions & 25 deletions apple/internal/resource_actions/png.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,41 @@ def copy_png(*, actions, input_file, output_file, platform_prerequisites):
platform_prerequisites: Struct containing information on the platform being targeted.
"""

# Xcode uses `xcrun copypng -strip-PNG-text -compress IN OUT`. But pngcrush
# is a perl script that doesn't properly handle when the process dies via a
# signal, so instead just expand out the comment to skip the script and
# directly run Xcode's copy of pngcrush with the same args.
apple_support.run(
actions = actions,
apple_fragment = platform_prerequisites.apple_fragment,
arguments = [
"pngcrush",
# -compress expands to:
"-q",
"-iphone",
"-f",
"0",
# "-strip-PNG-text",
"-rem",
"text",
input_file.path,
output_file.path,
],
executable = "/usr/bin/xcrun",
inputs = [input_file],
mnemonic = "CopyPng",
outputs = [output_file],
xcode_config = platform_prerequisites.xcode_version_config,
# Xcode does not use `pngcrush` on macOS, but allow override using a feature.
should_compress_png = (
platform_prerequisites.platform_type != apple_common.platform_type.macos or
"apple.macos_compress_png_files" in platform_prerequisites.features
)

if should_compress_png:
# Xcode uses `xcrun copypng -strip-PNG-text -compress IN OUT`. But pngcrush
# is a perl script that doesn't properly handle when the process dies via a
# signal, so instead just expand out the comment to skip the script and
# directly run Xcode's copy of pngcrush with the same args.
apple_support.run(
actions = actions,
apple_fragment = platform_prerequisites.apple_fragment,
arguments = [
"pngcrush",
# -compress expands to:
"-q",
"-iphone",
"-f",
"0",
# "-strip-PNG-text",
"-rem",
"text",
input_file.path,
output_file.path,
],
executable = "/usr/bin/xcrun",
inputs = [input_file],
mnemonic = "CopyPng",
outputs = [output_file],
xcode_config = platform_prerequisites.xcode_version_config,
)
else:
actions.symlink(
target_file = input_file,
output = output_file,
)
5 changes: 3 additions & 2 deletions doc/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,9 @@ on your use case for the resources.
* `.strings` and `.plist` files. string and generic plist (i.e. non Info.plist
files) files are processed using plutil to convert them into binary format,
to reduce their size.
* `.png` files: PNG files are processed using copypng to optimize them for iOS
devices.
* `.png` files: PNG files are processed using `copypng` to optimize them for
iOS devices. This is disabled by default on macOS, but can be enabled with
the `apple.macos_compress_png_files` feature.
* `.xib` files: XIB files are processed using `ibtoold`.
* `.xcdatamodel` and `.xcmappingmodel` files: These files are processed with
`momc` and `mapc` respectively.
Expand Down

0 comments on commit ed2a4dd

Please sign in to comment.