Rebuild for OCaml 4.04.1
[goaljobs-goals.git] / fedora_ocaml_rebuild.ml
1 (* Perform a complete Fedora OCaml rebuild, in build order. *)
2
3 open Unix
4 open Printf
5
6 open Goaljobs
7 open Config
8 open Git
9 open Fedora
10
11 let branch = "master"
12 let side_tag = Some "f27-ocaml"
13
14 let koji_target =
15   match side_tag with
16   | Some t -> t
17   | None -> "rawhide"
18
19 (* The name of the rebuild, and also the magic substring that must
20  * appear in the %changelog when the package has been rebuilt.
21  *)
22 let rebuild_name = "OCaml 4.04.1"
23
24 (* Local repository that contains build dependencies. *)
25 let yum_repo = "koji-rawhide"
26
27 (* Packages that have problems.  These block the packages and all
28  * dependent packages.
29  *)
30 let blocked = [
31   "brltty";                        (* broken for non-OCaml reasons *)
32 ]
33 let blocked pkg = List.mem pkg blocked
34
35 (* These packages are treated as if they have been rebuilt. *)
36 let ignored = [
37   "ocaml-srpm-macros";             (* don't need to build this *)
38   "ocaml";                         (* rebuilt by hand *)
39 ]
40 let ignored pkg = List.mem pkg ignored
41
42 (* List of OCaml-related source package names. *)
43 let source_packages =
44   let dirs = shlines "cd %s && ls -1d ocaml*" fedora_dir in
45   dirs @ [ "alt-ergo"; "apron"; "brltty"; "coccinelle"; "coq";
46            "cduce"; "frama-c"; "gappalib-coq"; "graphviz"; "hevea"; "hivex";
47            "plplot"; "virt-top"; "why3"; "z3";
48            "flocq" (* no OCaml code, but needs to be rebuilt after Coq *);
49            "guestfs-browser"; "libguestfs" ]
50
51 (* Dependencies of each package.  (pkg, [deps ...]) *)
52 let pkg_deps = dependencies branch source_packages
53
54 (* Remove blocked packages and packages which have a blocked package
55  * as a dependency (recursively).
56  *)
57 let source_packages =
58   let rec is_blocked pkg =
59     if blocked pkg then true
60     else (
61       let deps = List.assoc pkg pkg_deps in
62       List.exists is_blocked deps
63     )
64   in
65   List.filter (fun pkg -> not (is_blocked pkg)) source_packages
66
67 (* Short the dependencies lists so that the build order is stable
68  * each time it runs.
69  *)
70 let pkg_deps =
71   List.map (fun (pkg, deps) -> pkg, List.sort compare deps) pkg_deps
72
73 (* Sort the source packages so that the packages with the largest
74  * number of reverse dependencies [other packages that depend on it]
75  * appear earlier in the list, on the basis that building these
76  * packages first has the greatest advantage.
77  *)
78 let source_packages =
79   let rdeps pkg =
80     Utils.filter_map (
81       fun (rdep, deps) -> if List.mem pkg deps then Some rdep else None
82     ) pkg_deps
83   in
84   let cmp p1 p2 =
85     let r1 = rdeps p1 and r2 = rdeps p2 in
86     let n1 = List.length r1 and n2 = List.length r2 in
87     if n1 <> n2 then compare n2 n1 else compare p1 p2
88   in
89   List.sort cmp source_packages
90
91 let () =
92   printf "final list of source packages = %s\n%!"
93     (String.concat " " source_packages)
94
95 (*
96 (* We could make this a goal, but it's cheap enough to run it unconditionally. *)
97 let install_build_dependencies pkg =
98   sh "sudo yum clean all --disablerepo=\\* --enablerepo=%s"
99     (quote yum_repo);
100   sh "sudo yum-builddep -y --disablerepo=\\* --enablerepo=%s %s"
101     (quote yum_repo) (fedora_specfile pkg branch)
102  *)
103
104 (* Unset MAKEFLAGS so it doesn't affect local builds. *)
105 let () = Unix.putenv "MAKEFLAGS" ""
106
107 (* Goal: rebuild all packages. *)
108 let rec goal all () =
109   let n = List.length source_packages in
110   List.iteri (
111     fun i pkg ->
112       require (rebuild_started pkg);
113       printf "*** *** rebuilt %d/%d packages *** ***\n%!" (i+1) n
114   ) source_packages
115
116 (* Goal: That 'package' has been rebuilt and exists in Koji. *)
117 and rebuilt pkg =
118   let specfile = fedora_specfile pkg branch in
119
120   (* Note: verrel may change as we go along, so don't assign it to
121    * variable.
122    *)
123
124   (* Note the target must be both of these because the old verrel
125    * could exist as a koji build without it having been part of the
126    * rebuild.
127    *)
128   target (ignored pkg ||
129             (file_contains_string specfile rebuild_name &&
130                koji_build_state (fedora_verrel pkg branch) == `Complete));
131
132   (* Ignored packages are treated as if they have been rebuilt. *)
133   if not (ignored pkg) then (
134
135     (* Start the rebuild. *)
136     require (rebuild_started pkg);
137
138     (* Wait for the build state to reach a conclusion. *)
139     let rec loop () =
140       match koji_build_state (fedora_verrel pkg branch) with
141       | `No_such_build ->
142         failwith (sprintf "rebuild of package %s: no build found" pkg)
143       | `Building ->
144         sleep 60;
145         loop ()
146       | `Complete ->
147         ()
148       | `Deleted ->
149         failwith (sprintf "rebuild of package %s: deleted" pkg)
150       | `Failed ->
151         failwith (sprintf "rebuild of package %s: failed" pkg)
152       | `Canceled ->
153         failwith (sprintf "rebuild of package %s: canceled" pkg)
154     in
155     loop ();
156
157     (* Wait for the build to appear in Koji repo. *)
158     koji_wait_repo koji_target (fedora_verrel pkg branch)
159   )
160
161 (* Goal: The rebuild of the package has started, but we haven't waited
162  * for it to finish.
163  *)
164 and rebuild_started pkg =
165   let deps = List.assoc pkg pkg_deps in
166   let specfile = fedora_specfile pkg branch in
167
168   (* Note the target must be both of these because the old verrel
169    * could exist as a koji build without it having been part of the
170    * rebuild.
171    *)
172   target (ignored pkg ||
173             (file_contains_string specfile rebuild_name &&
174                (match koji_build_state (fedora_verrel pkg branch) with
175                | `Building | `Complete -> true
176                | `Deleted | `Failed | `Canceled | `No_such_build -> false)));
177
178   (* All dependent packages must have been fully rebuilt and in the
179    * repo first.
180    *)
181   List.iter (fun dep -> require (rebuilt dep)) deps;
182
183   (* Ignored packages are treated as if they have been rebuilt. *)
184   if not (ignored pkg) then (
185 (*
186     (* A local test build must succeed. *)
187     require (local_build_succeeded pkg);
188 *)
189     (* local_build_succeeded normally does this ... *)
190     require (specfile_updated pkg);
191
192     (* Rebuild the package in Koji.  Don't wait ... *)
193     koji_build ~wait:false ?side_tag pkg branch;
194
195     (* ... but the build doesn't appear in Koji (eg. in 'koji
196      * buildinfo') until the SRPM has been built.  This can take quite
197      * some time.  Loop here until the build appears.
198      *)
199     let rec loop () =
200       match koji_build_state (fedora_verrel pkg branch) with
201       | `No_such_build ->
202         sleep 60;
203         loop ();
204       | `Building | `Complete ->
205         ()
206       | `Deleted ->
207         failwith (sprintf "rebuild of package %s: deleted" pkg)
208       | `Failed ->
209         failwith (sprintf "rebuild of package %s: failed" pkg)
210       | `Canceled ->
211         failwith (sprintf "rebuild of package %s: canceled" pkg)
212     in
213     loop ()
214   )
215
216 (*
217 and local_build_succeeded pkg =
218   (* The specfile must have been updated. *)
219   require (specfile_updated pkg);
220
221   let key =
222     sprintf "fedora_ocaml_local_build_%s_%s" pkg (fedora_verrel pkg branch) in
223
224   target (memory_exists key);
225
226   install_build_dependencies pkg;
227
228  (* Do a local test build to ensure the Koji build will work. *)
229   sh "
230     cd %s
231      fedpkg local
232   " (fedora_repo pkg branch);
233
234   memory_set key "1"
235 *)
236
237 and specfile_updated pkg =
238   let repodir = fedora_repo pkg branch in
239   let specfile = fedora_specfile pkg branch in
240
241   sh "
242     cd %s
243     rm -rf x86_64 noarch *.src.rpm .build* clog
244     git fetch
245   " repodir;
246
247   if not (git_has_local_changes repodir) then
248     sh "
249       cd %s
250       git pull --rebase
251     " repodir;
252
253 (* - XXX why did we do this here?
254   install_build_dependencies pkg;
255 *)
256
257   (* For rationale behind always bumping the spec file, see comment
258    * in 'fedora.ml'.
259    *)
260   let title =
261     if not (file_contains_string specfile rebuild_name) then
262       rebuild_name ^ " rebuild."
263     else
264       "Bump release and rebuild." in
265   sh "rpmdev-bumpspec -c %s %s" (quote title) specfile;
266
267   (* XXX Automate common specfile fixes. *)
268
269   sh "
270     cd %s
271     fedpkg commit -c
272     fedpkg push
273   " repodir