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