From 552ab950b68a1bd4a18b2d5e363300f0169d0349 Mon Sep 17 00:00:00 2001 From: Alasdair Date: Wed, 15 May 2024 21:04:27 +0100 Subject: [PATCH] Some more version logic Add an --exact-version command that checks a specific version exactly Use this in the CI check, rather than try to parse `sail -v` --- etc/ci_core_tests.sh | 4 +-- src/bin/manifest.ml.in | 4 +-- src/bin/sail.ml | 39 ++++++++++++++++++++++-------- src/sail_manifest/sail_manifest.ml | 2 +- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/etc/ci_core_tests.sh b/etc/ci_core_tests.sh index 9c1b08d59..e3136b8aa 100755 --- a/etc/ci_core_tests.sh +++ b/etc/ci_core_tests.sh @@ -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" diff --git a/src/bin/manifest.ml.in b/src/bin/manifest.ml.in index fe3a3cf46..54735f02e 100644 --- a/src/bin/manifest.ml.in +++ b/src/bin/manifest.ml.in @@ -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}%" diff --git a/src/bin/sail.ml b/src/bin/sail.ml index 6f0cdd135..3ef4ad22d 100644 --- a/src/bin/sail.ml +++ b/src/bin/sail.ml @@ -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 [] @@ -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 @@ -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 ... \n" let help options = raise (Arg.Help (Arg.usage_string options usage_msg)) @@ -355,6 +375,7 @@ let rec options = Arg.String (fun ver -> opt_require_version := Some ver), " 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), " produce verbose output"); ( "-explain_all_variables", Arg.Set Type_error.opt_explain_all_variables, @@ -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; @@ -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 -> () diff --git a/src/sail_manifest/sail_manifest.ml b/src/sail_manifest/sail_manifest.ml index 1f4fc2114..e752fedcb 100644 --- a/src/sail_manifest/sail_manifest.ml +++ b/src/sail_manifest/sail_manifest.ml @@ -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 " +let usage = "sail_manifest " let main () = Arg.parse options (fun _ -> ()) usage;