Merge: `nit_dir` as a service
authorJean Privat <jean@pryen.org>
Wed, 23 Apr 2014 20:48:32 +0000 (16:48 -0400)
committerJean Privat <jean@pryen.org>
Wed, 23 Apr 2014 20:48:32 +0000 (16:48 -0400)
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>

src/abstract_compiler.nit
src/android_platform.nit
src/mkcsrc
src/modelbuilder.nit
src/nitdoc.nit
src/nitunit.nit
src/nitx.nit
src/toolcontext.nit
tests/sav/base_import_alt3.res
tests/sav/error_mod_unk.res

index a62bac1..d03c1ea 100644 (file)
@@ -128,22 +128,19 @@ class MakefileToolchain
        # 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
index 6dc34f8..070ac1e 100644 (file)
@@ -183,14 +183,9 @@ $(call import-module,android/native_app_glue)
                ### 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
index b3b6069..a529c3d 100755 (executable)
@@ -20,4 +20,4 @@ done
 # 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"
index 9492734..8b6f6e9 100644 (file)
@@ -134,14 +134,11 @@ class ModelBuilder
                        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.
@@ -292,7 +289,7 @@ class ModelBuilder
        # 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]
 
index abcc91f..91f00b8 100644 (file)
@@ -105,14 +105,9 @@ class NitdocContext
                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
index f4f9719..2833445 100644 (file)
@@ -93,7 +93,13 @@ class NitUnitExecutor
                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
index 7f865b1..532914c 100644 (file)
@@ -55,9 +55,6 @@ class NitIndex
                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
index 85d7cd6..a7d17ce 100644 (file)
@@ -238,5 +238,31 @@ class ToolContext
                        # 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
index 04ccc25..91e89cf 100644 (file)
@@ -1 +1 @@
-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
index 6f9cec9..b2da22d 100644 (file)
@@ -1 +1 @@
-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