author | wenzelm |
Fri, 02 Sep 2022 23:19:02 +0200 | |
changeset 76041 | 4a1330addb4e |
parent 75394 | 42267c650205 |
child 76518 | b30b8e23383c |
permissions | -rw-r--r-- |
64483 | 1 |
/* Title: Pure/Admin/build_polyml.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Build Poly/ML from sources. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
69704 | 10 |
import scala.util.matching.Regex |
11 |
||
12 |
||
75393 | 13 |
object Build_PolyML { |
65880 | 14 |
/** platform-specific build **/ |
64496 | 15 |
|
64483 | 16 |
sealed case class Platform_Info( |
17 |
options: List[String] = Nil, |
|
64487 | 18 |
setup: String = "", |
72462
7c552a256ca5
misc tuning and clarification: prefer Executable.libraries_closure;
wenzelm
parents:
72455
diff
changeset
|
19 |
libs: Set[String] = Set.empty) |
64483 | 20 |
|
21 |
private val platform_info = Map( |
|
69704 | 22 |
"linux" -> |
67580
eb64467e8bcf
more robust access to shared libraries for poly executable: avoid global change of LD_LIBRARY_PATH (e.g. relevant for subprocesses);
wenzelm
parents:
66998
diff
changeset
|
23 |
Platform_Info( |
69704 | 24 |
options = List("LDFLAGS=-Wl,-rpath,_DUMMY_"), |
72462
7c552a256ca5
misc tuning and clarification: prefer Executable.libraries_closure;
wenzelm
parents:
72455
diff
changeset
|
25 |
libs = Set("libgmp")), |
69704 | 26 |
"darwin" -> |
64483 | 27 |
Platform_Info( |
73666
4d0df84a5b88
clarified options: implicitly support both x86_64 and arm64;
wenzelm
parents:
73340
diff
changeset
|
28 |
options = List("CFLAGS=-O3", "CXXFLAGS=-O3", "LDFLAGS=-segprot POLY rwx rwx"), |
69704 | 29 |
setup = "PATH=/usr/bin:/bin:/usr/sbin:/sbin", |
72462
7c552a256ca5
misc tuning and clarification: prefer Executable.libraries_closure;
wenzelm
parents:
72455
diff
changeset
|
30 |
libs = Set("libpolyml", "libgmp")), |
69704 | 31 |
"windows" -> |
64483 | 32 |
Platform_Info( |
33 |
options = |
|
67597 | 34 |
List("--host=x86_64-w64-mingw32", "CPPFLAGS=-I/mingw64/include", "--disable-windows-gui"), |
72427 | 35 |
setup = MinGW.environment_export, |
72468 | 36 |
libs = Set("libgcc_s_seh", "libgmp", "libstdc++", "libwinpthread"))) |
64483 | 37 |
|
38 |
def build_polyml( |
|
64489 | 39 |
root: Path, |
64495 | 40 |
sha1_root: Option[Path] = None, |
71726
a5fda30edae2
clarified signature: more uniform treatment of stopped/interrupted state;
wenzelm
parents:
71686
diff
changeset
|
41 |
progress: Progress = new Progress, |
64493 | 42 |
arch_64: Boolean = false, |
64485
e996c0a5eca9
support other bash executable (notably for msys on Windows);
wenzelm
parents:
64484
diff
changeset
|
43 |
options: List[String] = Nil, |
75393 | 44 |
mingw: MinGW = MinGW.none |
45 |
): Unit = { |
|
64489 | 46 |
if (!((root + Path.explode("configure")).is_file && (root + Path.explode("PolyML")).is_dir)) |
47 |
error("Bad Poly/ML root directory: " + root) |
|
64483 | 48 |
|
72352
f4bd6f123fdf
more systematic platform support, including arm64-linux;
wenzelm
parents:
72351
diff
changeset
|
49 |
val platform = Isabelle_Platform.self |
69704 | 50 |
|
73667 | 51 |
val polyml_platform = |
52 |
(if (arch_64) platform.arch_64 else platform.arch_64_32) + "-" + platform.os_name |
|
72352
f4bd6f123fdf
more systematic platform support, including arm64-linux;
wenzelm
parents:
72351
diff
changeset
|
53 |
val sha1_platform = platform.arch_64 + "-" + platform.os_name |
64493 | 54 |
|
64483 | 55 |
val info = |
72352
f4bd6f123fdf
more systematic platform support, including arm64-linux;
wenzelm
parents:
72351
diff
changeset
|
56 |
platform_info.getOrElse(platform.os_name, |
f4bd6f123fdf
more systematic platform support, including arm64-linux;
wenzelm
parents:
72351
diff
changeset
|
57 |
error("Bad OS platform: " + quote(platform.os_name))) |
64483 | 58 |
|
72455 | 59 |
if (platform.is_linux) Isabelle_System.require_command("chrpath") |
70977 | 60 |
|
64491 | 61 |
|
64495 | 62 |
/* bash */ |
63 |
||
64504 | 64 |
def bash( |
73672
70d3c7009a65
proper support for macOS/Rosetta: let "uname -m" report arm64 instead of x86_64;
wenzelm
parents:
73667
diff
changeset
|
65 |
cwd: Path, script: String, |
70d3c7009a65
proper support for macOS/Rosetta: let "uname -m" report arm64 instead of x86_64;
wenzelm
parents:
73667
diff
changeset
|
66 |
redirect: Boolean = false, |
75393 | 67 |
echo: Boolean = false |
68 |
): Process_Result = { |
|
73672
70d3c7009a65
proper support for macOS/Rosetta: let "uname -m" report arm64 instead of x86_64;
wenzelm
parents:
73667
diff
changeset
|
69 |
val script1 = |
70d3c7009a65
proper support for macOS/Rosetta: let "uname -m" report arm64 instead of x86_64;
wenzelm
parents:
73667
diff
changeset
|
70 |
if (platform.is_arm && platform.is_macos) { |
70d3c7009a65
proper support for macOS/Rosetta: let "uname -m" report arm64 instead of x86_64;
wenzelm
parents:
73667
diff
changeset
|
71 |
"arch -arch arm64 bash -c " + Bash.string(script) |
70d3c7009a65
proper support for macOS/Rosetta: let "uname -m" report arm64 instead of x86_64;
wenzelm
parents:
73667
diff
changeset
|
72 |
} |
70d3c7009a65
proper support for macOS/Rosetta: let "uname -m" report arm64 instead of x86_64;
wenzelm
parents:
73667
diff
changeset
|
73 |
else mingw.bash_script(script) |
70d3c7009a65
proper support for macOS/Rosetta: let "uname -m" report arm64 instead of x86_64;
wenzelm
parents:
73667
diff
changeset
|
74 |
progress.bash(script1, cwd = cwd.file, redirect = redirect, echo = echo) |
64504 | 75 |
} |
64495 | 76 |
|
77 |
||
64491 | 78 |
/* configure and make */ |
79 |
||
64483 | 80 |
val configure_options = |
67783
839de121665c
more robust build: prevent problems seen with Poly/ML eb94e2820013 on Mac OS X;
wenzelm
parents:
67609
diff
changeset
|
81 |
List("--disable-shared", "--enable-intinf-as-int", "--with-gmp") ::: |
69704 | 82 |
info.options ::: options ::: (if (arch_64) Nil else List("--enable-compact32bit")) |
64483 | 83 |
|
64495 | 84 |
bash(root, |
64487 | 85 |
info.setup + "\n" + |
64485
e996c0a5eca9
support other bash executable (notably for msys on Windows);
wenzelm
parents:
64484
diff
changeset
|
86 |
""" |
e996c0a5eca9
support other bash executable (notably for msys on Windows);
wenzelm
parents:
64484
diff
changeset
|
87 |
[ -f Makefile ] && make distclean |
e996c0a5eca9
support other bash executable (notably for msys on Windows);
wenzelm
parents:
64484
diff
changeset
|
88 |
{ |
e996c0a5eca9
support other bash executable (notably for msys on Windows);
wenzelm
parents:
64484
diff
changeset
|
89 |
./configure --prefix="$PWD/target" """ + Bash.strings(configure_options) + """ |
e996c0a5eca9
support other bash executable (notably for msys on Windows);
wenzelm
parents:
64484
diff
changeset
|
90 |
rm -rf target |
74635 | 91 |
make && make install |
64485
e996c0a5eca9
support other bash executable (notably for msys on Windows);
wenzelm
parents:
64484
diff
changeset
|
92 |
} || { echo "Build failed" >&2; exit 2; } |
64501 | 93 |
""", redirect = true, echo = true).check |
64485
e996c0a5eca9
support other bash executable (notably for msys on Windows);
wenzelm
parents:
64484
diff
changeset
|
94 |
|
64491 | 95 |
|
64495 | 96 |
/* sha1 library */ |
97 |
||
98 |
val sha1_files = |
|
99 |
if (sha1_root.isDefined) { |
|
100 |
val dir1 = sha1_root.get |
|
72352
f4bd6f123fdf
more systematic platform support, including arm64-linux;
wenzelm
parents:
72351
diff
changeset
|
101 |
bash(dir1, "./build " + sha1_platform, redirect = true, echo = true).check |
64505 | 102 |
|
72352
f4bd6f123fdf
more systematic platform support, including arm64-linux;
wenzelm
parents:
72351
diff
changeset
|
103 |
val dir2 = dir1 + Path.explode(sha1_platform) |
72462
7c552a256ca5
misc tuning and clarification: prefer Executable.libraries_closure;
wenzelm
parents:
72455
diff
changeset
|
104 |
File.read_dir(dir2).map(entry => dir2 + Path.basic(entry)) |
64495 | 105 |
} |
106 |
else Nil |
|
107 |
||
108 |
||
72462
7c552a256ca5
misc tuning and clarification: prefer Executable.libraries_closure;
wenzelm
parents:
72455
diff
changeset
|
109 |
/* install */ |
64484 | 110 |
|
72462
7c552a256ca5
misc tuning and clarification: prefer Executable.libraries_closure;
wenzelm
parents:
72455
diff
changeset
|
111 |
val platform_dir = Path.explode(polyml_platform) |
7c552a256ca5
misc tuning and clarification: prefer Executable.libraries_closure;
wenzelm
parents:
72455
diff
changeset
|
112 |
Isabelle_System.rm_tree(platform_dir) |
7c552a256ca5
misc tuning and clarification: prefer Executable.libraries_closure;
wenzelm
parents:
72455
diff
changeset
|
113 |
Isabelle_System.make_directory(platform_dir) |
64483 | 114 |
|
73317 | 115 |
for (file <- sha1_files) Isabelle_System.copy_file(file, platform_dir) |
67592
66253039d5ca
enforce shared libpoly on all platforms, with File.copy before File.move;
wenzelm
parents:
67589
diff
changeset
|
116 |
|
64483 | 117 |
for { |
64484 | 118 |
d <- List("target/bin", "target/lib") |
64489 | 119 |
dir = root + Path.explode(d) |
64483 | 120 |
entry <- File.read_dir(dir) |
73317 | 121 |
} Isabelle_System.move_file(dir + Path.explode(entry), platform_dir) |
67584 | 122 |
|
72469 | 123 |
Executable.libraries_closure( |
124 |
platform_dir + Path.basic("poly").platform_exe, mingw = mingw, filter = info.libs) |
|
125 |
||
67584 | 126 |
|
127 |
/* polyc: directory prefix */ |
|
128 |
||
72378 | 129 |
val Header = "#! */bin/sh".r |
75205 | 130 |
File.change_lines(platform_dir + Path.explode("polyc")) { |
131 |
case Header() :: lines => |
|
132 |
val lines1 = |
|
133 |
lines.map(line => |
|
134 |
if (line.startsWith("prefix=")) "prefix=\"$(cd \"$(dirname \"$0\")\"; pwd)\"" |
|
135 |
else if (line.startsWith("BINDIR=")) "BINDIR=\"$prefix\"" |
|
136 |
else if (line.startsWith("LIBDIR=")) "LIBDIR=\"$prefix\"" |
|
137 |
else line) |
|
138 |
"#!/usr/bin/env bash" :: lines1 |
|
139 |
case lines => |
|
140 |
error(cat_lines("Cannot patch polyc -- undetected header:" :: lines.take(3))) |
|
75202 | 141 |
} |
64483 | 142 |
} |
143 |
||
144 |
||
64496 | 145 |
|
65880 | 146 |
/** skeleton for component **/ |
147 |
||
75393 | 148 |
private def extract_sources(source_archive: Path, component_dir: Path): Unit = { |
71396
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
149 |
if (source_archive.get_ext == "zip") { |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
150 |
Isabelle_System.bash( |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
151 |
"unzip -x " + File.bash_path(source_archive.absolute), cwd = component_dir.file).check |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
152 |
} |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
153 |
else { |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
154 |
Isabelle_System.gnutar("-xzf " + File.bash_path(source_archive), dir = component_dir).check |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
155 |
} |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
156 |
|
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
157 |
val src_dir = component_dir + Path.explode("src") |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
158 |
File.read_dir(component_dir) match { |
73317 | 159 |
case List(s) => Isabelle_System.move_file(component_dir + Path.basic(s), src_dir) |
71396
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
160 |
case _ => error("Source archive contains multiple directories") |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
161 |
} |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
162 |
|
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
163 |
val lines = split_lines(File.read(src_dir + Path.explode("RootX86.ML"))) |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
164 |
val ml_files = |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
165 |
for { |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
166 |
line <- lines |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
167 |
rest <- Library.try_unprefix("use", line) |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
168 |
} yield "ML_file" + rest |
65880 | 169 |
|
71396
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
170 |
File.write(src_dir + Path.explode("ROOT.ML"), |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
171 |
"""(* Poly/ML Compiler root file. |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
172 |
|
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
173 |
When this file is open in the Prover IDE, the ML files of the Poly/ML |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
174 |
compiler can be explored interactively. This is a separate copy: it does |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
175 |
not affect the running ML session. *) |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
176 |
""" + ml_files.mkString("\n", "\n", "\n")) |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
177 |
} |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
178 |
|
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
179 |
def build_polyml_component( |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
180 |
source_archive: Path, |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
181 |
component_dir: Path, |
75393 | 182 |
sha1_root: Option[Path] = None |
183 |
): Unit = { |
|
72377 | 184 |
Isabelle_System.new_directory(component_dir) |
71396
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
185 |
extract_sources(source_archive, component_dir) |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
186 |
|
73317 | 187 |
Isabelle_System.copy_file(Path.explode("~~/Admin/polyml/README"), component_dir) |
71396
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
188 |
|
72376 | 189 |
val etc_dir = Isabelle_System.make_directory(component_dir + Path.explode("etc")) |
73317 | 190 |
Isabelle_System.copy_file(Path.explode("~~/Admin/polyml/settings"), etc_dir) |
64483 | 191 |
|
65880 | 192 |
sha1_root match { |
193 |
case Some(dir) => |
|
71396
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
194 |
Mercurial.repository(dir).archive(File.standard_path(component_dir + Path.explode("sha1"))) |
65880 | 195 |
case None => |
196 |
} |
|
197 |
} |
|
198 |
||
199 |
||
200 |
||
201 |
/** Isabelle tool wrappers **/ |
|
202 |
||
203 |
val isabelle_tool1 = |
|
75394 | 204 |
Isabelle_Tool("build_polyml", "build Poly/ML from sources", Scala_Project.here, |
205 |
{ args => |
|
206 |
var mingw = MinGW.none |
|
207 |
var arch_64 = false |
|
208 |
var sha1_root: Option[Path] = None |
|
64483 | 209 |
|
75394 | 210 |
val getopts = Getopts(""" |
64489 | 211 |
Usage: isabelle build_polyml [OPTIONS] ROOT [CONFIGURE_OPTIONS] |
64483 | 212 |
|
213 |
Options are: |
|
72429 | 214 |
-M DIR msys/mingw root specification for Windows |
72352
f4bd6f123fdf
more systematic platform support, including arm64-linux;
wenzelm
parents:
72351
diff
changeset
|
215 |
-m ARCH processor architecture (32 or 64, default: """ + |
f4bd6f123fdf
more systematic platform support, including arm64-linux;
wenzelm
parents:
72351
diff
changeset
|
216 |
(if (arch_64) "64" else "32") + """) |
69691 | 217 |
-s DIR sha1 sources, see https://isabelle.sketis.net/repos/sha1 |
64483 | 218 |
|
64499 | 219 |
Build Poly/ML in the ROOT directory of its sources, with additional |
67593 | 220 |
CONFIGURE_OPTIONS (e.g. --without-gmp). |
64483 | 221 |
""", |
75394 | 222 |
"M:" -> (arg => mingw = MinGW(Path.explode(arg))), |
223 |
"m:" -> |
|
224 |
{ |
|
225 |
case "32" => arch_64 = false |
|
226 |
case "64" => arch_64 = true |
|
227 |
case bad => error("Bad processor architecture: " + quote(bad)) |
|
228 |
}, |
|
229 |
"s:" -> (arg => sha1_root = Some(Path.explode(arg)))) |
|
64483 | 230 |
|
75394 | 231 |
val more_args = getopts(args) |
232 |
val (root, options) = |
|
233 |
more_args match { |
|
234 |
case root :: options => (Path.explode(root), options) |
|
235 |
case Nil => getopts.usage() |
|
236 |
} |
|
237 |
build_polyml(root, sha1_root = sha1_root, progress = new Console_Progress, |
|
238 |
arch_64 = arch_64, options = options, mingw = mingw) |
|
239 |
}) |
|
65880 | 240 |
|
241 |
val isabelle_tool2 = |
|
72763 | 242 |
Isabelle_Tool("build_polyml_component", "make skeleton for Poly/ML component", |
75394 | 243 |
Scala_Project.here, |
244 |
{ args => |
|
245 |
var sha1_root: Option[Path] = None |
|
246 |
||
247 |
val getopts = Getopts(""" |
|
71396
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
248 |
Usage: isabelle build_polyml_component [OPTIONS] SOURCE_ARCHIVE COMPONENT_DIR |
65880 | 249 |
|
250 |
Options are: |
|
69691 | 251 |
-s DIR sha1 sources, see https://isabelle.sketis.net/repos/sha1 |
65880 | 252 |
|
71396
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
253 |
Make skeleton for Poly/ML component, based on official source archive |
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
254 |
(zip or tar.gz). |
65880 | 255 |
""", |
75394 | 256 |
"s:" -> (arg => sha1_root = Some(Path.explode(arg)))) |
65880 | 257 |
|
75394 | 258 |
val more_args = getopts(args) |
71396
c1c61d0d8e7c
clarified build_polyml_component: include IDE entry point for ML compiler;
wenzelm
parents:
71117
diff
changeset
|
259 |
|
75394 | 260 |
val (source_archive, component_dir) = |
261 |
more_args match { |
|
262 |
case List(archive, dir) => (Path.explode(archive), Path.explode(dir)) |
|
263 |
case _ => getopts.usage() |
|
264 |
} |
|
265 |
build_polyml_component(source_archive, component_dir, sha1_root = sha1_root) |
|
266 |
}) |
|
64483 | 267 |
} |