Top Level Namespace

Defined Under Namespace

Classes: P4, P4ApiVersion

Instance Method Summary collapse

Instance Method Details

#calculate_gccObject



118
119
120
121
# File 'ext/P4/extconf.rb', line 118

def calculate_gcc
  gcc = RbConfig::CONFIG["GCC"]
  return gcc
end

#calculate_p4osnameObject



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

def calculate_p4osname
  osname = RbConfig::CONFIG['arch'].split(/-/)[1].upcase
  osname = osname.gsub(/MSWIN32(_\d+)?/, "NT")
  osname = osname.split('-').shift

  case osname
    when /FREEBSD/
      osname = 'FREEBSD'
    when /DARWIN/
      osname = 'DARWIN'
    when /AIX/
      osname = 'AIX'
    when /SOLARIS/
      osname = 'SOLARIS'
  end

  return osname
end

#calculate_p4osplatObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'ext/P4/extconf.rb', line 30

def calculate_p4osplat
  plat = RbConfig::CONFIG['arch'].split(/-/)[0].upcase

  # On Mac OSX, fix the build to 64 bit arch
  if p4osname == 'DARWIN'
    plat = 'X86_64'
  end

  # Translate Ruby's arch names into Perforce's. Mostly the same so
  # only the exceptions are handled here.
  case plat
    when /^I.86$/
      plat = 'X86'
    when /^AMD64$/
      plat = 'X86_64'
    when 'POWERPC'
      plat = 'PPC'
  end

  return plat
end

#calculate_p4osverObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'ext/P4/extconf.rb', line 79

def calculate_p4osver
  ver = ''

  case p4osname
    when 'NT'
      # do nothing
    when /MINGW/
      # do nothing
    when /FREEBSD([0-9]+)/
      ver = $1
    when /DARWIN/
      ver = CONFIG['arch'].upcase.gsub(/.*DARWIN(\d+).*/, '\1')
    when /AIX(5)\.(\d)/
      ver = $1 + $2
    when /SOLARIS2\.(\d+)/
      ver = $1
    else
      # use uname -r to see if it works
      begin
        ver=`uname -r`.chomp
        ver_re = /^(\d+)\.(\d+)/
        md = ver_re.match(ver)
        if (md)
          maj = md[1].to_i
          min = md[2].to_i
          ver = maj.to_s + min.to_s
        end
      rescue
        # Nothing - if it failed, it failed.
      end
  end

  return ver
end

#calculate_uname_platformObject



127
128
129
130
131
132
133
134
135
136
# File 'ext/P4/extconf.rb', line 127

def calculate_uname_platform
  plat = "UNKNOWN"
  begin
    plat = `uname -p`
    plat = plat.chomp.upcase
  rescue
    # Nothing - if it failed, it failed.
  end
  plat
end

#create_p4rubyconf_header(p4api_version, libs) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
# File 'ext/P4/extconf.rb', line 355

def create_p4rubyconf_header(p4api_version, libs)
  File.open("p4rubyconf.h", "w") do
  |ch|
    ch.puts(macro_def("P4APIVER_STRING", p4api_version.to_s))
    ch.puts(macro_def("P4APIVER_ID", p4api_version.to_i, false))
    ch.puts(macro_def("P4API_PATCHLEVEL", p4api_version.patchlevel, false))
    ch.puts(macro_def("P4API_PATCHLEVEL_STRING", p4api_version.patchlevel.to_s))
    ch.puts(macro_def("P4RUBY_VERSION", P4::VERSION))
    ch.puts(macro_def("WITH_LIBS", libs, true))
  end
end

#download_api_via_httpsObject

Downloads the C++ P4API via HTTPS to the local directory, then ‘initializes’ it by unpacking it.



519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'ext/P4/extconf.rb', line 519

def download_api_via_https

  uri=URI('https://ftp.perforce.com:443')
  dir = download_dir(p4api_version_dir)

  puts "Downloading #{filename} from #{dir} on https://ftp.perforce.com"

  Net::HTTP.start(uri.host, uri.port, :use_ssl =>true) do |http|
      resp = http.get("/" + dir + "/" + filename)
      open(filename, "wb") do |file|
          file.write(resp.body)
      end
  end
end

#download_dir(version) ⇒ Object



471
472
473
# File 'ext/P4/extconf.rb', line 471

def download_dir(version)
  "perforce/#{version}/#{platform_dir_name}"
end

#downloaded_p4api_dirObject



542
543
544
# File 'ext/P4/extconf.rb', line 542

def downloaded_p4api_dir
  File.expand_path(Dir.entries('.').select { |x| x =~ /^p4api/ and File.directory?(x) }.first)
end

#filenameObject



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'ext/P4/extconf.rb', line 475

def filename
  openssl_number = OpenSSL::OPENSSL_VERSION.split(' ')[1].to_s
  openssl_number = openssl_number.slice(0, (openssl_number.rindex('.')))

  if RbConfig::CONFIG['target_os'].downcase =~ /nt|mswin|mingw/
    filename = 'p4api.zip'
    if !openssl_number.to_s.empty?
        case openssl_number.to_s
            when /1.1/
                filename = 'p4api-openssl1.1.1.zip'
            when /1.0/
                filename = 'p4api-openssl1.0.2.zip'
            when /3.*/
                filename = 'p4api-openssl3.zip'
        end
    end
  elsif RbConfig::CONFIG['target_os'].downcase =~ /darwin19|darwin[2-9][0-9]/ || RbConfig::CONFIG['host_cpu'] =~ /aarch64|arm64/
    if !openssl_number.to_s.empty?
      case openssl_number.to_s
          when /1.1/
              filename = 'p4api-openssl1.1.1.tgz'
          when /3.*/
              filename = 'p4api-openssl3.tgz'
      end
    end
  else
    filename = 'p4api.tgz'
    if !openssl_number.to_s.empty?
        case openssl_number.to_s
            when /1.1/
                filename = 'p4api-glibc2.3-openssl1.1.1.tgz'
            when /1.0/
                filename = 'p4api-glibc2.3-openssl1.0.2.tgz'
            when /3.*/
                filename = 'p4api-glibc2.3-openssl3.tgz'
        end
    end
  end
  return filename
end

#gccObject



114
115
116
# File 'ext/P4/extconf.rb', line 114

def gcc
  @gcc ||= calculate_gcc
end

#macro_def(macro, value, string = true) ⇒ Object



347
348
349
350
351
352
353
# File 'ext/P4/extconf.rb', line 347

def macro_def(macro, value, string=true)
  if (string)
    %Q{#define #{macro}\t"#{value}"}
  else
    %Q{#define #{macro}\t#{value}}
  end
end

#p4_cpu(os) ⇒ Object

Our ‘cpu’ label we use as part of the directory name on ftp.perforce.com



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'ext/P4/extconf.rb', line 426

def p4_cpu(os)
  cpu = RbConfig::CONFIG['target_cpu']
  case os
    when :darwin, :linux
      if cpu =~ /i686/
        'x86'
      elsif cpu =~ /universal/
        'x86_64'
      else
        cpu
      end
    else
      case cpu
        when /ia/i
          'ia64'
        else
          cpu
      end
  end
end

#p4_platform_labelObject

The p4_platform is our label that basically ends up being part of the directory name where we can download files from.



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'ext/P4/extconf.rb', line 449

def p4_platform_label
  case RbConfig::CONFIG["target_os"].downcase
    when /nt|mswin|mingw|cygwin|msys/
      # Ruby on windows is only MinGW via Rubyinstaller.org, though this may
      # not work on all rubies.
      # There are too many permutations of Windows p4api, to automate.
      raise 'Automatic fetching of p4api from perforce FTP is not supported on Windows'
    when /darwin19|darwin[2-9][0-9]/
      "macosx12#{p4_cpu(:darwin)}"
    when /darwin/      
      "darwin90#{p4_cpu(:darwin)}"
    when /solaris/
      "solaris10#{p4_cpu(:solaris)}"
    when /linux/
      "linux26#{p4_cpu(:linux)}"    
  end
end

#p4api_version_dirObject

Set this to the main version directory we look up in ftp.perforce.com for the P4API This is ignored if you specify the version on the command line. Changed the hardcoded string so that the version is now derived from version.rb file P4API_VERSION_DIR = ‘r19.1’



15
16
17
18
19
20
# File 'ext/P4/extconf.rb', line 15

def p4api_version_dir
        ver=P4::Version.split(".")
        p4_major = ver[0].chars.last(2).join
        p4_minor = ver[1]
        dir = "r" + p4_major + "." + p4_minor
end

#p4osnameObject



52
53
54
# File 'ext/P4/extconf.rb', line 52

def p4osname
  @p4osname ||= calculate_p4osname
end

#p4osplatObject

Provide platform variables in P4-specific format



26
27
28
# File 'ext/P4/extconf.rb', line 26

def p4osplat
  @p4osplat ||= calculate_p4osplat
end

#p4osverObject



75
76
77
# File 'ext/P4/extconf.rb', line 75

def p4osver
  @p4osver ||= calculate_p4osver
end

#platform_dir_nameObject



467
468
469
# File 'ext/P4/extconf.rb', line 467

def platform_dir_name
  "bin.#{p4_platform_label}"
end

#resolve_p4api_dirObject

If the user has not specified –with-p4api-dir, check the –enable-p4api-download flag, and download the p4api before proceeding, unless that’s disabled.

This may be a little confusing. If people specify –with-p4api-dir, we want use only use that setting. If that setting is wrong, we want to fail.

If they don’t set the –with-p4api-dir, we’ll proceed as if –enable-p4api-download has been set. Otherwise, they can –disable-p4api-download to ensure we just don’t bother doing anything.



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'ext/P4/extconf.rb', line 382

def resolve_p4api_dir
  p4api_dir = nil

  # When running rake compile, use this instead of other options, I'm not sure how
  # gem/bundler options are passed through via rake
  if ENV.has_key?('p4api_dir')
    p4api_dir = ENV['p4api_dir']
    dir_config('p4api', "#{p4api_dir}/include", "#{p4api_dir}/lib")
  end

  if !p4api_dir && !with_config('p4api-dir') && enable_config('p4api-download', true)
    download_api_via_https
    unzip_file
    p4api_dir = downloaded_p4api_dir
    dir_config('p4api', "#{p4api_dir}/include", "#{p4api_dir}/lib")
  elsif with_config('p4api_dir')
    p4api_dir = with_config('p4api-dir')
    dir_config('p4api', "#{p4api_dir}/include", "#{p4api_dir}/lib")
  elsif !p4api_dir
    raise '--with-p4api-dir option has not been specified, and --disable-p4api-download is in effect'
  end

  p4api_dir
end

#resolve_ssl_dirsObject



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'ext/P4/extconf.rb', line 407

def resolve_ssl_dirs
  ssl_dir = nil
  # When running rake compile, use this instead of other options, I'm not sure how
  # gem/bundler options are passed through via rake
  if ENV.has_key?('ssl_dir')
    ssl_dir = ENV['ssl_dir']
    dir_config('ssl', "#{ssl_dir}/include", "#{ssl_dir}/lib")
    puts "SSL Path #{ssl_dir}"
  end
  if ENV.has_key?('ssl_include_dir') && ENV.has_key?('ssl_lib_dir')
    ssl_include_dir = ENV['ssl_include_dir']
    ssl_lib_dir = ENV['ssl_lib_dir']
    dir_config('ssl', ssl_include_dir, ssl_lib_dir)
    puts "SSL Includes #{ssl_include_dir} Lib #{ssl_lib_dir}"
  end
  ssl_dir
end

#set_platform_cflagsObject



209
210
211
212
213
214
215
# File 'ext/P4/extconf.rb', line 209

def set_platform_cflags
  if (p4osname == 'DARWIN')
    # Only build for 64 bit if we have more than one arch defined in CFLAGS
    $CFLAGS.slice!("-arch i386")
    $CFLAGS.slice!("-arch ppc");
  end
end

#set_platform_cppflagsObject



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
# File 'ext/P4/extconf.rb', line 183

def set_platform_cppflags
  $CPPFLAGS += "-DOS_#{p4osname} "
  $CPPFLAGS += "-DOS_#{p4osname}#{p4osver} "
  $CPPFLAGS += "-DOS_#{p4osname}#{p4osver}#{p4osplat} "

  if (p4osname == 'NT')
    $CPPFLAGS += '/DCASE_INSENSITIVE '
  end

  if (p4osname == 'MINGW32')
    $CPPFLAGS += '-DOS_NT -DCASE_INSENSITIVE '
  end

  if (p4osname == 'MINGW')
    $CPPFLAGS += '-DOS_NT -DCASE_INSENSITIVE '
  end

  if (p4osname == 'SOLARIS')
    $CPPFLAGS += '-Dsolaris '
  end

  if (p4osname == 'DARWIN')
    $CPPFLAGS += '-DCASE_INSENSITIVE '
  end
end

#set_platform_cxxflagsObject



176
177
178
179
180
# File 'ext/P4/extconf.rb', line 176

def set_platform_cxxflags
  if (p4osname == 'LINUX') && (gcc == 'yes')
    $CXXFLAGS += " -std=c++11 "
  end
end

#set_platform_libsObject

Initialize the base sets of platform libraries *before other initializers* to preserve linking order.



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'ext/P4/extconf.rb', line 219

def set_platform_libs

  case p4osname
    when 'SOLARIS'
      osver = `uname -r`
      osver.gsub!(/5\./, '2')
      if (osver == '25')
        $LDFLAGS += '/usr/ucblib/libucb.a '
      end
      have_library('nsl')
      have_library('socket')
    when 'NT'
      have_library('advapi32')
      have_library('wsock32')
      have_library('kernel32')
      have_library('oldnames')
    when 'CYGWIN'
      # Clear out 'bogus' libs on cygwin
      CONFIG['LIBS'] = ''
    when 'DARWIN'
      if p4osver.to_i >= 8
        # Only build for 64 bit if we have more than one arch defined in CFLAGS
        $LDFLAGS.slice!('-arch i386')
        $LDFLAGS.slice!('-arch ppc')
        $LDFLAGS += ' -framework CoreFoundation -framework Foundation -framework CoreGraphics'
      end
    when 'LINUX', 'MINGW32', 'MINGW'
      $LDFLAGS += ' -Wl,--allow-multiple-definition'
      have_library('supc++')
  end
end

#set_platform_optsObject

Setup additional compiler and linker options.

We generally need to launch these things before we configure most of the flags. (See the main script at the end.)



144
145
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
# File 'ext/P4/extconf.rb', line 144

def set_platform_opts

  # Expand any embedded variables (like '$(CC)')
  CONFIG["CC"] = RbConfig::CONFIG["CC"]
  CONFIG["LDSHARED"] = RbConfig::CONFIG["LDSHARED"]

  # Make sure we have a CXX value (sometimes there isn't one)
  CONFIG["CXX"] = CONFIG["CC"] unless CONFIG.has_key?("CXX")

  # O/S specific oddities

  case p4osname
    when /DARWIN/
      CONFIG['CC'] = 'xcrun c++'
      CONFIG['CXX'] = 'xcrun c++'
      CONFIG['LDSHARED'] = CONFIG['CXX'] + ' -bundle'
    when /FREEBSD/, /LINUX/
      # FreeBSD 6 and some Linuxes use 'cc' for linking by default. The
      # gcc detection patterns above won't catch that, so for these
      # platforms, we specifically convert cc to c++.
      CONFIG['LDSHARED'].sub!(/^cc/, 'c++')
    when /MINGW32/, /MINGW/
      # When building with MinGW we need to statically link libgcc
      # and make sure we're linking with gcc and not g++. On older
      # Rubies, they use LDSHARED; newer ones (>=1.9) use LDSHAREDXX
      CONFIG['LDSHARED'].sub!(/g\+\+/, 'gcc')
      CONFIG['LDSHAREDXX'].sub!(/g\+\+/, 'gcc')
      CONFIG['LDSHARED'] = CONFIG['LDSHARED'] + ' -static-libgcc'
      CONFIG['LDSHAREDXX'] = CONFIG['LDSHARED'] + ' -static-libgcc'
  end
end

#uname_platformObject



123
124
125
# File 'ext/P4/extconf.rb', line 123

def uname_platform
  @uname_platform ||= calculate_uname_platform
end

#unzip_fileObject



534
535
536
537
538
539
540
# File 'ext/P4/extconf.rb', line 534

def unzip_file
  if RbConfig::CONFIG['target_os'].downcase =~ /nt|mswin|mingw/
    `unzip #{filename}`
  else
    `tar xzf #{filename}`
  end
end