Skip to content

Commit 7f5b532

Browse files
committed
Integrated ocamlc and ocamlbuild into the binary and interface rules
1 parent d46181a commit 7f5b532

File tree

3 files changed

+121
-85
lines changed

3 files changed

+121
-85
lines changed

WORKSPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
workspace(name = "io_bazel_rules_ocaml")
22

3-
load("//ocaml:ocaml.bzl", "ocaml_repositories")
3+
load("//ocaml:repo.bzl", "ocaml_repositories")
44
ocaml_repositories()

ocaml/ocaml.bzl

Lines changed: 17 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
11
OCAML_FILETYPES = FileType([".ml"])
22
OCAML_VERSION = "4.03.0"
33

4-
_opam_binary_attrs = {
4+
_ocaml_toolchain_attrs = {
55
"_opam": attr.label(
66
default = Label("@opam//:opam"),
77
executable = True,
88
single_file = True,
99
allow_files = True,
1010
cfg = "host",
1111
),
12-
}
13-
14-
_ocaml_toolchain_attrs = {
1512
"_ocamlc": attr.label(
1613
default = Label("@ocaml_toolchain//:ocamlc"),
1714
executable = True,
1815
single_file = True,
1916
allow_files = True,
2017
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",
2125
)
22-
} + _opam_binary_attrs
26+
}
2327

2428
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],
2931
outputs = [ctx.outputs.mli],
30-
progress_message = "ocaml %s" % ctx.label,
32+
progress_message = "Compiling interface file %s" % ctx.label,
3133
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),
3335
)
3436

35-
return struct(mli = ctx.outputs.mli)
37+
return struct(mli = ctx.outputs.mli.path)
3638

3739
ocaml_interface = rule(
3840
implementation = _ocaml_interface_impl,
@@ -65,12 +67,9 @@ def _get_src_root(ctx, root_file_names = ["main.ml"]):
6567
def _ocaml_binary_impl(ctx):
6668
src_root = _get_src_root(ctx)
6769
src = _strip_ml_extension(src_root.path)
68-
ocamlbuild_bin = "ocamlbuild"
70+
ocamlbuild = ctx.executable._ocamlbuild
6971
opts = "-build-dir %s" % ctx.outputs.build_dir.path
7072

71-
opam_path = ctx.executable._opam
72-
print(opam_path)
73-
7473
if (ctx.attr.bin_type == "native"):
7574
target_bin = "%s.native" % src
7675
else:
@@ -87,10 +86,10 @@ def _ocaml_binary_impl(ctx):
8786
# opam_command = " ".join([opam_path, "install"] + opam_packages + ["ocamlbuild", "&&"])
8887

8988
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])
9190

9291
ctx.action(
93-
inputs = ctx.files.srcs,
92+
inputs = ctx.files.srcs + [ocamlbuild],
9493
command = command,
9594
outputs = [ctx.outputs.executable, ctx.outputs.build_dir],
9695
use_default_shell_env=True,
@@ -133,69 +132,3 @@ def ocaml_bytecode_binary(name, srcs, **kwargs):
133132
**kwargs
134133
)
135134

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()

ocaml/repo.bzl

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
OCAML_VERSION = "4.04.0"
2+
OCAMLBUILD_VERSION = "0.11.0"
3+
COMPILER_NAME = "ocaml-base-compiler.%s" % OCAML_VERSION
4+
DEBUG_QUIET = True
5+
6+
_opam_binary_attrs = {
7+
"_opam": attr.label(
8+
default = Label("@opam//:opam"),
9+
executable = True,
10+
single_file = True,
11+
allow_files = True,
12+
cfg = "host",
13+
),
14+
}
15+
16+
# Set up OCaml's toolchain (ocamlc, ocamlbuild, ocamlfind)
17+
_OCAML_TOOLCHAIN_BUILD = """
18+
filegroup(
19+
name = "ocamlc",
20+
srcs = ["opam_dir/{compiler}/bin/ocamlc"],
21+
visibility = ["//visibility:public"],
22+
)
23+
24+
filegroup(
25+
name = "ocamlbuild",
26+
srcs = ["opam_dir/{compiler}/bin/ocamlbuild"],
27+
visibility = ["//visibility:public"],
28+
)
29+
""".format(compiler = COMPILER_NAME)
30+
31+
def _ocaml_toolchain_impl(repository_ctx):
32+
opam_dir = "opam_dir"
33+
opam_path = repository_ctx.path(repository_ctx.attr._opam)
34+
35+
# Initialize opam and its root directory
36+
repository_ctx.execute([
37+
opam_path,
38+
"init",
39+
"--root", opam_dir,
40+
"--no-setup",
41+
"--comp", COMPILER_NAME
42+
], quiet = DEBUG_QUIET)
43+
44+
# Download the OCaml compiler
45+
repository_ctx.execute([
46+
opam_path,
47+
"switch", COMPILER_NAME,
48+
"--root", opam_dir
49+
], quiet = DEBUG_QUIET)
50+
51+
# Install OCamlbuild
52+
repository_ctx.execute([
53+
opam_path,
54+
"install",
55+
"ocamlbuild=%s" % OCAMLBUILD_VERSION,
56+
"--yes",
57+
"--root", opam_dir
58+
], quiet = DEBUG_QUIET)
59+
60+
repository_ctx.file("WORKSPACE", "", False)
61+
repository_ctx.file("BUILD", _OCAML_TOOLCHAIN_BUILD, False)
62+
63+
_ocaml_toolchain_repo = repository_rule(
64+
implementation = _ocaml_toolchain_impl,
65+
attrs = _opam_binary_attrs,
66+
)
67+
68+
def _ocaml_toolchain():
69+
_ocaml_toolchain_repo(name = "ocaml_toolchain")
70+
71+
# Set up OPAM
72+
def _opam_binary_impl(repository_ctx):
73+
os_name = repository_ctx.os.name.lower()
74+
if os_name.find("windows") != -1:
75+
fail("Windows is not supported yet, sorry!")
76+
elif os_name.startswith("mac os"):
77+
repository_ctx.download(
78+
"https://github.com/ocaml/opam/releases/download/2.0.0-beta4/opam-2.0.0-beta4-x86_64-darwin",
79+
"opam",
80+
"d23c06f4f03de89e34b9d26ebb99229a725059abaf6242ae3b9e9bf946b445e1",
81+
executable = True,
82+
)
83+
else:
84+
repository_ctx.download(
85+
"https://github.com/ocaml/opam/releases/download/2.0.0-beta4/opam-2.0.0-beta4-x86_64-linux",
86+
"opam",
87+
"3de4b78a263d4c1e46760c26bdc2b02fdbce980a9fc9141385058c2b0174708c",
88+
executable = True,
89+
)
90+
repository_ctx.file("WORKSPACE", "", False)
91+
repository_ctx.file("BUILD", "exports_files([\"opam\"])", False)
92+
93+
_opam_binary_repo = repository_rule(
94+
implementation = _opam_binary_impl,
95+
attrs = {}
96+
)
97+
98+
def _opam_binary():
99+
_opam_binary_repo(name = "opam")
100+
101+
def ocaml_repositories():
102+
_opam_binary()
103+
_ocaml_toolchain()

0 commit comments

Comments
 (0)