2024-01-02 16:21:30 +08:00
# Copyright 2013-2024 Lawrence Livermore National Security, LLC and other
2020-07-01 03:26:27 +08:00
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
#################################################################################
#
# This file is part of Spack and sets up the spack environment for the friendly
# interactive shell (fish). This includes module support, and it also puts spack
# in your path. The script also checks that at least module support exists, and
# provides suggestions if it doesn't. Source it like this:
#
# source /path/to/spack/share/spack/setup-env.fish
#
#################################################################################
# This is a wrapper around the spack command that forwards calls to 'spack load'
# and 'spack unload' to shell functions. This in turn allows them to be used to
# invoke environment modules functions.
#
# 'spack load' is smarter than just 'load' because it converts its arguments into
# a unique spack spec that is then passed to module commands. This allows the
# user to load packages without knowing all their installation details.
#
# e.g., rather than requiring a full spec for libelf, the user can type:
#
# spack load libelf
#
# This will first find the available libelf modules and load a matching one. If
# there are two versions of libelf, the user would need to be more specific,
# e.g.:
#
# spack load libelf@0.8.13
#
# This is very similar to how regular spack commands work and it avoids the need
# to come up with a user-friendly naming scheme for spack dotfiles.
#################################################################################
2020-07-06 13:35:01 +08:00
# prevent infinite recursion when spack shells out (e.g., on cray for modules)
if test -n " $_sp_initializing "
exit 0
end
set -x _sp_initializing true
2020-07-01 03:26:27 +08:00
#
# Test for STDERR-NOCARET feature: if this is off, fish will redirect stderr to
# a file named in the string after `^`
#
if status test-feature stderr-nocaret
else
echo "WARNING: you have not enabled the 'stderr-nocaret' feature."
echo "This means that you have to escape the caret (^) character when defining specs."
echo "Consider enabling stderr-nocaret: https://fishshell.com/docs/current/index.html#featureflags"
end
#
# SPACK wrapper function, preprocessing arguments and flags.
#
function spack -d "wrapper for the `spack` command"
2023-08-30 00:12:19 +08:00
#
# DEFINE SUPPORT FUNCTIONS HERE
#
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
#
# ALLOCATE_SP_SHARED, and DELETE_SP_SHARED allocate (and delete) temporary
# global variables
#
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
function allocate_sp_shared -d "allocate shared (global variables)"
set -gx __sp_remaining_args
set -gx __sp_subcommand_args
set -gx __sp_module_args
set -gx __sp_stat
set -gx __sp_stdout
set -gx __sp_stderr
end
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
function delete_sp_shared -d "deallocate shared (global variables)"
set -e __sp_remaining_args
set -e __sp_subcommand_args
set -e __sp_module_args
set -e __sp_stat
set -e __sp_stdout
set -e __sp_stderr
end
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
#
# STREAM_ARGS and SHIFT_ARGS: helper functions manipulating the `argv` array:
# -> STREAM_ARGS: echos the `argv` array element-by-element
# -> SHIFT_ARGS: echos the `argv` array element-by-element starting with the
# second element. If `argv` has only one element, echo the
# empty string `""`.
# NOTE: while `stream_args` is not strictly necessary, it adds a nice symmetry
# to `shift_args`
#
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
function stream_args -d "echos args as a stream"
# return the elements of `$argv` as an array
# -> since we want to be able to call it as part of `set x (shift_args
# $x)`, we return these one-at-a-time using echo... this means that the
# sub-command stream will correctly concatenate the output into an array
for elt in $argv
echo $elt
end
end
function shift_args -d "simulates bash shift"
2020-07-01 03:26:27 +08:00
#
2023-08-30 00:12:19 +08:00
# Returns argv[2..-1] (as an array)
# -> if argv has only 1 element, then returns the empty string. This
# simulates the behavior of bash `shift`
2020-07-01 03:26:27 +08:00
#
2023-08-30 00:12:19 +08:00
if test -z " $argv [2] "
# there are no more element, returning the empty string
echo ""
else
# return the next elements `$argv[2..-1]` as an array
2020-07-01 03:26:27 +08:00
# -> since we want to be able to call it as part of `set x (shift_args
2023-08-30 00:12:19 +08:00
# $x)`, we return these one-at-a-time using echo... this means that
# the sub-command stream will correctly concatenate the output into
# an array
for elt in $argv [ 2 .. - 1 ]
2020-07-01 03:26:27 +08:00
echo $elt
end
end
2023-08-30 00:12:19 +08:00
end
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
#
# CAPTURE_ALL: helper function used to capture stdout, stderr, and status
# -> CAPTURE_ALL: there is a bug in fish, that prevents stderr re-capture
# from nested command substitution:
# https://github.com/fish-shell/fish-shell/issues/6459
#
function capture_all
begin ;
begin ;
eval $argv [ 1 ]
set $argv [ 2 ] $status # read sets the `status` flag => capture here
end 2 > | read -z __err
end 1 > | read -z __out
# output arrays
set $argv [ 3 ] ( echo $__out | string split \n )
set $argv [ 4 ] ( echo $__err | string split \n )
return 0
end
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
#
# GET_SP_FLAGS, and GET_MOD_ARGS: support functions for extracting arguments and
# flags. Note bash's `shift` operation is simulated by the `__sp_remaining_args`
# array which is roughly equivalent to `$@` in bash.
#
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
function get_sp_flags -d "return leading flags"
2020-07-01 03:26:27 +08:00
#
2023-08-30 00:12:19 +08:00
# Accumulate initial flags for main spack command. NOTE: Sets the external
# array: `__sp_remaining_args` containing all unprocessed arguments.
2020-07-01 03:26:27 +08:00
#
2023-08-30 00:12:19 +08:00
# initialize argument counter
set -l i 1
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
# iterate over elements (`elt`) in `argv` array
for elt in $argv
# match element `elt` of `argv` array to check if it has a leading dash
if echo $elt | string match -r -q "^-"
# by echoing the current `elt`, the calling stream accumulates list
# of valid flags. NOTE that this can also be done by adding to an
# array, but fish functions can only return integers, so this is the
# most elegant solution.
echo $elt
else
# bash compatibility: stop when the match first fails. Upon failure,
# we pack the remainder of `argv` into a global `__sp_remaining_args`
# array (`i` tracks the index of the next element).
set __sp_remaining_args ( stream_args $argv [ $i .. - 1 ] )
return
end
# increment argument counter: used in place of bash's `shift` command
set -l i ( math $i +1 )
2020-07-01 03:26:27 +08:00
end
2023-08-30 00:12:19 +08:00
# if all elements in `argv` are matched, make sure that `__sp_remaining_args`
# is deleted (this might be overkill...).
set -e __sp_remaining_args
end
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
#
# CHECK_SP_FLAGS, CONTAINS_HELP_FLAGS, CHECK_ENV_ACTIVATE_FLAGS, and
# CHECK_ENV_DEACTIVATE_FLAGS: support functions for checking arguments and flags.
#
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
function check_sp_flags -d "check spack flags for h/V flags"
2020-07-01 03:26:27 +08:00
#
2023-08-30 00:12:19 +08:00
# Check if inputs contain h or V flags.
2020-07-01 03:26:27 +08:00
#
2023-08-30 00:12:19 +08:00
# combine argument array into single string (space seperated), to be passed
# to regular expression matching (`string match -r`)
set -l _a " $argv "
2020-09-10 23:01:44 +08:00
2023-08-30 00:12:19 +08:00
# skip if called with blank input. Notes: [1] (cf. EOF)
if test -n " $_a "
if echo $_a | string match -r -q ".*h.*"
return 0
end
if echo $_a | string match -r -q ".*V.*"
return 0
2023-08-29 11:16:44 +08:00
end
2020-09-10 23:01:44 +08:00
end
2023-08-30 00:12:19 +08:00
return 1
end
2020-09-10 23:01:44 +08:00
2023-08-30 00:12:19 +08:00
function match_flag -d "checks all combinations of flags ocurring inside of a string"
2020-09-10 23:01:44 +08:00
2023-08-30 00:12:19 +08:00
# Remove leading and trailing spaces -- but we need to insert a "guard" (x)
# so that eg. `string trim -h` doesn't trigger the help string for `string trim`
set -l _a ( string sub -s 2 ( string trim " x $argv [1] " ) )
set -l _b ( string sub -s 2 ( string trim " x $argv [2] " ) )
2020-09-10 23:01:44 +08:00
2023-08-30 00:12:19 +08:00
if test -z " $_a " ; or test -z " $_b "
return 0
end
2020-09-10 23:01:44 +08:00
2023-08-30 00:12:19 +08:00
# surrounded by spaced
if echo " $_a " | string match -r -q " + $_b + "
return 0
end
# beginning of string + trailing space
if echo " $_a " | string match -r -q " ^ $_b + "
return 0
end
# end of string + leadingg space
if echo " $_a " | string match -r -q " + $_b \$ "
return 0
end
2020-09-10 23:01:44 +08:00
2023-08-30 00:12:19 +08:00
# entire string
if echo " $_a " | string match -r -q " ^ $_b \$ "
return 0
2023-08-29 11:16:44 +08:00
end
2020-09-10 23:01:44 +08:00
2023-08-30 00:12:19 +08:00
return 1
2020-09-10 23:01:44 +08:00
2023-08-30 00:12:19 +08:00
end
function check_env_activate_flags -d "check spack env subcommand flags for -h, --sh, --csh, or --fish"
#
# Check if inputs contain -h/--help, --sh, --csh, or --fish
#
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
# combine argument array into single string (space seperated), to be passed
# to regular expression matching (`string match -r`)
set -l _a " $argv "
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
# skip if called with blank input. Notes: [1] (cf. EOF)
if test -n " $_a "
2020-09-10 23:01:44 +08:00
2023-08-30 00:12:19 +08:00
# looks for a single `-h`
if match_flag $_a "-h"
2020-09-10 23:01:44 +08:00
return 0
end
2023-08-30 00:12:19 +08:00
# looks for a single `--help`
if match_flag $_a "--help"
2020-07-01 03:26:27 +08:00
return 0
end
2023-08-30 00:12:19 +08:00
# looks for a single `--sh`
if match_flag $_a "--sh"
2020-07-01 03:26:27 +08:00
return 0
end
2023-08-30 00:12:19 +08:00
# looks for a single `--csh`
if match_flag $_a "--csh"
2020-07-01 03:26:27 +08:00
return 0
end
2023-08-30 00:12:19 +08:00
# looks for a single `--fish`
if match_flag $_a "--fish"
2020-07-01 03:26:27 +08:00
return 0
end
2023-08-30 00:12:19 +08:00
# looks for a single `--list`
if match_flag $_a "--list"
return 0
end
2021-11-05 15:58:29 +08:00
2020-07-01 03:26:27 +08:00
end
2023-08-30 00:12:19 +08:00
return 1
end
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
function check_env_deactivate_flags -d "check spack env subcommand flags for --sh, --csh, or --fish"
#
# Check if inputs contain --sh, --csh, or --fish
#
2023-08-29 11:16:44 +08:00
2023-08-30 00:12:19 +08:00
# combine argument array into single string (space seperated), to be passed
# to regular expression matching (`string match -r`)
set -l _a " $argv "
2023-08-29 11:16:44 +08:00
2023-08-30 00:12:19 +08:00
# skip if called with blank input. Notes: [1] (cf. EOF)
if test -n " $_a "
2023-08-29 11:16:44 +08:00
2023-08-30 00:12:19 +08:00
# looks for a single `--sh`
if match_flag $_a "--sh"
return 0
end
2023-08-29 11:16:44 +08:00
2023-08-30 00:12:19 +08:00
# looks for a single `--csh`
if match_flag $_a "--csh"
return 0
end
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
# looks for a single `--fish`
if match_flag $_a "--fish"
return 0
2020-07-01 03:26:27 +08:00
end
end
2023-08-30 00:12:19 +08:00
return 1
end
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
#
# SPACK RUNNER function, this does all the work!
#
2023-08-29 11:16:44 +08:00
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
function spack_runner -d "Runner function for the `spack` wrapper"
# Store DYLD_* variables from spack shell function
# This is necessary because MacOS System Integrity Protection clears
# variables that affect dyld on process start.
for var in DYLD_LIBRARY_PATH DYLD_FALLBACK_LIBRARY_PATH
if set -q $var
set -gx SPACK_ $var $$ var
2023-07-04 16:48:56 +08:00
end
end
2020-07-01 03:26:27 +08:00
#
2023-08-30 00:12:19 +08:00
# Accumulate initial flags for main spack command
2020-07-01 03:26:27 +08:00
#
2023-08-30 00:12:19 +08:00
set __sp_remaining_args # remaining (unparsed) arguments
set -l sp_flags ( get_sp_flags $argv ) # sets __sp_remaining_args
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
#
# h and V flags don't require further output parsing.
#
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
if check_sp_flags $sp_flags
command spack $sp_flags $__sp_remaining_args
return
end
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
#
# Isolate subcommand and subcommand specs. Notes: [1] (cf. EOF)
#
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
set -l sp_subcommand ""
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
if test -n " $__sp_remaining_args [1] "
set sp_subcommand $__sp_remaining_args [ 1 ]
set __sp_remaining_args ( shift_args $__sp_remaining_args ) # simulates bash shift
end
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
set -l sp_spec $__sp_remaining_args
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
#
# Filter out cd, env, and load and unload. For any other commands, just run
# the spack command as is.
#
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
switch $sp_subcommand
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
# CASE: spack subcommand is `cd`: if the sub command arg is `-h`, nothing
# further needs to be done. Otherwise, test the location referring the
# subcommand and cd there (if it exists).
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
case "cd"
2023-08-29 11:16:44 +08:00
2023-08-30 00:12:19 +08:00
set -l sp_arg ""
2023-08-29 11:16:44 +08:00
2023-08-30 00:12:19 +08:00
# Extract the first subcommand argument. Notes: [1] (cf. EOF)
if test -n " $__sp_remaining_args [1] "
set sp_arg $__sp_remaining_args [ 1 ]
set __sp_remaining_args ( shift_args $__sp_remaining_args ) # simulates bash shift
end
2023-08-29 11:16:44 +08:00
2023-08-30 00:12:19 +08:00
# Notes: [2] (cf. EOF)
if test " x $sp_arg " = "x-h" ; or test " x $sp_arg " = "x--help"
# nothing more needs to be done for `-h` or `--help`
command spack cd -h
return
else
# extract location using the subcommand (fish `(...)`)
set -l LOC ( command spack location $sp_arg $__sp_remaining_args )
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
# test location and cd if exists:
if test -d " $LOC "
cd $LOC
2023-08-29 11:16:44 +08:00
return
else
2023-08-30 00:12:19 +08:00
return 1
2023-08-29 11:16:44 +08:00
end
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
end
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
# CASE: spack subcommand is `env`. Here we get the spack runtime to
# supply the appropriate shell commands for setting the environment
# varibles. These commands are then run by fish (using the `capture_all`
# function, instead of a command substitution).
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
case "env"
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
set -l sp_arg ""
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
# Extract the first subcommand argument. Notes: [1] (cf. EOF)
if test -n " $__sp_remaining_args [1] "
set sp_arg $__sp_remaining_args [ 1 ]
set __sp_remaining_args ( shift_args $__sp_remaining_args ) # simulates bash shift
end
# Notes: [2] (cf. EOF)
if test " x $sp_arg " = "x-h" ; or test " x $sp_arg " = "x--help"
# nothing more needs to be done for `-h` or `--help`
command spack env -h
return
else
switch $sp_arg
case "activate"
set -l _a ( stream_args $__sp_remaining_args )
if check_env_activate_flags $_a
# no args or args contain -h/--help, --sh, or --csh: just execute
command spack env activate $_a
return
else
# actual call to activate: source the output
set -l sp_env_cmd " command spack $sp_flags env activate --fish $__sp_remaining_args "
capture_all $sp_env_cmd __sp_stat __sp_stdout __sp_stderr
eval $__sp_stdout
if test -n " $__sp_stderr "
echo -s \n $__sp_stderr 1 > & 2 # current fish bug: handle stderr manually
2020-07-01 03:26:27 +08:00
end
2023-08-30 00:12:19 +08:00
return $__sp_stat
end
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
case "deactivate"
set -l _a ( stream_args $__sp_remaining_args )
if check_env_deactivate_flags $_a
# just execute the command if --sh, --csh, or --fish are provided
command spack env deactivate $_a
return
# Test of further (unparsed arguments). Any other
# arguments are an error or help, so just run help
# -> TODO: This should throw and error but leave as is
# for compatibility with setup-env.sh
# -> Notes: [1] (cf. EOF).
else if test -n " $__sp_remaining_args "
command spack env deactivate -h
return
else
# no args: source the output of the command
set -l sp_env_cmd " command spack $sp_flags env deactivate --fish "
capture_all $sp_env_cmd __sp_stat __sp_stdout __sp_stderr
if test $__sp_stat -ne 0
if test -n " $__sp_stderr "
echo -s \n $__sp_stderr 1 > & 2 # current fish bug: handle stderr manually
2023-08-29 11:16:44 +08:00
end
2023-08-30 00:12:19 +08:00
return $__sp_stat
2023-08-29 11:16:44 +08:00
end
2023-08-30 00:12:19 +08:00
eval $__sp_stdout
end
2023-08-29 11:16:44 +08:00
2023-08-30 00:12:19 +08:00
case "*"
# if $__sp_remaining_args is empty, then don't include it
# as argument (otherwise it will be confused as a blank
# string input!)
if test -n " $__sp_remaining_args "
command spack env $sp_arg $__sp_remaining_args
return
else
command spack env $sp_arg
return
end
2020-07-01 03:26:27 +08:00
end
2023-08-30 00:12:19 +08:00
end
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
# CASE: spack subcommand is either `load`, or `unload`. These statements
# deal with the technical details of actually using modules. Especially
# to deal with the substituting latest version numbers to the module
# command.
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
case "load" or "unload"
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
set -l _a ( stream_args $__sp_remaining_args )
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
if check_env_activate_flags $_a
# no args or args contain -h/--help, --sh, or --csh: just execute
command spack $sp_flags $sp_subcommand $__sp_remaining_args
return
else
# actual call to activate: source the output
set -l sp_env_cmd " command spack $sp_flags $sp_subcommand --fish $__sp_remaining_args "
capture_all $sp_env_cmd __sp_stat __sp_stdout __sp_stderr
if test $__sp_stat -ne 0
if test -n " $__sp_stderr "
echo -s \n $__sp_stderr 1 > & 2 # current fish bug: handle stderr manually
2020-07-01 03:26:27 +08:00
end
2023-08-30 00:12:19 +08:00
return $__sp_stat
2020-07-01 03:26:27 +08:00
end
2023-08-30 00:12:19 +08:00
eval $__sp_stdout
end
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
# CASE: Catch-all
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
case "*"
command spack $argv
return
2020-07-01 03:26:27 +08:00
end
2023-08-30 00:12:19 +08:00
end
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
#
# RUN SPACK_RUNNER HERE
#
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
#
# Allocate temporary global variables used for return extra arguments from
# functions. NOTE: remember to call delete_sp_shared whenever returning from
# this function.
#
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
allocate_sp_shared
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
#
# Run spack command using the spack_runner.
#
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
spack_runner $argv
# Capture state of spack_runner (returned below)
set -l stat $status
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
#
# Delete temprary global variabels allocated in `allocated_sp_shared`.
#
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
delete_sp_shared
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
return $stat
2020-07-01 03:26:27 +08:00
end
#################################################################################
# Prepends directories to path, if they exist.
# pathadd /path/to/dir # add to PATH
# or pathadd OTHERPATH /path/to/dir # add to OTHERPATH
#################################################################################
function spack_pathadd -d "Add path to specified variable (defaults to PATH)"
#
# Adds (existing only) paths to specified (defaults to PATH)
# variable. Does not warn attempting to add non-existing path. This is not a
# bug because the MODULEPATH setup tries add all possible compatible systems
# and therefore sp_multi_pathadd relies on this function failing silently.
#
# If no variable name is supplied, just append to PATH otherwise append to
# that variable.
# -> Notes: [1] (cf. EOF).
if test -n " $argv [2] "
set pa_varname $argv [ 1 ]
set pa_new_path $argv [ 2 ]
else
true # this is a bit of a strange hack! Notes: [3] (cf EOF).
set pa_varname PATH
set pa_new_path $argv [ 1 ]
end
set pa_oldvalue $$ pa_varname
# skip path is not existing directory
# -> Notes: [1] (cf. EOF).
if test -d " $pa_new_path "
# combine argument array into single string (space seperated), to be
# passed to regular expression matching (`string match -r`)
set -l _a " $pa_oldvalue "
2024-01-06 01:31:32 +08:00
# skip path if it is already the first in the variable
2020-07-01 03:26:27 +08:00
# note spaces in regular expression: we're matching to a space delimited
# list of paths
2024-01-06 01:31:32 +08:00
if not echo $_a | string match -q -r " ^ $pa_new_path * "
2020-07-01 03:26:27 +08:00
if test -n " $pa_oldvalue "
set $pa_varname $pa_new_path $pa_oldvalue
else
true # this is a bit of a strange hack! Notes: [3] (cf. EOF)
set $pa_varname $pa_new_path
end
end
end
end
function sp_multi_pathadd -d "Helper for adding module-style paths by incorporating compatible systems into pathadd" --inherit-variable _sp_compatible_sys_types
#
# Calls spack_pathadd in path inputs, adding all compatible system types
# (sourced from $_sp_compatible_sys_types) to input paths.
#
for pth in $argv [ 2 ]
for systype in $_sp_compatible_sys_types
spack_pathadd $argv [ 1 ] " $pth / $systype "
end
end
end
#
# Figure out where this file is. Below code only needs to work in fish
#
2023-08-30 00:12:19 +08:00
set -l sp_source_file ( status -f ) # name of current file
2020-07-01 03:26:27 +08:00
2021-02-13 02:52:44 +08:00
#
# Identify and lock the python interpreter
#
for cmd in " $SPACK_PYTHON " python3 python python2
2021-02-17 02:49:05 +08:00
set -l _sp_python ( command -v " $cmd " )
2021-02-13 02:52:44 +08:00
if test $status -eq 0
set -x SPACK_PYTHON $_sp_python
break
end
end
2020-07-01 03:26:27 +08:00
#
# Find root directory and add bin to path.
#
set -l sp_share_dir ( realpath ( dirname $sp_source_file ) )
set -l sp_prefix ( realpath ( dirname ( dirname $sp_share_dir ) ) )
spack_pathadd PATH " $sp_prefix /bin "
set -xg SPACK_ROOT $sp_prefix
#
# No need to determine which shell is being used (obviously it's fish)
#
2023-08-30 00:12:19 +08:00
set -xg SPACK_SHELL "fish"
set -xg _sp_shell "fish"
2020-07-01 03:26:27 +08:00
2021-07-08 23:07:26 +08:00
if test -z " $SPACK_SKIP_MODULES "
#
# Check whether we need environment-variables (module) <= `use` is not available
#
2023-08-30 00:12:19 +08:00
set -l need_module "no"
2021-07-08 23:07:26 +08:00
if not functions -q use; and not functions -q module
2023-08-30 00:12:19 +08:00
set need_module "yes"
2021-07-08 23:07:26 +08:00
end
2020-07-01 03:26:27 +08:00
2021-07-08 23:07:26 +08:00
#
# Make environment-modules available to shell
#
function sp_apply_shell_vars -d "applies expressions of the type `a='b'` as `set a b`"
2020-07-01 03:26:27 +08:00
2021-07-08 23:07:26 +08:00
# convert `a='b' to array variable `a b`
set -l expr_token ( string trim -c "'" ( string split "=" $argv ) )
2020-07-01 03:26:27 +08:00
2021-07-08 23:07:26 +08:00
# run set command to takes, converting lists of type `a:b:c` to array
# variables `a b c` by splitting around the `:` character
set -xg $expr_token [ 1 ] ( string split ":" $expr_token [ 2 ] )
end
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
if test " $need_module " = "yes"
2021-07-08 23:07:26 +08:00
set -l sp_shell_vars ( command spack --print-shell-vars sh,modules)
2020-07-01 03:26:27 +08:00
2021-07-08 23:07:26 +08:00
for sp_var_expr in $sp_shell_vars
sp_apply_shell_vars $sp_var_expr
end
2020-07-01 03:26:27 +08:00
2021-07-08 23:07:26 +08:00
# _sp_module_prefix is set by spack --print-sh-vars
2023-08-30 00:12:19 +08:00
if test " $_sp_module_prefix " != "not_installed"
2021-07-08 23:07:26 +08:00
set -xg MODULE_PREFIX $_sp_module_prefix
spack_pathadd PATH " $MODULE_PREFIX /bin "
end
2020-07-01 03:26:27 +08:00
2021-07-08 23:07:26 +08:00
else
2020-07-01 03:26:27 +08:00
2021-07-08 23:07:26 +08:00
set -l sp_shell_vars ( command spack --print-shell-vars sh)
2020-07-01 03:26:27 +08:00
2021-07-08 23:07:26 +08:00
for sp_var_expr in $sp_shell_vars
sp_apply_shell_vars $sp_var_expr
end
2020-07-01 03:26:27 +08:00
2021-07-08 23:07:26 +08:00
end
2020-07-01 03:26:27 +08:00
2023-08-30 00:12:19 +08:00
if test " $need_module " = "yes"
2021-07-08 23:07:26 +08:00
function module -d "wrapper for the `module` command to point at Spack's modules instance" --inherit-variable MODULE_PREFIX
eval $MODULE_PREFIX /bin/modulecmd $SPACK_SHELL $argv
end
2020-07-01 03:26:27 +08:00
end
2021-07-08 23:07:26 +08:00
#
# set module system roots
#
2020-07-01 03:26:27 +08:00
2021-07-08 23:07:26 +08:00
# Search of MODULESPATHS by trying all possible compatible system types as
# module roots.
if test -z " $MODULEPATH "
set -gx MODULEPATH
end
sp_multi_pathadd MODULEPATH $_sp_tcl_roots
2020-07-01 03:26:27 +08:00
end
2023-07-22 21:55:12 +08:00
# Add programmable tab completion for fish
#
set -l fish_version ( string split '.' $FISH_VERSION )
if test $fish_version [ 1 ] -gt 3
or test $fish_version [ 1 ] -eq 3
and test $fish_version [ 2 ] -ge 2
2020-07-01 03:26:27 +08:00
2023-07-22 21:55:12 +08:00
source $sp_share_dir /spack-completion.fish
end
2020-07-01 03:26:27 +08:00
#
# NOTES
#
# [1]: `test -n` requires exactly 1 argument. If `argv` is undefined, or if it
# is an array, `test -n $argv` is unpredictable. Instead, encapsulate
# `argv` in a string, and test the string.
#
# [2]: `test "$a" = "$b$` is dangerous if `a` and `b` contain flags at index 1,
# as `test $a` can be interpreted as `test $a[1] $a[2..-1]`. Solution is to
# prepend a non-flag character, eg: `test "x$a" = "x$b"`.
#
# [3]: When the test in the if statement fails, the `status` flag is set to 1.
# `true` here manuallt resets the value of `status` to 0. Since `set`
# passes `status` along, we thus avoid the function returning 1 by mistake.
2020-07-06 13:35:01 +08:00
# done: unset sentinel variable as we're no longer initializing
set -e _sp_initializing