Build Intellij WslTools

Bug: 332975386
Change-Id: I926b5e6ac882a12288ba8d14af5e16e6e8ddcb12
diff --git a/intellij-native/Dockerfile b/intellij-native/Dockerfile
index df5bef7..92f2af9 100644
--- a/intellij-native/Dockerfile
+++ b/intellij-native/Dockerfile
@@ -11,4 +11,9 @@
 
 RUN useradd -ms /bin/bash builder
 USER builder
-WORKDIR /home/builder
\ No newline at end of file
+
+ADD --chown=builder:builder https://musl.libc.org/releases/musl-1.2.2.tar.gz /home/builder/
+RUN chmod 644 /home/builder/musl-1.2.2.tar.gz
+
+WORKDIR /home/builder
+
diff --git a/intellij-native/android-studio.cfg b/intellij-native/android-studio.cfg
index d817402..e6cdba9 100644
--- a/intellij-native/android-studio.cfg
+++ b/intellij-native/android-studio.cfg
@@ -1,5 +1,5 @@
 service=remotebuildexecution.googleapis.com:443
 instance=projects/google.com:android-studio-alphasource/instances/default_instance
 use_application_default_credentials=true
-platform=container-image=docker://gcr.io/google.com/android-studio-alphasource/intellij_native@sha256:0f7ada9ac7c34c4661fc9637acf3959d0e864ad9fb9c75fe948eca45bdf60728
+platform=container-image=docker://gcr.io/google.com/android-studio-alphasource/intellij_native@sha256:6e0e0b553b1a5ba226e081f7de6b8004388a3b9a5212fa4fe11a1e8a4829fbda
 server_address=unix:///tmp/reproxy.sock
\ No newline at end of file
diff --git a/intellij-native/linux_tools.sh b/intellij-native/linux_tools.sh
index 30e8b0c..dc4ea6f 100755
--- a/intellij-native/linux_tools.sh
+++ b/intellij-native/linux_tools.sh
@@ -10,13 +10,60 @@
 declare -r dist_dir=$(make_target_dir "$2")
 declare -r build_number="$3"
 
+# Converts version string to comparable number `12.3` -> 012003000000. Works for at most 4 fields
+function ver { printf "%03d%03d%03d%03d" $(echo "$1" | tr '.' ' '); }
+
+# Check that file exists and is compiled for both: x86_64 and arm64
+declare -r MAX_GLIBC_VERSION="2.31"
+function verify_glibc() {
+  glibc_version="$(objdump -x "$1" | grep -o "GLIBC_.*" | sort | uniq | cut -d _ -f 2 | sort -V | tail -n 1)"
+  if [ $(ver ${glibc_version:-0}) -gt $(ver $MAX_GLIBC_VERSION) ]; then
+    echo "ERROR: $1 uses glibc version $glibc_version which is newer than $MAX_GLIBC_VERSION"
+    exit 2
+  fi
+}
+
+function verify_statically_linked() {
+  if [[ $(ldd $1) != *"not a dynamic executable"* ]]; then
+    echo "ERROR: $1 have dynamically linked libraries. Abort."
+    exit 3
+  fi
+}
+
 # Build fsNotifier
 (
     mkdir $out_dir/fsNotifier && cd $out_dir/fsNotifier
     cp -r $top/tools/idea/native/fsNotifier/linux/. ./
     ./make.sh
+
+    verify_glibc fsnotifier
     cp fsnotifier $dist_dir/.
     chmod +x $dist_dir/fsnotifier
 )
 
+# Build WslTools
+(
+    mkdir $out_dir/WslTools && cd $out_dir/WslTools
+    cp -r $top/tools/idea/native/WslTools/. ./
+
+    # Prepare musl
+    cp /home/builder/musl-1.2.2.tar.gz ./
+    tar xfvz musl-1.2.2.tar.gz && mv musl-1.2.2 musl
+
+    make
+
+    verify_statically_linked wslhash
+    verify_statically_linked wslproxy
+    verify_statically_linked ttyfix
+
+    cp wslhash $dist_dir/.
+    chmod +x $dist_dir/wslhash
+
+    cp wslproxy $dist_dir/.
+    chmod +x $dist_dir/wslproxy
+
+    cp ttyfix $dist_dir/.
+    chmod +x $dist_dir/ttyfix
+)
+
 echo "Done Building IntelliJ Linux Tools!"
\ No newline at end of file
diff --git a/intellij-native/linux_tools_re_proxy.sh b/intellij-native/linux_tools_re_proxy.sh
index 35ab74d..1dc5f64 100755
--- a/intellij-native/linux_tools_re_proxy.sh
+++ b/intellij-native/linux_tools_re_proxy.sh
@@ -36,12 +36,18 @@
     cp ${script_dir}/linux_tools.sh .
     mkdir -p ./tools/idea/native/ && cp -r ${top}/tools/idea/native/. ./tools/idea/native/
 
+    # ubuntu:14.04 have too old GCC version
+    # - that use -std=c90 by default
+    # - don't like `{0}` style initialization: https://stackoverflow.com/questions/1538943
+    # update compile flags for WSL tools, so that code compiles
+    sed -i -e 's/CFLAGS =/CFLAGS = -std=c99 -Wno-missing-field-initializers -Wno-missing-braces /g' ./tools/idea/native/WslTools/Makefile
+
     ${re_client}/rewrapper  --labels=type=tool --exec_strategy=remote \
     --cfg=$cfg \
     --inputs=. \
-    --output_files=dist/fsnotifier \
+    --output_files=dist/fsnotifier,dist/wslhash,dist/ttyfix,dist/wslproxy \
     -- ./linux_tools.sh out dist $build_number
   )
 )
 
-cp $out_dir/re_files/dist/fsnotifier $dist_dir/
\ No newline at end of file
+cp $out_dir/re_files/dist/* $dist_dir/
\ No newline at end of file