From 45c2ffcd55cb1cca8e570d0432eb4041082e7464 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= <kamil.sliwak@codepoets.it>
Date: Fri, 23 Jun 2023 10:24:15 +0200
Subject: [PATCH] CI: Merge prepare_bytecode_report and storebytecode.sh script
 into a new script and use it for all platforms in CI

---
 .circleci/config.yml                     | 70 ++++++++---------------
 .circleci/parallel_bytecode_report.sh    | 73 ++++++++++++++++++++++++
 scripts/bytecodecompare/storebytecode.sh | 73 ------------------------
 3 files changed, 98 insertions(+), 118 deletions(-)
 create mode 100755 .circleci/parallel_bytecode_report.sh
 delete mode 100755 scripts/bytecodecompare/storebytecode.sh

diff --git a/.circleci/config.yml b/.circleci/config.yml
index eb2709a63..b618d1574 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -76,34 +76,31 @@ commands:
     parameters:
       label:
         type: string
+      binary_type:
+        type: enum
+        enum:
+          - solcjs
+          - native
       binary_path:
         type: string
     steps:
-      - run: mkdir test-cases/
       - run:
-          name: Prepare input files
+          name: Generate bytecode reports for the selected preset
           command: |
-            cd test-cases/
-            python3 ../scripts/isolate_tests.py ../test/
-      - run:
-          name: Generate bytecode report
-          command: |
-            cd test-cases/
-            interface=$(echo -e "standard-json\ncli" | circleci tests split)
-            echo "Selected interface: ${interface}"
-
-            python3 ../scripts/bytecodecompare/prepare_report.py \
-              << parameters.binary_path >> \
-              --interface "$interface" \
-              --report-file "../bytecode-report-<< parameters.label >>-${interface}.txt"
+            .circleci/parallel_bytecode_report.sh \
+                "<< parameters.label >>" \
+                "<< parameters.binary_type >>" \
+                "${PWD}/<< parameters.binary_path >>"
       - store_artifacts:
           path: bytecode-report-<< parameters.label >>-standard-json.txt
       - store_artifacts:
           path: bytecode-report-<< parameters.label >>-cli.txt
+      - store_artifacts:
+          path: bytecode-report-<< parameters.label >>.txt
       - persist_to_workspace:
           root: .
           paths:
-            - bytecode-report-<< parameters.label >>-*.txt
+            - bytecode-report-<< parameters.label >>*.txt
       - matrix_notify_failure_unless_pr
 
   install_python3:
@@ -1485,10 +1482,6 @@ jobs:
   # It can be safely removed once we move both to the same Ubuntu version.
   b_bytecode_ubu_static:
     <<: *base_ubuntu2004_small
-    environment:
-      TERM: xterm
-      MAKEFLAGS: -j 2
-      LC_ALL: C
     parallelism: 2 # For prepare_bytecode_report
     steps:
       - checkout
@@ -1496,14 +1489,11 @@ jobs:
           at: build
       - prepare_bytecode_report:
           label: "ubuntu2004-static"
-          binary_path: "../build/solc/solc-static-linux"
+          binary_type: native
+          binary_path: "build/solc/solc-static-linux"
 
   b_bytecode_ubu:
     <<: *base_ubuntu2204_small
-    environment:
-      TERM: xterm
-      MAKEFLAGS: -j 2
-      LC_ALL: C
     parallelism: 2 # For prepare_bytecode_report
     steps:
       - checkout
@@ -1511,14 +1501,11 @@ jobs:
           at: build
       - prepare_bytecode_report:
           label: "ubuntu"
-          binary_path: "../build/solc/solc"
+          binary_type: native
+          binary_path: "build/solc/solc"
 
   b_bytecode_osx:
     <<: *base_osx
-    environment:
-      TERM: xterm
-      MAKEFLAGS: -j 5
-      LC_ALL: C
     parallelism: 2 # For prepare_bytecode_report
     steps:
       - checkout
@@ -1526,12 +1513,11 @@ jobs:
           at: .
       - prepare_bytecode_report:
           label: "osx"
-          binary_path: "../build/solc/solc"
+          binary_type: native
+          binary_path: "build/solc/solc"
 
   b_bytecode_win:
     <<: *base_win
-    environment:
-      LC_ALL: C
     parallelism: 2 # For prepare_bytecode_report
     steps:
       # NOTE: For bytecode generation we need the input files to be byte-for-byte identical on all
@@ -1544,25 +1530,19 @@ jobs:
           at: build
       - prepare_bytecode_report:
           label: "windows"
-          binary_path: "../build/solc/Release/solc.exe"
+          binary_type: native
+          binary_path: "build/solc/Release/solc.exe"
 
   b_bytecode_ems:
     <<: *base_node_small
-    environment:
-      SOLC_EMSCRIPTEN: "On"
-      LC_ALL: C
     steps:
       - checkout
       - attach_workspace:
           at: emscripten_build/libsolc
-      - run: scripts/bytecodecompare/storebytecode.sh && mv -v report.txt bytecode-report-emscripten.txt
-      - store_artifacts:
-          path: bytecode-report-emscripten.txt
-      - persist_to_workspace:
-          root: .
-          paths:
-            - bytecode-report-emscripten.txt
-      - matrix_notify_failure_unless_pr
+      - prepare_bytecode_report:
+          label: "emscripten"
+          binary_type: solcjs
+          binary_path: "emscripten_build/libsolc/soljson.js"
 
   t_bytecode_compare:
     <<: *base_ubuntu2204_small
diff --git a/.circleci/parallel_bytecode_report.sh b/.circleci/parallel_bytecode_report.sh
new file mode 100755
index 000000000..fd435bf61
--- /dev/null
+++ b/.circleci/parallel_bytecode_report.sh
@@ -0,0 +1,73 @@
+#!/usr/bin/env bash
+set -euo pipefail
+
+#------------------------------------------------------------------------------
+# Splits all test source code into multiple files, generates bytecode and metadata
+# for each file and combines it into a single report.txt file.
+#
+# The script is meant to be executed in CI on all supported platforms. All generated
+# reports must be identical for a given compiler version.
+#
+# ------------------------------------------------------------------------------
+# This file is part of solidity.
+#
+# solidity is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# solidity is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with solidity.  If not, see <http://www.gnu.org/licenses/>
+#
+# (c) 2023 solidity contributors.
+#------------------------------------------------------------------------------
+
+(( $# == 3 )) || { >&2 echo "Wrong number of arguments."; exit 1; }
+label="$1"
+binary_type="$2"
+binary_path="$3" # This path must be absolute
+
+[[ $binary_type == native || $binary_type == solcjs ]] || { >&2 echo "Invalid binary type: ${binary_type}"; exit 1; }
+
+# NOTE: Locale affects the order of the globbed files.
+export LC_ALL=C
+
+mkdir test-cases/
+cd test-cases/
+
+echo "Preparing input files"
+python3 ../scripts/isolate_tests.py ../test/
+
+if [[ $binary_type == native ]]; then
+    interface=$(echo -e "standard-json\ncli" | circleci tests split)
+    echo "Selected interface: ${interface}"
+
+    echo "Generating bytecode reports"
+    python3 ../scripts/bytecodecompare/prepare_report.py \
+        "$binary_path" \
+        --interface "$interface" \
+        --report-file "../bytecode-report-${label}-${interface}.txt"
+else
+    echo "Installing solc-js"
+    git clone --depth 1 https://github.com/ethereum/solc-js.git solc-js
+    cp "$binary_path" solc-js/soljson.js
+
+    cd solc-js/
+    npm install
+    npm run build
+
+    cd ..
+    npm install ./solc-js/dist
+
+    cp ../scripts/bytecodecompare/prepare_report.js .
+
+    echo "Generating bytecode reports"
+    # shellcheck disable=SC2035
+    ./prepare_report.js \
+        *.sol > "../bytecode-report-${label}.txt"
+fi
diff --git a/scripts/bytecodecompare/storebytecode.sh b/scripts/bytecodecompare/storebytecode.sh
deleted file mode 100755
index beeb79d09..000000000
--- a/scripts/bytecodecompare/storebytecode.sh
+++ /dev/null
@@ -1,73 +0,0 @@
-#!/usr/bin/env bash
-
-#------------------------------------------------------------------------------
-# Script used for cross-platform comparison of the bytecode generated by the compiler.
-# Splits all test source code into multiple files, generates bytecode and metadata
-# for each file and combines it into a single report.txt file.
-#
-# The script is meant to be executed in CI on all supported platforms. All generated
-# reports must be identical for a given compiler version.
-#
-# ------------------------------------------------------------------------------
-# This file is part of solidity.
-#
-# solidity is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# solidity is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with solidity.  If not, see <http://www.gnu.org/licenses/>
-#
-# (c) 2017 solidity contributors.
-#------------------------------------------------------------------------------
-
-set -e
-
-REPO_ROOT="$(dirname "$0")"/../..
-cd "$REPO_ROOT"
-REPO_ROOT=$(pwd) # make it absolute
-
-BUILD_DIR="${1:-${REPO_ROOT}/build}"
-
-# Set locale to C to prevent it from affecting glob sort order.
-export LC_ALL=C
-
-echo "Compiling all test contracts into bytecode..."
-TMPDIR=$(mktemp -d)
-(
-    cd "$TMPDIR"
-
-    "$REPO_ROOT"/scripts/isolate_tests.py "$REPO_ROOT"/test/
-
-    if [[ "$SOLC_EMSCRIPTEN" = "On" ]]
-    then
-        echo "Installing solc-js..."
-        # npm install solc
-        git clone --depth 1 https://github.com/ethereum/solc-js.git solc-js
-        cp "$REPO_ROOT/emscripten_build/libsolc/soljson.js" solc-js/
-        pushd solc-js/
-        npm install
-        npm run build
-        popd
-
-        cp "$REPO_ROOT/scripts/bytecodecompare/prepare_report.js" .
-        npm install ./solc-js/dist
-
-        echo "Running the compiler..."
-        # shellcheck disable=SC2035
-        ./prepare_report.js *.sol > report.txt
-        echo "Finished running the compiler."
-    else
-        "$REPO_ROOT/scripts/bytecodecompare/prepare_report.py" "$BUILD_DIR/solc/solc"
-    fi
-
-    cp report.txt "$REPO_ROOT"
-)
-rm -rf "$TMPDIR"
-echo "Storebytecode finished."