Class: MiniPortile
- Inherits:
-
Object
- Object
- MiniPortile
- Defined in:
- lib/mini_portile2/version.rb,
lib/mini_portile2/mini_portile.rb
Direct Known Subclasses
Constant Summary collapse
- VERSION =
"2.8.8"
- DEFAULT_TIMEOUT =
10
Instance Attribute Summary collapse
- #configure_options ⇒ Object
-
#files ⇒ Object
Returns the value of attribute files.
-
#host ⇒ Object
Returns the value of attribute host.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#original_host ⇒ Object
readonly
Returns the value of attribute original_host.
-
#patch_files ⇒ Object
Returns the value of attribute patch_files.
-
#source_directory ⇒ Object
Returns the value of attribute source_directory.
-
#target ⇒ Object
Returns the value of attribute target.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
- .darwin? ⇒ Boolean
- .freebsd? ⇒ Boolean
- .linux? ⇒ Boolean
-
.mingw? ⇒ Boolean
GNU MinGW compiled Ruby?.
-
.mswin? ⇒ Boolean
MS Visual-C compiled Ruby?.
- .native_path(path) ⇒ Object
- .openbsd? ⇒ Boolean
- .posix_path(path) ⇒ Object
- .solaris? ⇒ Boolean
- .target_cpu ⇒ Object
- .target_os ⇒ Object
- .windows? ⇒ Boolean
Instance Method Summary collapse
- #activate ⇒ Object
- #apply_patch(patch_file) ⇒ Object
- #cc_cmd ⇒ Object (also: #gcc_cmd)
- #compile ⇒ Object
- #configure ⇒ Object
- #configured? ⇒ Boolean
- #cook ⇒ Object
- #cxx_cmd ⇒ Object
- #download ⇒ Object
- #downloaded? ⇒ Boolean
- #extract ⇒ Object
- #include_path ⇒ Object
-
#initialize(name, version, **kwargs) ⇒ MiniPortile
constructor
A new instance of MiniPortile.
- #install ⇒ Object
- #installed? ⇒ Boolean
- #lib_path ⇒ Object
- #make_cmd ⇒ Object
-
#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.
- #patch ⇒ Object
- #path ⇒ Object
- #prepare_build_directory ⇒ Object
Constructor Details
#initialize(name, version, **kwargs) ⇒ MiniPortile
Returns a new instance of MiniPortile.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/mini_portile2/mini_portile.rb', line 100 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_options ⇒ Object
174 175 176 |
# File 'lib/mini_portile2/mini_portile.rb', line 174 def @configure_options ||= configure_defaults end |
#files ⇒ Object
Returns the value of attribute files.
38 39 40 |
# File 'lib/mini_portile2/mini_portile.rb', line 38 def files @files end |
#host ⇒ Object
Returns the value of attribute host.
38 39 40 |
# File 'lib/mini_portile2/mini_portile.rb', line 38 def host @host end |
#logger ⇒ Object
Returns the value of attribute logger.
38 39 40 |
# File 'lib/mini_portile2/mini_portile.rb', line 38 def logger @logger end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
36 37 38 |
# File 'lib/mini_portile2/mini_portile.rb', line 36 def name @name end |
#original_host ⇒ Object (readonly)
Returns the value of attribute original_host.
36 37 38 |
# File 'lib/mini_portile2/mini_portile.rb', line 36 def original_host @original_host end |
#patch_files ⇒ Object
Returns the value of attribute patch_files.
38 39 40 |
# File 'lib/mini_portile2/mini_portile.rb', line 38 def patch_files @patch_files end |
#source_directory ⇒ Object
Returns the value of attribute source_directory.
36 37 38 |
# File 'lib/mini_portile2/mini_portile.rb', line 36 def source_directory @source_directory end |
#target ⇒ Object
Returns the value of attribute target.
38 39 40 |
# File 'lib/mini_portile2/mini_portile.rb', line 38 def target @target end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
36 37 38 |
# File 'lib/mini_portile2/mini_portile.rb', line 36 def version @version end |
Class Method Details
.darwin? ⇒ Boolean
54 55 56 |
# File 'lib/mini_portile2/mini_portile.rb', line 54 def self.darwin? target_os =~ /darwin/ end |
.freebsd? ⇒ Boolean
58 59 60 |
# File 'lib/mini_portile2/mini_portile.rb', line 58 def self.freebsd? target_os =~ /freebsd/ end |
.linux? ⇒ Boolean
66 67 68 |
# File 'lib/mini_portile2/mini_portile.rb', line 66 def self.linux? target_os =~ /linux/ end |
.mingw? ⇒ Boolean
GNU MinGW compiled Ruby?
45 46 47 |
# File 'lib/mini_portile2/mini_portile.rb', line 45 def self.mingw? target_os =~ /mingw/ end |
.mswin? ⇒ Boolean
MS Visual-C compiled Ruby?
50 51 52 |
# File 'lib/mini_portile2/mini_portile.rb', line 50 def self.mswin? target_os =~ /mswin/ end |
.native_path(path) ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/mini_portile2/mini_portile.rb', line 82 def self.native_path(path) path = File.(path) if File::ALT_SEPARATOR path.tr(File::SEPARATOR, File::ALT_SEPARATOR) else path end end |
.openbsd? ⇒ Boolean
62 63 64 |
# File 'lib/mini_portile2/mini_portile.rb', line 62 def self.openbsd? target_os =~ /openbsd/ end |
.posix_path(path) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/mini_portile2/mini_portile.rb', line 91 def self.posix_path(path) path = File.(path) if File::ALT_SEPARATOR "/" + path.tr(File::ALT_SEPARATOR, File::SEPARATOR).tr(":", File::SEPARATOR) else path end end |
.solaris? ⇒ Boolean
70 71 72 |
# File 'lib/mini_portile2/mini_portile.rb', line 70 def self.solaris? target_os =~ /solaris/ end |
.target_cpu ⇒ Object
78 79 80 |
# File 'lib/mini_portile2/mini_portile.rb', line 78 def self.target_cpu RbConfig::CONFIG['target_cpu'] end |
.target_os ⇒ Object
74 75 76 |
# File 'lib/mini_portile2/mini_portile.rb', line 74 def self.target_os RbConfig::CONFIG['target_os'] end |
.windows? ⇒ Boolean
40 41 42 |
# File 'lib/mini_portile2/mini_portile.rb', line 40 def self.windows? target_os =~ /mswin|mingw/ end |
Instance Method Details
#activate ⇒ Object
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 271 272 |
# File 'lib/mini_portile2/mini_portile.rb', line 243 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
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/mini_portile2/mini_portile.rb', line 144 def apply_patch(patch_file) ( # Not a class variable because closures will capture self. @apply_patch ||= case when which('git') lambda { |file| "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| "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_cmd ⇒ Object Also known as: gcc_cmd
375 376 377 |
# File 'lib/mini_portile2/mini_portile.rb', line 375 def cc_cmd (ENV["CC"] || @cc_command || RbConfig::CONFIG["CC"] || "gcc").dup end |
#compile ⇒ Object
193 194 195 |
# File 'lib/mini_portile2/mini_portile.rb', line 193 def compile execute('compile', make_cmd) end |
#configure ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/mini_portile2/mini_portile.rb', line 178 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 .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 + , altlog: "config.log") end |
#configured? ⇒ Boolean
210 211 212 213 214 215 216 217 218 219 |
# File 'lib/mini_portile2/mini_portile.rb', line 210 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') = File.exist?(cache_file) ? File.read(cache_file) : "" = .to_s ( == ) && newer?(makefile, configure) end |
#cook ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/mini_portile2/mini_portile.rb', line 228 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_cmd ⇒ Object
380 381 382 |
# File 'lib/mini_portile2/mini_portile.rb', line 380 def cxx_cmd (ENV["CXX"] || @cxx_command || RbConfig::CONFIG["CXX"] || "g++").dup end |
#download ⇒ Object
130 131 132 133 134 135 |
# File 'lib/mini_portile2/mini_portile.rb', line 130 def download files_hashs.each do |file| download_file(file[:url], file[:local_path]) verify_file(file) end end |
#downloaded? ⇒ Boolean
202 203 204 205 206 207 208 |
# File 'lib/mini_portile2/mini_portile.rb', line 202 def downloaded? missing = files_hashs.detect do |file| !File.exist?(file[:local_path]) end missing ? false : true end |
#extract ⇒ Object
137 138 139 140 141 142 |
# File 'lib/mini_portile2/mini_portile.rb', line 137 def extract files_hashs.each do |file| verify_file(file) extract_file(file[:local_path], tmp_path) end end |
#include_path ⇒ Object
367 368 369 |
# File 'lib/mini_portile2/mini_portile.rb', line 367 def include_path File.join(path, "include") end |
#install ⇒ Object
197 198 199 200 |
# File 'lib/mini_portile2/mini_portile.rb', line 197 def install return if installed? execute('install', %Q(#{make_cmd} install)) end |
#installed? ⇒ Boolean
221 222 223 224 225 226 |
# File 'lib/mini_portile2/mini_portile.rb', line 221 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_path ⇒ Object
371 372 373 |
# File 'lib/mini_portile2/mini_portile.rb', line 371 def lib_path File.join(path, "lib") end |
#make_cmd ⇒ Object
384 385 386 |
# File 'lib/mini_portile2/mini_portile.rb', line 384 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.
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 282 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 |
#patch ⇒ Object
167 168 169 170 171 172 |
# File 'lib/mini_portile2/mini_portile.rb', line 167 def patch @patch_files.each do |full_path| next unless File.exist?(full_path) apply_patch(full_path) end end |
#path ⇒ Object
363 364 365 |
# File 'lib/mini_portile2/mini_portile.rb', line 363 def path File.(port_path) end |
#prepare_build_directory ⇒ Object
123 124 125 126 127 128 |
# File 'lib/mini_portile2/mini_portile.rb', line 123 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 |