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