Skip to content

Commit

Permalink
Some more version logic
Browse files Browse the repository at this point in the history
Add an --exact-version command that checks a specific version exactly

Use this in the CI check, rather than try to parse `sail -v`
  • Loading branch information
Alasdair committed May 15, 2024
1 parent cc5122f commit 552ab95
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
4 changes: 2 additions & 2 deletions etc/ci_core_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ set -eu

# Verify `sail --version` matches `git describe`.
SAIL_VERSION="$(sail --version)"
GIT_DESCRIBE="Sail $(git describe --abbrev=0) ($(git rev-parse --abbrev-ref HEAD) @ $(git rev-parse HEAD))"
GIT_DESCRIBE="$(git describe --abbrev=0)"

if [[ "$SAIL_VERSION" != "$GIT_DESCRIBE" ]]; then
if ! sail --require-version $GIT_DESCRIBE --exact-version; then
echo "Sail version not set correctly:"
echo "sail --version: $SAIL_VERSION"
echo "git describe: $GIT_DESCRIBE"
Expand Down
4 changes: 1 addition & 3 deletions src/bin/manifest.ml.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
let dir = "%{sail:share}%"

let commit = "opam-v%{opam-version}%"
let commit = "opam-v%{opam-version}% %{sail:version}"

let branch = "%{sail:name}%"

let version = "%{sail:version}%"
39 changes: 29 additions & 10 deletions src/bin/sail.ml
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@

open Libsail

type version = { major : int; minor : int; patch : int }

(* Current version of Sail. Must be updated manually. CI checks this matches
`git describe`. *)
let version = [0; 15]
the tag given by `git describe`. *)
let version = { major = 0; minor = 17; patch = 1 }

let opt_new_cli = ref false
let opt_free_arguments : string list ref = ref []
Expand All @@ -81,6 +83,7 @@ let opt_interactive_script : string option ref = ref None
let opt_splice : string list ref = ref []
let opt_print_version = ref false
let opt_require_version : string option ref = ref None
let opt_exact_version : bool ref = ref false
let opt_memo_z3 = ref true
let opt_have_feature = ref None
let opt_all_modules = ref false
Expand Down Expand Up @@ -169,13 +172,30 @@ let load_plugin opts plugin =
with Dynlink.Error msg -> prerr_endline ("Failed to load plugin " ^ plugin ^ ": " ^ Dynlink.error_message msg)

(* Version as a string, e.g. "1.2.3". *)
let version_string = String.concat "." (List.map string_of_int version)
let version_string = Printf.sprintf "%d.%d.%d" version.major version.minor version.patch

(* Full version string including Git branch & commit. *)
let version_full =
let open Manifest in
Printf.sprintf "Sail %s (%s @ %s)" version_string branch commit

(* Convert a string like "1.2.3" to a list [1; 2; 3] *)
let parse_version dotted_version =
let open Util.Option_monad in
let* version = String.split_on_char '.' dotted_version |> List.map int_of_string_opt |> Util.option_all in
match version with
| [major; minor; patch] -> Some { major; minor; patch }
| [major; minor] -> Some { major; minor; patch = 0 }
| [major] -> Some { major; minor = 0; patch = 0 }
| _ -> None

let version_check ~exact ~required =
if exact then version = required
else
required.major < version.major
|| (required.major = version.major && required.minor < version.minor)
|| (required.major = version.major && required.minor = version.minor && required.patch <= version.patch)

let usage_msg = version_string ^ "\nusage: sail <options> <file1.sail> ... <fileN.sail>\n"

let help options = raise (Arg.Help (Arg.usage_string options usage_msg))
Expand Down Expand Up @@ -355,6 +375,7 @@ let rec options =
Arg.String (fun ver -> opt_require_version := Some ver),
"<min_version> exit with non-zero status if Sail version requirement is not met"
);
("-exact_version", Arg.Set opt_exact_version, " if used with --require-version, then the version must be exact");
("-verbose", Arg.Int (fun verbosity -> Util.opt_verbosity := verbosity), "<verbosity> produce verbose output");
( "-explain_all_variables",
Arg.Set Type_error.opt_explain_all_variables,
Expand Down Expand Up @@ -539,10 +560,6 @@ let parse_config_file file =
Reporting.warn "" Parse_ast.Unknown (Printf.sprintf "Failed to parse configuration file: %s" message);
None

(* Convert a string like "1.2.3" to a list [1; 2; 3] *)
let parse_version dotted_version =
String.split_on_char '.' dotted_version |> List.map int_of_string_opt |> Util.option_all

let main () =
if Option.is_some (Sys.getenv_opt "SAIL_NEW_CLI") then opt_new_cli := true;

Expand Down Expand Up @@ -578,9 +595,11 @@ let main () =
| Some v -> v
| None -> raise (Reporting.err_general Unknown ("Couldn't parse required version '" ^ required_version ^ "'"))
in
if version < required_version_parsed then (
print_endline
("Sail compiler version " ^ version_string ^ " is older than requested version " ^ required_version);
let exact = !opt_exact_version in
if not (version_check ~exact ~required:required_version_parsed) then (
Printf.eprintf "Sail version %s is %s requested version %s" version_string
(if exact then "not equal to" else "older than")
required_version;
exit 1
)
| None -> ()
Expand Down
2 changes: 1 addition & 1 deletion src/sail_manifest/sail_manifest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ let gen_manifest () =
ksprintf print_endline "let branch = \"%s\""
(Option.value (git_command "rev-parse --abbrev-ref HEAD") ~default:"unknown branch")

let usage = "sail_install_tool <options>"
let usage = "sail_manifest <options>"

let main () =
Arg.parse options (fun _ -> ()) usage;
Expand Down

0 comments on commit 552ab95

Please sign in to comment.