src/Pure/System/isabelle_env.scala
changeset 73890 8f6b2eb15240
child 73891 6c9044f04756
equal deleted inserted replaced
73889:5ec68c1a07d8 73890:8f6b2eb15240
       
     1 /*  Title:      Pure/System/isabelle_env.scala
       
     2     Author:     Makarius
       
     3 
       
     4 Fundamental Isabelle system environment: quasi-static module with
       
     5 optional init operation.
       
     6 */
       
     7 
       
     8 package isabelle
       
     9 
       
    10 
       
    11 import java.io.{File => JFile}
       
    12 
       
    13 import scala.jdk.CollectionConverters._
       
    14 
       
    15 
       
    16 
       
    17 object Isabelle_Env
       
    18 {
       
    19   /** bootstrap information **/
       
    20 
       
    21   def jdk_home(): String =
       
    22   {
       
    23     val java_home = System.getProperty("java.home", "")
       
    24     val home = new JFile(java_home)
       
    25     val parent = home.getParent
       
    26     if (home.getName == "jre" && parent != null &&
       
    27         (new JFile(new JFile(parent, "bin"), "javac")).exists) parent
       
    28     else java_home
       
    29   }
       
    30 
       
    31   def bootstrap_directory(
       
    32     preference: String, envar: String, property: String, description: String): String =
       
    33   {
       
    34     val value =
       
    35       proper_string(preference) orElse  // explicit argument
       
    36       proper_string(System.getenv(envar)) orElse  // e.g. inherited from running isabelle tool
       
    37       proper_string(System.getProperty(property)) getOrElse  // e.g. via JVM application boot process
       
    38       error("Unknown " + description + " directory")
       
    39 
       
    40     if ((new JFile(value)).isDirectory) value
       
    41     else error("Bad " + description + " directory " + quote(value))
       
    42   }
       
    43 
       
    44 
       
    45 
       
    46   /** raw process **/
       
    47 
       
    48   /* raw process */
       
    49 
       
    50   def process(command_line: List[String],
       
    51     cwd: JFile = null,
       
    52     env: Map[String, String] = settings(),
       
    53     redirect: Boolean = false): Process =
       
    54   {
       
    55     val proc = new ProcessBuilder
       
    56 
       
    57     // fragile on Windows:
       
    58     // see https://docs.microsoft.com/en-us/cpp/cpp/main-function-command-line-args?view=msvc-160
       
    59     proc.command(command_line.asJava)
       
    60 
       
    61     if (cwd != null) proc.directory(cwd)
       
    62     if (env != null) {
       
    63       proc.environment.clear()
       
    64       for ((x, y) <- env) proc.environment.put(x, y)
       
    65     }
       
    66     proc.redirectErrorStream(redirect)
       
    67     proc.start
       
    68   }
       
    69 
       
    70   def process_output(proc: Process): (String, Int) =
       
    71   {
       
    72     proc.getOutputStream.close()
       
    73 
       
    74     val output = File.read_stream(proc.getInputStream)
       
    75     val rc =
       
    76       try { proc.waitFor }
       
    77       finally {
       
    78         proc.getInputStream.close()
       
    79         proc.getErrorStream.close()
       
    80         proc.destroy()
       
    81         Exn.Interrupt.dispose()
       
    82       }
       
    83     (output, rc)
       
    84   }
       
    85 
       
    86 
       
    87 
       
    88   /** implicit settings environment **/
       
    89 
       
    90   abstract class Service
       
    91 
       
    92   @volatile private var _settings: Option[Map[String, String]] = None
       
    93   @volatile private var _services: Option[List[Class[Service]]] = None
       
    94 
       
    95   def settings(): Map[String, String] =
       
    96   {
       
    97     if (_settings.isEmpty) init()  // unsynchronized check
       
    98     _settings.get
       
    99   }
       
   100 
       
   101   def services(): List[Class[Service]] =
       
   102   {
       
   103     if (_services.isEmpty) init()  // unsynchronized check
       
   104     _services.get
       
   105   }
       
   106 
       
   107   def getenv_error(name: String): Nothing =
       
   108     error("Undefined Isabelle environment variable: " + quote(name))
       
   109 
       
   110   def init(isabelle_root: String = "", cygwin_root: String = ""): Unit = synchronized
       
   111   {
       
   112     if (_settings.isEmpty || _services.isEmpty) {
       
   113       val isabelle_root1 =
       
   114         bootstrap_directory(isabelle_root, "ISABELLE_ROOT", "isabelle.root", "Isabelle root")
       
   115 
       
   116       val cygwin_root1 =
       
   117         if (Platform.is_windows)
       
   118           bootstrap_directory(cygwin_root, "CYGWIN_ROOT", "cygwin.root", "Cygwin root")
       
   119         else ""
       
   120 
       
   121       if (Platform.is_windows) Cygwin.init(isabelle_root1, cygwin_root1)
       
   122 
       
   123       def set_cygwin_root(): Unit =
       
   124       {
       
   125         if (Platform.is_windows)
       
   126           _settings = Some(_settings.getOrElse(Map.empty) + ("CYGWIN_ROOT" -> cygwin_root1))
       
   127       }
       
   128 
       
   129       set_cygwin_root()
       
   130 
       
   131       def default(env: Map[String, String], entry: (String, String)): Map[String, String] =
       
   132         if (env.isDefinedAt(entry._1) || entry._2 == "") env
       
   133         else env + entry
       
   134 
       
   135       val env =
       
   136       {
       
   137         val temp_windows =
       
   138         {
       
   139           val temp = if (Platform.is_windows) System.getenv("TEMP") else null
       
   140           if (temp != null && temp.contains('\\')) temp else ""
       
   141         }
       
   142         val user_home = System.getProperty("user.home", "")
       
   143         val isabelle_app = System.getProperty("isabelle.app", "")
       
   144 
       
   145         default(
       
   146           default(
       
   147             default(sys.env + ("ISABELLE_JDK_HOME" -> File.standard_path(jdk_home())),
       
   148               "TEMP_WINDOWS" -> temp_windows),
       
   149             "HOME" -> user_home),
       
   150           "ISABELLE_APP" -> isabelle_app)
       
   151       }
       
   152 
       
   153       val settings =
       
   154       {
       
   155         val dump = JFile.createTempFile("settings", null)
       
   156         dump.deleteOnExit
       
   157         try {
       
   158           val cmd1 =
       
   159             if (Platform.is_windows)
       
   160               List(cygwin_root1 + "\\bin\\bash", "-l",
       
   161                 File.standard_path(isabelle_root1 + "\\bin\\isabelle"))
       
   162             else
       
   163               List(isabelle_root1 + "/bin/isabelle")
       
   164           val cmd = cmd1 ::: List("getenv", "-d", dump.toString)
       
   165 
       
   166           val (output, rc) = process_output(process(cmd, env = env, redirect = true))
       
   167           if (rc != 0) error(output)
       
   168 
       
   169           val entries =
       
   170             space_explode('\u0000', File.read(dump)).flatMap(
       
   171               {
       
   172                 case Properties.Eq(a, b) => Some(a -> b)
       
   173                 case s => if (s.isEmpty || s.startsWith("=")) None else Some(s -> "")
       
   174               }).toMap
       
   175           entries + ("PATH" -> entries("PATH_JVM")) - "PATH_JVM"
       
   176         }
       
   177         finally { dump.delete }
       
   178       }
       
   179       _settings = Some(settings)
       
   180       set_cygwin_root()
       
   181 
       
   182       val variable = "ISABELLE_SCALA_SERVICES"
       
   183       val services =
       
   184         for (name <- space_explode(':', settings.getOrElse(variable, getenv_error(variable))))
       
   185         yield {
       
   186           def err(msg: String): Nothing =
       
   187             error("Bad entry " + quote(name) + " in " + variable + "\n" + msg)
       
   188           try { Class.forName(name).asInstanceOf[Class[Service]] }
       
   189           catch {
       
   190             case _: ClassNotFoundException => err("Class not found")
       
   191             case exn: Throwable => err(Exn.message(exn))
       
   192           }
       
   193         }
       
   194       _services = Some(services)
       
   195     }
       
   196   }
       
   197 }