A few more changes to the package list.
[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-0.8.git10e45753.fc22"
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-omake";                        (* build failure on 4.02.0 with hevea *)
27   "ocaml-pa-do";                        (* build failure, complex *)
28   "ocaml-lwt";                          (* build failure on 4.02.0 *)
29   "ocaml-preludeml";                    (* build failure *)
30   "cduce";                              (* broken again with latest 4.02 *)
31   "frama-c";                            (* build failure *)
32   "gappalib-coq";                       (* build failure in configure script *)
33 ]
34 let blocked pkg = List.mem pkg blocked
35
36 (* These packages are treated as if they have been rebuilt. *)
37 let ignored = [
38   "ocaml-srpm-macros";             (* don't need to build this *)
39   "ocaml";                         (* rebuilt by hand *)
40   "whenjobs";                      (* obsolete *)
41   "libguestfs";                    (* rebuilt by hand *)
42   "graphviz";                      (* rebuilt by hand *)
43   "xen";                           (* already done *)
44   "plplot";                        (* already done *)
45 ]
46 let ignored pkg = List.mem pkg ignored
47
48 (* List of OCaml-related source package names. *)
49 let source_packages =
50   let dirs = shlines "cd %s && ls -1d ocaml*" fedora_dir in
51   dirs @ [ "alt-ergo"; "apron"; "brltty"; "coccinelle"; "coq";
52            "cduce"; "frama-c"; "gappalib-coq"; "graphviz"; "hevea"; "hivex";
53            "js-of-ocaml"; "llvm"; "plplot"; "virt-top"; "why3"; "xen" ]
54
55 (* Dependencies of each package.  (pkg, [deps ...]) *)
56 let pkg_deps = dependencies branch source_packages
57
58 (* Remove blocked packages and packages which have a blocked package
59  * as a dependency (recursively).
60  *)
61 let source_packages =
62   let rec is_blocked pkg =
63     if blocked pkg then true
64     else (
65       let deps = List.assoc pkg pkg_deps in
66       List.exists is_blocked deps
67     )
68   in
69   List.filter (fun pkg -> not (is_blocked pkg)) source_packages
70
71 (* Short the dependencies lists so that the build order is stable
72  * each time it runs.
73  *)
74 let pkg_deps =
75   List.map (fun (pkg, deps) -> pkg, List.sort compare deps) pkg_deps
76
77 (* Sort the source packages so that the packages with the largest
78  * number of reverse dependencies [other packages that depend on it]
79  * appear earlier in the list, on the basis that building these
80  * packages first has the greatest advantage.
81  *)
82 let source_packages =
83   let rdeps pkg =
84     Utils.filter_map (
85       fun (rdep, deps) -> if List.mem pkg deps then Some rdep else None
86     ) pkg_deps
87   in
88   let cmp p1 p2 =
89     let r1 = rdeps p1 and r2 = rdeps p2 in
90     let n1 = List.length r1 and n2 = List.length r2 in
91     if n1 <> n2 then compare n2 n1 else compare p1 p2
92   in
93   List.sort cmp source_packages
94
95 let () =
96   printf "final list of source packages = %s\n%!"
97     (String.concat " " source_packages)
98
99 (* We could make this a goal, but it's cheap enough to run it unconditionally. *)
100 let install_build_dependencies pkg =
101   sh "sudo yum clean all --disablerepo=\\* --enablerepo=%s"
102     (quote yum_repo);
103   sh "sudo yum-builddep -y --disablerepo=\\* --enablerepo=%s %s"
104     (quote yum_repo) (fedora_specfile pkg branch)
105
106 (* Unset MAKEFLAGS so it doesn't affect local builds. *)
107 let () = Unix.putenv "MAKEFLAGS" ""
108
109 (* Goal: rebuild all packages. *)
110 let rec goal all () =
111   let n = List.length source_packages in
112   List.iteri (
113     fun i pkg ->
114       require (rebuild_started pkg);
115       printf "*** *** rebuilt %d/%d packages *** ***\n%!" (i+1) n
116   ) source_packages
117
118 (* Goal: That 'package' has been rebuilt and exists in Koji. *)
119 and rebuilt pkg =
120   let specfile = fedora_specfile pkg branch in
121
122   (* Note: verrel may change as we go along, so don't assign it to
123    * variable.
124    *)
125
126   (* Note the target must be both of these because the old verrel
127    * could exist as a koji build without it having been part of the
128    * rebuild.
129    *)
130   target (ignored pkg ||
131             (file_contains_string specfile rebuild_name &&
132                koji_build_state (fedora_verrel pkg branch) == `Complete));
133
134   (* Ignored packages are treated as if they have been rebuilt. *)
135   if not (ignored pkg) then (
136
137     (* Start the rebuild. *)
138     require (rebuild_started pkg);
139
140     (* Wait for the build state to reach a conclusion. *)
141     let rec loop () =
142       match koji_build_state (fedora_verrel pkg branch) with
143       | `No_such_build ->
144         failwith (sprintf "rebuild of package %s: no build found" pkg)
145       | `Building ->
146         sleep 60;
147         loop ()
148       | `Complete ->
149         ()
150       | `Deleted ->
151         failwith (sprintf "rebuild of package %s: deleted" pkg)
152       | `Failed ->
153         failwith (sprintf "rebuild of package %s: failed" pkg)
154       | `Canceled ->
155         failwith (sprintf "rebuild of package %s: canceled" pkg)
156     in
157     loop ();
158
159     (* Wait for the build to appear in Koji repo. *)
160     koji_wait_repo koji_target (fedora_verrel pkg branch)
161   )
162
163 (* Goal: The rebuild of the package has started, but we haven't waited
164  * for it to finish.
165  *)
166 and rebuild_started pkg =
167   let deps = List.assoc pkg pkg_deps in
168   let specfile = fedora_specfile pkg branch in
169
170   (* Note the target must be both of these because the old verrel
171    * could exist as a koji build without it having been part of the
172    * rebuild.
173    *)
174   target (ignored pkg ||
175             (file_contains_string specfile rebuild_name &&
176                (match koji_build_state (fedora_verrel pkg branch) with
177                | `Building | `Complete -> true
178                | `Deleted | `Failed | `Canceled | `No_such_build -> false)));
179
180   (* All dependent packages must have been fully rebuilt and in the
181    * repo first.
182    *)
183   List.iter (fun dep -> require (rebuilt dep)) deps;
184
185   (* Ignored packages are treated as if they have been rebuilt. *)
186   if not (ignored pkg) then (
187     (* A local test build must succeed. *)
188     require (local_build_succeeded 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 and local_build_succeeded pkg =
215   (* The specfile must have been updated. *)
216   require (specfile_updated pkg);
217
218   let key =
219     sprintf "fedora_ocaml_local_build_%s_%s" pkg (fedora_verrel pkg branch) in
220
221   target (memory_exists key);
222
223   install_build_dependencies pkg;
224
225  (* Do a local test build to ensure the Koji build will work. *)
226   sh "
227     cd %s
228      fedpkg local
229   " (fedora_repo pkg branch);
230
231   memory_set key "1"
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   install_build_dependencies pkg;
250
251   (* For rationale behind always bumping the spec file, see comment
252    * in 'fedora.ml'.
253    *)
254   let title =
255     if not (file_contains_string specfile rebuild_name) then
256       rebuild_name ^ " rebuild."
257     else
258       "Bump release and rebuild." in
259   sh "rpmdev-bumpspec -c %s %s" (quote title) specfile;
260
261   (* XXX Automate common specfile fixes. *)
262
263   sh "
264     cd %s
265     echo 'Please make further changes as required to the spec file %s.spec'
266     echo '(Press return key)'
267     read
268     emacs -nw %s
269     echo 'OK to commit this change? (press ^C if not)'
270     read
271     fedpkg commit -c
272     echo 'OK to push this change? (press ^C if not)'
273     read
274     fedpkg push
275   " repodir
276     pkg
277     specfile