From 41a9d93d9d056cd997edbe492995a36e5566423c Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Mon, 29 Nov 2021 04:45:57 +0800 Subject: [PATCH] support m1 devices (#149) as title fix #147 fix #119 Co-authored-by: Matti R Reviewed-on: https://gitea.com/gitea/homebrew-gitea/pulls/149 Reviewed-by: Andrew Thornton Reviewed-by: John Olheiser Co-authored-by: techknowlogick Co-committed-by: techknowlogick --- changelog.rb | 4 +++- gitea-head.rb | 4 +++- gitea.rb | 3 +++ tea-head.rb | 6 ++++-- tea.rb | 4 +++- utils/macos_codesign.rb | 35 +++++++++++++++++++++++++++++++++++ 6 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 utils/macos_codesign.rb diff --git a/changelog.rb b/changelog.rb index 310bccf..7ea6366 100644 --- a/changelog.rb +++ b/changelog.rb @@ -1,3 +1,5 @@ +require './utils/macos_codesign.rb' + class Changelog < Formula desc "Generate changelog of gitea repository" homepage "https://gitea.com/gitea/changelog" @@ -28,13 +30,13 @@ class Changelog < Formula url @@url, using: @@using - bottle :unneeded def install if stable.using.blank? filename = Changelog.class_variable_get("@@filename") else filename = downloader.cached_location end + apply_ad_hoc_signature(filename) bin.install filename => "changelog" end diff --git a/gitea-head.rb b/gitea-head.rb index 90287f4..531feac 100644 --- a/gitea-head.rb +++ b/gitea-head.rb @@ -1,3 +1,5 @@ +require './utils/macos_codesign.rb' + class GiteaHead < Formula desc "Git with a cup of tea, painless self-hosted git service" homepage "https://gitea.io" @@ -29,13 +31,13 @@ class GiteaHead < Formula using: @@using conflicts_with "gitea", because: "both install gitea binaries" - bottle :unneeded def install if stable.using.blank? filename = GiteaHead.class_variable_get("@@filename") else filename = downloader.cached_location end + apply_ad_hoc_signature(filename) bin.install filename => "gitea" end diff --git a/gitea.rb b/gitea.rb index 3d1caa8..42157db 100644 --- a/gitea.rb +++ b/gitea.rb @@ -1,3 +1,5 @@ +require './utils/macos_codesign.rb' + class Gitea < Formula desc "Git with a cup of tea, painless self-hosted git service" homepage "https://gitea.io" @@ -43,6 +45,7 @@ class Gitea < Formula else filename = downloader.cached_location end + apply_ad_hoc_signature(filename) bin.install filename => "gitea" end diff --git a/tea-head.rb b/tea-head.rb index 7b26889..753f6b2 100644 --- a/tea-head.rb +++ b/tea-head.rb @@ -1,3 +1,5 @@ +require './utils/macos_codesign.rb' + class TeaHead < Formula desc "A command line tool to interact with Gitea servers" homepage "https://gitea.com/gitea/tea" @@ -29,13 +31,13 @@ class TeaHead < Formula using: @@using conflicts_with "tea", because: "both install tea binaries" - bottle :unneeded def install if stable.using.blank? filename = TeaHead.class_variable_get("@@filename") else filename = downloader.cached_location - end + end + apply_ad_hoc_signature(filename) bin.install filename => "tea" end diff --git a/tea.rb b/tea.rb index 45b4732..e02cff0 100644 --- a/tea.rb +++ b/tea.rb @@ -1,3 +1,5 @@ +require './utils/macos_codesign.rb' + class Tea < Formula desc "A command line tool to interact with Gitea servers" homepage "https://gitea.com/gitea/tea" @@ -37,13 +39,13 @@ class Tea < Formula using: @@using conflicts_with "tea-head", because: "both install tea binaries" - bottle :unneeded def install if stable.using.blank? filename = Tea.class_variable_get("@@filename") else filename = downloader.cached_location end + apply_ad_hoc_signature(filename) bin.install filename => "tea" end diff --git a/utils/macos_codesign.rb b/utils/macos_codesign.rb new file mode 100644 index 0000000..858ea90 --- /dev/null +++ b/utils/macos_codesign.rb @@ -0,0 +1,35 @@ +## this file is licensed under BSD-2-Clause License (https://github.com/Homebrew/brew/blob/4665bb8e663c6d0ef452f4c124742732c96f8fd6/LICENSE.txt) +## source: https://github.com/homebrew/brew/blob/4665bb8e663c6d0ef452f4c124742732c96f8fd6/Library/Homebrew/os/mac/keg.rb + +def apply_ad_hoc_signature(file) + return if MacOS.version < :big_sur + return unless Hardware::CPU.arm? + + odebug "Codesigning #{file}" + # Use quiet_system to squash notifications about resigning binaries + # which already have valid signatures. + return if quiet_system("codesign", "--sign", "-", "--force", + "--preserve-metadata=entitlements,requirements,flags,runtime", + file) + + # If the codesigning fails, it may be a bug in Apple's codesign utility + # A known workaround is to copy the file to another inode, then move it back + # erasing the previous file. Then sign again. + # + # TODO: remove this once the bug in Apple's codesign utility is fixed + Dir::Tmpname.create("workaround") do |tmppath| + FileUtils.cp file, tmppath + FileUtils.mv tmppath, file, force: true + end + + # Try signing again + odebug "Codesigning (2nd try) #{file}" + return if quiet_system("codesign", "--sign", "-", "--force", + "--preserve-metadata=entitlements,requirements,flags,runtime", + file) + + # If it fails again, error out + onoe <<~EOS + Failed applying an ad-hoc signature to #{file} + EOS +end \ No newline at end of file