Unblock some packages.
[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.0+rc1"
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";                        (* build failure, complex *)
27   "frama-c";                            (* build failure *)
28   "gappalib-coq";                       (* build failure in configure script *)
29   "ocaml-camlimages";                   (* build failure, conflicting C types in header files *)
30 ]
31 let blocked pkg = List.mem pkg blocked
32
33 (* These packages are treated as if they have been rebuilt. *)
34 let ignored = [
35   "ocaml-srpm-macros";             (* don't need to build this *)
36   "ocaml";                         (* rebuilt by hand *)
37   "whenjobs";                      (* obsolete *)
38 ]
39 let ignored pkg = List.mem pkg ignored
40
41 (* List of OCaml-related source package names. *)
42 let source_packages =
43   let dirs = shlines "cd %s && ls -1d ocaml*" fedora_dir in
44   dirs @ [ "alt-ergo"; "apron"; "brltty"; "coccinelle"; "coq";
45            "cduce"; "frama-c"; "gappalib-coq"; "graphviz"; "hevea"; "hivex";
46            "js-of-ocaml"; "llvm"; "plplot"; "virt-top"; "why3"; "xen";
47            "flocq" (* no OCaml code, but needs to be rebuilt after Coq *);
48            "guestfs-browser";
49            "virt-dmesg" ]
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 (* We could make this a goal, but it's cheap enough to run it unconditionally. *)
96 let install_build_dependencies pkg =
97   sh "sudo yum clean all --disablerepo=\\* --enablerepo=%s"
98     (quote yum_repo);
99   sh "sudo yum-builddep -y --disablerepo=\\* --enablerepo=%s %s"
100     (quote yum_repo) (fedora_specfile pkg branch)
101
102 (* Unset MAKEFLAGS so it doesn't affect local builds. *)
103 let () = Unix.putenv "MAKEFLAGS" ""
104
105 (* Goal: rebuild all packages. *)
106 let rec goal all () =
107   let n = List.length source_packages in
108   List.iteri (
109     fun i pkg ->
110       require (rebuild_started pkg);
111       printf "*** *** rebuilt %d/%d packages *** ***\n%!" (i+1) n
112   ) source_packages
113
114 (* Goal: That 'package' has been rebuilt and exists in Koji. *)
115 and rebuilt pkg =
116   let specfile = fedora_specfile pkg branch in
117
118   (* Note: verrel may change as we go along, so don't assign it to
119    * variable.
120    *)
121
122   (* Note the target must be both of these because the old verrel
123    * could exist as a koji build without it having been part of the
124    * rebuild.
125    *)
126   target (ignored pkg ||
127             (file_contains_string specfile rebuild_name &&
128                koji_build_state (fedora_verrel pkg branch) == `Complete));
129
130   (* Ignored packages are treated as if they have been rebuilt. *)
131   if not (ignored pkg) then (
132
133     (* Start the rebuild. *)
134     require (rebuild_started pkg);
135
136     (* Wait for the build state to reach a conclusion. *)
137     let rec loop () =
138       match koji_build_state (fedora_verrel pkg branch) with
139       | `No_such_build ->
140         failwith (sprintf "rebuild of package %s: no build found" pkg)
141       | `Building ->
142         sleep 60;
143         loop ()
144       | `Complete ->
145         ()
146       | `Deleted ->
147         failwith (sprintf "rebuild of package %s: deleted" pkg)
148       | `Failed ->
149         failwith (sprintf "rebuild of package %s: failed" pkg)
150       | `Canceled ->
151         failwith (sprintf "rebuild of package %s: canceled" pkg)
152     in
153     loop ();
154
155     (* Wait for the build to appear in Koji repo. *)
156     koji_wait_repo koji_target (fedora_verrel pkg branch)
157   )
158
159 (* Goal: The rebuild of the package has started, but we haven't waited
160  * for it to finish.
161  *)
162 and rebuild_started pkg =
163   let deps = List.assoc pkg pkg_deps in
164   let specfile = fedora_specfile pkg branch in
165
166   (* Note the target must be both of these because the old verrel
167    * could exist as a koji build without it having been part of the
168    * rebuild.
169    *)
170   target (ignored pkg ||
171             (file_contains_string specfile rebuild_name &&
172                (match koji_build_state (fedora_verrel pkg branch) with
173                | `Building | `Complete -> true
174                | `Deleted | `Failed | `Canceled | `No_such_build -> false)));
175
176   (* All dependent packages must have been fully rebuilt and in the
177    * repo first.
178    *)
179   List.iter (fun dep -> require (rebuilt dep)) deps;
180
181   (* Ignored packages are treated as if they have been rebuilt. *)
182   if not (ignored pkg) then (
183 (*
184     (* A local test build must succeed. *)
185     require (local_build_succeeded pkg);
186 *)
187     (* local_build_succeeded normally does this ... *)
188     require (specfile_updated pkg);
189
190     (* Rebuild the package in Koji.  Don't wait ... *)
191     koji_build ~wait:false pkg branch;
192
193     (* ... but the build doesn't appear in Koji (eg. in 'koji
194      * buildinfo') until the SRPM has been built.  This can take quite
195      * some time.  Loop here until the build appears.
196      *)
197     let rec loop () =
198       match koji_build_state (fedora_verrel pkg branch) with
199       | `No_such_build ->
200         sleep 60;
201         loop ();
202       | `Building | `Complete ->
203         ()
204       | `Deleted ->
205         failwith (sprintf "rebuild of package %s: deleted" pkg)
206       | `Failed ->
207         failwith (sprintf "rebuild of package %s: failed" pkg)
208       | `Canceled ->
209         failwith (sprintf "rebuild of package %s: canceled" pkg)
210     in
211     loop ()
212   )
213
214 (*
215 and local_build_succeeded pkg =
216   (* The specfile must have been updated. *)
217   require (specfile_updated pkg);
218
219   let key =
220     sprintf "fedora_ocaml_local_build_%s_%s" pkg (fedora_verrel pkg branch) in
221
222   target (memory_exists key);
223
224   install_build_dependencies pkg;
225
226  (* Do a local test build to ensure the Koji build will work. *)
227   sh "
228     cd %s
229      fedpkg local
230   " (fedora_repo pkg branch);
231
232   memory_set key "1"
233 *)
234
235 and specfile_updated pkg =
236   let repodir = fedora_repo pkg branch in
237   let specfile = fedora_specfile pkg branch in
238
239   sh "
240     cd %s
241     rm -rf x86_64 noarch *.src.rpm .build* clog
242     git fetch
243   " repodir;
244
245   if not (git_has_local_changes repodir) then
246     sh "
247       cd %s
248       git pull --rebase
249     " repodir;
250
251 (* - XXX why did we do this here?
252   install_build_dependencies pkg;
253 *)
254
255   (* For rationale behind always bumping the spec file, see comment
256    * in 'fedora.ml'.
257    *)
258   let title =
259     if not (file_contains_string specfile rebuild_name) then
260       rebuild_name ^ " rebuild."
261     else
262       "Bump release and rebuild." in
263   sh "rpmdev-bumpspec -c %s %s" (quote title) specfile;
264
265   (* XXX Automate common specfile fixes. *)
266
267   sh "
268     cd %s
269     fedpkg commit -c
270     fedpkg push
271   " repodir