| author | wenzelm | 
| Sat, 10 Nov 2018 14:08:02 +0100 | |
| changeset 69277 | 258bef08b31e | 
| parent 69186 | 573b7fbd96a8 | 
| child 69367 | 34b7550b66c7 | 
| permissions | -rw-r--r-- | 
| 64929 | 1  | 
/* Title: Pure/Admin/build_jdk.scala  | 
2  | 
Author: Makarius  | 
|
3  | 
||
4  | 
Build Isabelle jdk component from original platform installations.  | 
|
5  | 
*/  | 
|
6  | 
||
7  | 
package isabelle  | 
|
8  | 
||
9  | 
||
| 
69186
 
573b7fbd96a8
updated to jdk-11+28 from https://adoptopenjdk.net -- with proper font rendering on Linux;
 
wenzelm 
parents: 
69128 
diff
changeset
 | 
10  | 
import java.io.{File => JFile}
 | 
| 64933 | 11  | 
import java.nio.file.Files  | 
| 64934 | 12  | 
import java.nio.file.attribute.PosixFilePermission  | 
| 64933 | 13  | 
|
| 64929 | 14  | 
import scala.util.matching.Regex  | 
15  | 
||
16  | 
||
17  | 
object Build_JDK  | 
|
18  | 
{
 | 
|
19  | 
/* version */  | 
|
20  | 
||
| 69128 | 21  | 
def detect_version(s: String): String =  | 
| 64929 | 22  | 
  {
 | 
| 
69186
 
573b7fbd96a8
updated to jdk-11+28 from https://adoptopenjdk.net -- with proper font rendering on Linux;
 
wenzelm 
parents: 
69128 
diff
changeset
 | 
23  | 
val Version_Dir_Entry = """^jdk-(\d+\+\d+)$""".r  | 
| 64929 | 24  | 
    s match {
 | 
| 69128 | 25  | 
case Version_Dir_Entry(version) => version  | 
| 64929 | 26  | 
      case _ => error("Cannot detect JDK version from " + quote(s))
 | 
27  | 
}  | 
|
28  | 
}  | 
|
29  | 
||
30  | 
||
31  | 
/* platform */  | 
|
32  | 
||
| 69128 | 33  | 
sealed case class JDK_Platform(name: String, home: String, exe: String, regex: Regex)  | 
| 64929 | 34  | 
  {
 | 
35  | 
override def toString: String = name  | 
|
36  | 
||
37  | 
def detect(jdk_dir: Path): Boolean =  | 
|
38  | 
    {
 | 
|
39  | 
val path = jdk_dir + Path.explode(exe)  | 
|
40  | 
      if (path.is_file) {
 | 
|
41  | 
        val file_descr = Isabelle_System.bash("file -b " + File.bash_path(path)).check.out
 | 
|
42  | 
regex.pattern.matcher(file_descr).matches  | 
|
43  | 
}  | 
|
44  | 
else false  | 
|
45  | 
}  | 
|
46  | 
}  | 
|
47  | 
val jdk_platforms =  | 
|
| 66906 | 48  | 
List(  | 
| 69128 | 49  | 
      JDK_Platform("x86_64-linux", ".", "bin/java", """.*ELF 64-bit.*x86[-_]64.*""".r),
 | 
50  | 
      JDK_Platform("x86_64-windows", ".", "bin/java.exe", """.*PE32\+ executable.*x86[-_]64.*""".r),
 | 
|
51  | 
      JDK_Platform("x86_64-darwin", "Contents/Home", "Contents/Home/bin/java",
 | 
|
52  | 
""".*Mach-O 64-bit.*x86[-_]64.*""".r))  | 
|
| 64929 | 53  | 
|
54  | 
||
55  | 
/* README */  | 
|
56  | 
||
| 69128 | 57  | 
def readme(version: String): String =  | 
58  | 
"""This is OpenJDK """ + version + """ as required for Isabelle.  | 
|
| 64929 | 59  | 
|
| 
69186
 
573b7fbd96a8
updated to jdk-11+28 from https://adoptopenjdk.net -- with proper font rendering on Linux;
 
wenzelm 
parents: 
69128 
diff
changeset
 | 
60  | 
See https://adoptopenjdk.net for the original downloads, which are covered  | 
| 69128 | 61  | 
the GPL2 (with various liberal exceptions, see legal/*).  | 
| 64929 | 62  | 
|
63  | 
Linux, Windows, Mac OS X all work uniformly, depending on certain  | 
|
64  | 
platform-specific subdirectories.  | 
|
65  | 
"""  | 
|
66  | 
||
67  | 
||
68  | 
/* settings */  | 
|
69  | 
||
70  | 
val settings =  | 
|
71  | 
"""# -*- shell-script -*- :mode=shellscript:  | 
|
72  | 
||
73  | 
case "$ISABELLE_PLATFORM_FAMILY" in  | 
|
74  | 
linux)  | 
|
| 66906 | 75  | 
ISABELLE_JAVA_PLATFORM="$ISABELLE_PLATFORM64"  | 
| 64929 | 76  | 
ISABELLE_JDK_HOME="$COMPONENT/$ISABELLE_JAVA_PLATFORM"  | 
77  | 
;;  | 
|
78  | 
windows)  | 
|
| 66906 | 79  | 
ISABELLE_JAVA_PLATFORM="$ISABELLE_WINDOWS_PLATFORM64"  | 
| 64929 | 80  | 
ISABELLE_JDK_HOME="$COMPONENT/$ISABELLE_JAVA_PLATFORM"  | 
81  | 
;;  | 
|
82  | 
macos)  | 
|
| 66906 | 83  | 
ISABELLE_JAVA_PLATFORM="$ISABELLE_PLATFORM64"  | 
84  | 
ISABELLE_JDK_HOME="$COMPONENT/$ISABELLE_JAVA_PLATFORM/Contents/Home"  | 
|
| 64929 | 85  | 
;;  | 
86  | 
esac  | 
|
87  | 
"""  | 
|
88  | 
||
89  | 
||
90  | 
/* extract archive */  | 
|
91  | 
||
| 
69186
 
573b7fbd96a8
updated to jdk-11+28 from https://adoptopenjdk.net -- with proper font rendering on Linux;
 
wenzelm 
parents: 
69128 
diff
changeset
 | 
92  | 
  private def suppress_name(name: String): Boolean = name.startsWith("._")
 | 
| 
 
573b7fbd96a8
updated to jdk-11+28 from https://adoptopenjdk.net -- with proper font rendering on Linux;
 
wenzelm 
parents: 
69128 
diff
changeset
 | 
93  | 
|
| 69128 | 94  | 
def extract_archive(dir: Path, archive: Path): (String, JDK_Platform) =  | 
| 64929 | 95  | 
  {
 | 
96  | 
    try {
 | 
|
97  | 
      val tmp_dir = dir + Path.explode("tmp")
 | 
|
98  | 
Isabelle_System.mkdirs(tmp_dir)  | 
|
| 69128 | 99  | 
|
100  | 
      if (archive.split_ext._2 == "zip") {
 | 
|
101  | 
Isabelle_System.bash(  | 
|
102  | 
"unzip -x " + File.bash_path(archive.absolute), cwd = tmp_dir.file).check  | 
|
103  | 
}  | 
|
104  | 
      else {
 | 
|
105  | 
Isabelle_System.gnutar(  | 
|
106  | 
"-C " + File.bash_path(tmp_dir) + " -xzf " + File.bash_path(archive)).check  | 
|
107  | 
}  | 
|
108  | 
||
| 64929 | 109  | 
val dir_entry =  | 
| 
69186
 
573b7fbd96a8
updated to jdk-11+28 from https://adoptopenjdk.net -- with proper font rendering on Linux;
 
wenzelm 
parents: 
69128 
diff
changeset
 | 
110  | 
        File.read_dir(tmp_dir).filterNot(suppress_name(_)) match {
 | 
| 64929 | 111  | 
case List(s) => s  | 
112  | 
          case _ => error("Archive contains multiple directories")
 | 
|
113  | 
}  | 
|
114  | 
val version = detect_version(dir_entry)  | 
|
115  | 
||
116  | 
val jdk_dir = tmp_dir + Path.explode(dir_entry)  | 
|
117  | 
val platform =  | 
|
118  | 
        jdk_platforms.find(_.detect(jdk_dir)) getOrElse error("Failed to detect JDK platform")
 | 
|
119  | 
||
120  | 
val platform_dir = dir + Path.explode(platform.name)  | 
|
121  | 
      if (platform_dir.is_dir) error("Directory already exists: " + platform_dir)
 | 
|
| 69128 | 122  | 
|
123  | 
      val jre_path = jdk_dir + Path.explode(platform.home) + Path.explode("jre")
 | 
|
124  | 
      Isabelle_System.bash("ln -s . " + File.bash_path(jre_path)).check
 | 
|
125  | 
||
| 64930 | 126  | 
File.move(jdk_dir, platform_dir)  | 
| 64929 | 127  | 
|
128  | 
(version, platform)  | 
|
129  | 
}  | 
|
130  | 
    catch { case ERROR(msg) => cat_error(msg, "The error(s) above occurred for " + archive) }
 | 
|
131  | 
}  | 
|
132  | 
||
133  | 
||
134  | 
/* build jdk */  | 
|
135  | 
||
136  | 
def build_jdk(  | 
|
137  | 
archives: List[Path],  | 
|
138  | 
progress: Progress = No_Progress,  | 
|
139  | 
target_dir: Path = Path.current)  | 
|
140  | 
  {
 | 
|
141  | 
    if (Platform.is_windows) error("Cannot build jdk on Windows")
 | 
|
142  | 
||
143  | 
    Isabelle_System.with_tmp_dir("jdk")(dir =>
 | 
|
144  | 
      {
 | 
|
145  | 
        progress.echo("Extracting ...")
 | 
|
146  | 
val extracted = archives.map(extract_archive(dir, _))  | 
|
147  | 
||
148  | 
val version =  | 
|
149  | 
          extracted.map(_._1).toSet.toList match {
 | 
|
150  | 
case List(version) => version  | 
|
151  | 
            case Nil => error("No archives")
 | 
|
152  | 
case versions =>  | 
|
| 69128 | 153  | 
              error("Archives contain multiple JDK versions: " + commas_quote(versions))
 | 
| 64929 | 154  | 
}  | 
155  | 
||
156  | 
val missing_platforms =  | 
|
157  | 
          jdk_platforms.filterNot(p1 => extracted.exists({ case (_, p2) => p1.name == p2.name }))
 | 
|
158  | 
if (missing_platforms.nonEmpty)  | 
|
159  | 
          error("Missing platforms: " + commas_quote(missing_platforms.map(_.name)))
 | 
|
160  | 
||
| 69128 | 161  | 
val jdk_name = "jdk-" + version  | 
| 64929 | 162  | 
val jdk_path = Path.explode(jdk_name)  | 
163  | 
val component_dir = dir + jdk_path  | 
|
164  | 
||
165  | 
        Isabelle_System.mkdirs(component_dir + Path.explode("etc"))
 | 
|
166  | 
        File.write(component_dir + Path.explode("etc/settings"), settings)
 | 
|
167  | 
        File.write(component_dir + Path.explode("README"), readme(version))
 | 
|
168  | 
||
| 64930 | 169  | 
for ((_, platform) <- extracted)  | 
170  | 
File.move(dir + Path.explode(platform.name), component_dir)  | 
|
| 64929 | 171  | 
|
| 64933 | 172  | 
        for (file <- File.find_files(component_dir.file, include_dirs = true)) {
 | 
173  | 
val path = file.toPath  | 
|
174  | 
val perms = Files.getPosixFilePermissions(path)  | 
|
| 64934 | 175  | 
perms.add(PosixFilePermission.OWNER_READ)  | 
176  | 
perms.add(PosixFilePermission.GROUP_READ)  | 
|
177  | 
perms.add(PosixFilePermission.OTHERS_READ)  | 
|
178  | 
perms.add(PosixFilePermission.OWNER_WRITE)  | 
|
| 64933 | 179  | 
          if (file.isDirectory) {
 | 
| 64934 | 180  | 
perms.add(PosixFilePermission.OWNER_WRITE)  | 
181  | 
perms.add(PosixFilePermission.OWNER_EXECUTE)  | 
|
182  | 
perms.add(PosixFilePermission.GROUP_EXECUTE)  | 
|
183  | 
perms.add(PosixFilePermission.OTHERS_EXECUTE)  | 
|
| 64933 | 184  | 
}  | 
185  | 
Files.setPosixFilePermissions(path, perms)  | 
|
186  | 
}  | 
|
| 64931 | 187  | 
|
188  | 
        File.find_files((component_dir + Path.explode("x86_64-darwin")).file,
 | 
|
| 
69186
 
573b7fbd96a8
updated to jdk-11+28 from https://adoptopenjdk.net -- with proper font rendering on Linux;
 
wenzelm 
parents: 
69128 
diff
changeset
 | 
189  | 
file => suppress_name(file.getName)).foreach(_.delete)  | 
| 64929 | 190  | 
|
191  | 
        progress.echo("Sharing ...")
 | 
|
| 64934 | 192  | 
val main_dir :: other_dirs =  | 
193  | 
jdk_platforms.map(platform => (component_dir + Path.explode(platform.name)).file.toPath)  | 
|
194  | 
        for {
 | 
|
195  | 
file1 <- File.find_files(main_dir.toFile).iterator  | 
|
196  | 
path1 = file1.toPath  | 
|
197  | 
dir2 <- other_dirs.iterator  | 
|
198  | 
        } {
 | 
|
199  | 
val path2 = dir2.resolve(main_dir.relativize(path1))  | 
|
200  | 
val file2 = path2.toFile  | 
|
| 
65879
 
a43a079156a6
avoid mixture of symlinks and hardlinks, which causes problems with BSD tar on macOS Sierra;
 
wenzelm 
parents: 
65873 
diff
changeset
 | 
201  | 
          if (!Files.isSymbolicLink(path2) && file2.isFile && File.eq_content(file1, file2)) {
 | 
| 64934 | 202  | 
file2.delete  | 
203  | 
Files.createLink(path2, path1)  | 
|
204  | 
}  | 
|
205  | 
}  | 
|
| 64929 | 206  | 
|
207  | 
        progress.echo("Archiving ...")
 | 
|
| 64936 | 208  | 
        Isabelle_System.gnutar("--owner=root --group=root -C " + File.bash_path(dir) +
 | 
209  | 
          " -czf " + File.bash_path(target_dir + jdk_path.ext("tar.gz")) + " " + jdk_name).check
 | 
|
| 64929 | 210  | 
})  | 
211  | 
}  | 
|
212  | 
||
213  | 
||
214  | 
/* Isabelle tool wrapper */  | 
|
215  | 
||
216  | 
val isabelle_tool =  | 
|
| 69128 | 217  | 
    Isabelle_Tool("build_jdk", "build Isabelle jdk component from original archives",
 | 
| 64929 | 218  | 
args =>  | 
219  | 
    {
 | 
|
220  | 
var target_dir = Path.current  | 
|
221  | 
||
222  | 
      val getopts = Getopts("""
 | 
|
| 65873 | 223  | 
Usage: isabelle build_jdk [OPTIONS] ARCHIVES...  | 
| 64929 | 224  | 
|
225  | 
Options are:  | 
|
226  | 
-D DIR target directory (default ".")  | 
|
227  | 
||
| 69128 | 228  | 
Build jdk component from tar.gz archives, with original jdk archives  | 
| 66906 | 229  | 
for x86_64 Linux, Windows, Mac OS X.  | 
| 64929 | 230  | 
""",  | 
231  | 
"D:" -> (arg => target_dir = Path.explode(arg)))  | 
|
232  | 
||
233  | 
val more_args = getopts(args)  | 
|
234  | 
if (more_args.isEmpty) getopts.usage()  | 
|
235  | 
||
236  | 
val archives = more_args.map(Path.explode(_))  | 
|
237  | 
val progress = new Console_Progress()  | 
|
238  | 
||
239  | 
build_jdk(archives = archives, progress = progress, target_dir = target_dir)  | 
|
| 
69277
 
258bef08b31e
support for user-defined Isabelle/Scala command-line tools;
 
wenzelm 
parents: 
69186 
diff
changeset
 | 
240  | 
})  | 
| 64929 | 241  | 
}  |