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