Skip to content

Commit

Permalink
Select unsafe.ml with just dune rules
Browse files Browse the repository at this point in the history
It is not necessary to use configurator to select a file depending on
the OCaml version. This pattern looks like this:

- a `config.exe` program (with no dependencies) checks
  `Sys.ocaml_version` (`scanf` will only look at the beginning of the
  string, so no need to parse the patch version etc) and just outputs
  the name of the file to copy
- a rule links a file name to the output of `config.exe`
- a rule copies the content of that file to `unsafe.ml`
  • Loading branch information
emillon committed Jun 30, 2020
1 parent d04477d commit accde1d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 58 deletions.
1 change: 0 additions & 1 deletion base64.opam
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ representation. It is specified in RFC 4648.
depends: [
"ocaml" {>="4.03.0"}
"base-bytes"
"dune-configurator"
"dune" {>= "2.0"}
"bos" {with-test}
"rresult" {with-test}
Expand Down
56 changes: 5 additions & 51 deletions config/config.ml
Original file line number Diff line number Diff line change
@@ -1,54 +1,8 @@
module Config = Configurator.V1

let pre407 =
{ocaml|external unsafe_set_uint16 : bytes -> int -> int -> unit = "%caml_string_set16u" [@@noalloc]|ocaml}

let standard =
{ocaml|external unsafe_set_uint16 : bytes -> int -> int -> unit = "%caml_bytes_set16u" [@@noalloc]|ocaml}

type t = { major : int; minor : int; patch : int option; extra : string option }

let v ?patch ?extra major minor = { major; minor; patch; extra }

let parse s =
try
Scanf.sscanf s "%d.%d.%d+%s" (fun major minor patch extra ->
v ~patch ~extra major minor)
with End_of_file | Scanf.Scan_failure _ -> (
try
Scanf.sscanf s "%d.%d+%s" (fun major minor extra -> v ~extra major minor)
with End_of_file | Scanf.Scan_failure _ -> (
try
Scanf.sscanf s "%d.%d.%d" (fun major minor patch ->
v ~patch major minor)
with End_of_file | Scanf.Scan_failure _ ->
Scanf.sscanf s "%d.%d" (fun major minor -> v major minor)))

let ( >|= ) x f = match x with Some x -> Some (f x) | None -> None

let ocaml_cp ~src ~dst =
let ic = open_in src in
let oc = open_out dst in
let bf = Bytes.create 0x1000 in
let rec go () =
match input ic bf 0 (Bytes.length bf) with
| 0 -> ()
| len ->
output oc bf 0 len ;
go ()
| exception End_of_file -> () in
go () ;
close_in ic ;
close_out oc
Scanf.sscanf s "%d.%d" (fun major minor -> (major, minor))

let () =
Config.main ~name:"config-base64" @@ fun t ->
match Config.ocaml_config_var t "version" >|= parse with
| Some version ->
let dst = "unsafe.ml" in

if (version.major, version.minor) >= (4, 7)
then ocaml_cp ~src:"unsafe_stable.ml" ~dst
else ocaml_cp ~src:"unsafe_pre407.ml" ~dst
| None -> Config.die "OCaml version is not available"
| exception exn -> Config.die "Got an exception: %s" (Printexc.to_string exn)
let version = parse Sys.ocaml_version in
if version >= (4, 7)
then print_string "unsafe_stable.ml"
else print_string "unsafe_pre407.ml"
8 changes: 6 additions & 2 deletions config/dune
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
(executable
(name config)
(libraries dune-configurator))
(name config))

(rule
(with-stdout-to
which-unsafe-file
(run ./config.exe)))
5 changes: 1 addition & 4 deletions src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
(libraries bytes))

(rule
(targets unsafe.ml)
(deps unsafe_pre407.ml unsafe_stable.ml)
(action
(run ../config/config.exe)))
(copy %{read:../config/which-unsafe-file} unsafe.ml))

(library
(name base64_rfc2045)
Expand Down

0 comments on commit accde1d

Please sign in to comment.