1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
package cbt;
import java.io.*;
import java.lang.reflect.*;
import java.net.*;
import java.nio.*;
import java.nio.file.*;
import static java.io.File.pathSeparator;
import java.security.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
/**
* This launcher allows to start the JVM without loading anything else permanently into its
* classpath except for the launcher itself. That's why it is written in Java without
* dependencies outside the JDK.
*/
public class NailgunLauncher{
public static String SCALA_VERSION = "2.11.8";
public static String SCALA_XML_VERSION = "1.0.5";
public static String ZINC_VERSION = "0.3.9";
public static String CBT_HOME = System.getenv("CBT_HOME");
public static String NAILGUN = System.getenv("NAILGUN");
public static String TARGET = System.getenv("TARGET");
public static String STAGE1 = CBT_HOME + "/stage1/";
public static String MAVEN_CACHE = CBT_HOME + "/cache/maven";
public static String MAVEN_URL = "https://repo1.maven.org/maven2";
/**
* Persistent cache for caching classloaders for the JVM life time. Can be used as needed by user
* code to improve startup time.
*/
public static ConcurrentHashMap<String, Object> classLoaderCacheKeys = new ConcurrentHashMap<String,Object>();
public static ConcurrentHashMap<Object, ClassLoader> classLoaderCacheValues = new ConcurrentHashMap<Object,ClassLoader>();
public static SecurityManager defaultSecurityManager = System.getSecurityManager();
public static long lastSuccessfullCompile = 0;
static ClassLoader stage1classLoader = null;
public static void main(String[] args) throws ClassNotFoundException,
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException,
MalformedURLException,
IOException,
NoSuchAlgorithmException {
long now = System.currentTimeMillis();
//System.err.println("ClassLoader: "+stage1classLoader);
//System.err.println("lastSuccessfullCompile: "+lastSuccessfullCompile);
//System.err.println("now: "+now);
_assert(CBT_HOME != null, CBT_HOME);
_assert(NAILGUN != null, NAILGUN);
_assert(TARGET != null, TARGET);
_assert(STAGE1 != null, STAGE1);
if(args[0].equals("check-alive")){
System.exit(33);
return;
}
List<File> stage1SourceFiles = new ArrayList<File>();
for( File f: new File(STAGE1).listFiles() ){
if( f.isFile() && f.toString().endsWith(".scala") ){
stage1SourceFiles.add(f);
}
}
Boolean stage1SourcesChanged = false;
for( File file: stage1SourceFiles ){
if( file.lastModified() > lastSuccessfullCompile ){
stage1SourcesChanged = true;
//System.err.println("File change: "+file.lastModified());
break;
}
}
if(stage1SourcesChanged || stage1classLoader == null){
EarlyDependencies earlyDeps = new EarlyDependencies();
int exitCode = zinc(earlyDeps, stage1SourceFiles);
if( exitCode == 0 ){
lastSuccessfullCompile = now;
} else {
System.exit( exitCode );
}
ClassLoader nailgunClassLoader;
if( classLoaderCacheKeys.containsKey( NAILGUN+TARGET ) ){
nailgunClassLoader = cacheGet( NAILGUN+TARGET );
} else {
nailgunClassLoader = cachePut( classLoader(NAILGUN+TARGET, earlyDeps.stage1), NAILGUN+TARGET ); // FIXME: key is wrong here, should be full CP
}
stage1classLoader = classLoader(STAGE1+TARGET, nailgunClassLoader);
}
try{
Integer exitCode =
(Integer) stage1classLoader
.loadClass("cbt.Stage1")
.getMethod("run", String[].class, ClassLoader.class, Boolean.class)
.invoke( null, (Object) args, stage1classLoader, stage1SourcesChanged);
System.exit(exitCode);
}catch(Exception e){
System.err.println(stage1classLoader);
throw e;
}
}
public static void _assert(Boolean condition, Object msg){
if(!condition){
throw new AssertionError("Assertion failed: "+msg);
}
}
public static void runMain(String cls, String[] args, ClassLoader cl) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException{
cl.loadClass(cls)
.getMethod("main", String[].class)
.invoke( null, (Object) args);
}
static int zinc( EarlyDependencies earlyDeps, List<File> sourceFiles ) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException{
String cp = NAILGUN+TARGET + pathSeparator + earlyDeps.scalaXml_1_0_5_File + pathSeparator + earlyDeps.scalaLibrary_2_11_8_File;
List<String> zincArgs = new ArrayList<String>(
Arrays.asList(
new String[]{
"-scala-compiler", earlyDeps.scalaCompiler_2_11_8_File,
"-scala-library", earlyDeps.scalaLibrary_2_11_8_File,
"-scala-extra", earlyDeps.scalaReflect_2_11_8_File,
"-sbt-interface", earlyDeps.sbtInterface_0_13_9_File,
"-compiler-interface", earlyDeps.compilerInterface_0_13_9_File,
"-cp", cp,
"-d", STAGE1+TARGET
}
)
);
for( File f: sourceFiles ){
zincArgs.add(f.toString());
}
try{
System.setSecurityManager( new TrapSecurityManager() );
PrintStream oldOut = System.out;
System.setOut(System.err);
runMain( "com.typesafe.zinc.Main", zincArgs.toArray(new String[zincArgs.size()]), earlyDeps.zinc );
System.setOut(oldOut);
return 0;//throw new RuntimeException("zinc should have thrown an exit code");
}catch( TrappedExitCode trapped ){
return trapped.exitCode;
} finally {
System.setSecurityManager(NailgunLauncher.defaultSecurityManager);
}
}
static ClassLoader classLoader( String file ) throws MalformedURLException{
return new CbtURLClassLoader(
new URL[]{ new URL("file:"+file) }
);
}
static ClassLoader classLoader( String file, ClassLoader parent ) throws MalformedURLException{
return new CbtURLClassLoader(
new URL[]{ new URL("file:"+file) }, parent
);
}
static ClassLoader cacheGet( String key ){
return classLoaderCacheValues.get(
classLoaderCacheKeys.get( key )
);
}
static ClassLoader cachePut( ClassLoader classLoader, String... jars ){
String key = String.join( pathSeparator, jars );
Object keyObject = new Object();
classLoaderCacheKeys.put( key, keyObject );
classLoaderCacheValues.put( keyObject, classLoader );
return classLoader;
}
public static void download(URL urlString, Path target, String sha1) throws IOException, NoSuchAlgorithmException {
final Path unverified = Paths.get(target+".unverified");
if(!Files.exists(target)) {
new File(target.toString()).getParentFile().mkdirs();
System.err.println("downloading " + urlString);
System.err.println("to " + target);
final InputStream stream = urlString.openStream();
Files.copy(stream, unverified, StandardCopyOption.REPLACE_EXISTING);
stream.close();
final String checksum = sha1(Files.readAllBytes(unverified));
if(sha1 == null || sha1.toUpperCase().equals(checksum)) {
Files.move(unverified, target, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
} else {
System.err.println(target + " checksum does not match.\nExpected: |" + sha1 + "|\nFound: |" + checksum + "|");
System.exit(1);
}
}
}
public static String sha1(byte[] bytes) throws NoSuchAlgorithmException {
final MessageDigest sha1 = MessageDigest.getInstance("SHA1");
sha1.update(bytes, 0, bytes.length);
return (new HexBinaryAdapter()).marshal(sha1.digest());
}
}
|