Revamp Windows build process

This commit is contained in:
Peter Wagenet 2017-03-03 12:18:27 -08:00
parent 4080132327
commit 0eba4fa4db
8 changed files with 59 additions and 19 deletions

View File

@ -1,12 +1,44 @@
use std::env;
use std::{env,fs};
use std::path::Path;
use std::process::Command;
// FIXME: Only do this on Windows
fn main() {
let target = env::var("TARGET").unwrap();
if target.contains("windows") {
let root = env::var("HELIX_ROOT").unwrap_or(String::from("."));
// FIXME: Don't hardcode Ruby version if possible
println!("cargo:rustc-flags=-L {root}/def -l dylib=msvcrt-ruby230 -L {root}/ruby/windows_build", root=root);
let root_str = env::var("HELIX_ROOT").unwrap_or(String::from("."));
let out_dir_str = env::var("OUT_DIR").expect("couldn't get OUT_DIR");
println!("out_dir_str: {}", out_dir_str);
let root = Path::new(root_str.as_str());
let out_dir = Path::new(out_dir_str.as_str());
// TODO: Clean this all up. There has to be a prettier way.
// Read info about current Ruby install
let raw_ruby_info = Command::new("ruby")
.arg(root.join("scripts/ruby_info.rb"))
.output()
.expect("failed to get Ruby info");
let raw_ruby_output = String::from_utf8_lossy(&raw_ruby_info.stdout);
let mut raw_ruby_lines = raw_ruby_output.lines();
let ruby_libdir = Path::new(raw_ruby_lines.next().expect("Ruby info has no libdir"));
let libruby = raw_ruby_lines.next().expect("Ruby info has no LIBRUBY");
let libruby_so = raw_ruby_lines.next().expect("Ruby info has no LIBRUBY_SO");
if raw_ruby_lines.next() != None {
panic!("Unexpected information returned in Ruby info");
}
let ruby_libname = libruby_so.split('.').next().expect("can't extract Ruby lib name");
// Copy .dll.a file to .lib since Rust msvc looks for .lib files only
fs::copy(ruby_libdir.join(libruby), out_dir.join(ruby_libname).with_extension("lib"))
.expect("unable to copy libruby");
// Set up linker
println!("cargo:rustc-flags=-L {libpath} -l dylib={libruby} -L {root}/ruby/windows_build",
libpath=out_dir.to_str().expect("can't get str from out_dir"),
libruby=ruby_libname,
root=root.to_str().expect("can't get str from root"));
}
}

View File

@ -35,8 +35,7 @@ impl RubyException {
pub const EMPTY_EXCEPTION: RubyException = RubyException(0);
// FIXME: Windows only
#[link(kind="dylib", name="helix_runtime")]
#[cfg_attr(windows, link(name="helix-runtime"))]
extern "C" {
#[link_name = "HELIX_Qfalse"]
pub static Qfalse: VALUE;

View File

@ -25,6 +25,7 @@ GEM
PLATFORMS
ruby
x64-mingw32
x86-mingw32
DEPENDENCIES

2
ruby/.gitignore vendored
View File

@ -7,8 +7,8 @@
/pkg/
/spec/reports/
/spec/support/dummy/lib
/windows_build
/tmp/
/windows_build
*.bundle
*.gem
Makefile

View File

@ -11,14 +11,24 @@ Rake::ExtensionTask.new do |ext|
ext.lib_dir = "lib/helix_runtime"
end
if RUBY_PLATFORM =~ /mingw/
Rake::Task["compile:native:#{RUBY_PLATFORM}"].enhance do
mkdir_p "#{__dir__}/windows_build"
res = system("lib -def:#{__dir__}/ext/helix_runtime/native/native.def -out:#{__dir__}/windows_build/helix-runtime.lib")
abort "can't build helix-runtime.lib" unless res
# Windows looks for a .dll in PATH
cp "#{__dir__}/lib/helix_runtime/native.so", "#{__dir__}/windows_build/helix-runtime.dll"
end
CLOBBER.include("windows_build")
end
Rake::ExtensionTask.new do |ext|
ext.name = "dummy"
ext.ext_dir = "spec/support/dummy/ext/dummy"
ext.lib_dir = "spec/support/dummy/lib"
end
CLOBBER.include("windows_build")
RSpec::Core::RakeTask.new(:rspec) do |t|
t.verbose = false
end

View File

@ -1,3 +1,4 @@
LIBRARY helix-runtime
EXPORTS
HELIX_Data_Get_Struct_Value
HELIX_Data_Set_Struct_Value

View File

@ -6,15 +6,7 @@ dir_config "dummy"
find_header "helix_runtime.h", "#{root_dir}/ext/helix_runtime/native"
if RUBY_PLATFORM =~ /mingw/
# Is this the correct place to run these commands?
FileUtils.mkdir_p("#{root_dir}/windows_build")
res = system("lib -def:#{root_dir}/ext/helix_runtime/native/native.def -out:#{root_dir}/windows_build/helix_runtime.lib")
abort "can't build helix_runtime.lib" unless res
append_ldflags("-L#{root_dir}/windows_build -lhelix_runtime")
# Windows looks for a .dll in PATH
FileUtils.cp "#{root_dir}/lib/helix_runtime/native.so", "#{root_dir}/windows_build/native.dll"
append_ldflags("-L#{root_dir}/windows_build -lhelix-runtime")
end
# Allows proper HELIX_EXTERN

5
scripts/ruby_info.rb Normal file
View File

@ -0,0 +1,5 @@
require 'rbconfig'
puts RbConfig::CONFIG['libdir'] # C:/Ruby23/lib
puts RbConfig::CONFIG['LIBRUBY'] # libmsvcrt-ruby230.dll.a
puts RbConfig::CONFIG['LIBRUBY_SO'] # msvcrt-ruby230.dll