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