'How to add RiscV as a target for Clang, for cross-compiling on OSX?

I'm trying to compile to RiscV using clang and I see this answer as a useful starting point.

Started by checking if llvm supports RISCV, which it does

% llvm-objdump --version | grep riscv
    riscv32    - 32-bit RISC-V
    riscv64    - 64-bit RISC-V

So, I tried compiling in the same way but it fails:

$ clang --target=riscv64 -march=rv64gc rotate.s -c -o rotate.o

clang -cc1as: error: unknown target triple 'riscv64-unknown-unknown', please use -triple or -arch

Let's look for the target:

clang -print-targets     
  Registered Targets:
    aarch64    - AArch64 (little endian)
    aarch64_32 - AArch64 (little endian ILP32)
    aarch64_be - AArch64 (big endian)
    arm        - ARM
    arm64      - ARM64 (little endian)
    arm64_32   - ARM64 (little endian ILP32)
    armeb      - ARM (big endian)
    thumb      - Thumb
    thumbeb    - Thumb (big endian)
    x86        - 32-bit X86: Pentium-Pro and above
    x86-64     - 64-bit X86: EM64T and AMD64

So, I understand that RISCV is not registered as a target for clang. So, how do I add it?

Also, once I add the target, how do I get the right libc?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source