blob: 2c935968f195828e195fb3cb9b4d32104e5da4ef [file] [log] [blame]
Brian Silvermanb67da232015-09-12 23:50:30 -04001ZIP_PATH = '/usr/bin/zip'
2
3ruby_file_types = FileType(['.rb'])
4
5def _collect_transitive_sources(ctx):
6 source_files = set(order='compile')
7 for dep in ctx.attr.deps:
8 source_files += dep.transitive_ruby_files
9
10 source_files += ruby_file_types.filter(ctx.files.srcs)
11 return source_files
12
13def _ruby_library_impl(ctx):
14 transitive_sources = _collect_transitive_sources(ctx)
15 return struct(
16 transitive_ruby_files = transitive_sources,
17 )
18
19def _ruby_binary_impl(ctx):
20 main_file = ctx.files.srcs[0]
21 transitive_sources = _collect_transitive_sources(ctx)
22 executable = ctx.outputs.executable
23 manifest_file = ctx.outputs.manifest
24
25 path_map = '\n'.join([('%s|%s' % (item.short_path, item.path))
26 for item in transitive_sources])
27 ctx.file_action(output=manifest_file, content=path_map)
28
29 ctx.action(
30 inputs = list(transitive_sources) + [manifest_file],
31 outputs = [ executable ],
32 arguments = [ manifest_file.path, executable.path, main_file.path ],
33 executable = ctx.executable._ruby_linker,
34 )
35
36 return struct(
37 files = set([executable]),
38 runfiles = ctx.runfiles(collect_data = True),
39 )
40
41
42_ruby_attrs = {
43 'srcs': attr.label_list(
44 allow_files = ruby_file_types,
45 mandatory = True,
46 non_empty = True,
47 ),
48 'deps': attr.label_list(
49 providers = ['transitive_ruby_files'],
50 allow_files = False,
51 ),
52 'data': attr.label_list(
53 allow_files = True,
Brian Silvermanb200c172017-01-02 17:35:35 -080054 cfg = 'data',
Brian Silvermanb67da232015-09-12 23:50:30 -040055 ),
56}
57
58'''Packages ruby code into a library.
59
60The files can use require with paths from the base of the workspace and/or
61require_relative with other files that are part of the library.
62require also works from the filesystem as usual.
63
64Attrs:
65 srcs: Ruby source files to include.
66 deps: Other ruby_library rules to include.
67'''
68ruby_library = rule(
69 implementation = _ruby_library_impl,
70 attrs = _ruby_attrs,
71)
72
73'''Packages ruby code into a binary which can be run.
74
75See ruby_library for details on how require works.
76
77Attrs:
78 srcs: Ruby source files to include. The first one is loaded to at startup.
79 deps: ruby_library rules to include.
80'''
81ruby_binary = rule(
82 implementation = _ruby_binary_impl,
83 executable = True,
84 attrs = _ruby_attrs + {
85 '_ruby_linker': attr.label(
86 executable = True,
Brian Silvermanb200c172017-01-02 17:35:35 -080087 cfg = 'host',
Brian Silvermanb67da232015-09-12 23:50:30 -040088 default = Label('//tools/ruby:standalone_ruby'),
89 )
90 },
91 outputs = {
92 'manifest': '%{name}.tar_manifest',
93 },
94)