src/Pure/Admin/build_sqlite.scala
changeset 72346 93e533198bf6
child 72362 5f17bf3709b8
equal deleted inserted replaced
72345:14be5c341377 72346:93e533198bf6
       
     1 /*  Title:      Pure/Admin/build_sqlite.scala
       
     2     Author:     Makarius
       
     3 
       
     4 Build Isabelle sqlite-jdbc component from official download.
       
     5 */
       
     6 
       
     7 package isabelle
       
     8 
       
     9 
       
    10 object Build_SQLite
       
    11 {
       
    12   /* build sqlite */
       
    13 
       
    14   def build_sqlite(
       
    15     download_url: String,
       
    16     progress: Progress = new Progress,
       
    17     target_dir: Path = Path.current)
       
    18   {
       
    19     val Download_Name = """^.*/([^/]+)\.jar""".r
       
    20     val download_name =
       
    21       download_url match {
       
    22         case Download_Name(download_name) => download_name
       
    23         case _ => error("Malformed jar download URL: " + quote(download_url))
       
    24       }
       
    25 
       
    26 
       
    27     /* component */
       
    28 
       
    29     val component_dir = target_dir + Path.basic(download_name)
       
    30     if (component_dir.is_dir) error("Component directory already exists: " + component_dir)
       
    31     else {
       
    32       progress.echo("Component " + component_dir)
       
    33       Isabelle_System.mkdirs(component_dir)
       
    34     }
       
    35 
       
    36 
       
    37     /* README */
       
    38 
       
    39     File.write(component_dir + Path.basic("README"),
       
    40       "This is " + download_name + " from\n" + download_url +
       
    41         "\n\n    Makarius\n    " + Date.Format.date(Date.now()) + "\n")
       
    42 
       
    43 
       
    44     /* settings */
       
    45 
       
    46     val etc_dir = component_dir + Path.basic("etc")
       
    47     Isabelle_System.mkdirs(etc_dir)
       
    48 
       
    49     File.write(etc_dir + Path.basic("settings"),
       
    50 """# -*- shell-script -*- :mode=shellscript:
       
    51 
       
    52 ISABELLE_SQLITE_HOME="$COMPONENT"
       
    53 
       
    54 classpath "$ISABELLE_SQLITE_HOME/""" + download_name + """.jar"
       
    55 """)
       
    56 
       
    57 
       
    58     /* jar */
       
    59 
       
    60     val jar = component_dir + Path.basic(download_name).ext("jar")
       
    61     progress.echo("Getting " + quote(download_url))
       
    62     try {
       
    63       Isabelle_System.bash("curl --fail --silent --location " + Bash.string(download_url) +
       
    64         " > " + File.bash_path(jar)).check
       
    65     }
       
    66     catch {
       
    67       case ERROR(msg) => cat_error("Failed to download " + quote(download_url), msg)
       
    68     }
       
    69 
       
    70     Isabelle_System.with_tmp_dir("sqlite")(jar_dir =>
       
    71     {
       
    72       progress.echo("Unpacking " + jar)
       
    73       Isabelle_System.bash("isabelle_jdk jar xf " + File.bash_path(jar.absolute),
       
    74         cwd = jar_dir.file).check
       
    75 
       
    76       val jar_files =
       
    77         List(
       
    78           "META-INF/maven/org.xerial/sqlite-jdbc/LICENSE" -> ".",
       
    79           "META-INF/maven/org.xerial/sqlite-jdbc/LICENSE.zentus" -> ".",
       
    80           "org/sqlite/native/Linux/aarch64/libsqlitejdbc.so" -> "arm64-linux",
       
    81           "org/sqlite/native/Linux/x86_64/libsqlitejdbc.so" -> "x86_64-linux",
       
    82           "org/sqlite/native/Mac/x86_64/libsqlitejdbc.jnilib" -> "x86_64-darwin",
       
    83           "org/sqlite/native/Windows/x86_64/sqlitejdbc.dll" -> "x86_64-windows")
       
    84 
       
    85       for ((file, dir) <- jar_files) {
       
    86         val target = component_dir + Path.explode(dir)
       
    87         Isabelle_System.mkdirs(target)
       
    88         File.copy(jar_dir + Path.explode(file), target)
       
    89       }
       
    90 
       
    91       File.set_executable(component_dir + Path.explode("x86_64-windows/sqlitejdbc.dll"), true)
       
    92     })
       
    93   }
       
    94 
       
    95 
       
    96   /* Isabelle tool wrapper */
       
    97 
       
    98   val isabelle_tool =
       
    99     Isabelle_Tool("build_sqlite", "build Isabelle sqlite-jdbc component from official download",
       
   100     args =>
       
   101     {
       
   102       var target_dir = Path.current
       
   103 
       
   104       val getopts = Getopts("""
       
   105 Usage: isabelle build_sqlite [OPTIONS] DOWNLOAD
       
   106 
       
   107   Options are:
       
   108     -D DIR       target directory (default ".")
       
   109 
       
   110   Build sqlite-jdbc component from the specified download URL (JAR), see also
       
   111   https://github.com/xerial/sqlite-jdbc/releases
       
   112 """,
       
   113         "D:" -> (arg => target_dir = Path.explode(arg)))
       
   114 
       
   115       val more_args = getopts(args)
       
   116       val download_url =
       
   117         more_args match {
       
   118           case List(download_url) => download_url
       
   119           case _ => getopts.usage()
       
   120         }
       
   121 
       
   122       val progress = new Console_Progress()
       
   123 
       
   124       build_sqlite(download_url, progress = progress, target_dir = target_dir)
       
   125     })
       
   126 }