Top Level Namespace

Defined Under Namespace

Modules: Curl

Constant Summary collapse

DEFAULT_PARALLEL_JOBS =
begin
  n = Etc.respond_to?(:nprocessors) ? Etc.nprocessors.to_i : 1
  n = 1 if n <= 0
  # Use half the CPUs by default (rounded up), but at least 1
  jobs = [(n.to_f / 2).ceil, 1].max
  jobs
rescue
  1
end
PARALLEL_JOBS =
begin
  if job_hints && job_hints > 0
    job_hints
  else
    # If no hints provided, default to half the CPUs.
    DEFAULT_PARALLEL_JOBS
  end
end
PARALLEL_CONSTANT_CHECKS =

Only enable if the platform supports fork. On Windows this stays sequential.

PARALLEL_JOBS > 1 && Process.respond_to?(:fork)

Instance Method Summary collapse

Instance Method Details

#define(s, v = 1) ⇒ Object

Check arch flags TODO: detect mismatched arch types when libcurl mac ports is mixed with native mac ruby or vice versa archs = $CFLAGS.scan(/-archs(.*?)s/).first # get the first arch flag if archs and archs.size >= 1

# need to reduce the number of archs...
# guess the first one is correct... at least the first one is probably the ruby installed arch...
# this could lead to compiled binaries that crash at runtime...
$CFLAGS.gsub!(/-arch\s(.*?)\s/,' ')
$CFLAGS << " -arch #{archs.first}"
puts "Selected arch: #{archs.first}"

end



46
47
48
# File 'ext/extconf.rb', line 46

def define(s, v = 1)
  $defs.push(format("-D HAVE_%s=%d", s.to_s.upcase, v))
end

#detect_job_hintsObject



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'ext/extconf.rb', line 75

def detect_job_hints
  # Priority: explicit extconf hint, then common envs used by bundler/rubygems
  [
    ENV['EXTCONF_JOBS'],
    ENV['JOBS'],
    ENV['BUNDLE_JOBS'],
    parse_jobs_from_makeflags(ENV['MAKEFLAGS'])
  ].each do |v|
    n = v.to_i if v
    return n if n && n > 0
  end
  nil
end

#flush_constant_checksObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'ext/extconf.rb', line 146

def flush_constant_checks
  return if $queued_constants.empty?

  constants = $queued_constants.uniq
  $queued_constants.clear

  # On platforms without fork (e.g., Windows), run sequentially.
  unless PARALLEL_CONSTANT_CHECKS
    constants.each { |name| have_constant(name) }
    return
  end

  # Run constant probes in isolated child processes to avoid mkmf
  # scratch file and logfile contention.
  results = {}
  pending = constants.dup
  running = {}
  max_jobs = PARALLEL_JOBS

  spawn_probe = lambda do |const_name|
    r, w = IO.pipe
    pid = fork do
      begin
        r.close
        Dir.mktmpdir('curb-mkmf-') do |dir|
          Dir.chdir(dir) do
            begin
              # Avoid clobbering the main mkmf.log
              Logging::logfile = File.open(File::NULL, 'w') rescue File.open(File.join(dir, 'mkmf.log'), 'w')
            rescue
              # best-effort
            end
            sname = const_name.is_a?(Symbol) ? const_name.to_s : const_name.upcase
            ok = try_constant_compile(sname)
            w.write([const_name, ok ? 1 : 0].join("\t"))
          end
        end
      rescue
        # Treat as failure if anything unexpected happens in child
        begin
          w.write([const_name, 0].join("\t"))
        rescue
        end
      ensure
        begin w.close rescue nil end
        # Ensure the child exits without running at_exit handlers
        exit! 0
      end
    end
    w.close
    running[pid] = r
  end

  # Start initial batch
  while running.size < max_jobs && !pending.empty?
    spawn_probe.call(pending.shift)
  end

  # Collect results and keep spawning until done
  until running.empty?
    pid = Process.wait
    io = running.delete(pid)
    if io
      begin
        msg = io.read.to_s
        name_str, ok_str = msg.split("\t", 2)
        if name_str
          # Map back to the original object if symbol-like
          original = constants.find { |n| n.to_s == name_str }
          results[original || name_str] = ok_str.to_i == 1
        end
      ensure
        begin io.close rescue nil end
      end
    end
    # Fill next task slot
    spawn_probe.call(pending.shift) unless pending.empty?
  end

  # Apply results to $defs and output summary via checking_for
  results.each do |const_name, ok|
    sname = const_name.is_a?(Symbol) ? const_name.to_s : const_name.upcase
    checking_for const_name do
      if ok
        define const_name
        true
      else
        false
      end
    end
  end
end

#have_constant(name) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'ext/extconf.rb', line 128

def have_constant(name)
  # Queue constants for optional parallel probing. Falls back to sequential.
  if PARALLEL_CONSTANT_CHECKS
    $queued_constants << name
    true
  else
    sname = name.is_a?(Symbol) ? name.to_s : name.upcase
    checking_for name do
      if try_constant_compile(sname)
        define name
        true
      else
        false
      end
    end
  end
end

#parse_jobs_from_makeflags(flags) ⇒ Object

Optional parallelization support for constant checks only. Enable automatically when job hints are present (JOBS, BUNDLE_JOBS, MAKEFLAGS -jN), or explicitly via EXTCONF_JOBS/EXTCONF_PARALLEL.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'ext/extconf.rb', line 54

def parse_jobs_from_makeflags(flags)
  return nil if flags.nil? || flags.empty?
  tokens = flags.to_s.split(/\s+/)
  jobs = nil
  tokens.each_with_index do |tok, i|
    case tok
    when /\A-j(\d+)\z/
      jobs = $1.to_i
    when '-j'
      nxt = tokens[i + 1]
      jobs = nxt.to_i if nxt && nxt =~ /\A\d+\z/
    when /\A--jobs(?:=(\d+)|\s+(\d+))\z/
      jobs = ($1 || $2).to_i
    when /\Aj(\d+)\z/ # sometimes make condenses flags
      jobs = $1.to_i
    end
    break if jobs && jobs > 0
  end
  jobs
end

#test_for(name, const, src) ⇒ Object

do some checking to detect ruby 1.8 hash.c vs ruby 1.9 hash.c



625
626
627
628
629
630
631
632
633
634
# File 'ext/extconf.rb', line 625

def test_for(name, const, src)
  checking_for name do
    if try_compile(src,"#{$CFLAGS} #{$LIBS}")
      define const
      true
    else
      false
    end
  end
end

#try_constant_compile(sname) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'ext/extconf.rb', line 116

def try_constant_compile(sname)
  src = %{
    #include <curl/curl.h>
    int main() {
      int test = (int)#{sname};
      (void)test;
      return 0;
    }
  }
  try_compile(src, "#{$CFLAGS} #{$LIBS}")
end