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