1
1
OCAML_FILETYPES = FileType ([".ml" ])
2
2
OCAML_VERSION = "4.03.0"
3
3
4
- _opam_binary_attrs = {
4
+ _ocaml_toolchain_attrs = {
5
5
"_opam" : attr .label (
6
6
default = Label ("@opam//:opam" ),
7
7
executable = True ,
8
8
single_file = True ,
9
9
allow_files = True ,
10
10
cfg = "host" ,
11
11
),
12
- }
13
-
14
- _ocaml_toolchain_attrs = {
15
12
"_ocamlc" : attr .label (
16
13
default = Label ("@ocaml_toolchain//:ocamlc" ),
17
14
executable = True ,
18
15
single_file = True ,
19
16
allow_files = True ,
20
17
cfg = "host" ,
18
+ ),
19
+ "_ocamlbuild" : attr .label (
20
+ default = Label ("@ocaml_toolchain//:ocamlbuild" ),
21
+ executable = True ,
22
+ single_file = True ,
23
+ allow_files = True ,
24
+ cfg = "host" ,
21
25
)
22
- } + _opam_binary_attrs
26
+ }
23
27
24
28
def _ocaml_interface_impl (ctx ):
25
- ctx .run_shell (
26
- # executable = ctx.executable._ocamlc,
27
- # arguments = ["-i", "-c"], #, "-o", ctx.outputs.mli.path, ctx.file.src.path],
28
- inputs = [ctx .file .src ],
29
+ ctx .actions .run_shell (
30
+ inputs = [ctx .file .src , ctx .executable ._ocamlc ],
29
31
outputs = [ctx .outputs .mli ],
30
- progress_message = "ocaml %s" % ctx .label ,
32
+ progress_message = "Compiling interface file %s" % ctx .label ,
31
33
mnemonic = "OCamlc" ,
32
- command = "ocamlc -i -c %s > %s" % (ctx .file .src , ctx .outputs .mli ),
34
+ command = "%s -i -c %s > %s" % (ctx .executable . _ocamlc . path , ctx . file .src . path , ctx .outputs .mli . path ),
33
35
)
34
36
35
- return struct (mli = ctx .outputs .mli )
37
+ return struct (mli = ctx .outputs .mli . path )
36
38
37
39
ocaml_interface = rule (
38
40
implementation = _ocaml_interface_impl ,
@@ -65,12 +67,9 @@ def _get_src_root(ctx, root_file_names = ["main.ml"]):
65
67
def _ocaml_binary_impl (ctx ):
66
68
src_root = _get_src_root (ctx )
67
69
src = _strip_ml_extension (src_root .path )
68
- ocamlbuild_bin = "ocamlbuild"
70
+ ocamlbuild = ctx . executable . _ocamlbuild
69
71
opts = "-build-dir %s" % ctx .outputs .build_dir .path
70
72
71
- opam_path = ctx .executable ._opam
72
- print (opam_path )
73
-
74
73
if (ctx .attr .bin_type == "native" ):
75
74
target_bin = "%s.native" % src
76
75
else :
@@ -87,10 +86,10 @@ def _ocaml_binary_impl(ctx):
87
86
# opam_command = " ".join([opam_path, "install"] + opam_packages + ["ocamlbuild", "&&"])
88
87
89
88
mv_command = "&& cp -L %s %s" % (intermediate_bin , ctx .outputs .executable .path )
90
- command = " " .join ([ocamlbuild_bin , opts , pkgs , target_bin , mv_command ])
89
+ command = " " .join ([ocamlbuild . path , opts , pkgs , target_bin , mv_command ])
91
90
92
91
ctx .action (
93
- inputs = ctx .files .srcs ,
92
+ inputs = ctx .files .srcs + [ ocamlbuild ] ,
94
93
command = command ,
95
94
outputs = [ctx .outputs .executable , ctx .outputs .build_dir ],
96
95
use_default_shell_env = True ,
@@ -133,69 +132,3 @@ def ocaml_bytecode_binary(name, srcs, **kwargs):
133
132
** kwargs
134
133
)
135
134
136
- # Set up OCaml's toolchain (ocamlc, ocamlbuild, ocamlfind)
137
- _OCAML_TOOLCHAIN_BUILD = """
138
- filegroup(
139
- name = "ocamlc",
140
- srcs = ["opam_dir/4.03.0/bin/ocamlc"],
141
- visibility = ["//visibility:public"],
142
- )
143
-
144
- filegroup(
145
- name = "ocamlbuild",
146
- srcs = ["opam_dir/4.03.0/bin/ocamlbuild"],
147
- visibility = ["//visibility:public"],
148
- )
149
- """
150
-
151
- def _ocaml_toolchain_impl (repository_ctx ):
152
- opam_dir = "opam_dir"
153
- opam_path = repository_ctx .path (repository_ctx .attr ._opam )
154
- repository_ctx .execute ([opam_path , "init" , "--root" , opam_dir , "--no-setup" ], quiet = False )
155
- repository_ctx .execute ([opam_path , "switch" , "create" , "4.03.0" , "ocaml-base-compiler.4.03.0" , "--root" , opam_dir ], quiet = False )
156
- repository_ctx .execute ([opam_path , "install" , "ocamlbuild" , "--yes" , "--root" , opam_dir ], quiet = False )
157
- # repository_ctx.execute([opam_path, "install", "ocamlbuild", "--yes", "--root", opam_dir], quiet = False)
158
- repository_ctx .file ("WORKSPACE" , "" , False )
159
- repository_ctx .file ("BUILD" , _OCAML_TOOLCHAIN_BUILD , False )
160
-
161
- _ocaml_toolchain = repository_rule (
162
- implementation = _ocaml_toolchain_impl ,
163
- attrs = _opam_binary_attrs ,
164
- )
165
-
166
- def ocaml_toolchain ():
167
- _ocaml_toolchain (name = "ocaml_toolchain" )
168
-
169
- # Set up OPAM
170
- def _opam_binary_impl (repository_ctx ):
171
- os_name = repository_ctx .os .name .lower ()
172
- if os_name .find ("windows" ) != - 1 :
173
- fail ("Windows is not supported yet, sorry!" )
174
- elif os_name .startswith ("mac os" ):
175
- repository_ctx .download (
176
- "https://github.com/ocaml/opam/releases/download/2.0-alpha4/opam-2.0-alpha4-x86_64-Darwin" ,
177
- "opam" ,
178
- "70120e5ded040ddad16914ee56180a2be9c7d64e332f16f7a6f47c41069d9e93" ,
179
- executable = True ,
180
- )
181
- else :
182
- repository_ctx .download (
183
- "https://github.com/ocaml/opam/releases/download/2.0-alpha4/opam-2.0-alpha4-x86_64-Linux" ,
184
- "opam" ,
185
- "3171aa1b10df13aa657cffdd5c616f8e5a7c624f8335de72db2e28db51435fe0" ,
186
- executable = True ,
187
- )
188
- repository_ctx .file ("WORKSPACE" , "" , False )
189
- repository_ctx .file ("BUILD" , "exports_files([\" opam\" ])" , False )
190
-
191
- _opam_binary = repository_rule (
192
- implementation = _opam_binary_impl ,
193
- attrs = {}
194
- )
195
-
196
- def opam_binary ():
197
- _opam_binary (name = "opam" )
198
-
199
- def ocaml_repositories ():
200
- opam_binary ()
201
- ocaml_toolchain ()
0 commit comments