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