Guess a possible nit_dir.

It uses, in order:

  • the option opt_nit_dir
  • the environment variable NIT_DIR
  • the runpath of the program from argv[0]
  • the runpath of the process from /proc
  • the search in PATH

If there is errors (e.g. the indicated path is invalid) or if no path is found, then an error is displayed and the program exits.

The result is returned without being assigned to nit_dir. This function is automatically called by process_options

Property definitions

nitc $ ToolContext :: locate_nit_dir
	# Guess a possible nit_dir.
	#
	# It uses, in order:
	#
	# * the option `opt_nit_dir`
	# * the environment variable `NIT_DIR`
	# * the runpath of the program from argv[0]
	# * the runpath of the process from /proc
	# * the search in PATH
	#
	# If there is errors (e.g. the indicated path is invalid) or if no
	# path is found, then an error is displayed and the program exits.
	#
	# The result is returned without being assigned to `nit_dir`.
	# This function is automatically called by `process_options`
	fun locate_nit_dir: String
	do
		# the option has precedence
		var res = opt_nit_dir.value
		if res != null then
			if not check_nit_dir(res) then
				fatal_error(null, "Fatal Error: the value of --nit-dir does not seem to be a valid base Nit directory: {res}.")
			end
			return res
		end

		# then the environ variable has precedence
		res = "NIT_DIR".environ
		if not res.is_empty then
			if not check_nit_dir(res) then
				fatal_error(null, "Fatal Error: the value of NIT_DIR does not seem to be a valid base Nit directory: {res}.")
			end
			return res
		end

		# find the runpath of the program from argv[0]
		res = "{sys.program_name.dirname}/.."
		if check_nit_dir(res) then return res.simplify_path

		# find the runpath of the process from /proc
		var exe = "/proc/self/exe"
		if exe.file_exists then
			res = exe.realpath
			res = res.dirname.join_path("..")
			if check_nit_dir(res) then return res.simplify_path
		end

		# search in the PATH
		var path_sep = if is_windows then ";" else ":"
		var ps = "PATH".environ.split(path_sep)
		for p in ps do
			res = p/".."
			if check_nit_dir(res) then return res.simplify_path
		end

		fatal_error(null, "Fatal Error: cannot locate a valid base Nit directory. It is quite unexpected. Try to set the environment variable `NIT_DIR` or to use the `--nit-dir` option.")
		abort
	end
src/toolcontext.nit:571,2--628,4