Skip to content

Commit

Permalink
Extract libiconv also for macOS
Browse files Browse the repository at this point in the history
Since Homebrew's libiconv does not come with a pkgconfig file, cannot
rely on `--wildcards` for extraction.

Instead extract the full package and then copy only the found .a and .pc
files.
  • Loading branch information
luislavena committed Aug 22, 2024
1 parent dbb80cb commit ff43afa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
7 changes: 3 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,14 @@ RUN --mount=type=cache,sharing=private,target=/var/cache/apk \
# macOS (Monterey), supports only Apple Silicon (aarch64/arm64)
{ \
pkg_path="/opt/multiarch-libs/aarch64-apple-darwin"; \
mkdir -p $pkg_path/lib/pkgconfig; \
# run homebrew-downloader
crystal run /homebrew-downloader.cr -- \
$pkg_path \
gmp \
libevent \
libgc \
libiconv \
libyaml \
openssl \
openssl@3 \
pcre2 \
sqlite \
zlib \
Expand All @@ -114,7 +113,7 @@ RUN --mount=type=cache,sharing=private,target=/var/cache/apk \

# copy macOS dependencies back into `base`
FROM base
COPY --from=macos-packages --chmod=0444 /opt/multiarch-libs/aarch64-apple-darwin /opt/multiarch-libs/aarch64-apple-darwin
COPY --from=macos-packages /opt/multiarch-libs/aarch64-apple-darwin /opt/multiarch-libs/aarch64-apple-darwin

# install macOS SDK
RUN --mount=type=cache,sharing=private,target=/var/cache/apk \
Expand Down
22 changes: 19 additions & 3 deletions scripts/homebrew-downloader.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "file_utils"
require "http/client"
require "json"
require "log"
Expand Down Expand Up @@ -53,7 +54,9 @@ def main(argv = ARGV, log = Log)

# ensure directory exists
log.debug &.emit("Creating target directory", target_dir: target_dir)
Dir.mkdir_p(target_dir)
target_dir_lib = File.join(target_dir, "lib")
target_dir_pkgconfig = File.join(target_dir_lib, "pkgconfig")
Dir.mkdir_p(target_dir_pkgconfig, 0o755)

packages = argv[0..-1].to_set
if packages.empty?
Expand Down Expand Up @@ -92,16 +95,29 @@ def main(argv = ARGV, log = Log)
temp_io.flush
log.debug &.emit("File downloaded", path: temp_io.path, size: temp_io.size)

# extract temporary only .a and .pc from .tar.gz in `target_dir`
status = Process.run("tar", {"-xf", temp_io.path, "-C", target_dir, "--strip-components=2", "--wildcards", "--no-anchored", "*.a", "*.pc"})
temp_dir = File.tempname
Dir.mkdir_p(temp_dir, 0o755)

log.debug &.emit("Extracting package", path: temp_io.path, target: temp_dir)
status = Process.run("tar", {"-xf", temp_io.path, "-C", temp_dir, "--strip-components=2"})
unless status.success?
log.error &.emit("Unable to extract package", name: entry.name, version: entry.version, exit_status: status.exit_status)
exit 1
end

Dir.glob(File.join(temp_dir, "**/*.a")).each do |source|
FileUtils.cp(source, target_dir_lib)
end

Dir.glob(File.join(temp_dir, "**/*.pc")).each do |source|
FileUtils.cp(source, target_dir_pkgconfig)
end

log.info &.emit("Extracted files (.a, .pc) from package", name: entry.name, version: entry.version)
ensure
log.debug &.emit("Cleanup after package", name: entry.name, version: entry.version)
temp_io.delete if temp_io
FileUtils.rm_rf(temp_dir) if temp_dir
end
end

Expand Down

0 comments on commit ff43afa

Please sign in to comment.