Top Level Namespace

Defined Under Namespace

Modules: Caffe

Constant Summary collapse

CONFTEST_CC =
"#{CONFTEST}.cc".freeze

Instance Method Summary collapse

Instance Method Details

#configuration(srcdir) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'ext/caffe/mkmf_cxx.rb', line 13

def configuration(srcdir)
  configuration = configuration_orig(srcdir)
  configuration.each do |config|
    # Make sure we link the extension using the C++ compiler
    config.gsub!(/^LDSHARED\s*=.*$/, "LDSHARED = #{$LDSHARED_CXX}")

    # Make sure set the C++ flags correctly
    config.gsub!(/^CXXFLAGS\s*=.*$/, "CXXFLAGS = $(CFLAGS) #{$CXXFLAGS}")
  end
  configuration
end

#create_tmpsrc_cxx(src) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'ext/caffe/mkmf_cxx.rb', line 28

def create_tmpsrc_cxx(src)
  src = "#{COMMON_HEADERS}\n#{src}"
  src = yield(src) if block_given?
  src.gsub!(/[ \t]+$/, '')
  src.gsub!(/\A\n+|^\n+$/, '')
  src.sub!(/[^\n]\z/, "\\&\n")
  count = 0
  begin
    open(CONFTEST_CC, 'wb') do |cfile|
      cfile.print src
    end
  rescue Errno::EACCES
    if (count += 1) < 5
      sleep 0.2
      retry
    end
  end
  src
end

#cxxpp_command(outfile, opt = '') ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'ext/caffe/mkmf_cxx.rb', line 101

def cxxpp_command(outfile, opt = '')
  conf = RbConfig::CONFIG.merge('hdrdir' => $hdrdir.quote,
                                'srcdir' => $srcdir.quote,
                                'arch_hdrdir' => $arch_hdrdir.quote,
                                'top_srcdir' => $top_srcdir.quote)
  if $universal && (arch_flag = conf['ARCH_FLAG']) && !arch_flag.empty?
    conf['ARCH_FLAG'] = arch_flag.gsub(/(?:\G|\s)-arch\s+\S+/, '')
  end
  flags = [$INCFLAGS, $CPPFLAGS, $CFLAGS, opt].join ' '
  RbConfig.expand("$(CXXPP) #{flags} #{CONFTEST_CC} #{outfile}", conf)
end

#header_cxx?(header, preheaders = nil, opt = '', &b) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
129
130
131
132
133
134
135
136
# File 'ext/caffe/mkmf_cxx.rb', line 126

def header_cxx?(header, preheaders = nil, opt = '', &b)
  opt = "#{trans_opt opt} #{trans_opt $defs}"
  checking_for header do
    if try_cxxpp(cpp_include(preheaders) + cpp_include(header), opt, &b)
      $defs.push(format('-DHAVE_%s', header.tr_cpp))
      true
    else
      false
    end
  end
end

#library?(lib, opt = '', &b) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'ext/caffe/mkmf_cxx.rb', line 143

def library?(lib, opt = '', &b)
  lib = with_config(lib + 'lib', lib)
  checking_for format(LIBARG, lib) do
    if COMMON_LIBS.include?(lib)
      true
    else
      libs = append_library($libs, lib)
      if try_library(libs, opt, &b)
        $libs = libs
        true
      else
        false
      end
    end
  end
end


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'ext/caffe/mkmf_cxx.rb', line 64

def link_command(ldflags, opt = '', libpath = $DEFLIBPATH | $LIBPATH)
  librubyarg = $extmk ? $LIBRUBYARG_STATIC : '$(LIBRUBYARG)'
  conf = RbConfig::CONFIG.merge('hdrdir' => $hdrdir.quote,
                                'src' => CONFTEST_CC,
                                'arch_hdrdir' => $arch_hdrdir.quote,
                                'top_srcdir' => $top_srcdir.quote,
                                'INCFLAGS' => $INCFLAGS,
                                'CPPFLAGS' => $CPPFLAGS,
                                'CXXFLAGS' => $CXXFLAGS,
                                'CFLAGS' => $CFLAGS,
                                'ARCH_FLAG' => $ARCH_FLAG,
                                'LDFLAGS' => "#{$LDFLAGS} #{ldflags}",
                                'LOCAL_LIBS' => "#{$LOCAL_LIBS} #{$libs}",
                                'LIBS' => "#{librubyarg} #{opt} #{$LIBS}")
  conf['LIBPATH'] = libpathflag(libpath.map do |s|
    RbConfig.expand(s.dup, conf)
  end)
  RbConfig.expand(TRY_LINK.dup, conf)
end

#trans_opt(opt) ⇒ Object



120
121
122
123
124
# File 'ext/caffe/mkmf_cxx.rb', line 120

def trans_opt(opt)
  [[:to_str], [:join, ' '], [:to_s]].each do |meth, *args|
    return opt.send(meth, *args) if opt.respond_to?(meth)
  end
end

#try_cxxpp(src, opt = '', *opts, &b) ⇒ Object



113
114
115
116
117
118
# File 'ext/caffe/mkmf_cxx.rb', line 113

def try_cxxpp(src, opt = '', *opts, &b)
  try_do_cxx(src, cxxpp_command(CPPOUTFILE, opt), *opts, &b) &&
    File.file?('conftest.i')
ensure
  MakeMakefile.rm_f 'conftest*'
end

#try_do_cxx(src, command, *opts, &b) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'ext/caffe/mkmf_cxx.rb', line 48

def try_do_cxx(src, command, *opts, &b)
  unless have_devel?
    raise <<MSG
The compiler failed to generate an executable file.
You have to install development tools first.
MSG
  end
  begin
    src = create_tmpsrc_cxx(src, &b)
    xsystem(command, *opts)
  ensure
    log_src(src)
    MakeMakefile.rm_rf "#{CONFTEST}.dSYM"
  end
end

#try_library(libs, opt = '', &b) ⇒ Object



138
139
140
141
# File 'ext/caffe/mkmf_cxx.rb', line 138

def try_library(libs, opt = '', &b)
  opt = "#{trans_opt opt} #{libs}"
  try_link(MAIN_DOES_NOTHING, opt, &b)
end

#try_link0(src, opt = '', *opts, &b) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'ext/caffe/mkmf_cxx.rb', line 84

def try_link0(src, opt = '', *opts, &b)
  cmd = link_command('', opt)
  if $universal
    require 'tmpdir'
    Dir.mktmpdir('mkmf_', oldtmpdir = ENV['TMPDIR']) do |tmpdir|
      begin
        ENV['TMPDIR'] = tmpdir
        try_do_cxx(src, cmd, *opts, &b)
      ensure
        ENV['TMPDIR'] = oldtmpdir
      end
    end
  else
    try_do_cxx(src, cmd, *opts, &b)
  end && File.executable?(CONFTEST + $EXEEXT)
end