1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
type t = {
depext : bool;
build_dir : Fpath.t;
builder_name : string;
unikernel_opam_name : Misc.Name.Opam.t;
extra_repo : (string * string) list;
}
let v ?( = []) ~build_dir ~builder_name ~depext unikernel_opam_name =
{ depext; build_dir; builder_name; unikernel_opam_name; extra_repo }
let depext_rules =
{|
depext-lockfile: $(MIRAGE_DIR)/$(UNIKERNEL_NAME).opam.locked
echo " ↳ install external dependencies for monorepo"
env OPAMVAR_monorepo="opam-monorepo" $(OPAM) monorepo depext -y -l $<
|}
let opam_repo_add_rule =
let buf = Buffer.create 0x100 in
let ppf = Format.formatter_of_buffer buf in
Fmt.pf ppf
{|repo-add:
@@printf "\033[2musing overlay repository mirage: %a \033[0m\n"
|}
Fmt.(brackets (list ~sep:(any ", ") (using fst string)))
extra;
List.iter
(fun (name, repo) ->
Fmt.pf ppf "\t$(OPAM) repo add %s %s || $(OPAM) repo set-url %s %s\n" name
repo name repo)
extra;
Buffer.contents buf
let opam_repo_remove_rule =
let buf = Buffer.create 0x100 in
let ppf = Format.formatter_of_buffer buf in
Fmt.pf ppf
{|repo-rm:
@@printf "\033[2mremoving overlay repository %a\033[0m\n"
|}
Fmt.(brackets (list ~sep:(any ", ") (using fst string)))
extra;
List.iter
(fun (name, repo) -> Fmt.pf ppf "\t$(OPAM) repo remove %s %s\n" name repo)
extra;
Buffer.contents buf
let ppf t =
let rules, targets =
match t.depext with
| true -> ([ depext_rules ], [ "depext-lockfile" ])
| false -> ([], [])
in
let rules, targets =
match t.extra_repo with
| _ :: _ as ->
( opam_repo_add_rule extra :: opam_repo_remove_rule extra :: rules,
"repo-add" :: "repo-rm" :: targets )
| [] -> (rules, targets)
in
match rules with
| [] -> ()
| _ ->
Fmt.pf ppf " %a\n\n"
(Fmt.list ~sep:(fun ppf () -> Fmt.pf ppf " ") Fmt.string)
targets;
Fmt.pf ppf "%a"
(Fmt.list ~sep:(fun ppf () -> Fmt.pf ppf "\n\n") Fmt.string)
rules
let pp ppf t =
let mirage_dir = Fpath.(t.build_dir / t.builder_name) in
let pp_depext_lockfile ppf = function
| true -> Fmt.string ppf "\n\t@$(MAKE) -s depext-lockfile"
| false -> ()
and pp_no_depext ppf = function
| true -> ()
| false -> Fmt.string ppf " --no-depexts"
and pp_add_repo ppf = function
| _ :: _ -> Fmt.string ppf "\n\t@$(MAKE) -s repo-add"
| [] -> ()
and pp_or_remove_repo ppf = function
| _ :: _ -> Fmt.string ppf "; (ret=$$?; $(MAKE) -s repo-rm && exit $$ret)"
| [] -> ()
in
Fmt.pf ppf
{|-include Makefile.user
BUILD_DIR = %a
MIRAGE_DIR = %a
UNIKERNEL_NAME = %s
OPAM = opam
all::
@@$(MAKE) --no-print-directory depends
@@$(MAKE) --no-print-directory build
.PHONY: all lock install-switch pull clean depend depends build%a
$(MIRAGE_DIR)/$(UNIKERNEL_NAME).opam.locked: $(MIRAGE_DIR)/$(UNIKERNEL_NAME).opam%a
@@echo " ↳ generate lockfile for monorepo dependencies"
@@env OPAMVAR_monorepo="opam-monorepo" $(OPAM) monorepo lock --require-cross-compile --build-only $(UNIKERNEL_NAME) -l $@@ --ocaml-version $(shell ocamlc --version)%a
lock::
@@$(MAKE) -B $(MIRAGE_DIR)/$(UNIKERNEL_NAME).opam.locked
pull:: $(MIRAGE_DIR)/$(UNIKERNEL_NAME).opam.locked
@@echo " ↳ fetch monorepo dependencies in the duniverse folder"
@@env OPAMVAR_monorepo="opam-monorepo" $(OPAM) monorepo pull -l $< -r $(abspath $(BUILD_DIR))
install-switch:: $(MIRAGE_DIR)/$(UNIKERNEL_NAME).opam
@@echo " ↳ opam install switch dependencies"
@@$(OPAM) install $< --deps-only --yes%a%a
depends depend::
@@$(MAKE) --no-print-directory lock
@@$(MAKE) --no-print-directory install-switch
@@$(MAKE) --no-print-directory pull
build::
dune build --profile release --root . $(BUILD_DIR)dist
clean::
mirage clean
|}
Fpath.pp t.build_dir Fpath.pp mirage_dir
(Misc.Name.Opam.to_string t.unikernel_opam_name)
pp_extra_rules t pp_add_repo t.extra_repo pp_or_remove_repo t.extra_repo
pp_no_depext t.depext pp_depext_lockfile t.depext