a80029f287f571e89fff23fc1fc1b09d8de42478
[alexxy/gromacs.git] / cmake / gmxManageNvccConfig.cmake
1 # Manage CUDA nvcc compilation configuration, try to be smart to ease the users' 
2 # pain as much as possible: 
3 # - use the CUDA_NVCC_HOST_COMPILER if defined by the user, otherwise
4 # - auto-detect compatible nvcc host compiler and set nvcc -ccbin (if not MPI wrapper)
5 # - set icc compatiblity mode to gcc 4.4 (CUDA 4.0 is not compatible with gcc >v4.4.x)
6 # - (advanced) variables set:
7 #   * CUDA_NVCC_HOST_COMPILER       - the compier nvcc is forced to use (via -ccbin)
8 #   * CUDA_NVCC_HOST_COMPILER_OPTIONS   - the full host-compiler related option list passed to nvcc
9 if (NOT DEFINED CUDA_NVCC_FLAGS_SET)
10     set(CUDA_NVCC_FLAGS_SET TRUE CACHE INTERNAL "True if NVCC flags have been set" FORCE)
11
12     # Set the host compiler for nvcc explicitly if the current cimpiler is
13     # supported, otherwise warn if the host compiler is not supported.
14     # Note that with MSVC nvcc sets the -compiler-bindir option behind the
15     # scenes; to avoid conflicts we shouldn't set -ccbin automatically.
16     if (NOT DEFINED CUDA_NVCC_HOST_COMPILER AND NOT MSVC)
17         if (NOT CMAKE_COMPILER_IS_GNUCC AND
18             NOT (CMAKE_C_COMPILER_ID MATCHES "Intel" AND UNIX AND NOT APPLE))
19             message(WARNING "
20             Will not set the nvcc host compiler because the current C compiler (ID: ${CMAKE_C_COMPILER_ID}): 
21             ${CMAKE_C_COMPILER}
22             is not compatible with nvcc. Compatible compilers are: gcc on Linux and Mac OS X,
23             Intel Compiler on 64-bit Linux and MSVC on Windows. nvcc will pick the platform
24             default; however, note that mixing compilers might lead to errors. 
25             To set the nvcc host compiler, edit CUDA_NVCC_FLAGS or re-configure with
26             CUDA_NVCC_HOST_COMPILER variable.")
27         else()
28             # the MPI wrappers might not work for compilation
29             if (GMX_MPI AND NOT GMX_THREAD_MPI)
30                 message(WARNING "
31             Will not set the nvcc host compiler because the current C compiler is an MPI 
32             compiler wrapper: ${CMAKE_C_COMPILER}
33             which is prone to not work with nvcc, but you might get lucky.
34             To set the nvcc host compiler, edit CUDA_NVCC_FLAGS or re-configure with 
35             CUDA_NVCC_HOST_COMPILER variable.")
36             else()
37                 set(CUDA_NVCC_HOST_COMPILER "${CMAKE_C_COMPILER}")
38                 set(CUDA_NVCC_HOST_COMPILER_AUTOSET TRUE CACHE INTERNAL
39                     "True if CUDA_NVCC_HOST_COMPILER is automatically set" FORCE)
40             endif()
41         endif()
42     endif()
43
44     if(DEFINED CUDA_NVCC_HOST_COMPILER)
45         message(STATUS "Setting the nvcc host compiler to: ${CUDA_NVCC_HOST_COMPILER}")
46         set(CUDA_NVCC_HOST_COMPILER ${CUDA_NVCC_HOST_COMPILER}
47             CACHE PATH "Host compiler for nvcc (do not edit!)" FORCE)
48         
49         set(CUDA_NVCC_HOST_COMPILER_OPTIONS "-ccbin=${CUDA_NVCC_HOST_COMPILER}")
50         # force icc in gcc 4.4 compatiblity mode on *NIX to make nvcc 3.2/4.0 happy
51         if (UNIX AND CMAKE_C_COMPILER_ID MATCHES "Intel" AND
52             CUDA_NVCC_HOST_COMPILER_AUTOSET)
53             message(STATUS "Setting Intel Compiler compatibity mode to gcc 4.4 for nvcc host compilation")
54             set(CUDA_NVCC_HOST_COMPILER_OPTIONS "${CUDA_NVCC_HOST_COMPILER_OPTIONS};-Xcompiler;-gcc-version=440;")
55         endif()
56         set(CUDA_NVCC_HOST_COMPILER_OPTIONS "${CUDA_NVCC_HOST_COMPILER_OPTIONS}"
57             CACHE STRING "Host-side compiler and options for nvcc (do not edit!)." FORCE)
58
59         mark_as_advanced(CUDA_NVCC_HOST_COMPILER CUDA_NVCC_HOST_COMPILER_OPTIONS)
60     endif()
61
62     # on Linux we need to add -fPIC when building shared gmx libs
63     # Note: will add -fPIC for any compiler that supports it as it shouldn't hurt
64     if(BUILD_SHARED_LIBS)
65         GMX_TEST_CXXFLAG(CXXFLAG_FPIC "-fPIC" _FPIC_NVCC_FLAG)
66         if(_FPIC_NVCC_FLAG)
67             set(_FPIC_NVCC_FLAG "-Xcompiler;${_FPIC_NVCC_FLAG};")
68         endif()
69     endif()
70
71     # set the CUDA architectures to compile for
72     # with CUDA >v4.2 compute capability 3.0 is not supported
73     if(CUDA_VERSION VERSION_LESS "4.2.0")
74         set(_CUDA_ARCH_STR "-gencode;arch=compute_20,code=sm_20;-gencode;arch=compute_20,code=sm_21;-gencode;arch=compute_20,code=compute_20")
75     else()
76         set(_CUDA_ARCH_STR "-gencode;arch=compute_20,code=sm_20;-gencode;arch=compute_20,code=sm_21;-gencode;arch=compute_30,code=sm_30;-gencode;arch=compute_30,code=compute_30")
77     endif()
78
79     # finally set the damn flags
80     set(CUDA_NVCC_FLAGS
81         "${_FPIC_NVCC_FLAG}${_CUDA_ARCH_STR};-use_fast_math;${CUDA_NVCC_HOST_COMPILER_OPTIONS}"
82         CACHE STRING "Compiler flags for nvcc." FORCE)
83 endif()