Class: HelixRuntime::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/helix_runtime/project.rb

Defined Under Namespace

Classes: OutdatedBuildError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Project

Returns a new instance of Project.



17
18
19
20
21
# File 'lib/helix_runtime/project.rb', line 17

def initialize(root)
  @root = find_root(root)
  @debug_rust = ENV['DEBUG_RUST']
  @build_root = @root
end

Instance Attribute Details

#build_rootObject

Returns the value of attribute build_root.



15
16
17
# File 'lib/helix_runtime/project.rb', line 15

def build_root
  @build_root
end

#debug_rustObject

Returns the value of attribute debug_rust.



14
15
16
# File 'lib/helix_runtime/project.rb', line 14

def debug_rust
  @debug_rust
end

#helix_lib_dirObject

Returns the value of attribute helix_lib_dir.



13
14
15
# File 'lib/helix_runtime/project.rb', line 13

def helix_lib_dir
  @helix_lib_dir
end

#rootObject

Returns the value of attribute root.



12
13
14
# File 'lib/helix_runtime/project.rb', line 12

def root
  @root
end

Instance Method Details

#autobuildObject



65
66
67
# File 'lib/helix_runtime/project.rb', line 65

def autobuild
  build if outdated_build?
end

#buildObject



123
124
125
# File 'lib/helix_runtime/project.rb', line 123

def build
  cargo_build && copy_native
end

#build_pathObject



35
36
37
# File 'lib/helix_runtime/project.rb', line 35

def build_path
  File.expand_path(debug_rust? ? 'target/debug' : 'target/release', build_root)
end

#cargo_buildObject



69
70
71
72
73
74
75
76
77
78
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
# File 'lib/helix_runtime/project.rb', line 69

def cargo_build
  HelixRuntime.ensure_dll!

  # We have to do this here since Cargo has no internal means of specifying `-C` flags
  link_args = if IS_WINDOWS
    # SAFESEH is added to i686 Rust hosts
    # https://github.com/rust-lang/rust/blob/1.15.1/src/librustc_back/target/i686_pc_windows_msvc.rs#L25
    if `rustc -vV` =~ /host:\s+i686/
      '/SAFESEH:NO' # Can't use SAFESEH with .libs from dlltool
    end
  else
    # Allowing all methods to be undefined is a bit risky, would be nice to have a specific list.
    '-Wl,-undefined,dynamic_lookup'
  end

  env = {}
  env['HELIX_LIB_DIR'] = helix_lib_dir if helix_lib_dir

  cargo_args = []
  rustc_args = []

  if ENV['DEBUG_RUST_MACROS']
    rustc_args << "--pretty expanded"
    rustc_args << "-Z unstable-options"
  end
  unless debug_rust?
    cargo_args << ["--release"]
  end
  if ENV['VERBOSE']
    cargo_args << " --verbose"
  end
  if link_args
    rustc_args << "-C link-args=#{link_args}"
  end

  unless rustc_args.empty?
    cargo_args << "-- #{rustc_args.join(' ')}"
  end

  run env, "cargo rustc #{cargo_args.join(' ')}"
end

#cargo_cleanObject



111
112
113
# File 'lib/helix_runtime/project.rb', line 111

def cargo_clean
  run("cargo clean")
end

#cargo_toml_pathObject



31
32
33
# File 'lib/helix_runtime/project.rb', line 31

def cargo_toml_path
  "#{root}/Cargo.toml"
end

#clobberObject



127
128
129
130
# File 'lib/helix_runtime/project.rb', line 127

def clobber
  cargo_clean
  FileUtils.rm_f native_path
end

#copy_nativeObject



115
116
117
118
119
120
121
# File 'lib/helix_runtime/project.rb', line 115

def copy_native
  source = "#{build_path}/#{native_lib}"
  raise "native source doesn't exist, run `cargo_build` first; source=#{source}" unless File.exist?(source)
  FileUtils.mkdir_p(File.dirname(native_path))
  FileUtils.cp source, native_path
  true
end

#debug_rust?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/helix_runtime/project.rb', line 23

def debug_rust?
  !!debug_rust
end

#ensure_built!Object

Raises:



61
62
63
# File 'lib/helix_runtime/project.rb', line 61

def ensure_built!
  raise OutdatedBuildError.new(name) if outdated_build?
end

#lib_pathObject



39
40
41
# File 'lib/helix_runtime/project.rb', line 39

def lib_path
  "#{root}/lib/#{name}"
end

#libfile_prefixObject



43
44
45
# File 'lib/helix_runtime/project.rb', line 43

def libfile_prefix
  IS_WINDOWS ? '' : 'lib'
end

#nameObject



27
28
29
# File 'lib/helix_runtime/project.rb', line 27

def name
  @name ||= Tomlrb.load_file(cargo_toml_path)["package"]["name"]
end

#native_libObject



51
52
53
# File 'lib/helix_runtime/project.rb', line 51

def native_lib
  "#{libfile_prefix}#{name.gsub('-', '_')}.#{Platform.libext}"
end

#native_pathObject



47
48
49
# File 'lib/helix_runtime/project.rb', line 47

def native_path
  "#{lib_path}/native.#{Platform.dlext}"
end

#outdated_build?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
# File 'lib/helix_runtime/project.rb', line 55

def outdated_build?
  mtime = Dir["#{root}/src/**/*.rs"].map{|file| File.mtime(file) }.max
  native = "#{root}/lib/#{name}/native.#{Platform.dlext}"
  !File.exist?(native) || File.mtime(native) < mtime
end