| 72448 |      1 | /*  Title:      Pure/System/executable.scala
 | 
|  |      2 |     Author:     Makarius
 | 
|  |      3 | 
 | 
|  |      4 | Support for platform-specific executables.
 | 
|  |      5 | */
 | 
|  |      6 | 
 | 
|  |      7 | package isabelle
 | 
|  |      8 | 
 | 
|  |      9 | 
 | 
|  |     10 | object Executable
 | 
|  |     11 | {
 | 
| 72460 |     12 |   def libraries_closure(path: Path,
 | 
| 72454 |     13 |     mingw: MinGW = MinGW.none,
 | 
| 72460 |     14 |     filter: String => Boolean = _ => true,
 | 
|  |     15 |     patchelf: Boolean = false): List[String] =
 | 
| 72448 |     16 |   {
 | 
| 72460 |     17 |     val exe_path = path.expand
 | 
| 72454 |     18 |     val exe_dir = exe_path.dir
 | 
|  |     19 |     val exe = exe_path.base
 | 
| 72448 |     20 | 
 | 
| 72454 |     21 |     val ldd_lines =
 | 
| 72448 |     22 |     {
 | 
| 72454 |     23 |       val ldd = if (Platform.is_macos) "otool -L" else "ldd"
 | 
|  |     24 |       val script = mingw.bash_script(ldd + " " + File.bash_path(exe))
 | 
|  |     25 |       Library.split_lines(Isabelle_System.bash(script, cwd = exe_dir.file).check.out)
 | 
| 72448 |     26 |     }
 | 
|  |     27 | 
 | 
| 72454 |     28 |     def lib_name(lib: String): String =
 | 
| 72468 |     29 |       Library.take_prefix[Char](c => c != '.' && c != '-',
 | 
| 72454 |     30 |         Library.take_suffix[Char](_ != '/', lib.toList)._2)._1.mkString
 | 
|  |     31 | 
 | 
|  |     32 |     val libs =
 | 
|  |     33 |       if (Platform.is_macos) {
 | 
|  |     34 |         val Pattern = """^\s*(/.+)\s+\(.*\)$""".r
 | 
|  |     35 |         for {
 | 
|  |     36 |           Pattern(lib) <- ldd_lines
 | 
|  |     37 |           if !lib.startsWith("@executable_path/") && filter(lib_name(lib))
 | 
|  |     38 |         } yield lib
 | 
|  |     39 |       }
 | 
|  |     40 |       else {
 | 
|  |     41 |         val Pattern = """^.*=>\s*(/.+)\s+\(.*\)$""".r
 | 
|  |     42 |         val prefix =
 | 
|  |     43 |           mingw.root match {
 | 
|  |     44 |             case None => ""
 | 
|  |     45 |             case Some(path) => path.absolute.implode
 | 
|  |     46 |           }
 | 
|  |     47 |         for { Pattern(lib) <- ldd_lines if filter(lib_name(lib)) }
 | 
|  |     48 |           yield prefix + lib
 | 
|  |     49 |       }
 | 
|  |     50 | 
 | 
|  |     51 |     if (libs.nonEmpty) {
 | 
| 73317 |     52 |       libs.foreach(lib => Isabelle_System.copy_file(Path.explode(lib), exe_dir))
 | 
| 72454 |     53 | 
 | 
|  |     54 |       if (Platform.is_linux) {
 | 
| 72460 |     55 |         if (patchelf) {
 | 
|  |     56 |           // requires e.g. Ubuntu 16.04 LTS
 | 
|  |     57 |           Isabelle_System.require_command("patchelf")
 | 
|  |     58 |           Isabelle_System.bash("patchelf --force-rpath --set-rpath '$ORIGIN' " + File.bash_path(exe_path)).check
 | 
|  |     59 |         }
 | 
|  |     60 |         else {
 | 
|  |     61 |           // requires e.g. LDFLAGS=-Wl,-rpath,_DUMMY_
 | 
|  |     62 |           Isabelle_System.require_command("chrpath")
 | 
|  |     63 |           Isabelle_System.bash("chrpath -r '$ORIGIN' " + File.bash_path(exe_path)).check
 | 
|  |     64 |         }
 | 
| 72454 |     65 |       }
 | 
|  |     66 |       else if (Platform.is_macos) {
 | 
|  |     67 |         val script =
 | 
|  |     68 |           ("install_name_tool" ::
 | 
|  |     69 |             libs.map(file => "-change " + Bash.string(file) + " " +
 | 
|  |     70 |               Bash.string("@executable_path/" + Path.explode(file).file_name) + " " +
 | 
|  |     71 |               File.bash_path(exe))).mkString(" ")
 | 
|  |     72 |         Isabelle_System.bash(script, cwd = exe_dir.file).check
 | 
| 72448 |     73 |       }
 | 
|  |     74 |     }
 | 
| 72454 |     75 | 
 | 
|  |     76 |     libs
 | 
| 72448 |     77 |   }
 | 
|  |     78 | }
 |