Permalink
Browse files

Continue 13fad27

When we add --switch=SWITCH on the command line, we really want to see the environment variable for this switch (but not when we set $OPAMSWITCH).
  • Loading branch information...
1 parent 2a2b583 commit 8b09603725918304d2d0bedf34c7058b1354f376 @samoht committed Mar 27, 2013
Showing with 36 additions and 18 deletions.
  1. +5 −2 src/client/opamMain.ml
  2. +24 −12 src/client/opamState.ml
  3. +1 −1 src/client/opamSwitchCommand.ml
  4. +6 −3 src/core/opamGlobals.ml
@@ -41,7 +41,10 @@ let set_global_options o =
);
OpamGlobals.debug := !OpamGlobals.debug || o.debug;
OpamGlobals.verbose := (not o.quiet) && (!OpamGlobals.verbose || o.verbose);
- OpamGlobals.switch := o.switch;
+ begin match o.switch with
+ | None -> ()
+ | Some s -> OpamGlobals.switch := `Command_line s
+ end;
OpamGlobals.root_dir := OpamSystem.real_path o.root;
OpamGlobals.yes := !OpamGlobals.yes || o.yes;
OpamGlobals.no_base_packages := !OpamGlobals.no_base_packages || o.no_base_packages
@@ -264,7 +267,7 @@ let global_options =
mk_opt ~section ["switch"]
"SWITCH" "Use $(docv) as the current compiler switch. \
This is equivalent to setting $(b,\\$OPAMSWITCH) to $(i,SWITCH)."
- Arg.(some string) !OpamGlobals.switch in
+ Arg.(some string) None in
let yes =
mk_flag ~section ["y";"yes"]
"Disable interactive mode and answer yes \
@@ -368,8 +368,10 @@ let load_repository_state call_site =
let fn repo_p = OpamRepository.read_prefix repo_p in
make_repository_name_map fn root config in
let switch = match !OpamGlobals.switch with
- | None -> OpamFile.Config.switch config
- | Some a -> OpamSwitch.of_string a in
+ | `Command_line s
+ | `Env s -> OpamSwitch.of_string s
+ | `Not_set -> OpamFile.Config.switch config in
+
let partial = true in
(* evertything else is empty *)
@@ -398,8 +400,9 @@ let load_env_state call_site =
let config_p = OpamPath.config root in
let config = OpamFile.Config.read config_p in
let switch = match !OpamGlobals.switch with
- | None -> OpamFile.Config.switch config
- | Some a -> OpamSwitch.of_string a in
+ | `Command_line s
+ | `Env s -> OpamSwitch.of_string s
+ | `Not_set -> OpamFile.Config.switch config in
let aliases = OpamFile.Aliases.safe_read (OpamPath.aliases root) in
let compiler =
try OpamSwitch.Map.find switch aliases
@@ -619,8 +622,9 @@ let load_state ?(save_cache=true) call_site =
let partial = false in
let switch = match !OpamGlobals.switch with
- | None -> OpamFile.Config.switch config
- | Some a -> OpamSwitch.of_string a in
+ | `Command_line s
+ | `Env s -> OpamSwitch.of_string s
+ | `Not_set -> OpamFile.Config.switch config in
let aliases = OpamFile.Aliases.safe_read (OpamPath.aliases root) in
let switch, compiler =
try switch, OpamSwitch.Map.find switch aliases
@@ -629,8 +633,9 @@ let load_state ?(save_cache=true) call_site =
(OpamFilename.to_string (OpamPath.aliases root))
(OpamSwitch.to_string switch);
match !OpamGlobals.switch with
- | Some s -> OpamSwitch.not_installed (OpamSwitch.of_string s)
- | None ->
+ | `Command_line s
+ | `Env s -> OpamSwitch.not_installed (OpamSwitch.of_string s)
+ | `Not_set ->
if OpamSwitch.Map.cardinal aliases > 0 then (
let new_switch, new_compiler = OpamSwitch.Map.choose aliases in
OpamGlobals.error "The current switch (%s) is an unknown compiler switch. Switching back to %s ..."
@@ -917,9 +922,15 @@ let env_updates t =
display the environment variables. We have to make sure that
OPAMSWITCH is always the one being reported in '~/.opa/config'
otherwise we can have very weird results (as the inability to switch
- between compilers). *)
+ between compilers).
+
+ Note: when we do the later command with --switch=SWITCH, this mean
+ we really want to get the environment for this switch. *)
let get_opam_env t =
- let t = { t with switch = OpamFile.Config.switch t.config } in
+ let t = match !OpamGlobals.switch with
+ | `Command_line _
+ | `Not_set -> t
+ | `Env _ -> { t with switch = OpamFile.Config.switch t.config } in
add_to_env t [] (env_updates t)
let get_full_env t =
@@ -1501,8 +1512,9 @@ let check f =
OpamFilename.with_flock
(OpamPath.lock root)
(fun () -> match !OpamGlobals.switch with
- | None -> OpamFile.Config.switch (OpamFile.Config.read (OpamPath.config root))
- | Some a -> OpamSwitch.of_string a)
+ | `Command_line s
+ | `Env s -> OpamSwitch.of_string s
+ | `Not_set -> OpamFile.Config.switch (OpamFile.Config.read (OpamPath.config root)))
() in
(* XXX: We can have a small race just here ... *)
let t = load_state "switch-lock" in
@@ -148,7 +148,7 @@ let install_with_packages ~quiet ~packages switch compiler =
OpamState.install_compiler t ~quiet switch compiler;
(* install the compiler packages *)
- OpamGlobals.switch := Some (OpamSwitch.to_string switch);
+ OpamGlobals.switch := `Command_line (OpamSwitch.to_string switch);
let t = OpamState.load_state "switch-install-with-packages-2" in
let to_install, roots = match packages with
@@ -61,9 +61,12 @@ let default_package = "conf-ocaml"
let system = "system"
-let switch = ref (
- try Some (OpamMisc.getenv "OPAMSWITCH")
- with _ -> None
+let switch: [`Env of string
+ | `Command_line of string
+ | `Not_set ] ref
+ = ref (
+ try `Env (OpamMisc.getenv "OPAMSWITCH")
+ with _ -> `Not_set
)
let opam_version = "1"

0 comments on commit 8b09603

Please sign in to comment.