ToolContext exposes a `nit_dir` service that tools can use.
It is so good that, now, the `bin` directory can be directly put in the PATH on most system while `NIT_DIR` is unset.
Previous direct users of `NIT_DIR` are transformed to use the new service.
Also, `nitunit` uses the new service to find the compiler instead of a hard-coded path.
Closes #392
Closes #408
Pull-Request: #412
Reviewed-by: Alexis Laferrière <alexis.laf@xymus.net>
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>
# The list is initially set with :
# * the toolcontext --cc-path option
# * the NIT_CC_PATH environment variable
- # * some heuristics including the NIT_DIR environment variable and the progname of the process
+ # * `toolcontext.nit_dir`
# Path can be added (or removed) by the client
var cc_paths = new Array[String]
protected fun gather_cc_paths
do
# Look for the the Nit clib path
- var path_env = "NIT_DIR".environ
- if not path_env.is_empty then
+ var path_env = toolcontext.nit_dir
+ if path_env != null then
var libname = "{path_env}/clib"
if libname.file_exists then cc_paths.add(libname)
end
- var libname = "{sys.program_name.dirname}/../clib"
- if libname.file_exists then cc_paths.add(libname.simplify_path)
-
if cc_paths.is_empty then
toolcontext.error(null, "Cannot determine the nit clib path. define envvar NIT_DIR.")
end
### Link to png sources
# libpng is not available on Android NDK
# FIXME make obtionnal when we have alternatives to mnit
- var nit_dir = "NIT_DIR".environ
- var share_dir
- if not nit_dir.is_empty then
- share_dir = "{nit_dir}/share/"
- else
- share_dir = "{sys.program_name.dirname}/../share/"
- end
- if not share_dir.file_exists then
+ var nit_dir = toolcontext.nit_dir
+ var share_dir = "{nit_dir}/share/"
+ if nit_dir == null or not share_dir.file_exists then
print "Android project error: Nit share directory not found, please use the environment variable NIT_DIR"
exit 1
end
# Update references in file
perl -i -npe 's#"\.\./.*?([^/]*.h)"#"\1"#' "$out"/*.[ch]
perl -i -npe 's#\S*/([^/]*.[ch])#\1#' "$out/Makefile"
-perl -i -npe 's#../clib#.#' "$out/Makefile"
+perl -i -npe 's#\.\./clib#.#' "$out/Makefile"
paths.append(path_env.split_with(':'))
end
- path_env = "NIT_DIR".environ
- if not path_env.is_empty then
- var libname = "{path_env}/lib"
+ var nit_dir = toolcontext.nit_dir
+ if nit_dir != null then
+ var libname = "{nit_dir}/lib"
if libname.file_exists then paths.add(libname)
end
-
- var libname = "{sys.program_name.dirname}/../lib"
- if libname.file_exists then paths.add(libname.simplify_path)
end
# Load a bunch of modules.
# The list is initially set with :
# * the toolcontext --path option
# * the NIT_PATH environment variable
- # * some heuristics including the NIT_DIR environment variable and the progname of the process
+ # * `toolcontext.nit_dir`
# Path can be added (or removed) by the client
var paths: Array[String] = new Array[String]
if opt_sharedir.value != null then
share_dir = opt_sharedir.value
else
- var dir = "NIT_DIR".environ
- if dir.is_empty then
- dir = "{sys.program_name.dirname}/../share/nitdoc"
- else
- dir = "{dir}/share/nitdoc"
- end
- share_dir = dir
- if share_dir == null then
+ var dir = toolcontext.nit_dir
+ share_dir = "{dir}/share/nitdoc"
+ if dir == null or not share_dir.file_exists then
print "Error: Cannot locate nitdoc share files. Uses --sharedir or envvar NIT_DIR"
abort
end
end
f.close
- var cmd = "../bin/nitg --ignore-visibility --no-color '{file}' -I . >'{file}.out1' 2>&1 </dev/null -o '{file}.bin'"
+ var nit_dir = toolcontext.nit_dir
+ var nitg = "{nit_dir}/bin/nitg"
+ if nit_dir == null or not nitg.file_exists then
+ toolcontext.error(null, "Cannot find nitg. Set envvar NIT_DIR.")
+ toolcontext.check_errors
+ end
+ var cmd = "{nitg} --ignore-visibility --no-color '{file}' -I . >'{file}.out1' 2>&1 </dev/null -o '{file}.bin'"
var res = sys.system(cmd)
var res2 = 0
if res == 0 then
model = new Model
mbuilder = new ModelBuilder(model, toolcontext)
- # Here we load an process std modules
- #var dir = "NIT_DIR".environ
- #var mmodules = modelbuilder.parse_and_build(["{dir}/lib/standard/standard.nit"])
var mmodules = mbuilder.parse([arguments.first])
if mmodules.is_empty then return
mbuilder.run_phases
# Make sure the output directory exists
log_directory.mkdir
end
+
+ nit_dir = compute_nit_dir
+ end
+
+ # The identified root directory of the Nit project
+ var nit_dir: nullable String
+
+ private fun compute_nit_dir: nullable String
+ do
+ # a environ variable has precedence
+ var res = "NIT_DIR".environ
+ if not res.is_empty then return res
+
+ # find the runpath of the program from argv[0]
+ res = "{sys.program_name.dirname}/.."
+ if res.file_exists and "{res}/src/nit.nit".file_exists 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 res.file_exists and "{res}/src/nit.nit".file_exists then return res.simplify_path
+ end
+
+ return null
end
end
-alt/base_import_alt3.nit:1,8--21: Error: cannot find module fail from project1. tried ., ../lib/standard, ../lib/standard/collection, alt, ../lib, ../lib
+alt/base_import_alt3.nit:1,8--21: Error: cannot find module fail from project1. tried ., ../lib/standard, ../lib/standard/collection, alt, ../lib
-error_mod_unk.nit:17,8--11: Error: cannot find module dfgd from error_mod_unk. tried ., ../lib/standard, ../lib/standard/collection, alt, ../lib, ../lib
+error_mod_unk.nit:17,8--11: Error: cannot find module dfgd from error_mod_unk. tried ., ../lib/standard, ../lib/standard/collection, alt, ../lib