this repo has no description
at main 8.7 kB view raw
1module C = Configurator.V1 2 3let starts_with ~prefix s = 4 try 5 String.iteri 6 (fun i c -> if Char.equal (String.get s i) c then () else raise Not_found) 7 prefix; 8 let l = String.length s in 9 let o = String.length prefix in 10 Some (String.sub s o (l - o)) 11 with Not_found -> None 12 13let () = 14 C.main ~name:"discover" (fun c -> 15 let pkgconf = 16 C.Pkg_config.get c |> function 17 | Some p -> p 18 | None -> failwith "Need pkgconfig" 19 in 20 match C.Pkg_config.query pkgconf ~package:"libzfs" with 21 | None -> failwith "Pkgconfig failed to find libzfs" 22 | Some conf -> 23 C.Flags.write_sexp "c_flags.sexp" conf.cflags; 24 C.Flags.write_sexp "c_library_flags.sexp" conf.libs; 25 let errs, props, types = 26 C.C_define.import c 27 ~c_flags:("-D_GNU_SOURCE" :: conf.cflags) 28 ~includes: 29 [ 30 "unistd.h"; 31 "stdio.h"; 32 "stdint.h"; 33 "stdbool.h"; 34 "libzfs_core.h"; 35 "libzfs.h"; 36 ] 37 C.C_define.Type. 38 [ 39 (* Errors *) 40 ("EZFS_SUCCESS", Int); 41 ("EZFS_NOMEM", Int); 42 ("EZFS_BADPROP", Int); 43 ("EZFS_PROPREADONLY", Int); 44 ("EZFS_PROPTYPE", Int); 45 ("EZFS_PROPNONINHERIT", Int); 46 ("EZFS_PROPSPACE", Int); 47 ("EZFS_BADTYPE", Int); 48 ("EZFS_BUSY", Int); 49 ("EZFS_EXISTS", Int); 50 ("EZFS_NOENT", Int); 51 ("EZFS_BADSTREAM", Int); 52 ("EZFS_DSREADONLY", Int); 53 ("EZFS_VOLTOOBIG", Int); 54 ("EZFS_INVALIDNAME", Int); 55 ("EZFS_BADRESTORE", Int); 56 ("EZFS_BADBACKUP", Int); 57 ("EZFS_BADTARGET", Int); 58 ("EZFS_NODEVICE", Int); 59 ("EZFS_BADDEV", Int); 60 ("EZFS_NOREPLICAS", Int); 61 ("EZFS_RESILVERING", Int); 62 ("EZFS_BADVERSION", Int); 63 ("EZFS_POOLUNAVAIL", Int); 64 ("EZFS_DEVOVERFLOW", Int); 65 ("EZFS_BADPATH", Int); 66 ("EZFS_CROSSTARGET", Int); 67 ("EZFS_ZONED", Int); 68 ("EZFS_MOUNTFAILED", Int); 69 ("EZFS_UMOUNTFAILED", Int); 70 ("EZFS_UNSHARENFSFAILED", Int); 71 ("EZFS_SHARENFSFAILED", Int); 72 ("EZFS_PERM", Int); 73 ("EZFS_NOSPC", Int); 74 ("EZFS_FAULT", Int); 75 ("EZFS_IO", Int); 76 ("EZFS_INTR", Int); 77 ("EZFS_ISSPARE", Int); 78 ("EZFS_INVALCONFIG", Int); 79 ("EZFS_RECURSIVE", Int); 80 ("EZFS_NOHISTORY", Int); 81 ("EZFS_POOLPROPS", Int); 82 ("EZFS_POOL_NOTSUP", Int); 83 ("EZFS_POOL_INVALARG", Int); 84 ("EZFS_NAMETOOLONG", Int); 85 ("EZFS_OPENFAILED", Int); 86 ("EZFS_NOCAP", Int); 87 ("EZFS_LABELFAILED", Int); 88 ("EZFS_BADWHO", Int); 89 ("EZFS_BADPERM", Int); 90 ("EZFS_BADPERMSET", Int); 91 ("EZFS_NODELEGATION", Int); 92 ("EZFS_UNSHARESMBFAILED", Int); 93 ("EZFS_SHARESMBFAILED", Int); 94 ("EZFS_BADCACHE", Int); 95 ("EZFS_ISL2CACHE", Int); 96 ("EZFS_VDEVNOTSUP", Int); 97 ("EZFS_NOTSUP", Int); 98 ("EZFS_ACTIVE_SPARE", Int); 99 ("EZFS_UNPLAYED_LOGS", Int); 100 ("EZFS_REFTAG_RELE", Int); 101 ("EZFS_REFTAG_HOLD", Int); 102 ("EZFS_TAGTOOLONG", Int); 103 ("EZFS_PIPEFAILED", Int); 104 ("EZFS_THREADCREATEFAILED", Int); 105 ("EZFS_POSTSPLIT_ONLINE", Int); 106 ("EZFS_SCRUBBING", Int); 107 ("EZFS_ERRORSCRUBBING", Int); 108 ("EZFS_ERRORSCRUB_PAUSED", Int); 109 ("EZFS_NO_SCRUB", Int); 110 ("EZFS_DIFF", Int); 111 ("EZFS_DIFFDATA", Int); 112 ("EZFS_POOLREADONLY", Int); 113 ("EZFS_SCRUB_PAUSED", Int); 114 ("EZFS_SCRUB_PAUSED_TO_CANCEL", Int); 115 ("EZFS_ACTIVE_POOL", Int); 116 ("EZFS_CRYPTOFAILED", Int); 117 ("EZFS_NO_PENDING", Int); 118 ("EZFS_CHECKPOINT_EXISTS", Int); 119 ("EZFS_DISCARDING_CHECKPOINT", Int); 120 ("EZFS_NO_CHECKPOINT", Int); 121 ("EZFS_DEVRM_IN_PROGRESS", Int); 122 ("EZFS_VDEV_TOO_BIG", Int); 123 ("EZFS_IOC_NOTSUPPORTED", Int); 124 ("EZFS_TOOMANY", Int); 125 ("EZFS_INITIALIZING", Int); 126 ("EZFS_NO_INITIALIZE", Int); 127 ("EZFS_WRONG_PARENT", Int); 128 ("EZFS_TRIMMING", Int); 129 ("EZFS_NO_TRIM", Int); 130 ("EZFS_TRIM_NOTSUP", Int); 131 ("EZFS_NO_RESILVER_DEFER", Int); 132 ("EZFS_EXPORT_IN_PROGRESS", Int); 133 ("EZFS_REBUILDING", Int); 134 ("EZFS_VDEV_NOTSUP", Int); 135 ("EZFS_NOT_USER_NAMESPACE", Int); 136 ("EZFS_CKSUM", Int); 137 ("EZFS_RESUME_EXISTS", Int); 138 ("EZFS_SHAREFAILED", Int); 139 ("EZFS_UNKNOWN", Int); 140 (* Properties *) 141 ("ZFS_PROP_CREATION", Int); 142 ("ZFS_PROP_USED", Int); 143 ("ZFS_PROP_AVAILABLE", Int); 144 ("ZFS_PROP_REFERENCED", Int); 145 ("ZFS_PROP_COMPRESSRATIO", Int); 146 ("ZFS_PROP_COMPRESSION", Int); 147 ("ZFS_PROP_SNAPDIR", Int); 148 ("ZFS_PROP_ENCRYPTION", Int); 149 (* Types *) 150 ("ZFS_TYPE_INVALID", Int); 151 ("ZFS_TYPE_FILESYSTEM", Int); 152 ("ZFS_TYPE_SNAPSHOT", Int); 153 ("ZFS_TYPE_VOLUME", Int); 154 ("ZFS_TYPE_POOL", Int); 155 ("ZFS_TYPE_BOOKMARK", Int); 156 ("ZFS_TYPE_VDEV", Int); 157 ("ZFS_TYPE_DATASET", Int); 158 ] 159 |> List.fold_left 160 (fun (errs, props, types) -> function 161 | name, C.C_define.Value.Int v -> ( 162 let type_ name = 163 Printf.sprintf "val %s : t" 164 (String.lowercase_ascii name) 165 in 166 let definition name = 167 Printf.sprintf "let %s : t = 0x%x" 168 (String.lowercase_ascii name) 169 v 170 in 171 match starts_with ~prefix:"EZFS_" name with 172 | Some r -> 173 ((type_ r, definition r) :: errs, props, types) 174 | None -> ( 175 match starts_with ~prefix:"ZFS_PROP_" name with 176 | Some r -> 177 (errs, (type_ r, definition r) :: props, types) 178 | None -> ( 179 match starts_with ~prefix:"ZFS_TYPE_" name with 180 | Some t -> 181 ( errs, 182 props, 183 (type_ t, definition t) :: types ) 184 | None -> failwith "Unknown ZFS static value"))) 185 | _ -> assert false) 186 ([], [], []) 187 in 188 let with_module ~name defs = 189 [ Printf.sprintf "module %s = struct\n type t = int\n" name ] 190 @ defs @ [ "end" ] 191 in 192 let with_module_type ~name defs = 193 [ 194 Printf.sprintf "module type %s = sig\n type t = private int\n" 195 name; 196 ] 197 @ defs @ [ "end" ] 198 in 199 let defs = 200 with_module ~name:"Error" (List.map snd errs) 201 @ with_module ~name:"Props" (List.map snd props) 202 @ with_module ~name:"Types" (List.map snd types) 203 in 204 let types = 205 with_module_type ~name:"ERROR" (List.map fst errs) 206 @ with_module_type ~name:"PROPS" (List.map fst props) 207 @ with_module_type ~name:"TYPES" (List.map fst types) 208 in 209 C.Flags.write_lines "config.ml" (defs @ types))