Class: MiniPortile

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_portile2/version.rb,
lib/mini_portile2/mini_portile.rb

Direct Known Subclasses

MiniPortileCMake

Constant Summary collapse

VERSION =
"2.8.9"
DEFAULT_TIMEOUT =
10

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, version, **kwargs) ⇒ MiniPortile

Returns a new instance of MiniPortile.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/mini_portile2/mini_portile.rb', line 101

def initialize(name, version, **kwargs)
  @name = name
  @version = version
  @target = 'ports'
  @files = []
  @patch_files = []
  @log_files = {}
  @logger = kwargs[:logger] || STDOUT
  @source_directory = nil

  @cc_command = kwargs[:cc_command] || kwargs[:gcc_command]
  @cxx_command = kwargs[:cxx_command]
  @make_command = kwargs[:make_command]
  @open_timeout = kwargs[:open_timeout] || DEFAULT_TIMEOUT
  @read_timeout = kwargs[:read_timeout] || DEFAULT_TIMEOUT

  @original_host = @host = detect_host
end

Instance Attribute Details

#configure_optionsObject



175
176
177
# File 'lib/mini_portile2/mini_portile.rb', line 175

def configure_options
  @configure_options ||= configure_defaults
end

#filesObject

Returns the value of attribute files.



39
40
41
# File 'lib/mini_portile2/mini_portile.rb', line 39

def files
  @files
end

#hostObject

Returns the value of attribute host.



39
40
41
# File 'lib/mini_portile2/mini_portile.rb', line 39

def host
  @host
end

#loggerObject

Returns the value of attribute logger.



39
40
41
# File 'lib/mini_portile2/mini_portile.rb', line 39

def logger
  @logger
end

#nameObject (readonly)

Returns the value of attribute name.



37
38
39
# File 'lib/mini_portile2/mini_portile.rb', line 37

def name
  @name
end

#original_hostObject (readonly)

Returns the value of attribute original_host.



37
38
39
# File 'lib/mini_portile2/mini_portile.rb', line 37

def original_host
  @original_host
end

#patch_filesObject

Returns the value of attribute patch_files.



39
40
41
# File 'lib/mini_portile2/mini_portile.rb', line 39

def patch_files
  @patch_files
end

#source_directoryObject

Returns the value of attribute source_directory.



37
38
39
# File 'lib/mini_portile2/mini_portile.rb', line 37

def source_directory
  @source_directory
end

#targetObject

Returns the value of attribute target.



39
40
41
# File 'lib/mini_portile2/mini_portile.rb', line 39

def target
  @target
end

#versionObject (readonly)

Returns the value of attribute version.



37
38
39
# File 'lib/mini_portile2/mini_portile.rb', line 37

def version
  @version
end

Class Method Details

.darwin?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/mini_portile2/mini_portile.rb', line 55

def self.darwin?
  target_os =~ /darwin/
end

.freebsd?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/mini_portile2/mini_portile.rb', line 59

def self.freebsd?
  target_os =~ /freebsd/
end

.linux?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/mini_portile2/mini_portile.rb', line 67

def self.linux?
  target_os =~ /linux/
end

.mingw?Boolean

GNU MinGW compiled Ruby?

Returns:

  • (Boolean)


46
47
48
# File 'lib/mini_portile2/mini_portile.rb', line 46

def self.mingw?
  target_os =~ /mingw/
end

.mswin?Boolean

MS Visual-C compiled Ruby?

Returns:

  • (Boolean)


51
52
53
# File 'lib/mini_portile2/mini_portile.rb', line 51

def self.mswin?
  target_os =~ /mswin/
end

.native_path(path) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/mini_portile2/mini_portile.rb', line 83

def self.native_path(path)
  path = File.expand_path(path)
  if File::ALT_SEPARATOR
    path.tr(File::SEPARATOR, File::ALT_SEPARATOR)
  else
    path
  end
end

.openbsd?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/mini_portile2/mini_portile.rb', line 63

def self.openbsd?
  target_os =~ /openbsd/
end

.posix_path(path) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/mini_portile2/mini_portile.rb', line 92

def self.posix_path(path)
  path = File.expand_path(path)
  if File::ALT_SEPARATOR
    "/" + path.tr(File::ALT_SEPARATOR, File::SEPARATOR).tr(":", File::SEPARATOR)
  else
    path
  end
end

.solaris?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/mini_portile2/mini_portile.rb', line 71

def self.solaris?
  target_os =~ /solaris/
end

.target_cpuObject



79
80
81
# File 'lib/mini_portile2/mini_portile.rb', line 79

def self.target_cpu
  RbConfig::CONFIG['target_cpu']
end

.target_osObject



75
76
77
# File 'lib/mini_portile2/mini_portile.rb', line 75

def self.target_os
  RbConfig::CONFIG['target_os']
end

.windows?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/mini_portile2/mini_portile.rb', line 41

def self.windows?
  target_os =~ /mswin|mingw/
end

Instance Method Details

#activateObject



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/mini_portile2/mini_portile.rb', line 244

def activate
  vars = {
    'PATH'          => File.join(port_path, 'bin'),
    'CPATH'         => include_path,
    'LIBRARY_PATH'  => lib_path,
  }.reject { |env, path| !File.directory?(path) }

  output "Activating #{@name} #{@version} (from #{port_path})..."
  vars.each do |var, path|
    full_path = native_path(path)

    # save current variable value
    old_value = ENV[var] || ''

    unless old_value.include?(full_path)
      ENV[var] = "#{full_path}#{File::PATH_SEPARATOR}#{old_value}"
    end
  end

  # rely on LDFLAGS when cross-compiling
  if File.exist?(lib_path) && (@host != @original_host)
    full_path = native_path(lib_path)

    old_value = ENV.fetch("LDFLAGS", "")

    unless old_value.include?(full_path)
      ENV["LDFLAGS"] = "-L#{full_path} #{old_value}".strip
    end
  end
end

#apply_patch(patch_file) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/mini_portile2/mini_portile.rb', line 145

def apply_patch(patch_file)
  (
    # Not a class variable because closures will capture self.
    @apply_patch ||=
    case
    when which('git')
      lambda { |file|
        message "Running git apply with #{file}... "
        Dir.mktmpdir do |tmp_git_dir|
          execute('patch', ["git", "--git-dir=#{tmp_git_dir}", "--work-tree=.", "apply", "--whitespace=warn", file], :initial_message => false)
        end
      }
    when which('patch')
      lambda { |file|
        message "Running patch with #{file}... "
        execute('patch', ["patch", "-p1", "-i", file], :initial_message => false)
      }
    else
      raise "Failed to complete patch task; patch(1) or git(1) is required."
    end
  ).call(patch_file)
end

#cc_cmdObject Also known as: gcc_cmd



376
377
378
# File 'lib/mini_portile2/mini_portile.rb', line 376

def cc_cmd
  (ENV["CC"] || @cc_command || RbConfig::CONFIG["CC"] || "gcc").dup
end

#compileObject



194
195
196
# File 'lib/mini_portile2/mini_portile.rb', line 194

def compile
  execute('compile', make_cmd)
end

#configureObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/mini_portile2/mini_portile.rb', line 179

def configure
  return if configured?

  FileUtils.mkdir_p(tmp_path)
  cache_file = File.join(tmp_path, 'configure.options_cache')
  File.open(cache_file, "w") { |f| f.write computed_options.to_s }

  command = Array(File.join((source_directory || "."), "configure"))
  if RUBY_PLATFORM=~/mingw|mswin/
    # Windows doesn't recognize the shebang.
    command.unshift("sh")
  end
  execute('configure', command + computed_options, altlog: "config.log")
end

#configured?Boolean

Returns:

  • (Boolean)


211
212
213
214
215
216
217
218
219
220
# File 'lib/mini_portile2/mini_portile.rb', line 211

def configured?
  configure = File.join((source_directory || work_path), 'configure')
  makefile  = File.join(work_path, 'Makefile')
  cache_file  = File.join(tmp_path, 'configure.options_cache')

  stored_options  = File.exist?(cache_file) ? File.read(cache_file) : ""
  current_options = computed_options.to_s

  (current_options == stored_options) && newer?(makefile, configure)
end

#cookObject



229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/mini_portile2/mini_portile.rb', line 229

def cook
  if source_directory
    prepare_build_directory
  else
    download unless downloaded?
    extract
    patch
  end
  configure unless configured?
  compile
  install unless installed?

  return true
end

#cxx_cmdObject



381
382
383
# File 'lib/mini_portile2/mini_portile.rb', line 381

def cxx_cmd
  (ENV["CXX"] || @cxx_command || RbConfig::CONFIG["CXX"] || "g++").dup
end

#downloadObject



131
132
133
134
135
136
# File 'lib/mini_portile2/mini_portile.rb', line 131

def download
  files_hashs.each do |file|
    download_file(file[:url], file[:local_path])
    verify_file(file)
  end
end

#downloaded?Boolean

Returns:

  • (Boolean)


203
204
205
206
207
208
209
# File 'lib/mini_portile2/mini_portile.rb', line 203

def downloaded?
  missing = files_hashs.detect do |file|
    !File.exist?(file[:local_path])
  end

  missing ? false : true
end

#extractObject



138
139
140
141
142
143
# File 'lib/mini_portile2/mini_portile.rb', line 138

def extract
  files_hashs.each do |file|
    verify_file(file)
    extract_file(file[:local_path], tmp_path)
  end
end

#include_pathObject



368
369
370
# File 'lib/mini_portile2/mini_portile.rb', line 368

def include_path
  File.join(path, "include")
end

#installObject



198
199
200
201
# File 'lib/mini_portile2/mini_portile.rb', line 198

def install
  return if installed?
  execute('install', %Q(#{make_cmd} install))
end

#installed?Boolean

Returns:

  • (Boolean)


222
223
224
225
226
227
# File 'lib/mini_portile2/mini_portile.rb', line 222

def installed?
  makefile  = File.join(work_path, 'Makefile')
  target_dir = Dir.glob("#{port_path}/*").find { |d| File.directory?(d) }

  newer?(target_dir, makefile)
end

#lib_pathObject



372
373
374
# File 'lib/mini_portile2/mini_portile.rb', line 372

def lib_path
  File.join(path, "lib")
end

#make_cmdObject



385
386
387
# File 'lib/mini_portile2/mini_portile.rb', line 385

def make_cmd
  (ENV["MAKE"] || @make_command || ENV["make"] || "make").dup
end

#mkmf_config(pkg: nil, dir: nil, static: nil) ⇒ Object

pkg: the pkg-config file name (without the .pc extension) dir: inject the directory path for the pkg-config file (probably only useful for tests) static: the name of the static library archive (without the “lib” prefix or the file extension), or nil for dynamic linking

we might be able to be terribly clever and infer the name of the static archive file, but unfortunately projects have so much freedom in what they can report (for name, for libs, etc.) that it feels unreliable to try to do so, so I’m preferring to just have the developer make it explicit.



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/mini_portile2/mini_portile.rb', line 283

def mkmf_config(pkg: nil, dir: nil, static: nil)
  require "mkmf"

  if pkg
    dir ||= File.join(lib_path, "pkgconfig")
    pcfile = File.join(dir, "#{pkg}.pc")
    unless File.exist?(pcfile)
      raise ArgumentError, "pkg-config file '#{pcfile}' does not exist"
    end

    output "Configuring MakeMakefile for #{File.basename(pcfile)} (in #{File.dirname(pcfile)})\n"

    # on macos, pkg-config will not return --cflags without this
    ENV["PKG_CONFIG_ALLOW_SYSTEM_CFLAGS"] = "t"

    # append to PKG_CONFIG_PATH as we go, so later pkg-config files can depend on earlier ones
    ENV["PKG_CONFIG_PATH"] = [ENV["PKG_CONFIG_PATH"], dir].compact.join(File::PATH_SEPARATOR)

    incflags = minimal_pkg_config(pcfile, "cflags-only-I")
    cflags = minimal_pkg_config(pcfile, "cflags-only-other")
    if static
      ldflags = minimal_pkg_config(pcfile, "libs-only-L", "static")
      libflags = minimal_pkg_config(pcfile, "libs-only-l", "static")
    else
      ldflags = minimal_pkg_config(pcfile, "libs-only-L")
      libflags = minimal_pkg_config(pcfile, "libs-only-l")
    end
  else
    output "Configuring MakeMakefile for #{@name} #{@version} (from #{path})\n"

    lib_name = name.sub(/\Alib/, "") # TODO: use delete_prefix when we no longer support ruby 2.4

    incflags = Dir.exist?(include_path) ? "-I#{include_path}" : ""
    cflags = ""
    ldflags = Dir.exist?(lib_path) ? "-L#{lib_path}" : ""
    libflags = Dir.exist?(lib_path) ? "-l#{lib_name}" : ""
  end

  if static
    libdir = lib_path
    if pcfile
      pcfile_libdir = minimal_pkg_config(pcfile, "variable=libdir").strip
      libdir = pcfile_libdir unless pcfile_libdir.empty?
    end

    #
    # keep track of the libraries we're statically linking against, and fix up ldflags and
    # libflags to make sure we link statically against the recipe's libaries.
    #
    # this avoids the unintentionally dynamically linking against system libraries, and makes sure
    # that if multiple pkg-config files reference each other that we are able to intercept flags
    # from dependent packages that reference the static archive.
    #
    $MINI_PORTILE_STATIC_LIBS[static] = libdir
    static_ldflags = $MINI_PORTILE_STATIC_LIBS.values.map { |v| "-L#{v}" }
    static_libflags = $MINI_PORTILE_STATIC_LIBS.keys.map { |v| "-l#{v}" }

    # remove `-L#{libdir}` and `-lfoo`. we don't need them since we link against the static
    # archive using the full path.
    ldflags = ldflags.shellsplit.reject { |f| static_ldflags.include?(f) }.shelljoin
    libflags = libflags.shellsplit.reject { |f| static_libflags.include?(f) }.shelljoin

    # prepend the full path to the static archive to the linker flags
    static_archive = File.join(libdir, "lib#{static}.#{$LIBEXT}")
    libflags = [static_archive, libflags].join(" ").strip
  end

  # prefer this package by prepending to search paths and library flags
  #
  # convert the ldflags into a list of directories and append to $LIBPATH (instead of just using
  # $LDFLAGS) to ensure we get the `-Wl,-rpath` linker flag for re-finding shared libraries.
  $INCFLAGS = [incflags, $INCFLAGS].join(" ").strip
  libpaths = ldflags.shellsplit.map { |f| f.sub(/\A-L/, "") }
  $LIBPATH = libpaths | $LIBPATH
  $libs = [libflags, $libs].join(" ").strip

  # prefer this package's compiler flags by appending them to the command line
  $CFLAGS = [$CFLAGS, cflags].join(" ").strip
  $CXXFLAGS = [$CXXFLAGS, cflags].join(" ").strip
end

#patchObject



168
169
170
171
172
173
# File 'lib/mini_portile2/mini_portile.rb', line 168

def patch
  @patch_files.each do |full_path|
    next unless File.exist?(full_path)
    apply_patch(full_path)
  end
end

#pathObject



364
365
366
# File 'lib/mini_portile2/mini_portile.rb', line 364

def path
  File.expand_path(port_path)
end

#prepare_build_directoryObject



124
125
126
127
128
129
# File 'lib/mini_portile2/mini_portile.rb', line 124

def prepare_build_directory
  raise "source_directory is not set" if source_directory.nil?
  output "Building #{@name} from source at '#{source_directory}'"
  FileUtils.mkdir_p(File.join(tmp_path, [name, version].join("-")))
  FileUtils.rm_rf(port_path) # make sure we always re-install
end