1

I am looking to set various compilers for different folders in my project, which should compile to a shared library.

The project structure is as follows -

/Cuda
    a.cu
    b.cu
    c.cu
    header.cuh
/SYCL
    a.cpp
    b.cpp
    c.cpp
    header.h
main.cpp
test.cpp

All the files under the Cuda folder must be compiled by nvcc, and the files under the SYCL folder by a specific compiler that is present at a path in the system. All the files outside these folders (namely main.cpp and test.cpp) are normal C++ code and use the headers present in these two folders and must be compiled with GCC.

How do I go about writing the CMake for such a project structure(which aims to be a shared lib).

Edit - The project needn't have only one dedicated CMake. My approach was as follows -

  • Each Folder(Cuda and SYCL) can have their dedicated CmakeLists.txt which would specify the compiler and the various flags to go with it.
  • A master CMake outside the folder can use the add_subdirectory command. And this is where I get stuck, I am not sure what to do next, how to link these two folders with the main and the test files.
2
  • This post may answer your question. Put it shortly, it is not supported by CMake to use multiple compiler for the same language in the same CMake project. Commented Jun 27, 2021 at 8:01
  • Thank you for your reply @JérômeRichard. Though one CMake file might not have the functionality, I thought maybe each folder could contain a dedicated CMakeLists.txt and the "master" CMake can use the add_subdirectory command. And the dedicated Cmake can specify the compilers and everything else. Also, I suppose that answer is old. As this answer suggests (stackoverflow.com/questions/58861467/…), one can specify the compile language for certain files in the project. Commented Jun 27, 2021 at 8:12

1 Answer 1

2

CMake allows one compiler per language, so simply writing this is enough:

cmake_minimum_required(VERSION 3.20)
project(example LANGUAGES CXX CUDA)

add_subdirectory(Cuda)
add_subdirectory(SYCL)

You can separately set the C++ and CUDA compilers by setting CMAKE_CXX_COMPILER and CMAKE_CUDA_COMPILER at the configure command line.

$ cmake -S . -B build -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_CXX_COMPILER=g++ -DCMAKE_CUDA_COMPILER=nvcc

Also, I want to clear up this misconception:

Each Folder(Cuda and SYCL) can have their dedicated CmakeLists.txt which would specify the compiler and the various flags to go with it.

The CMakeLists.txt file should not attempt to specify the compiler. It's tricky to do correctly, can't always be done (especially in the add_subdirectory case) and unnecessarily restricts your ability to switch out the compiler. Maybe you have both GCC 10 and 11 installed and want to compare the two.

Similarly, you should not specify flags in the CMakeLists.txt file that aren't absolutely required to build, and you should always check the CMake documentation to see if the flags you're interested in have been abstracted for you. For instance, CMake has special handling of the C++ language standard (via target_compile_features) and CUDA separable compilation (via the CUDA_SEPARABLE_COMPILATION target property).

The best solution, as I have detailed here, is to set optional flags via the *_FLAGS* variables in a preset or toolchain.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for replying, I will try to proceed on those lines and post it as an answer so that the community could review and inform more about the best practices. Also, SYCL is essentially a C++ abstraction of OpenCL. So the third compiler in question is essentially a clang based compiler that compiles the SYCL code in a C++ file and generates SPIR. So I will probably expose the compiler to these files. Anyway, the linker language is still C++.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.