Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ruby/private/binary/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package(default_visibility = ["//ruby/private:__pkg__"])

exports_files(
[
"binary_wrapper.tpl",
"binary_launcher.tpl",
"main_binary_wrapper.tpl",
],
visibility = ["//visibility:public"],
)
22 changes: 18 additions & 4 deletions ruby/private/binary/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def rb_binary_macro(ctx, main, srcs):

executable = ctx.actions.declare_file(ctx.attr.name)

launcher_name = ctx.attr.name + "-launcher"
launcher_executable = ctx.actions.declare_file(launcher_name)

deps = _transitive_deps(
ctx,
extra_files = [executable],
Expand All @@ -73,12 +76,12 @@ def rb_binary_macro(ctx, main, srcs):
rubyopt = reversed(deps.rubyopt.to_list())

ctx.actions.expand_template(
template = ctx.file._wrapper_template,
output = executable,
template = ctx.file._launcher_template,
output = launcher_executable,
substitutions = {
"{loadpaths}": repr(deps.incpaths.to_list()),
"{rule_name}": ctx.attr.name,
"{rubyopt}": repr(rubyopt),
"{main}": repr(_to_manifest_path(ctx, main)),
"{interpreter}": _to_manifest_path(ctx, interpreter),
"{gem_path}": gem_path,
"{bundle_path}": bundle_path,
Expand All @@ -88,9 +91,20 @@ def rb_binary_macro(ctx, main, srcs):
},
)

ctx.actions.expand_template(
template = ctx.file._wrapper_template,
output = executable,
substitutions = {
"{launcher_name}": launcher_name,
"{main}": repr(_to_manifest_path(ctx, main)),
},
)

runfiles = ctx.runfiles(files = [launcher_executable])
runfiles = runfiles.merge(deps.default_files.merge(deps.data_files))
info = DefaultInfo(
executable = executable,
runfiles = deps.default_files.merge(deps.data_files),
runfiles = runfiles,
)

return [info]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
require 'rbconfig'

def find_runfiles
stub_filename = File.absolute_path($0)
stub_dirname = File.dirname(File.absolute_path($0))
stub_filename = "#{stub_dirname}/{rule_name}"
runfiles = "#{stub_filename}.runfiles"
loop do
case
Expand Down Expand Up @@ -113,6 +114,8 @@ def setup_gem_path(runfiles)
end

def main(args)
raise "Must provide target to launch" if args.empty?

runfiles = find_runfiles

runfiles_envkey, runfiles_envvalue = runfiles_envvar(runfiles)
Expand All @@ -128,7 +131,7 @@ def main(args)

ruby_program = find_rb_binary

main = {main}
main = args.shift
main = File.join(runfiles, main)
rubyopt = {rubyopt}.map do |opt|
opt.gsub(/\${(.+?)}/o) do
Expand Down
23 changes: 23 additions & 0 deletions ruby/private/binary/main_binary_wrapper.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env ruby

if __FILE__ == $0
##
# Find the launcher
##

curr_file = File.absolute_path(__FILE__)
curr_dir = File.dirname(curr_file)

# Check if it is in this directory
if File.exists?("#{curr_dir}/{launcher_name}")
exec("#{curr_dir}/{launcher_name}", {main}, *ARGV)
end

# Check if this file is a symlink to the runfiles directory... then it will be there
if File.symlink?(curr_file) && %r!(.*\.runfiles)/.*!o =~ File.readlink(curr_file)
runfiles_dir = File.dirname(File.readlink(curr_file))
exec("#{runfiles_dir}/{launcher_name}", {main}, *ARGV)
end

raise "Could not find launcher file! This is likely an issue with rules_ruby"
end
6 changes: 5 additions & 1 deletion ruby/private/constants.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ RUBY_ATTRS = {
),
"_wrapper_template": attr.label(
allow_single_file = True,
default = "//ruby/private/binary:binary_wrapper.tpl",
default = "//ruby/private/binary:main_binary_wrapper.tpl",
),
"_launcher_template": attr.label(
allow_single_file = True,
default = "//ruby/private/binary:binary_launcher.tpl",
),
"_misc_deps": attr.label_list(
allow_files = True,
Expand Down