Skip to content

Commit

Permalink
Continue 13fad27
Browse files Browse the repository at this point in the history
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
samoht committed Mar 27, 2013
1 parent 2a2b583 commit 8b09603
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
7 changes: 5 additions & 2 deletions src/client/opamMain.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 \
Expand Down
36 changes: 24 additions & 12 deletions src/client/opamState.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 *)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 ..."
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/client/opamSwitchCommand.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions src/core/opamGlobals.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 8b09603

Please sign in to comment.