Class: PngCheck::Recipe

Inherits:
MiniPortile
  • Object
show all
Defined in:
lib/pngcheck/recipe.rb

Constant Summary collapse

COMMON_FLAGS =
"-shared -fPIC -Wall -O -DUSE_ZLIB"

Instance Method Summary collapse

Constructor Details

#initializeRecipe

Returns a new instance of Recipe.



35
36
37
38
39
40
41
# File 'lib/pngcheck/recipe.rb', line 35

def initialize
  super("pngcheck", PNGCHECK_VER)
  files_to_load_all
  files_to_load_cross
  @target = ROOT.join(@target).to_s
  @printed = {}
end

Instance Method Details

#ccObject



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/pngcheck/recipe.rb', line 184

def cc
  @cc ||=
    if target_platform.eql?(host_platform)
      "gcc"
    else
      case target_platform
      when "aarch64-linux"
        "aarch64-linux-gnu-gcc"
      when "arm64-darwin"
        "gcc -target arm64-apple-macos11"
      else
        "gcc"
      end
    end
end

#cflagsObject



200
201
202
203
204
205
206
207
208
# File 'lib/pngcheck/recipe.rb', line 200

def cflags
  @cflags ||=
    if target_platform.eql?("aarch64-linux") &&
        !host_platform.eql?("aarch64-linux")
      "-I./usr/include -L./usr/lib/#{target_platform}-gnu"
    else
      ""
    end
end

#checkpointObject



82
83
84
# File 'lib/pngcheck/recipe.rb', line 82

def checkpoint
  File.join(@target, "#{name}-#{version}-#{host}.installed")
end

#configureObject



86
87
88
89
90
91
92
# File 'lib/pngcheck/recipe.rb', line 86

def configure
  FileUtils.cp(ROOT.join("ext", "wrapper.c"), work_path, verbose: false)
  if target_platform.eql?("aarch64-linux") &&
      !host_platform.eql?("aarch64-linux")
    extract_file("#{work_path}/../data.tar.xz", work_path.to_s)
  end
end

#cookObject



64
65
66
67
# File 'lib/pngcheck/recipe.rb', line 64

def cook
  super
  FileUtils.touch(checkpoint)
end

#cook_if_notObject



60
61
62
# File 'lib/pngcheck/recipe.rb', line 60

def cook_if_not
  cook unless File.exist?(checkpoint)
end

#download_file_file(uri, full_path) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/pngcheck/recipe.rb', line 69

def download_file_file(uri, full_path)
  if MiniPortile.windows?
    FileUtils.mkdir_p File.dirname(full_path)
    FileUtils.cp uri.to_s.delete_prefix("file:"), full_path
  else
    super
  end
end

#execute(action, command, command_opts = {}) ⇒ Object



117
118
119
# File 'lib/pngcheck/recipe.rb', line 117

def execute(action, command, command_opts = {})
  super(action, command, command_opts.merge(debug: false))
end

#files_to_load_allObject



18
19
20
21
22
23
# File 'lib/pngcheck/recipe.rb', line 18

def files_to_load_all
  @files << {
    url: "file:#{ROOT}/#{PNGCHECK_LOCAL}",
    sha256: "f8ff6033dc0008a90a468213a96ac5d72476b1e59378e267341fcf3734ebb2b3", # rubocop:disable Layout/LineLength
  }
end

#files_to_load_crossObject



25
26
27
28
29
30
31
32
33
# File 'lib/pngcheck/recipe.rb', line 25

def files_to_load_cross
  if target_platform.eql?("aarch64-linux") &&
      !host_platform.eql?("aarch64-linux")
    @files << {
      url: "http://ports.ubuntu.com/pool/main/z/zlib/zlib1g-dev_1.2.11.dfsg-2ubuntu1.3_arm64.deb", # rubocop:disable Layout/LineLength
      sha256: "0ebadc1ff2a70f0958d4e8e21ffa97d9fa4da23555eaae87782e963044a26fcf", # rubocop:disable Layout/LineLength
    }
  end
end

#host_platformObject

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/CyclomaticComplexity



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/pngcheck/recipe.rb', line 134

def host_platform
  @host_platform ||=
    case @host
    when /\Ax86_64-w64-mingw32/
      "x64-mingw32"
    when /\Ax86_64-w64-mingw-ucrt/
      "x64-mingw-ucrt"
    when /\A(arm64|aarch64).*linux/
      "aarch64-linux"
    when /\Ax86_64.*(darwin|macos|osx)/
      "x86_64-darwin"
    when /\A(arm64|aarch64).*(darwin|macos|osx)/
      "arm64-darwin"
    else
      @host
    end
end

#installObject



109
110
111
112
113
114
115
# File 'lib/pngcheck/recipe.rb', line 109

def install
  libs = Dir.glob(File.join(work_path, "*"))
    .grep(%r{/(?:lib)?[a-zA-Z0-9\-]+\.(?:so|dylib|dll)$})

  FileUtils.cp_r(libs, ROOT.join("lib", "pngcheck"), verbose: false)
  verify_lib
end

#lib_filenameObject



43
44
45
46
47
48
49
50
# File 'lib/pngcheck/recipe.rb', line 43

def lib_filename
  @lib_filename ||=
    if MiniPortile.windows?
      "pngcheck.dll"
    else
      "pngcheck.so"
    end
end

#lib_workpathObject



52
53
54
# File 'lib/pngcheck/recipe.rb', line 52

def lib_workpath
  @lib_workpath ||= File.join(work_path, lib_filename)
end

#make_cmdObject



56
57
58
# File 'lib/pngcheck/recipe.rb', line 56

def make_cmd
  "#{cc} #{cflags} #{COMMON_FLAGS} -o #{lib_filename} wrapper.c -lz"
end

#message(text) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/pngcheck/recipe.rb', line 121

def message(text)
  return super unless text.start_with?("\rDownloading")

  match = text.match(/(\rDownloading .*)\(\s*\d+%\)/)
  pattern = match ? match[1] : text
  return if @printed[pattern]

  @printed[pattern] = true
  super
end

#target_formatObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/pngcheck/recipe.rb', line 166

def target_format
  @target_format ||=
    case target_platform
    when "arm64-darwin"
      "Mach-O 64-bit dynamically linked shared library arm64"
    when "x86_64-darwin"
      "Mach-O 64-bit dynamically linked shared library x86_64"
    when "aarch64-linux"
      "ELF 64-bit LSB shared object, ARM aarch64"
    when /\Ax86_64.*linux.*/
      "ELF 64-bit LSB shared object, x86-64"
    when /\Ax64-mingw(32|-ucrt)/
      "PE32+ executable (DLL) (console) x86-64, for MS Windows"
    else
      "skip"
    end
end

#target_platformObject



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/pngcheck/recipe.rb', line 152

def target_platform
  @target_platform ||=
    case ENV.fetch("target_platform", nil)
    when /\A(arm64|aarch64).*(darwin|macos|osx)/
      "arm64-darwin"
    when /\Ax86_64.*(darwin|macos|osx)/
      "x86_64-darwin"
    when /\A(arm64|aarch64).*linux/
      "aarch64-linux"
    else
      ENV.fetch("target_platform", host_platform)
    end
end

#tmp_pathObject



78
79
80
# File 'lib/pngcheck/recipe.rb', line 78

def tmp_path
  "tmp/#{@host}/ports"
end

#verify_libObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/pngcheck/recipe.rb', line 94

def verify_lib
  begin
    out, = Open3.capture2("file #{lib_workpath}")
  rescue StandardError
    message("Failed to call file, skipped library verification ...\n")
    return
  end

  unless out.include?(target_format)
    raise "Invalid file format '#{out.strip}', '#{@target_format}' expected"
  end

  message("Verifying #{lib_workpath} ... OK\n")
end