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