How to link against all available targets - problems with NVPTX?

As the question asks, I’m having some trouble using initializeAllTargets().

I have a CMakeLists that uses:

llvm_map_components_to_libnames(LLVM_LIBS …
AllTargetsAsmParsers
AllTargetsAsmPrinters
AllTargetsDescs
AllTargetsInfos
…)

However, even with these, when I try to compile my program, I get linker errors:

CMakeFiles/sxhc.dir/src/main.cpp.o: In function llvm::InitializeAllTargets()': /home/bollu/.local/include/llvm/Config/Targets.def:27: undefined reference to LLVMInitializeNVPTXTarget’
CMakeFiles/sxhc.dir/src/main.cpp.o: In function llvm::InitializeAllAsmPrinters()': /home/bollu/.local/include/llvm/Config/AsmPrinters.def:28: undefined reference to LLVMInitializeNVPTXAsmPrinter’
collect2: error: ld returned 1 exit status
CMakeFiles/sxhc.dir/build.make:285: recipe for target ‘sxhc’ failed
make[2]: *** [sxhc] Error 1
CMakeFiles/Makefile2:131: recipe for target ‘CMakeFiles/sxhc.dir/all’ failed
make[1]: *** [CMakeFiles/sxhc.dir/all] Error 2
Makefile:83: recipe for target ‘all’ failed
make: *** [all] Error 2

That is, NVPTX is not initialized as a target properly. I’m somewhat puzzled by this, because I would have assumed that the NVPTXAsmPrinter would have been linked by AllTargetsAsmPrinters.

The hack I have right now is to manually add NVPTXCodeGen to the list of llvm_map_components_to_libnames.

Clearly, I’m doing something wrong, could someone point me to what I might be screwing up? In short, what is the correct way to setup targets? Is what I’m asking for (automatically link against all targets picked up by initializeAllTargets() an anti-pattern? if so, why?)

Thanks,

~Siddharth

Hi Siddharth,

can you verify that LLVMNVPTXAsmPrinter is actually the missing component library and not LLVMNVPTXCodeGen? Because AllTargetsAsmPrinter should absolutely pull in the former, but I can see how the latter is missing.

That being said, if you want to just link to everything all the included targets require, you can do this:

llvm_map_components_to_libnames(LLVM_LIBS ${LLVM_TARGETS_TO_BUILD})

Cheers,
Philip