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