[CMake] ExternalProject_Add contributing to top-level Package

Rob McDonald rob.a.mcdonald at gmail.com
Sat Apr 4 15:57:32 EDT 2015


All,

I have a project that is typically compiled with CLang/LLVM.  However,
I'm now adding a component that is preferentially built with OpenMP.
CLang doesn't support OpenMP, so I'd like to build that component with
GCC while still building everything else with CLang.

So far, I'm approaching the problem as shown in the below
CMakeLists.txt.  The program understands -DUSE_OPENMP, so if OpenMP
isn't found, it will build a uni-processor version.  The user can
optionally pass -DC_OMP_COMPILER and -DCXX_OMP_COMPILER when CMake is
run.  Those will specify alternate OpenMP capable compilers.  The
EP_BUILD variable prevents infinite recursion.

So, in the case where the primary compiler does not support OpenMP,
and the user specifies an alternate compiler, this will use
ExternalProject_Add to establish a new build environment with the
alternate compiler, and build that.  So far, so good.

The problem I have is that I would like the executable built by
ExternalProject to be added to the top-level project's package.  So,
instead of the recursion protected INSTALL at the bottom, I want a
roughly equivalent INSTALL command that specifically works in the
ExternalProject case.

Best,

Rob

#####################################
FIND_PACKAGE( OpenMP )

if(OPENMP_FOUND)
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS} -DUSE_OPENMP")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS} -DUSE_OPENMP")
  set(BUILD_IT true)
else()

if( CXX_OMP_COMPILER AND NOT EP_BUILD )

  INCLUDE( ExternalProject )

  set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
  INCLUDE( ExternalProject_ForceBuild )

  ExternalProject_Add( ALTBUILD
    URL ${CMAKE_CURRENT_SOURCE_DIR}
    CMAKE_ARGS -DCMAKE_C_COMPILER=${C_OMP_COMPILER}
        -DCMAKE_CXX_COMPILER=${CXX_OMP_COMPILER}
        -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
        -DEP_BUILD=TRUE
    INSTALL_COMMAND ""
    )
    EP_ForceBuild( ALTBUILD )

  else()
    set(BUILD_IT true)
  endif()
endif()

if(BUILD_IT)

  ADD_EXECUTABLE( myprogram
  main.cpp
  )

  TARGET_LINK_LIBRARIES( myprogram
  )

  if ( NOT EP_BUILD )
    INSTALL( TARGETS myprogram RUNTIME DESTINATION . )
  endif()

endif()


More information about the CMake mailing list