Skip to content
This repository has been archived by the owner on Feb 8, 2022. It is now read-only.

Commit

Permalink
add files
Browse files Browse the repository at this point in the history
  • Loading branch information
tamuratak committed Feb 22, 2016
1 parent d480184 commit 57d79b6
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'rake/testtask'
require 'rake/clean'

Rake::TestTask.new do |t|
t.libs << 'test'
end
desc "Run tests"

task :default => [:test]

21 changes: 21 additions & 0 deletions bin/mkmf-cu-nvcc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env ruby
require "mkmf-cu/opt"

opt, opt_h = build_optparser()

$stderr.puts 'given command line options: ' + ARGV.join(' ')
parse_ill_short(ARGV, opt_h)
parse_ill_short_with_arg(ARGV, opt_h)

argv = opt.parse(ARGV)

if opt_h["-c"].size > 0
s = generate_compiling_command_line(opt_h)
elsif opt_h['-dynamic'].size > 0 or opt_h['-shared'].size > 0
s = generate_linking_command_line(argv, opt_h)
else
raise
end

$stderr.puts "nvcc" + s
Open3.capture2("/Developer/NVIDIA/CUDA-7.5/bin/nvcc " + s)
6 changes: 6 additions & 0 deletions lib/mkmf-cu.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "mkmf"

MakeMakefile::CONFIG["CC"] = "mkmf-cu-nvcc --mkmf-cu-ext=c"
MakeMakefile::CONFIG["CXX"] = "mkmf-cu-nvcc --mkmf-cu-ext=cxx"
MakeMakefile::CXX_EXT << "cu"
MakeMakefile::SRC_EXT << "cu"
132 changes: 132 additions & 0 deletions lib/mkmf-cu/opt.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
require "optparse"
require "rbconfig"
require "open3"


def build_optparser

opt = OptionParser.new
opt_h = Hash.new{|h, k| h[k] = [] }

opt.on('-I path') {|v| opt_h["-I"] << v }
opt.on('-D flag') {|v| opt_h["-D"] << v }
opt.on('-W flag') {|v| opt_h["-W"] << v }
opt.on('-o output') {|v| opt_h["-o"] << v }
opt.on('-c file') {|v| opt_h["-c"] << v }
opt.on('-f flag') {|v| opt_h["-f"] << v }
opt.on('-l file') {|v| opt_h["-l"] << v }
opt.on('-L path') {|v| opt_h["-L"] << v }
opt.on('-x pat', "--x pat") {|v| opt_h["-x"] << v }
opt.on('-O num'){|v| opt_h["-O"] << v if /[0-9]/ =~ v }
opt.on('--mkmf-cu-ext ext'){|v| opt_h["--mkmf-cu-ext"] << v}
return [opt, opt_h]
end

def parse_ill_short(argv, opt_h)
["-arch", "-shared", "-rdynamic", "-dynamic", "-bundle", "-stdlib", "-pipe"].each{|opt|
if ind = argv.find_index(opt)
if "-arch" == opt
opt_h[opt] << "-arch," + argv[ind+1]
argv.delete_at(ind)
argv.delete_at(ind)
else
opt_h[opt] << opt
argv.delete_at(ind)
end
end
}
end

def parse_ill_short_with_arg(argv, opt_h)
added = []
argv.each_with_index{|e, i|
if /\A\-stdlib=.*/ =~ e
added << e
opt_h["-stdlib"] << e
elsif /\A\-Wl,(.*)/ =~ e
added << e
opt_h["-Wl"] << $1
end
}
added.each{|e|
argv.delete(e)
}
end

def compiler_option(opt_h)
ret = ""
["-f", "-W"].each{|op|
opt_h[op].each{|e|
ret << " --compiler-options " + "#{op}#{e}"
}
}
["-stdlib"].each{|op|
opt_h[op].each{|e|
ret << " --compiler-options " + e
}
}
return ret
end

def linker_option(opt_h)
ret = ""
["-dynamic", "-bundle", "-shared", "-rdynamic"].each{|op|
opt_h[op].each{|e|
ret << " --linker-options " + op
}
}

opt_h["-Wl"].each{|e|
ret << " --linker-options " + e
}

return ret
end

def generate_compiling_command_line(opt_h)
s = ""
["-x", "-I", "-D", "-o", "-c", "-O"].each{|op|
opt_h[op].each{|e|
case op
when "-o", "-c", "-x"
s << " #{op} #{e}"
else
s << " #{op}#{e}"
end
}
}
s << compiler_option(opt_h)

if opt_h["--mkmf-cu-ext"][0] == "c"
s << " --compiler-bindir " + RbConfig::CONFIG["CC"]
elsif opt_h["--mkmf-cu-ext"][0] == "cxx"
s << " --compiler-bindir " + RbConfig::CONFIG["CXX"]
end

return s
end

def generate_linking_command_line(argv, opt_h)
s = ""
["-L", "-l", "-o", "-c", "-O"].each{|op|
opt_h[op].each{|e|
case op
when "-o", "-c"
s << " #{op} #{e}"
s << " " + argv[0] + " " if op == "-o"
else
s << " #{op}#{e}"
end
}
}
s << compiler_option(opt_h)
s << linker_option(opt_h)

if opt_h["--mkmf-cu-ext"][0] == "c"
s << " --compiler-bindir " + RbConfig::CONFIG["CC"]
elsif opt_h["--mkmf-cu-ext"][0] == "cxx"
s << " --compiler-bindir " + RbConfig::CONFIG["CXX"]
end

return s
end
14 changes: 14 additions & 0 deletions mkmf-cu.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Gem::Specification.new do |s|
s.name = 'mkmf-cu'
s.version = '0.1.0'
s.date = '2016-02-21'
s.summary = "mkmf wrapper for CUDA"
s.description = "mkmf wrapper for CUDA"
s.authors = ["Takashi Tamura"]
s.email = ''
s.files = ["lib/mkmf-cu.rb", "lib/mkmf-cu/opt.rb"]
s.homepage =
'http://rubygems.org/gems/hola'
s.license = 'MIT'
s.executables << "mkmf-cu-nvcc"
end
20 changes: 20 additions & 0 deletions test/test_mkmf-cu.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "test/unit"
require "mkmf-cu/opt"

class TestMkmfCuOpt < Test::Unit::TestCase
def test_opt
opt_h = Hash.new{|h, k| h[k] = [] }
argv = ["--mkmf-cu-ext=cxx", "-I.", "-I/opt/local/include/ruby-2.3.0/x86_64-darwin13", "-I/opt/local/include/ruby-2.3.0/ruby/backward", "-I/opt/local/include/ruby-2.3.0", "-I../../../../ext/culib", "-I/opt/local/include", "-I/opt/local/include", "-D_XOPEN_SOURCE", "-D_DARWIN_C_SOURCE", "-D_DARWIN_UNLIMITED_SELECT", "-D_REENTRANT", "-fno-common", "-pipe", "-Os", "-stdlib=libc++", "-x", "cu", "-O2", "-Wall", "-I/Developer/NVIDIA/CUDA-7.5/samples/common/inc/", "-arch", "x86_64", "-o", "culib.o", "-c", "../../../../ext/culib/culib.cxx"]
# p argv
parse_ill_short(argv, opt_h)
p argv
p opt_h

opt_h = Hash.new{|h, k| h[k] = [] }
argv = ["--mkmf-cu-ext=cxx", "-dynamic", "-bundle", "-o", "culib.bundle", "culib.o", "-L.", "-L/opt/local/lib", "-L/opt/local/lib", "-L.", "-L/opt/local/lib", "-Wl,-headerpad_max_install_names", "-fstack-protector", "-L/opt/local/lib", "-Wl,-undefined,dynamic_lookup", "-Wl,-multiply_defined,suppress", "-L/opt/local/lib", "-arch", "x86_64", "-lruby.2.3.0", "-lc++", "-lpthread", "-ldl", "-lobjc"]
parse_ill_short_with_arg(argv, opt_h)
assert_equal(["-headerpad_max_install_names", "-undefined,dynamic_lookup", "-multiply_defined,suppress"].sort,
opt_h["-Wl"].sort)

end
end

0 comments on commit 57d79b6

Please sign in to comment.