<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/57581>57581</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [RISCV][Runtime] Error when build libc and compiler-rt for cross compler
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Incarnation-p-lee
      </td>
    </tr>
</table>

<pre>
    # Table of contents

- [Purpose](#purpose)
- [Environment of the Building System](#environment-of-the-building-system)
- [Build Options](#build-options)
  - [Build with GNU Tool Chain](#build-with-gnu-tool-chain)
  - [The `libc` error](#the-libc-error)
  - [The `compiler-rt` error](#the-compiler-rt-error)
- [Questions](#questions)
- [Reference](#reference)
  - [Hello World Source Code for Cross Compile Testing](#hello-world-source-code-for-cross-compile-testing)

## Purpose 

Recently I would like to build the cross clang compiler target `RISCV`. I am trying to build the cross-clang compiler, as well as the related runtime libraries, headers, and the linker of the llvm project.

## Environment of the Building System

* X86_64 ubuntu `22.04`.
* Clang version is `14.0.0`.
* Pre-installed `riscv64-linux-gnu-gcc` version is `11.0`.

## Build Options
### Build with GNU Tool Chain
For the first approach, it works well and I can compile the hello world c source. Here is the CMake options for the first approach.

```shell
cmake -G Ninja -DCMAKE_BUILD_TYPE="Release" \
    -DCMAKE_INSTALL_PREFIX=../__INSTALL__ \
    -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \
    -DCMAKE_BUILD_TYPE=Release \
    -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;lld" \
    -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind;libc;compiler-rt" \
    -DLLVM_TARGETS_TO_BUILD="RISCV" \
    -DLLVM_DEFAULT_TARGET_TRIPLE="riscv64-linux-gnu" \
    ../llvm
```

Unfortunately, the `libc` and the `compiler-rt` cannot be built out successfully with the below error. Excluded the `libc` and `compiler-rt` are able to build successfully, but we may need to involve the gnu headers and related libraries.
### The `libc` error

```
CMake Error at /home/pli/repos/riscv/0000/llvm-project/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake:83 (message):
  Failed to compile: Change Dir:
  /home/pli/repos/riscv/0000/llvm-project/__BUILD_ALL_WITH_COMILER_RT/runtimes/runtimes-bins/libc/check_/CMakeFiles/CMakeTmp




  Run Build Command(s):/usr/local/bin/ninja cmTC_00ed2 && [1/2] Building CXX
  object CMakeFiles/cmTC_00ed2.dir/check_cpu_features.cpp.o

  FAILED: CMakeFiles/cmTC_00ed2.dir/check_cpu_features.cpp.o


  /home/pli/repos/riscv/0000/llvm-project/__BUILD_ALL_WITH_COMILER_RT/./bin/clang++
  --target=riscv64-linux-gnu -fPIC -fno-semantic-interposition
  -fvisibility-inlines-hidden -Werror=date-time
  -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter
  -Wwrite-strings -Wcast-qual -Wmissing-field-initializers
  -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type
  -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override
  -Wno-comment -Wstring-conversion -Wmisleading-indentation
  -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections
  -fdata-sections -march=native -std=gnu++17 -MD -MT
  CMakeFiles/cmTC_00ed2.dir/check_cpu_features.cpp.o -MF
  CMakeFiles/cmTC_00ed2.dir/check_cpu_features.cpp.o.d -o
  CMakeFiles/cmTC_00ed2.dir/check_cpu_features.cpp.o -c
  /home/pli/repos/riscv/0000/llvm-project/__BUILD_ALL_WITH_COMILER_RT/runtimes/runtimes-bins/libc/cpu_features/check_cpu_features.cpp


  clang-16: error: invalid arch name 'native', string must begin with
  rv32{i,e,g} or rv64{i,g}

  ninja: build stopped: subcommand failed.



Call Stack (most recent call first):
  /home/pli/repos/riscv/0000/llvm-project/libc/CMakeLists.txt:108 (include)
```

### The `compiler-rt` error

```
FAILED: projects/compiler-rt/lib/builtins/CMakeFiles/clang_rt.builtins-riscv64.dir/riscv/save.S.o
/usr/bin/clang -DVISIBILITY_HIDDEN -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/pli/repos/riscv/0000/llvm-project/__BUILD_ALL_WITH_COMILER_RT/projects/compiler-rt/lib/builtins -I/home/pli/repos/riscv/0000/llvm-project/compiler-rt/lib/builtins -I/home/pli/repos/riscv/0000/llvm-project/__BUILD_ALL_WITH_COMILER_RT/include -I/home/pli/repos/riscv/0000/llvm-project/llvm/include -fPIC -O3 -DNDEBUG -fno-lto -std=c11 -MD -MT projects/compiler-rt/lib/builtins/CMakeFiles/clang_rt.builtins-riscv64.dir/riscv/save.S.o -MF projects/compiler-rt/lib/builtins/CMakeFiles/clang_rt.builtins-riscv64.dir/riscv/save.S.o.d -o projects/compiler-rt/lib/builtins/CMakeFiles/clang_rt.builtins-riscv64.dir/riscv/save.S.o -c /home/pli/repos/riscv/0000/llvm-project/compiler-rt/lib/builtins/riscv/save.S
/home/pli/repos/riscv/0000/llvm-project/compiler-rt/lib/builtins/riscv/save.S:185:3: error: "xlen must be 32 or 64 for save-restore implementation
# error "xlen must be 32 or 64 for save-restore implementation
```

## Questions

* Is there anything/steps missing to make it work?
* What is the support status of the RISCV `libc` and `compiler-rt` if it doesn't work?
* If there is a gap, what is the plan, and is there anything I can do to make it happen?

## Reference

### Hello World Source Code for Cross Compile Testing

```c
#include <stdio.h>

int main()
{
        printf("Hello, llvm for riscv!\n");
        return 0;
}
```



</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzFWFt34jgS_jXkRUc-YAJJHvJADHR7l1wWSKfniSNsGTRtJI8kh2R__VbJF2wgOzPdp3v6pG0j1VWq-qqktYrfbzt-nyzZOuVEJSRS0nJpTac77nRHxZOSzuDuKdeZMrwzGHf8a2DJyt_-TYNoIl-FVnIHElCY3XJyl4s0FnJDFu_G8l3N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