diff --git a/Admin/lib/Tools/build_setup b/Admin/lib/Tools/build_setup --- a/Admin/lib/Tools/build_setup +++ b/Admin/lib/Tools/build_setup @@ -1,84 +1,84 @@ #!/usr/bin/env bash # # Author: Makarius # # DESCRIPTION: build component for Isabelle/Java setup tool ## sources declare -a SOURCES=( - "Build_Scala.java" + "Build.java" "Environment.java" "Setup.java" ) ## usage PRG=$(basename "$0") function usage() { echo echo "Usage: isabelle $PRG [OPTIONS] COMPONENT_DIR" echo echo " Build component for Isabelle/Java setup tool." echo exit 1 } function fail() { echo "$1" >&2 exit 2 } ## process command line [ "$#" -ge 1 ] && { COMPONENT_DIR="$1"; shift; } [ "$#" -ne 0 -o -z "$COMPONENT_DIR" ] && usage ## main [ -d "$COMPONENT_DIR" ] && fail "Directory already exists: \"$COMPONENT_DIR\"" # build jar TARGET_DIR="$COMPONENT_DIR/lib" mkdir -p "$TARGET_DIR/isabelle/setup" declare -a ARGS=("-Xlint:unchecked") for SRC in "${SOURCES[@]}" do ARGS["${#ARGS[@]}"]="$(platform_path "$ISABELLE_HOME/src/Tools/Setup/isabelle/setup/$SRC")" done isabelle_jdk javac -d "$TARGET_DIR" -classpath "$(platform_path "$ISABELLE_CLASSPATH")" "${ARGS[@]}" || \ fail "Failed to compile sources" isabelle_jdk jar -c -f "$(platform_path "$TARGET_DIR/isabelle_setup.jar")" \ -e "isabelle.setup.Setup" -C "$TARGET_DIR" isabelle || fail "Failed to produce jar" rm -rf "$TARGET_DIR/isabelle" # etc/settings mkdir -p "$COMPONENT_DIR/etc" cat > "$COMPONENT_DIR/etc/settings" < "$COMPONENT_DIR/README" < get_list(String name) { List list = new LinkedList(); for (String s : props.getProperty(name, "").split("\\s+")) { if (!s.isEmpty()) { list.add(s); } } return List.copyOf(list); } public List sources() { return get_list("sources"); } public List resources() { return get_list("resources"); } public List services() { return get_list("services"); } public Path path(String file) { return component_dir.resolve(file); } public boolean exists(String file) { return Files.exists(path(file)); } public String item_name(String s) { int i = s.indexOf(':'); return i > 0 ? s.substring(0, i) : s; } public String item_target(String s) { int i = s.indexOf(':'); return i > 0 ? s.substring(i + 1) : s; } public String shasum(String file) throws IOException, NoSuchAlgorithmException { if (exists(file)) { MessageDigest sha = MessageDigest.getInstance("SHA"); sha.update(Files.readAllBytes(path(file))); String digest = String.format(Locale.ROOT, "%040x", new BigInteger(1, sha.digest())); return digest + " *" + file + "\n"; } else { return ""; } } } /** compile sources **/ public static void compile_sources( Path target_dir, List deps, String options, List sources) { ArrayList args = new ArrayList(); args.add("-d"); args.add(target_dir.toString()); args.add("-bootclasspath"); args.add(Environment.join_paths(deps)); for (String s : options.split("\\s+")) { if (!s.isEmpty()) { args.add(s); } } args.add("--"); for (Path p : sources) { args.add(p.toString()); } MainClass main = new MainClass(); boolean ok = main.process(args.toArray(String[]::new)); if (!ok) throw new RuntimeException("Failed to compile sources"); } /** create jar **/ public static void create_jar(Path dir, String main, Path jar) throws IOException { Files.deleteIfExists(jar); Manifest manifest = new Manifest(); Attributes attributes = manifest.getMainAttributes(); attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0"); attributes.put(new Attributes.Name("Created-By"), System.getProperty("java.version") + " (" + System.getProperty("java.vendor") + ")"); if (!main.isEmpty()) { attributes.put(Attributes.Name.MAIN_CLASS, main); } try (JarOutputStream out = new JarOutputStream(new BufferedOutputStream(Files.newOutputStream(jar)), manifest)) { for (Path path : Files.walk(dir).sorted().toArray(Path[]::new)) { boolean is_dir = Files.isDirectory(path); boolean is_file = Files.isRegularFile(path); if (is_dir || is_file) { String name = Environment.slashes(dir.relativize(path).toString()); JarEntry entry = new JarEntry(is_dir ? name + "/" : name); entry.setTime(path.toFile().lastModified()); out.putNextEntry(entry); if (is_file) { out.write(Files.readAllBytes(path)); } out.closeEntry(); } } } } /** build scala **/ public static void build_scala(Context context, boolean fresh) throws IOException, InterruptedException, NoSuchAlgorithmException { String jar_name = context.jar_name(); Path jar_path = context.path(jar_name); String shasum_name = context.shasum_name(); List sources = context.sources(); List resources = context.resources(); if (sources.isEmpty()) { Files.deleteIfExists(jar_path); Files.deleteIfExists(context.path(shasum_name)); } else { String shasum_old = context.exists(shasum_name) ? Files.readString(context.path(shasum_name)) : ""; String shasum_sources; { StringBuilder _shasum = new StringBuilder(); for (String s : resources) { _shasum.append(context.shasum(context.item_name(s))); } for (String s : sources) { _shasum.append(context.shasum(s)); } shasum_sources = _shasum.toString(); } if (fresh || !shasum_old.equals(context.shasum(jar_name) + shasum_sources)) { System.out.println( "### Building " + context.description() + " (" + jar_path + ") ..."); String scalac_options = Environment.getenv("ISABELLE_SCALAC_OPTIONS"); String isabelle_class_path = Environment.getenv("ISABELLE_CLASSPATH"); Path build_dir = Files.createTempDirectory("isabelle"); try { /* compile sources */ List compiler_deps = new LinkedList(); for (String s : isabelle_class_path.split(":", -1)) { if (!s.isEmpty()) { compiler_deps.add(Path.of(Environment.platform_path(s))); } } List compiler_sources = new LinkedList(); for (String s : sources) { compiler_sources.add(context.path(s)); } compile_sources(build_dir, compiler_deps, scalac_options, compiler_sources); /* copy resources */ for (String s : context.resources()) { String name = context.item_name(s); String target = context.item_target(s); Path file_name = Path.of(name).normalize().getFileName(); Path target_path = Path.of(target).normalize(); Path target_dir; Path target_file; { if (target.endsWith("/") || target.endsWith("/.")) { target_dir = build_dir.resolve(target_path); target_file = target_dir.resolve(file_name); } else { target_file = build_dir.resolve(target_path); target_dir = target_file.getParent(); } } Files.createDirectories(target_dir); Files.copy(context.path(name), target_file, StandardCopyOption.COPY_ATTRIBUTES); } /* packaging */ create_jar(build_dir, context.main(), jar_path); String shasum = context.shasum(jar_name) + shasum_sources; Files.writeString(context.path(shasum_name), shasum); } finally { try (Stream walk = Files.walk(build_dir)) { walk.sorted(Comparator.reverseOrder()) .map(Path::toFile) .forEach(File::delete); } } } } } }