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.5"
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.



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

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

  @gcc_command = kwargs[:gcc_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



172
173
174
# File 'lib/mini_portile2/mini_portile.rb', line 172

def configure_options
  @configure_options ||= configure_defaults
end

#filesObject

Returns the value of attribute files.



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

def files
  @files
end

#hostObject

Returns the value of attribute host.



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

def host
  @host
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#nameObject (readonly)

Returns the value of attribute name.



35
36
37
# File 'lib/mini_portile2/mini_portile.rb', line 35

def name
  @name
end

#original_hostObject (readonly)

Returns the value of attribute original_host.



35
36
37
# File 'lib/mini_portile2/mini_portile.rb', line 35

def original_host
  @original_host
end

#patch_filesObject

Returns the value of attribute patch_files.



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

def patch_files
  @patch_files
end

#source_directoryObject

Returns the value of attribute source_directory.



35
36
37
# File 'lib/mini_portile2/mini_portile.rb', line 35

def source_directory
  @source_directory
end

#targetObject

Returns the value of attribute target.



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

def target
  @target
end

#versionObject (readonly)

Returns the value of attribute version.



35
36
37
# File 'lib/mini_portile2/mini_portile.rb', line 35

def version
  @version
end

Class Method Details

.darwin?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/mini_portile2/mini_portile.rb', line 53

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

.freebsd?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/mini_portile2/mini_portile.rb', line 57

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

.linux?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/mini_portile2/mini_portile.rb', line 65

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

.mingw?Boolean

GNU MinGW compiled Ruby?

Returns:

  • (Boolean)


44
45
46
# File 'lib/mini_portile2/mini_portile.rb', line 44

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

.mswin?Boolean

MS Visual-C compiled Ruby?

Returns:

  • (Boolean)


49
50
51
# File 'lib/mini_portile2/mini_portile.rb', line 49

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

.native_path(path) ⇒ Object



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

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)


61
62
63
# File 'lib/mini_portile2/mini_portile.rb', line 61

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

.posix_path(path) ⇒ Object



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

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)


69
70
71
# File 'lib/mini_portile2/mini_portile.rb', line 69

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

.target_cpuObject



77
78
79
# File 'lib/mini_portile2/mini_portile.rb', line 77

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

.target_osObject



73
74
75
# File 'lib/mini_portile2/mini_portile.rb', line 73

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

.windows?Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#activateObject



241
242
243
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
# File 'lib/mini_portile2/mini_portile.rb', line 241

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



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

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

#compileObject



191
192
193
# File 'lib/mini_portile2/mini_portile.rb', line 191

def compile
  execute('compile', make_cmd)
end

#configureObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/mini_portile2/mini_portile.rb', line 176

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)


208
209
210
211
212
213
214
215
216
217
# File 'lib/mini_portile2/mini_portile.rb', line 208

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



226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/mini_portile2/mini_portile.rb', line 226

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

#downloadObject



128
129
130
131
132
133
# File 'lib/mini_portile2/mini_portile.rb', line 128

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

#downloaded?Boolean

Returns:

  • (Boolean)


200
201
202
203
204
205
206
# File 'lib/mini_portile2/mini_portile.rb', line 200

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

  missing ? false : true
end

#extractObject



135
136
137
138
139
140
# File 'lib/mini_portile2/mini_portile.rb', line 135

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

#gcc_cmdObject



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

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

#include_pathObject



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

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

#installObject



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

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

#installed?Boolean

Returns:

  • (Boolean)


219
220
221
222
223
224
# File 'lib/mini_portile2/mini_portile.rb', line 219

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



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

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

#make_cmdObject



379
380
381
# File 'lib/mini_portile2/mini_portile.rb', line 379

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.



280
281
282
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
# File 'lib/mini_portile2/mini_portile.rb', line 280

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
      variables = minimal_pkg_config(pcfile, "print-variables").split("\n").map(&:strip)
      if variables.include?("libdir")
        libdir = minimal_pkg_config(pcfile, "variable=libdir")
      end
    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



165
166
167
168
169
170
# File 'lib/mini_portile2/mini_portile.rb', line 165

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

#pathObject



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

def path
  File.expand_path(port_path)
end

#prepare_build_directoryObject



121
122
123
124
125
126
# File 'lib/mini_portile2/mini_portile.rb', line 121

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