Browse Source

Fix java command file in plugin
e43ef305d767fb94adc0f4f6e3901156fc3b7939

v-korovin 9 months ago
parent
commit
be66f533d5
4 changed files with 55 additions and 12 deletions
  1. 1 1
      build/conf/java.conf
  2. 10 11
      build/scripts/compile_java.py
  3. 43 0
      build/scripts/java_command_file.py
  4. 1 0
      build/scripts/ya.make

+ 1 - 1
build/conf/java.conf

@@ -1387,7 +1387,7 @@ elsewhen ($JDK_VERSION == "") {
 otherwise {
     EXTERNAL_JAVA_JDK_RESOURCE=$JDK_RESOURCE
 }
-COMPILE_JAVA=${cwd:ARCADIA_BUILD_ROOT} $YMAKE_PYTHON ${input:"build/scripts/compile_java.py"} --ya-start-command-file --java-bin $EXTERNAL_JAVA_JDK_RESOURCE/bin/java --javac-bin $EXTERNAL_JAVA_JDK_RESOURCE/bin/javac --jar-bin $JDK_RESOURCE/bin/jar --kotlin-compiler $KOTLIN_COMPILER_RESOURCE_GLOBAL/kotlin-compiler.jar $JAVA_VCS_MF_ARG $PACKAGE_PREFIX_ARGS --jar-output $TARGET --srcs-jar-output ${output;suf=-sources.jar:REALPRJNAME} $AUTO_INPUT DELIM $JAVAC_OPTS DELIM $MANAGED_PEERS_CLOSURE DELIM -no-stdlib -module-name $REALPRJNAME -jvm-target ${KOTLIN_JVM_TARGET} ${KOTLINC_OPTS_VALUE} --ya-end-command-file ${kv;hide:"p JV"} ${kv;hide:"pc light-blue"} ${kv;hide:"show_out"} ${requirements;hide:"cpu:2"}
+COMPILE_JAVA=${cwd:ARCADIA_BUILD_ROOT} $YMAKE_PYTHON3 ${input:"build/scripts/compile_java.py"} --ya-start-command-file --java-bin $EXTERNAL_JAVA_JDK_RESOURCE/bin/java --javac-bin $EXTERNAL_JAVA_JDK_RESOURCE/bin/javac --jar-bin $JDK_RESOURCE/bin/jar --kotlin-compiler $KOTLIN_COMPILER_RESOURCE_GLOBAL/kotlin-compiler.jar $JAVA_VCS_MF_ARG $PACKAGE_PREFIX_ARGS --jar-output $TARGET --srcs-jar-output ${output;suf=-sources.jar:REALPRJNAME} $AUTO_INPUT DELIM $JAVAC_OPTS DELIM $MANAGED_PEERS_CLOSURE DELIM -no-stdlib -module-name $REALPRJNAME -jvm-target ${KOTLIN_JVM_TARGET} ${KOTLINC_OPTS_VALUE} --ya-end-command-file ${kv;hide:"p JV"} ${kv;hide:"pc light-blue"} ${kv;hide:"show_out"} ${requirements;hide:"cpu:2"} ${hide;input:"build/scripts/java_command_file.py"} ${hide;input:"build/scripts/process_command_files.py"}
 
 ARGS_DELIM="MACRO_CALLS_DELIM"
 

+ 10 - 11
build/scripts/compile_java.py

@@ -1,6 +1,6 @@
 import argparse
 import contextlib
-from distutils import dir_util
+from shutil import copytree
 import os
 import shutil
 import subprocess as sp
@@ -9,6 +9,7 @@ import zipfile
 import sys
 
 import process_command_files as pcf
+import java_command_file as jcf
 
 
 def parse_args(args):
@@ -77,12 +78,10 @@ def main():
             ts.write(' '.join(srcs))
 
     if ktsrcs:
-        temp_kt_sources_file = 'temp.kt.sources.list'
-        with open(temp_kt_sources_file, 'w') as ts:
-            ts.write(' '.join(ktsrcs + srcs))
         kt_classes_dir = 'kt_cls'
         mkdir_p(kt_classes_dir)
-        sp.check_call(
+
+        jcf.call_java_with_command_file(
             [
                 opts.java_bin,
                 '-Didea.max.content.load.filesize=30720',
@@ -93,16 +92,16 @@ def main():
                 '-d',
                 kt_classes_dir,
             ]
-            + ktc_opts
-            + ['@' + temp_kt_sources_file]
+            + ktc_opts,
+            wrapped_args=ktsrcs + srcs,
         )
         classpath = os.pathsep.join([kt_classes_dir, classpath])
 
     if srcs:
-        sp.check_call(
+        jcf.call_java_with_command_file(
             [opts.javac_bin, '-nowarn', '-g', '-classpath', classpath, '-encoding', 'UTF-8', '-d', classes_dir]
-            + javac_opts
-            + ['@' + temp_sources_file]
+            + javac_opts,
+            wrapped_args=srcs,
         )
 
     for s in jsrcs:
@@ -115,7 +114,7 @@ def main():
                 zf.extractall(classes_dir)
 
     if ktsrcs:
-        dir_util.copy_tree(kt_classes_dir, classes_dir)
+        copytree(kt_classes_dir, classes_dir, dirs_exist_ok=True)
 
     if opts.vcs_mf:
         sp.check_call([opts.jar_bin, 'cfm', opts.jar_output, opts.vcs_mf, os.curdir], cwd=classes_dir)

+ 43 - 0
build/scripts/java_command_file.py

@@ -0,0 +1,43 @@
+import platform
+import subprocess as sp
+
+
+def _java_cmd_file_quote(s):
+    """ Wrap argument based on https://docs.oracle.com/en/java/javase/21/docs/specs/man/java.html#java-command-line-argument-files """
+    if not s:
+        return "''"
+
+    if not any(char.isspace() for char in s):
+        return s
+
+    return f'"{s.replace('\\', '\\\\')}"'
+
+
+def call_java_with_command_file(cmd, wrapped_args, **kwargs):
+    is_win = platform.system() == 'Windows'
+
+    args = cmd
+    args_to_wrap = wrapped_args
+    if is_win:
+        args = [cmd[0]]
+        args_to_wrap = cmd[1:] + args_to_wrap
+
+    commands_file = 'wrapped.args'
+    with open(commands_file, 'w') as f:
+        f.write(' '.join(_java_cmd_file_quote(arg) for arg in args_to_wrap))
+
+    if is_win:
+        # Some Windows machines has troubles with running cmd lines with `@` without shell=True
+        kwargs['shell'] = True
+
+    try:
+        return sp.check_output(
+            args + ["@" + commands_file],
+            **kwargs
+        )
+    except Exception as e:
+        if hasattr(e, "add_note"):
+            e.add_note(f"Original command: {cmd} {wrapped_args}")
+            e.add_note(f"Wrapped part: {wrapped_args}")
+
+        raise

+ 1 - 0
build/scripts/ya.make

@@ -90,6 +90,7 @@ ELSEIF (PY3)
         generate_pom.py
         generate_win_vfs.py
         go_proto_wrapper.py
+        java_command_file.py
         java_pack_to_file.py
         jni_swig.py
         kt_copy.py