[CMake] How to use CMake with icc via configuration options when needing interprocedural optimization?

Michael Hertling mhertling at online.de
Thu Feb 9 04:07:01 EST 2012


On 02/07/2012 02:43 PM, janitor 048 wrote:
> Hello,
> 
> this is a question I recently asked on stackoverflow (
> http://stackoverflow.com/questions/9129233/recommended-ways-to-use-cmake-with-icc-via-configuration-options)
> but that has not received any response since then. Maybe this mailing list
> is a better place to ask... Here goes
> 
> 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_COMPILER this property is ignored.
> 
> Is there another way to do this?. Some recommended way? Or at least a
> work-around?

If it actually works well when the compiler is specified via the
respective environment variable, you might try the following:

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

However, note that you must do this *before* the language is enabled,
i.e. before the PROJECT() or ENABLE_LANGUAGE() commands. Note further
that this can be done only for the *initial* configuration of a build
tree; afterwards, the compiler can't be changed anymore. In order to
make that approach more robust, you might consider some refinements:

IF (MY_COMPILER_OPTION STREQUAL "Intel")
  FIND_PROGRAM(ICPC_PROGRAM icpc ...)
  IF(ICPC_PROGRAM)
    MESSAGE(STATUS "** Compiling with Intel settings **")
    IF(ENV{CXX})
      MESSAGE(WARNING "Overwriting CXX envvar")
    ENDIF()
    SET(ENV{CXX} "${ICPC_PROGRAM}")
    SET(CMAKE_CXX_FLAGS_RELEASE "-O3 -w")
    SET(CMAKE_CXX_FLAGS_DEBUG "-g")
  ELSE()
    MESSAGE(FATAL_ERROR "Intel compiler not found")
  ENDIF()
ENDIF ()

Regards,

Michael


More information about the CMake mailing list