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