Skip to content

Commit 9e3d4f2

Browse files
committed
refactor all common code in bin/scalac to bin/common
1 parent df7968c commit 9e3d4f2

File tree

6 files changed

+126
-123
lines changed

6 files changed

+126
-123
lines changed
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
scala> val d: java.sql.Date = new java.sql.Date(100L)
2-
val d: java.sql.Date = 1970-01-01
3-
1+
scala> val d: Long = (new java.sql.Date(100L)).getTime
2+
val d: Long = 100

compiler/test/dotty/tools/scripting/ClasspathTests.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ClasspathTests:
1818
val packBinDir = "dist/target/pack/bin"
1919
val scalaCopy = makeTestableScriptCopy("scala")
2020
val scalacCopy = makeTestableScriptCopy("scalac")
21+
val commonCopy = makeTestableScriptCopy("common")
2122

2223
// only interested in classpath test scripts
2324
def testFiles = scripts("/scripting").filter { _.getName.matches("classpath.*[.]sc") }
@@ -38,7 +39,8 @@ class ClasspathTests:
3839
if Files.exists(scriptPath) then
3940
val lines = Files.readAllLines(scriptPath).asScala.map {
4041
_.replaceAll("/scalac", "/scalac-copy").
41-
replaceFirst("^eval(.*JAVACMD.*)", "echo $1")
42+
replaceAll("/common", "/common-copy").
43+
replaceFirst("^ *eval(.*JAVACMD.*)", "echo $1")
4244
}
4345
val bytes = (lines.mkString("\n")+"\n").getBytes
4446
Files.write(scriptCopy, bytes)

dist/bin/common

Lines changed: 94 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ case "`uname`" in
5454
esac
5555

5656
unset CYGPATHCMD
57-
if [[ $cygwin || $mingw || $msys ]]; then
57+
if [[ ${cygwin-} || ${mingw-} || ${msys-} ]]; then
5858
# ConEmu terminal is incompatible with jna-5.*.jar
59-
[[ ($CONEMUANSI || $ConEmuANSI) ]] && conemu=true
59+
[[ (${CONEMUANSI-} || ${ConEmuANSI-}) ]] && conemu=true
6060
# cygpath is used by various windows shells: cygwin, git-sdk, gitbash, msys, etc.
6161
CYGPATHCMD=`which cygpath 2>/dev/null`
6262
case "$TERM" in
6363
rxvt* | xterm* | cygwin*)
6464
stty -icanon min 1 -echo
65-
SCALA_OPTS="$SCALA_OPTS -Djline.terminal=unix"
65+
JAVA_OPTS="$JAVA_OPTS -Djline.terminal=unix"
6666
;;
6767
esac
6868
fi
@@ -111,14 +111,14 @@ CLASSPATH_SUFFIX=""
111111
PSEP=":"
112112

113113
# translate paths to Windows-mixed format before running java
114-
if [ -n "$CYGPATHCMD" ]; then
115-
[ -n "$PROG_HOME" ] &&
114+
if [ -n "${CYGPATHCMD-}" ]; then
115+
[ -n "${PROG_HOME-}" ] &&
116116
PROG_HOME=`"$CYGPATHCMD" -am "$PROG_HOME"`
117117
[ -n "$JAVA_HOME" ] &&
118118
JAVA_HOME=`"$CYGPATHCMD" -am "$JAVA_HOME"`
119119
CLASSPATH_SUFFIX=";"
120120
PSEP=";"
121-
elif [[ $mingw || $msys ]]; then
121+
elif [[ ${mingw-} || ${msys-} ]]; then
122122
# For Mingw / Msys, convert paths from UNIX format before anything is touched
123123
[ -n "$PROG_HOME" ] &&
124124
PROG_HOME="`(cd "$PROG_HOME"; pwd -W | sed 's|/|\\\\|g')`"
@@ -155,8 +155,95 @@ SBT_INTF=$(find_lib "*compiler-interface*")
155155
JLINE_READER=$(find_lib "*jline-reader-3*")
156156
JLINE_TERMINAL=$(find_lib "*jline-terminal-3*")
157157
JLINE_TERMINAL_JNA=$(find_lib "*jline-terminal-jna-3*")
158-
[[ $conemu ]] || JNA=$(find_lib "*jna-5*")
158+
[[ ${conemu-} ]] || JNA=$(find_lib "*jna-5*")
159159

160160
# debug
161161

162162
DEBUG_STR=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005
163+
164+
classpathArgs () {
165+
# echo "dotty-compiler: $DOTTY_COMP"
166+
# echo "dotty-interface: $DOTTY_INTF"
167+
# echo "dotty-library: $DOTTY_LIB"
168+
# echo "tasty-core: $TASTY_CORE"
169+
# echo "scala-asm: $SCALA_ASM"
170+
# echo "scala-lib: $SCALA_LIB"
171+
# echo "sbt-intface: $SBT_INTF"
172+
173+
toolchain=""
174+
toolchain+="$SCALA_LIB$PSEP"
175+
toolchain+="$DOTTY_LIB$PSEP"
176+
toolchain+="$SCALA_ASM$PSEP"
177+
toolchain+="$SBT_INTF$PSEP"
178+
toolchain+="$DOTTY_INTF$PSEP"
179+
toolchain+="$DOTTY_COMP$PSEP"
180+
toolchain+="$TASTY_CORE$PSEP"
181+
toolchain+="$DOTTY_STAGING$PSEP"
182+
toolchain+="$DOTTY_TASTY_INSPECTOR$PSEP"
183+
184+
# jine
185+
toolchain+="$JLINE_READER$PSEP"
186+
toolchain+="$JLINE_TERMINAL$PSEP"
187+
toolchain+="$JLINE_TERMINAL_JNA$PSEP"
188+
toolchain+="$JNA$PSEP"
189+
190+
jvm_cp_args="-classpath \"$toolchain\""
191+
}
192+
193+
default_java_opts="-Xmx768m -Xms768m"
194+
195+
addJava () {
196+
java_args+=("'$1'")
197+
}
198+
addScala () {
199+
scala_args+=("'$1'")
200+
}
201+
addResidual () {
202+
residual_args+=("'$1'")
203+
}
204+
addScripting () {
205+
scripting_args+=("'$1'")
206+
}
207+
prepScalacCommandLine () {
208+
withCompiler=true
209+
210+
CompilerMain=dotty.tools.dotc.Main
211+
DecompilerMain=dotty.tools.dotc.decompiler.Main
212+
ReplMain=dotty.tools.repl.Main
213+
ScriptingMain=dotty.tools.scripting.Main
214+
215+
while [[ $# -gt 0 ]]; do
216+
case "$1" in
217+
--) shift; for arg; do addResidual "$arg"; done; set -- ;;
218+
-v|-verbose) verbose=true && addScala "-verbose" && shift ;;
219+
-debug) DEBUG="$DEBUG_STR" && shift ;;
220+
-q|-quiet) quiet=true && shift ;;
221+
222+
# Optimize for short-running applications, see https://github.com/lampepfl/dotty/issues/222
223+
-Oshort) addJava "-XX:+TieredCompilation -XX:TieredStopAtLevel=1" && shift ;;
224+
-repl) PROG_NAME="$ReplMain" && shift ;;
225+
-script) PROG_NAME="$ScriptingMain" && target_script="$2" && shift && shift
226+
while [[ $# -gt 0 ]]; do addScripting "$1" && shift ; done ;;
227+
-compile) PROG_NAME="$CompilerMain" && shift ;;
228+
-decompile) PROG_NAME="$DecompilerMain" && shift ;;
229+
-print-tasty) PROG_NAME="$DecompilerMain" && addScala "-print-tasty" && shift ;;
230+
-run) PROG_NAME="$ReplMain" && shift ;;
231+
-colors) colors=true && shift ;;
232+
-no-colors) unset colors && shift ;;
233+
-with-compiler) jvm_cp_args="$PSEP$DOTTY_COMP$PSEP$TASTY_CORE" && shift ;;
234+
235+
# break out -D and -J options and add them to java_args so
236+
# they reach the JVM in time to do some good. The -D options
237+
# will be available as system properties.
238+
-D*) addJava "$1" && shift ;;
239+
-J*) addJava "${1:2}" && shift ;;
240+
*) addResidual "$1" && shift ;;
241+
esac
242+
done
243+
244+
classpathArgs
245+
246+
if [ "$PROG_NAME" == "$ScriptingMain" ]; then
247+
scripting_string="-script $target_script ${scripting_args[@]}"
248+
fi
249+
}

dist/bin/scala

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#!/usr/bin/env bash
22

33
# Try to autodetect real location of the script
4-
5-
if [ -z "$PROG_HOME" ] ; then
4+
if [ -z "${PROG_HOME-}" ] ; then
65
## resolve links - $0 may be a link to PROG_HOME
76
PRG="$0"
87

@@ -27,8 +26,8 @@ if [ -z "$PROG_HOME" ] ; then
2726
cd "$saveddir"
2827
fi
2928

30-
addJvmOptions () {
31-
jvm_options+=("'$1'")
29+
addJava () {
30+
java_args+=("'$1'")
3231
}
3332

3433
addScalacOptions () {
@@ -65,7 +64,7 @@ while [[ $# -gt 0 ]]; do
6564
execute_run=true
6665
shift
6766
;;
68-
-cp | -classpath )
67+
-cp | -classpath)
6968
CLASS_PATH="$2${PSEP}"
7069
class_path_count+=1
7170
shift
@@ -100,23 +99,27 @@ while [[ $# -gt 0 ]]; do
10099
;;
101100
-version)
102101
# defer to scalac, then exit
103-
addScalacOptions "${1}"
104102
shift
105-
eval "\"$PROG_HOME/bin/scalac\" ${cp_arg-} ${java_options[@]}"
103+
eval "\"$PROG_HOME/bin/scalac\" -version"
106104
scala_exit_status=$?
107105
onExit
108106
;;
109107
-J*)
110-
addJvmOptions "${1:2}"
108+
addJava "${1:2}"
111109
addScalacOptions "${1}"
112110
shift ;;
113111
*)
114112
if [ $execute_script == false ]; then
115-
# is a script if extension .scala or .sc or if has scala hash bang
116-
if [[ -e "$1" && ("$1" == *.scala || "$1" == *.sc || -f "$1" && `head -n 1 -- "$1" | grep '#!.*scala'`) ]]; then
113+
if [[ "$1" == *.scala || "$1" == *.sc ]]; then
114+
# is a script if extension .scala or .sc
115+
# (even if file not found, to avoid adding likely typo to residual_args)
116+
execute_script=true
117+
target_script="$1"
118+
elif [[ (-f "$1" && `head -n 1 -- "$1" | grep '#!.*scala'`) ]]; then
117119
execute_script=true
118120
target_script="$1"
119121
else
122+
# unrecognized args appearing prior to a script name
120123
residual_args+=("$1")
121124
fi
122125
else
@@ -128,8 +131,8 @@ while [[ $# -gt 0 ]]; do
128131
esac
129132
done
130133

134+
[ -n "${script_trace-}" ] && set -x
131135
if [ $execute_script == true ]; then
132-
[ -n "${script_trace-}" ] && set -x
133136
if [ "$CLASS_PATH" ]; then
134137
cp_arg="-classpath \"$CLASS_PATH\""
135138
fi
@@ -141,8 +144,10 @@ if [ $execute_script == true ]; then
141144
scala_exit_status=$?
142145
else
143146
[[ $save_compiled == true ]] && rm -f $target_jar
144-
residual_args+=($setScriptName)
145-
eval "\"$PROG_HOME/bin/scalac\" ${cp_arg-} ${java_options[@]} ${residual_args[@]} -script $target_script ${script_args[@]}"
147+
set -- ${cp_arg-} ${java_options[@]} ${residual_args[@]} -script "$target_script" ${script_args[@]}
148+
prepScalacCommandLine "$@"
149+
# exec here would prevent onExit from being called, leaving terminal in unusable state
150+
eval "\"$JAVACMD\"" "$DEBUG" "-classpath \"$cp_arg\"" "${java_args[@]}" "${residual_args[@]}"
146151
scala_exit_status=$?
147152
fi
148153
elif [ $execute_repl == true ] || ([ $execute_run == false ] && [ $options_indicator == 0 ]); then
@@ -151,7 +156,7 @@ elif [ $execute_repl == true ] || ([ $execute_run == false ] && [ $options_indic
151156
fi
152157
eval "\"$PROG_HOME/bin/scalac\" ${cp_arg-} ${java_options[@]} -repl ${residual_args[@]}"
153158
scala_exit_status=$?
154-
elif [ $execute_repl == true ] || [ ${#residual_args[@]} -ne 0 ]; then
159+
elif [ ${#residual_args[@]} -ne 0 ]; then
155160
cp_arg="$DOTTY_LIB$PSEP$SCALA_LIB"
156161
if [ -z "$CLASS_PATH" ]; then
157162
cp_arg+="$PSEP."
@@ -165,7 +170,7 @@ elif [ $execute_repl == true ] || [ ${#residual_args[@]} -ne 0 ]; then
165170
cp_arg+="$PSEP$DOTTY_COMP$PSEP$TASTY_CORE$PSEP$DOTTY_INTF$PSEP$SCALA_ASM$PSEP$DOTTY_STAGING$PSEP$DOTTY_TASTY_INSPECTOR"
166171
fi
167172
# exec here would prevent onExit from being called, leaving terminal in unusable state
168-
eval "\"$JAVACMD\"" "$DEBUG" "-classpath \"$cp_arg\"" "${jvm_options[@]}" "${residual_args[@]}"
173+
eval "\"$JAVACMD\"" "$DEBUG" "-classpath \"$cp_arg\"" "${java_args[@]}" "${residual_args[@]}"
169174
scala_exit_status=$?
170175
else
171176
echo "warning: command option is not correct."

dist/bin/scalac

Lines changed: 6 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env bash
22

3-
if [ -z "$PROG_HOME" ] ; then
3+
# Try to autodetect real location of the script
4+
if [ -z "${PROG_HOME-}" ] ; then
45
## resolve links - $0 may be a link to PROG_HOME
56
PRG="$0"
67

@@ -27,102 +28,11 @@ fi
2728

2829
source "$PROG_HOME/bin/common"
2930

30-
default_java_opts="-Xmx768m -Xms768m"
31-
withCompiler=true
32-
33-
CompilerMain=dotty.tools.dotc.Main
34-
DecompilerMain=dotty.tools.dotc.decompiler.Main
35-
ReplMain=dotty.tools.repl.Main
36-
ScriptingMain=dotty.tools.scripting.Main
37-
3831
PROG_NAME=$CompilerMain
3932

40-
addJava () {
41-
java_args+=("'$1'")
42-
}
43-
addScala () {
44-
scala_args+=("'$1'")
45-
}
46-
addResidual () {
47-
residual_args+=("'$1'")
48-
}
49-
addScripting () {
50-
scripting_args+=("'$1'")
51-
}
52-
53-
classpathArgs () {
54-
# echo "dotty-compiler: $DOTTY_COMP"
55-
# echo "dotty-interface: $DOTTY_INTF"
56-
# echo "dotty-library: $DOTTY_LIB"
57-
# echo "tasty-core: $TASTY_CORE"
58-
# echo "scala-asm: $SCALA_ASM"
59-
# echo "scala-lib: $SCALA_LIB"
60-
# echo "sbt-intface: $SBT_INTF"
61-
62-
toolchain=""
63-
toolchain+="$SCALA_LIB$PSEP"
64-
toolchain+="$DOTTY_LIB$PSEP"
65-
toolchain+="$SCALA_ASM$PSEP"
66-
toolchain+="$SBT_INTF$PSEP"
67-
toolchain+="$DOTTY_INTF$PSEP"
68-
toolchain+="$DOTTY_COMP$PSEP"
69-
toolchain+="$TASTY_CORE$PSEP"
70-
toolchain+="$DOTTY_STAGING$PSEP"
71-
toolchain+="$DOTTY_TASTY_INSPECTOR$PSEP"
72-
73-
# jine
74-
toolchain+="$JLINE_READER$PSEP"
75-
toolchain+="$JLINE_TERMINAL$PSEP"
76-
toolchain+="$JLINE_TERMINAL_JNA$PSEP"
77-
toolchain+="$JNA$PSEP"
78-
79-
jvm_cp_args="-classpath \"$toolchain\""
80-
}
81-
82-
while [[ $# -gt 0 ]]; do
83-
case "$1" in
84-
--) shift; for arg; do addResidual "$arg"; done; set -- ;;
85-
-v|-verbose) verbose=true && addScala "-verbose" && shift ;;
86-
-debug) DEBUG="$DEBUG_STR" && shift ;;
87-
-q|-quiet) quiet=true && shift ;;
33+
prepScalacCommandLine "$@"
8834

89-
# Optimize for short-running applications, see https://github.com/lampepfl/dotty/issues/222
90-
-Oshort) addJava "-XX:+TieredCompilation -XX:TieredStopAtLevel=1" && shift ;;
91-
-repl) PROG_NAME="$ReplMain" && shift ;;
92-
-script) PROG_NAME="$ScriptingMain" && target_script="$2" && shift && shift
93-
while [[ $# -gt 0 ]]; do addScripting "$1" && shift ; done ;;
94-
-compile) PROG_NAME="$CompilerMain" && shift ;;
95-
-decompile) PROG_NAME="$DecompilerMain" && shift ;;
96-
-print-tasty) PROG_NAME="$DecompilerMain" && addScala "-print-tasty" && shift ;;
97-
-run) PROG_NAME="$ReplMain" && shift ;;
98-
-colors) colors=true && shift ;;
99-
-no-colors) unset colors && shift ;;
100-
-with-compiler) jvm_cp_args="$PSEP$DOTTY_COMP$PSEP$TASTY_CORE" && shift ;;
101-
102-
# break out -D and -J options and add them to java_args so
103-
# they reach the JVM in time to do some good. The -D options
104-
# will be available as system properties.
105-
-D*) addJava "$1" && shift ;;
106-
-J*) addJava "${1:2}" && shift ;;
107-
*) addResidual "$1" && shift ;;
108-
esac
109-
done
110-
111-
classpathArgs
112-
113-
if [ "$PROG_NAME" == "$ScriptingMain" ]; then
114-
scripting_string="-script $target_script ${scripting_args[@]}"
115-
fi
116-
117-
eval "\"$JAVACMD\"" \
118-
${JAVA_OPTS:-$default_java_opts} \
119-
"${DEBUG-}" \
120-
"${java_args[@]}" \
121-
"$jvm_cp_args" \
122-
-Dscala.usejavacp=true \
123-
"$PROG_NAME" \
124-
"${scala_args[@]}" \
125-
"${residual_args[@]}" \
126-
"${scripting_string-}"
35+
# exec here would prevent onExit from being called, leaving terminal in unusable state
36+
eval "\"$JAVACMD\"" "$DEBUG" "-classpath \"$cp_arg\"" "${java_args[@]}" "${residual_args[@]}"
12737
scala_exit_status=$?
128-
onExit
38+

dist/bin/scaladoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env bash
22

3+
# Try to autodetect real location of the script
34
if [ -z "$PROG_HOME" ] ; then
45
## resolve links - $0 may be a link to PROG_HOME
56
PRG="$0"
@@ -27,7 +28,6 @@ fi
2728

2829
source "$PROG_HOME/bin/common"
2930

30-
default_java_opts="-Xmx768m -Xms768m"
3131
withCompiler=true
3232

3333
CompilerMain=dotty.tools.dotc.Main

0 commit comments

Comments
 (0)