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