aboutsummaryrefslogtreecommitdiff
path: root/cbt
blob: d4d51f3042abdc477eb8c6ff4978e1c8be78e36f (plain) (blame)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env bash
# Launcher bash script that bootstraps CBT from source.
# (Some of the code for reporting missing dependencies and waiting for nailgun to come up is a bit weird.)
# This is inentionally kept as small as posible.
# Welcome improvements to this file:
# - reduce code size through better ideas
# - reduce code size by moving more of this into type-checked Java/Scala code (if possible without performance loss).
# - reduction of dependencies
# - performance improvements

# utility function to log message to stderr with stating the time
log () {
	msg=$1
	enabled=1
	while test $# -gt 0; do
	    case "$1" in
	    	"-Dlog=time") enabled=0 ;;
	    	"-Dlog=all") enabled=0 ;;
	    esac
	    shift
	done
	if [ $enabled -eq 0 ]; then
		which gdate 2>&1 > /dev/null
		gdate_installed=$?
		if [ $gdate_installed -eq 0 ]; then
			i=`gdate +"%S.%N"`
			echo "[$i] $msg" 1>&2
		fi
	fi
}

log "Checking for dependencies" $*

which javac 2>&1 > /dev/null
javac_installed=$?
if [ ! $javac_installed -eq 0 ]; then
	echo "You need to install javac! CBT needs it to bootstrap from Java sources into Scala." 1>&2
	exit 1
fi
javac_version=$(javac -version 2>&1 | cut -d ' ' -f 2)
javac_version_minor=$(echo -n $javac_version | cut -d '.' -f 2)
if [ ! "$javac_version_minor" -ge "8" ]; then
	echo "You need to install javac version 1.8 or greater! CBT currently relies on Java 8." 2>&1
	echo "Current javac version is $javac_version" 2>&1
	exit 1
fi
which ng 2>&1 > /dev/null
ng_installed=$?
which ng-server 2>&1 > /dev/null
ng_server_installed=$?
nailgun_installed=0
if [ ! $ng_installed -eq 0 ] || [ ! $ng_server_installed -eq 0 ]; then
	nailgun_installed=1
	echo "(Note: nailgun not found. It makes CBT faster! Try 'brew install nailgun'.)" 1>&2
fi
which realpath 2>&1 > /dev/null
realpath_installed=$?
which gcc 2>&1 > /dev/null
gcc_installed=$?
if [ ! $realpath_installed -eq 0 ] && [ ! $gcc_installed -eq 0 ]; then
	echo "You need realpath or gcc installed! CBT needs it to locate itself reliably." 1>&2
	exit 1
fi

which gpg 2>&1 > /dev/null
gpg_installed=$?
if [ ! $gpg_installed -eq 0 ]; then
	echo "(Note: gpg not found. In order to use publishSigned you'll need it.)" 1>&2
fi

NAILGUN_PORT=4444
NG="ng --nailgun-port $NAILGUN_PORT"

CWD=$(pwd)
_DIR=$(dirname $(readlink "$0") 2>/dev/null || dirname "$0" 2>/dev/null )

log "Find out real path. Build realpath if needed." $*

export CBT_HOME=$(dirname $($_DIR/realpath/realpath.sh $0))

export SCALA_VERSION="2.11.7"
export NAILGUN=$CBT_HOME/nailgun_launcher/
export STAGE1=$CBT_HOME/stage1/
export TARGET=target/scala-2.11/classes/
mkdir -p $NAILGUN$TARGET
mkdir -p $STAGE1$TARGET

nailgun_out=$NAILGUN/target/nailgun.stdout.log
nailgun_err=$NAILGUN/target/nailgun.strerr.log
foo(){
	while test $# -gt 0; do
	    case "$1" in
	    	"-Dlog=nailgun")
				nailgun_out=/dev/stderr
				nailgun_err=/dev/stderr
	    	;;
	    	"-Dlog=all")
				nailgun_out=/dev/stderr
				nailgun_err=/dev/stderr
	    	;;
	    esac
	    shift
	done
}

foo $@

if [ "$1" = "kill" ]; then
	echo "Stopping nailgun" 1>&2
	$NG ng-stop >> $nailgun_out 2>> $nailgun_err &
	exit 1
fi

which nc 2>&1 > /dev/null
nc_installed=$?

log "Check for running nailgun with nc." $*

server_up=1
if [ $nc_installed -eq 0 ]; then
	nc -z -n -w 1 127.0.0.1 $NAILGUN_PORT > /dev/null 2>&1
	server_up=$?
else
	echo "(Note: nc not found. It will make slightly startup faster.)" 1>&2
fi

use_nailgun=0
if [ $nailgun_installed -eq 1 ] || [ "$1" = "publishSigned" ] || [ "$2" = "publishSigned" ] || [ "$1" = "direct" ] || [ "$2" = "direct" ]; then
	use_nailgun=1
fi

if [ $use_nailgun -eq 0 ] && [ ! $server_up -eq 0 ]; then
	log "Starting up nailgun server." $*
	# try to start nailgun-server, just in case it's not up
	ng-server 127.0.0.1:$NAILGUN_PORT >> $nailgun_out 2>> $nailgun_err &
fi

log "Downloading Scala jars if necessary..." $*
export SCALA_CLASSPATH=`$CBT_HOME/bootstrap_scala/bootstrap_scala $SCALA_VERSION`
if [ ! $? -eq 0 ]; then echo "Problem with bootstrap_scala" 1>&2; exit 1; fi

SCALAC="java -Xmx256M -Xms32M\
	-Xbootclasspath/a:$SCALA_CLASSPATH\
	-Dscala.usejavacp=true\
	-Denv.emacs=\
	scala.tools.nsc.Main\
	-deprecation\
	-feature"

stage1 () { 
	log "Checking for changes in cbt/nailgun_launcher" $*
	NAILGUN_INDICATOR=$NAILGUN$TARGET/cbt/NailgunLauncher.class
	changed=0
	for file in `ls $NAILGUN/*.java`; do  
		if [ $file -nt $NAILGUN_INDICATOR ]; then changed=1; fi
	done
	compiles=0
	if [ $changed -eq 1 ]; then
		#rm $NAILGUN$TARGET/cbt/*.class 2>/dev/null
		echo "Compiling cbt/nailgun_launcher" 1>&2
		javac -Xlint:deprecation -d $NAILGUN$TARGET `ls $NAILGUN*.java`
		compiles=$?
		if [ $compiles -ne 0 ]; then break; fi
		if [ $use_nailgun -eq 0 ]; then
			echo "Stopping nailgun" 1>&2
			$NG ng-stop >> $nailgun_out 2>> $nailgun_err &
			echo "Restarting nailgun" 1>&2
			ng-server 127.0.0.1:$NAILGUN_PORT >> $nailgun_out 2>> $nailgun_err &
		fi
	fi

	log "Checking for changes in cbt/stage1" $*
	STAGE1_INDICATOR=$STAGE1$TARGET/cbt/Stage1.class
	changed2=0
	for file in `ls $STAGE1*.scala`; do  
		if [ $file -nt $STAGE1_INDICATOR ]; then changed2=1; fi
	done
	compiles2=0

	if [ $changed2 -eq 1 ]; then
		echo "Compiling cbt/stage1" 1>&2
		rm $STAGE1$TARGET/cbt/*.class 2>/dev/null
		$SCALAC -cp $NAILGUN$TARGET -d $STAGE1$TARGET `ls $STAGE1/*.scala`
		compiles2=$?
		if [ $compiles2 -ne 0 ]; then break; fi
	fi

	log "run CBT and loop if desired. This allows recompiling CBT itself as part of compile looping." $*
	if [ "$1" = "admin" ]; then
		mainClass=cbt.AdminStage1
	else
		mainClass=cbt.Stage1
	fi

	CP=$STAGE1$TARGET:$SCALA_CLASSPATH
	if [ $use_nailgun -eq 1 ]
	then
		log "Running JVM directly" $*
		# -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=localhost:5005
		java -cp $NAILGUN$TARGET cbt.NailgunLauncher $mainClass $CP "$CWD" $*
	else
		log "Running via nailgun." $*
		for i in 0 1 2 3 4 5 6 7 8 9; do
			log "Adding classpath." $*
			$NG ng-cp $NAILGUN$TARGET >> $nailgun_out 2>> $nailgun_err
			log "Checking if nailgun is up yet." $*
			$NG cbt.NailgunLauncher cbt.CheckAlive $CP "$CWD" $* >> $nailgun_out 2>> $nailgun_err
			alive=$?
			if [ $alive -eq 131 ]; then
				echo "Nailgun call failed. Try 'cbt kill' and check the error log cbt/nailgun_launcher/target/nailgun.stderr.log" 1>&2
			elif [ $alive -eq 33 ]; then
				break
			else
				log "Nope. Sleeping for 0.5 seconds" $*
				if [ "$i" -gt "1" ]; then
					echo "Waiting for nailgun to start... (For problems try -Dlog=nailgun or check logs in cbt/nailgun_launcher/target/*.log)" 1>&2
				fi
				sleep 0.5
			fi
		done
		log "Running $mainClass via Nailgun." $*
		$NG cbt.NailgunLauncher $mainClass $CP "$CWD" $*
	fi
	log "Done running $mainClass." $*
} 

while true; do
	stage1 $*
	if [ ! "$1" = "loop" ]; then
		break
	fi
	echo "======= Restarting CBT =======" 1>&2
done

log "Exiting CBT" $*