clang target option for riscv64 doesn't work - clang

I'm trying to use clang to make IR for riscv64.
When I use llc --version, it shows many targets including riscv64.
But when I use the following command:
clang -target riscv64 hello.c
clang -target riscv64-unknown-linux hello.c
It shows clang-4.0: error: unknown target triple 'riscv64-unknown-linux', please use -triple or -arch
I'm not using ucb-bar's llvm for riscv. I'm using riscv from upstream of llvm.org.
Did I do something wrong or do I have to do something before building llvm?

Try elf instead of linux.
clang -target riscv32-unknown-elf
clang -target riscv64-unknown-elf
Edit: I created this repo for everyone interested in using RISC-V with LLVM.

Related

Build C Library from makefile for ios and not macos

I have a makefile that builds some C files and if I run it on an M1 mac the resulting library has the architecture arm64 which I thought it what is necessary for them to compile with an Xcode project for iOS. I discovered I can run the command otool -l libf2c.a | grep platform which should tell me what it was compiled for and in my case it returns platform1 which indicates macOS. Based on this, I think I need a value of platform2 for iOS.
The reason this is an issue is because in Xcode I get the error ld: building for iOS, but linking in object file built for macOS, file '/Users/e.../close.o' for architecture arm64.
Based on what I have been researching it seems iOS and macOS have the same architecture (arm64) but are a different 'platform'? But, I am not sure how the platform is determined. Is there some setting in my makefile I need to specify the platform? I am assuming that if I am able to get the platform to be iOS then Xcode will cooperate and be able to build the library I have generated.
The preferred way to compile for iOS via command line would probably be to use the xcrun command. This will allow you to specify the correct SDK for the platform you actually want to run on. For example:
prompt$ xcrun --sdk iphoneos --toolchain iphoneos clang -c test.c -o test.o -arch arm64
prompt$ otool -v -l test.o | grep platform
platform IOS
TL;DR: change your compiler invocation from plain clang to xcrun --sdk iphoneos --toolchain iphoneos clang.

what does clang command option -lm mean?

I found someone says that -lm means linking to the math library.
I want to figure out the meaning by using build-in help in the terminal.
So I run the $ clang --help
I don't find any stuff related to -l or -m
How can I get the meaning of -lm with the self-help terminal?
The -l{name} flag tells the linker to link against lib{name}. So -lm links against libm, the c math library.
This isn't a flag to the clang compiler but passed to the linker, which is why you won't find it with clang --help or man clang.
If you run clang with the --verbose flag you will see the invocation of the linker (in my case /usr/bin/ld) as the last step.By running man ld or ld --help (or whichever linker your clang version is using) you will find the documentation.
Keep in mind that not all flags are passed to the linker in this way (-l is probably passed for compatibility with the gcc compiler as melpomene said). To make sure an option is passed, use the -Wl option of clang.

Adding run-time shared library search-path to executable at compile-time | clang | Ubuntu

An executable I'm compiling needs the rpath to a library file at runtime. Currently, I'm compiling the executable with,
clang -O3 -mllvm -polly -mllvm -polly-target=gpu vector_add.c -lGPURuntime -ldl
And then using either of the following methods to provide the rpath,
Adding it to LD_LIBRARY_PATH
Using patchelf --set-rpath $RPATH a.out
I need a method to indicate the rpath in the clang .... command itself.
I'm running clang5.0.0-svn(7cf8dd5ce168bed45b57e019149e33300c56f94b) and llvm-svn(85f508cd9dba8a982471d98c4f649fb0d63f3451) with ld.gold in Ubuntu 14.04 x86_64.
Thank You !
Use clang ... -Wl,-rpath,/path/to/run-time/library's/dir/. It's a gcc style option that works in clang too.

Clang crosscompiling an assembler file

I am trying to crosscompile a library for iOS on Mac OSX. I tried this with differnt libs and it worked perferkt. Now I am trying to compile an asm file that has some defines inside like this:
#if defined(__i386__)
and it does not work as it has the wrong defines set. I compile using this command:
clang -arch armv7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS10.0.sdk -c asm.S
Am I missing some flags here? Thanks in Advance!

Using custom built OpenCV for iOS on XCode produces ___sincos_stret undefined symbol

I'm trying to use a C++ static library into my iPhone app which uses a modified version of OpenCV for iOS and I'm stuck with this issue at linking time:
Undefined symbols for architecture armv7:
"___sincos_stret", referenced from:
cv::initInterTab2D(int, bool) in opencv2(imgwarp.o)
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I really do not understand what I'm missing, that function does not seem to be present in OpenCV and I do not find anything meaningful on the web; I'm wondering if it's in some mathematical library, but I haven't found anything yet.
I've compiled OpenCV with Clang and using the default libc++ library.
The libmylibrary.a and the OpenCV2 framework are correctly in the list of linked libraries.
I'm new to XCode, therefore I could have missed something trivial in the compilation of the static library and/or in the linking of it into my project.
I haven't changed the source code of that object as my changes were related to another part of the imgproc module of OpenCV, therefore I guess that this could have happen even using the default version.
Do you have any clues?
For fixing this problem with the Xcode 5 toolchain I specified the minimum supported iOS version as a compiler option to match the configuration in Xcode. For example:
-miphoneos-version-min=5.0
You can add this to the C and CXX flags in the makefile
CFLAGS += -miphoneos-version-min=5.0
CXXFLAGS += -miphoneos-version-min=5.0
For those who don't want to build with lower version of XCode, try changing python build script of OpenCV iOS. In build_framework.py, I added IPHONEOS_DEPLOYMENT_TARGET=6.0 in lines and rebuilt OpenCV for iOS.
os.system("xcodebuild -parallelizeTargets ARCHS=%s -jobs 8 -sdk %s -configuration Release -target ALL_BUILD" % (arch, target.lower()))
os.system("xcodebuild ARCHS=%s -sdk %s -configuration Release -target install install" % (arch, target.lower()))
to get
os.system("xcodebuild IPHONEOS_DEPLOYMENT_TARGET=6.0 -parallelizeTargets ARCHS=%s -jobs 8 -sdk %s -configuration Release -target ALL_BUILD" % (arch, target.lower()))
os.system("xcodebuild IPHONEOS_DEPLOYMENT_TARGET=6.0 ARCHS=%s -sdk %s -configuration Release -target install install" % (arch, target.lower()))
For me that fixed the issue. A nice read on the symbol __sincos_stret
TODO: Though that fixes the issue, in OpenCV.xcodeproj (in build folder) generated from the python script, it still has deployment target as iOS 7.0. There might be a cleaner way.
I ran into this problem after installing the XCode 5 developer preview and building OpenCV with the build_framework.py script. ___sincos_stret appears to be coming from using the new compiler version.
I fixed this problem by changing the path to the command-line tools.
In Terminal, verify the XCode command-line path:
xcode-select --print-path
If it prints a path inside XCode5-DP.app, then switch to the tools for Xcode 4:
xcode-select --switch /Applications/XCode.app/Contents/Developer
And rebuild the framework. Then try recompiling the project.
Per a quick search in support of Adam's question elsewhere, the symbol is defined in [path to SDK]/usr/lib/system/libsystem_m.dylib. Proof:
nm /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk/usr/lib/system/libsystem_m.dylib | grep sincos
Rather than sticking to old versions of the tools or SDK, just make sure you're linking against that.

Resources