riscv-collab / v8

Port of Google v8 engine to RISC-V.
https://github.com/v8-riscv/v8/wiki
Other
238 stars 31 forks source link

Cross build v8 failed with error: unrecognized command-line option '--target=riscv64-linux-gnu' #687

Closed mollybuild closed 2 years ago

mollybuild commented 2 years ago

I build v8 for riscv64 like bellow:

$ mkdir V8_root && cd V8_root
$ fetch v8
$ gclient sync
$ ./build/install-build-deps.sh
## edit build/toolchain/linux/BUILD.gn, change toolprefix = "riscv64-unknown-linux-gnu"
$ gn gen out/riscv64.native.debug --args='is_component_build=false is_debug=true target_cpu="riscv64" v8_target_cpu="riscv64" use_goma=false goma_dir="None" treat_warnings_as_errors=false'
ninja -C out/riscv64.native.debug -j52

Error information:

[52/5097] CXX obj/torque_base/source-positions.o
FAILED: obj/torque_base/source-positions.o
riscv64-unknown-linux-gnu-g++ -MMD -MF obj/torque_base/source-positions.o.d -DUSE_UDEV -DUSE_AURA=1 -DUSE_GLIB=1 -DUSE_OZONE=1 -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_LIBCPP_ABI_NAMESPACE=Cr -D_LIBCPP_ABI_VERSION=2 -D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS -D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS -D_LIBCPP_ENABLE_NODISCARD -D_LIBCPP_DEBUG=0 -DCR_LIBCXX_REVISION=b1269813eaf5b8ac78e35e45a0f7cc320bd3e7d6 -D_DEBUG -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DCPPGC_VERIFY_HEAP -DENABLE_DISASSEMBLER -DV8_TYPED_ARRAY_MAX_SIZE_IN_HEAP=64 -DOBJECT_PRINT -DVERIFY_HEAP -DV8_TRACE_MAPS -DV8_ENABLE_ALLOCATION_TIMEOUT -DV8_ENABLE_FORCE_SLOW_PATH -DV8_ENABLE_DOUBLE_CONST_STORE_CHECK -DV8_INTL_SUPPORT -DENABLE_HANDLE_ZAPPING -DV8_CODE_COMMENTS -DV8_ENABLE_DEBUG_CODE -DV8_ENABLE_HEAP_SNAPSHOT_VERIFY -DV8_SNAPSHOT_NATIVE_CODE_COUNTERS -DV8_USE_EXTERNAL_STARTUP_DATA -DV8_ATOMIC_OBJECT_FIELD_WRITES -DV8_ENABLE_LAZY_SOURCE_POSITIONS -DV8_SHARED_RO_HEAP -DV8_WIN64_UNWINDING_INFO -DV8_ENABLE_REGEXP_INTERPRETER_THREADED_DISPATCH -DV8_ENABLE_WEBASSEMBLY -DV8_ALLOCATION_FOLDING -DV8_ALLOCATION_SITE_TRACKING -DV8_ADVANCED_BIGINT_ALGORITHMS -DV8_ENABLE_CHECKS -DV8_DEPRECATION_WARNINGS -DV8_IMMINENT_DEPRECATION_WARNINGS -DV8_TARGET_ARCH_RISCV64 -D__riscv_xlen=64 -DCAN_USE_FPU_INSTRUCTIONS -DV8_HAVE_TARGET_OS -DV8_TARGET_OS_LINUX -DDEBUG -DV8_RUNTIME_CALL_STATS -I../.. -Igen -I../../buildtools/third_party/libc++ -I../../include -Igen/include -Wall -Wno-unused-local-typedefs -Wno-maybe-uninitialized -Wno-deprecated-declarations -Wno-comments -Wno-packed-not-aligned -Wno-missing-field-initializers -Wno-unused-parameter -fno-ident -fno-strict-aliasing --param=ssp-buffer-size=4 -fstack-protector -funwind-tables -fPIC -pipe -pthread -mabi=lp64d --target=riscv64-linux-gnu -Wno-builtin-macro-redefined -D__DATE__= -D__TIME__= -D__TIMESTAMP__= -fno-omit-frame-pointer -gdwarf-4 -g2 -fno-builtin-abs -fvisibility=hidden -Wno-strict-overflow -Wno-return-type -Wno-int-in-bool-context -O3 -fdata-sections -ffunction-sections -Wno-narrowing -Wno-class-memaccess -std=gnu++17 -fno-aligned-new -nostdinc++ -isystem../../buildtools/third_party/libc++/trunk/include -isystem../../buildtools/third_party/libc++abi/trunk/include -fvisibility-inlines-hidden -fexceptions -frtti -c ../../src/torque/source-positions.cc -o obj/torque_base/source-positions.o
riscv64-unknown-linux-gnu-g++: error: unrecognized command-line option '--target=riscv64-linux-gnu'
ninja: build stopped: subcommand failed.

OS: Ubuntu 20.04 GCC version : 10.2.0

luyahan commented 2 years ago

what is your gcc_toolchain("riscv64") ?

mollybuild commented 2 years ago

what is your gcc_toolchain("riscv64") ?

gcc_toolchain("riscv64") {
  toolprefix = "riscv64-unknown-linux-gnu"

  cc = "${toolprefix}-gcc"
  cxx = "${toolprefix}-g++"

  readelf = "${toolprefix}-readelf"
  nm = "${toolprefix}-nm"
  ar = "${toolprefix}-ar"
  ld = cxx

  toolchain_args = {
    current_cpu = "riscv64"
    current_os = "linux"
    is_clang = false
  }
}
mollybuild commented 2 years ago

This issue caused by build/config/compiler/BUILD.gn,delete these two lines will fix: image

mollybuild commented 2 years ago

Then encounter this error: image This error appears when using riscv gcc 10.2.0. riscv gcc 11.1 doesn't have this issue.

mollybuild commented 2 years ago

compile obj/v8_compiler/pipeline.o failed when using GCC11.1: image

luyahan commented 2 years ago

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102479

luyahan commented 2 years ago
From 9e5ff0da991cac280c91cc6cfe3e1cca9471fd47 Mon Sep 17 00:00:00 2001
From: Lu Yahan <yahan@iscas.ac.cn>
Date: Thu, 23 Jun 2022 10:12:21 +0800
Subject: [PATCH] support clang

---
 build_config.h           |  5 +++++
 config/compiler/BUILD.gn | 26 +++++++++++++++++++++-----
 config/riscv.gni         | 18 ++++++++++++++++++
 toolchain/linux/BUILD.gn | 35 +++++++++++++++++++++++++++++++++++
 4 files changed, 79 insertions(+), 5 deletions(-)
 create mode 100644 config/riscv.gni

diff --git a/build_config.h b/build_config.h
index a993e1e44..f1d852c74 100644
--- a/build_config.h
+++ b/build_config.h
@@ -341,6 +341,11 @@
 #define ARCH_CPU_RISCV64 1
 #define ARCH_CPU_64_BITS 1
 #define ARCH_CPU_LITTLE_ENDIAN 1
+#elif defined(__riscv) && (__riscv_xlen == 32)
+#define ARCH_CPU_RISCV_FAMILY 1
+#define ARCH_CPU_RISCV32 1
+#define ARCH_CPU_32_BITS 1
+#define ARCH_CPU_LITTLE_ENDIAN 1
 #else
 #error Please add support for your architecture in build/build_config.h
 #endif
diff --git a/config/compiler/BUILD.gn b/config/compiler/BUILD.gn
index 676efeddd..471507e3a 100644
--- a/config/compiler/BUILD.gn
+++ b/config/compiler/BUILD.gn
@@ -30,6 +30,9 @@ if (current_cpu == "mipsel" || current_cpu == "mips64el" ||
     current_cpu == "mips" || current_cpu == "mips64") {
   import("//build/config/mips.gni")
 }
+if (current_cpu == "riscv32") {
+  import("//build/config/riscv.gni")
+}
 if (is_mac) {
   import("//build/config/apple/symbols.gni")
 }
@@ -935,6 +938,19 @@ config("compiler_cpu_abi") {
         # Outline atomics crash on Exynos 9810. http://crbug.com/1272795
         cflags += [ "-mno-outline-atomics" ]
       }
+    } else if (current_cpu == "riscv32") {
+      if (is_clang) {
+        cflags += [ "--target=riscv32-unknown-linux-gnu" ]
+        ldflags += [ "--target=riscv32-unknown-linux-gnu" ]
+        if (riscv_gcc_toolchain_path != "") {
+          cflags += [ "--gcc-toolchain=$riscv_gcc_toolchain_path" ]
+          ldflags += [ "--gcc-toolchain=$riscv_gcc_toolchain_path" ]
+        }
+        if (riscv_sysroot != "") {
+          cflags += [ "--sysroot=$riscv_sysroot" ]
+          ldflags += [ "--sysroot=$riscv_sysroot" ]
+        }
+      }
     } else if (current_cpu == "mipsel" && !is_nacl) {
       ldflags += [ "-Wl,--hash-style=sysv" ]
       if (custom_toolchain == "") {
@@ -1176,12 +1192,12 @@ config("compiler_cpu_abi") {
         ldflags += [ "-m64" ]
       }
     } else if (current_cpu == "riscv64") {
+      if(is_clang) {
+          cflags += [ "--target=mips64-linux-gnuabi64" ]
+          ldflags += [ "--target=mips64-linux-gnuabi64" ]
+      }
       cflags += [
-        "-mabi=lp64d",
-        "--target=riscv64-linux-gnu",
-      ]
-      ldflags += [
-        "--target=riscv64-linux-gnu",
+        "-mabi=lp64d"
       ]
     } else if (current_cpu == "s390x") {
       cflags += [ "-m64" ]
diff --git a/config/riscv.gni b/config/riscv.gni
new file mode 100644
index 000000000..9c3397d2a
--- /dev/null
+++ b/config/riscv.gni
@@ -0,0 +1,18 @@
+# Copyright 2022 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import("//build/config/v8_target_cpu.gni")
+
+# These are primarily relevant in current_cpu == "riscv*" contexts, where
+# RISCV code is being compiled.  But they can also be relevant in the
+# other contexts when the code will change its behavior based on the
+# cpu it wants to generate code for.
+declare_args() {
+    # riscv64 clang need the gcc toolchain to link
+    riscv_gcc_toolchain_path = ""
+    # riscv64 sysroot, clang may be need
+    riscv_sysroot = ""
+
+    use_relax = true
+}
\ No newline at end of file
diff --git a/toolchain/linux/BUILD.gn b/toolchain/linux/BUILD.gn
index e67f09f27..672271275 100644
--- a/toolchain/linux/BUILD.gn
+++ b/toolchain/linux/BUILD.gn
@@ -164,6 +164,13 @@ clang_v8_toolchain("clang_x64_v8_mips64") {
   }
 }

+clang_v8_toolchain("clang_x86_v8_riscv32") {
+  toolchain_args = {
+    current_cpu = "x86"
+    v8_current_cpu = "riscv32"
+  }
+}
+
 clang_v8_toolchain("clang_x64_v8_riscv64") {
   toolchain_args = {
     current_cpu = "x64"
@@ -301,6 +308,16 @@ clang_toolchain("clang_riscv64") {
   }
 }

+clang_toolchain("clang_riscv32") {
+  enable_linker_map = true
+  toolchain_args = {
+    current_cpu = "riscv32"
+    current_os = "linux"
+    is_clang = true
+  }
+}
+
+
 gcc_toolchain("riscv64") {
   toolprefix = "riscv64-linux-gnu"

@@ -319,6 +336,24 @@ gcc_toolchain("riscv64") {
   }
 }

+gcc_toolchain("riscv32") {
+  toolprefix = "riscv32-unknown-linux-gnu"
+
+  cc = "${toolprefix}-gcc"
+  cxx = "${toolprefix}-g++"
+
+  readelf = "${toolprefix}-readelf"
+  nm = "${toolprefix}-nm"
+  ar = "${toolprefix}-ar"
+  ld = cxx
+
+  toolchain_args = {
+    current_cpu = "riscv32"
+    current_os = "linux"
+    is_clang = false
+  }
+}
+
 clang_toolchain("clang_s390x") {
   toolchain_args = {
     current_cpu = "s390x"
-- 
2.25.1