Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended ways to use CMake with icc via configuration options?

Tags:

build

cmake

icc

I would like to use the Intel compiler icc (or icpc) with a CMake-based project (on Linux for what it's worth). I can of course export the CXX variable when calling cmake, e.g. like

CXX=icpc cmake ../

and this works fine. I would however like to make this choice available via a custom option. For this I parse custom option, e.g.

cmake -DMY_COMPILER_OPTION=Intel ..

as

IF (MY_COMPILER_OPTION STREQUAL "Intel")
  MESSAGE(STATUS "** Compiling with Intel settings **")
  SET(CMAKE_CXX_COMPILER "icpc")
  SET(CMAKE_CXX_FLAGS_RELEASE "-O3 -w")
  SET(CMAKE_CXX_FLAGS_DEBUG "-g")
ENDIF ()

and set CMAKE_CXX_COMPILER together with some compiler flags. This also works, however there is an important "but".

I would also like to use the option -ipo (interprocedural optimization) for my code when compiling with icc plus I need to compile a static library within the build process. For this to work, I need to use Intel's xiar (and also xilink I guess).

cmake actually offers a special property for this

set_property(TARGET mytarget PROPERTY INTERPROCEDURAL_OPTIMIZATION 1)

however this only seems to works properly when the compiler has been set via the environment variable (then xiar is used). When setting the compiler via CMAKE_CXX_COMPILERthis property is ignored.

Is there another way to do this?. Some recommended way? Or at least a work-around?

like image 853
janitor048 Avatar asked Feb 03 '12 13:02

janitor048


3 Answers

The recommended way is to run cmake with the appropriate CMAKE_<LANG>_COMPILER defined.

Linux users

cmake -DCMAKE_C_COMPILER=icc -DCMAKE_CXX_COMPILER=icpc -DCMAKE_Fortran_COMPILER=ifort ..

Windows users

From an Intel Compiler console:

cmake -G "NMake Makefiles" -DCMAKE_C_COMPILER=icl -DCMAKE_CXX_COMPILER=icl -DCMAKE_Fortran_COMPILER=ifort ..
like image 135
Mike T Avatar answered Oct 20 '22 03:10

Mike T


Ok, since there have been no answers here, I've also turned to the CMake mailing list list for help on this issue. The consent of the experts there seems to be that the way I was trying to do this is a rather bad idea. The reason is that the parsing of my custom flags happens to late in the initialization process. One should therefore rely on setting the compiler via environment variables and let CMake do its magic during the initial configuration run. I will modify my approach..

like image 41
janitor048 Avatar answered Oct 20 '22 01:10

janitor048


Basically, avoid trying to change the compiler. See the FAQ: How do I use a different compiler?. CMake Toolchain files provide a way to support some complex cases, like an exotic HPC compiler.

like image 2
mabraham Avatar answered Oct 20 '22 01:10

mabraham