Class: LIBUSB::CrossLibrary

Inherits:
OpenStruct
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/libusb/gem_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(ruby_platform, host_platform, libusb_dllname) ⇒ CrossLibrary

Returns a new instance of CrossLibrary.



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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/libusb/gem_helper.rb', line 85

def initialize(ruby_platform, host_platform, libusb_dllname)
  super()

  self.ruby_platform = ruby_platform
  self.recipe = LIBUSB::LibusbRecipe.new
  recipe.host = ruby_platform
  recipe.configure_options << "--host=#{host_platform}"
  recipe.configure_options << "CC=#{host_platform}-gcc -static-libgcc" if recipe.host =~ /mingw/
  self.libusb_dll = Pathname.new(recipe.path) + libusb_dllname

  file libusb_dll do
    recipe.cook
  end

  task "libusb_dll:#{ruby_platform}" => libusb_dll

  desc 'Cross compile libusb for all targets'
  task :cross => "cross:#{ruby_platform}"

  desc "Cross compile libusb for #{ruby_platform}"
  task "cross:#{ruby_platform}" => [ "libusb_dll:#{ruby_platform}" ] do |t|
    spec = Gem::Specification::load("libusb.gemspec").dup
    spec.platform = Gem::Platform.new(ruby_platform)
    spec.extensions = []

    # Remove files unnecessary for native gems
    spec.files -= `git ls-files ext`.split("\n")
    spec.files.reject!{|f| f.start_with?('ports') }
    spec_text_files = spec.files.dup

    # Add native libusb-dll
    spec.files << "lib/#{libusb_dll.basename}"

    # MiniPortile isn't required for native gems
    spec.dependencies.reject!{|d| d.name=="mini_portile2" }

    # Generate a package for this gem
    pkg = Gem::PackageTask.new(spec) do |pkg|
      pkg.need_zip = false
      pkg.need_tar = false
      # Do not copy any files per PackageTask, because
      # we need the files from the platform specific directory
      pkg.package_files.clear
    end

    # copy files of the gem to pkg directory
    file pkg.package_dir_path => spec_text_files do
      spec_text_files.each do |fn|
        f = File.join(pkg.package_dir_path, fn)
        fdir = File.dirname(f)
        mkdir_p(fdir) if !File.exist?(fdir)
        rm_f f
        safe_ln(fn, f)
      end

      # copy libusb.dll to pkg directory
      f = "#{pkg.package_dir_path}/lib/#{libusb_dll.basename}"
      mkdir_p File.dirname(f)
      rm_f f
      safe_ln libusb_dll.realpath, f
    end

    file "lib/#{libusb_dll.basename}" => [libusb_dll]
  end
end