[CMake] External_Project_Add() and custom build configurations ?

David Cole david.cole at kitware.com
Tue Jul 5 12:13:05 EDT 2011


On Tue, Jul 5, 2011 at 12:02 PM, Glenn Coombs <glenn.coombs at gmail.com>wrote:

> This had me stumped for a while until I came across the -P command line
> option to cmake.  I have now added a build command to my external project
> like so:
>
>
> ExternalProject_Add(external_proj
>     PREFIX                ${CMAKE_BINARY_DIR}/external_proj
>     SOURCE_DIR            ${CMAKE_SOURCE_DIR}/external_proj
>     CMAKE_ARGS            -DCMAKE_BUILD_TYPE:STRING=Release
>                         -DMETAGS_DIR=${METASIM_INSTALL_DIR}
>     BUILD_COMMAND        ${CMAKE_COMMAND}
>                         -DBUILD_TARGET:STRING=external_proj
>                         -DBUILD_CONFIG:STRING=${CMAKE_CFG_INTDIR}
>                         -P ${CMAKE_SOURCE_DIR}/ExProjectBuild.cmake
>     INSTALL_COMMAND        ""
> )
>
> And then the ExProjectBuild.cmake file invokes cmake --build with the build
> config changed appropriately when necessary:
>
> message("In ExternalProjectBuild:")
> message("    BUILD_TARGET=${BUILD_TARGET}")
> message("    BUILD_CONFIG=${BUILD_CONFIG}")
> message("    CMAKE_BINARY_DIR=${CMAKE_BINARY_DIR}")
>
> set (standardConfigs    Release Debug RelWithDebInfo MinSizeRel)
>
> # map each build config in extraConfigs to the item in the same position in
> extraConfigsMap
> set (extraConfigs        ReleaseNoHidebug    DebugPthreads
> DebugOptimised)
> set (extraConfigsMap    Release                Debug            Debug)
>
> list(FIND standardConfigs ${BUILD_CONFIG} pos)
> if (pos LESS 0)
>     list(FIND extraConfigs ${BUILD_CONFIG} pos)
>     if (pos LESS 0)
>         message(FATAL_ERROR "Unknown build config: ${BUILD_CONFIG}")
>     endif()
>     list(GET extraConfigsMap ${pos} BUILD_CONFIG)
>     message("  MAPPED CONFIG: ${BUILD_CONFIG}")
> endif()
>
> execute_process(
>     COMMAND ${CMAKE_COMMAND}
>     --build ${CMAKE_BINARY_DIR} --config ${BUILD_CONFIG}
> )
>
> Is that what you meant when you suggested using a custom BUILD_COMMAND ?
> Or was there a simpler way to do it ?
>

Yes, that's what I meant. No, I can't think of a simpler/generic way to do
this. Only you can know the relationship between your customized configs and
the well-known configs...



>
> Also, I note that when I clean the external project in Visual Studio it
> doesn't really clean it (i.e. after the clean a build still thinks there is
> nothing to do).  And the clean doesn't invoke my ExProjectBuild script.
>
> If I manually run "cmake --build externalProjectDir --target clean" from
> the command line then it does clean the project properly.  Is
> ExternalProject_Add() missing some functionality here, or have I
> misunderstood something ?
>

It is (intentionally) missing some functionality. You have not
misunderstood.

The general case of ExternalProject is completely arbitrary, although we do
use reasonable default commands for cmake projects and configure/make
projects as they are conventionally used... Since it's completely arbitrary,
we do not know if there is a 'clean' target at each level.

The best 'clean' is starting with an absolutely empty build tree, and
running CMake and your build system from there.

Since the best 'clean' is always going to be better than any target-based
clean we could come up with, I wouldn't even attempt to add this
functionality unless nearly everybody unanimously agreed that we should have
it. And only then if somebody writes a patch to provide it.


HTH,
David




> --
> Glenn
>
>
>
> On 1 July 2011 18:01, David Cole <david.cole at kitware.com> wrote:
>
>> On Fri, Jul 1, 2011 at 7:23 AM, Glenn Coombs <glenn.coombs at gmail.com>wrote:
>>
>>> I have just started using some externally supplied cmake projects in my
>>> cmake project.  To do this I added these lines to my top level
>>> CMakeLists.txt file:
>>>
>>> include(ExternalProject)
>>>
>>> ExternalProject_Add(external_proj
>>>     PREFIX                ${CMAKE_BINARY_DIR}/external_proj
>>>     SOURCE_DIR            ${CMAKE_SOURCE_DIR}/external/proj/dir
>>>     CMAKE_ARGS            -DCMAKE_BUILD_TYPE:STRING=Release
>>>                           -DMETAGS_DIR=${CMAKE_BINARY_DIR}/meta_install
>>>     INSTALL_COMMAND        ""
>>> )
>>>
>>> And that worked very well.  Inside Visual Studio when I build my project
>>> it automatically runs the cmake configure step for the external projects and
>>> builds them as well.  The problem I have just discovered is when using a
>>> build configuration other than the default ones.  I can build Debug and
>>> Release configurations fine.  My project also has a DebugOptimised
>>> configuration but the external projects don't.  When I try to build my
>>> project it fails to build the external projects.  I used build_command() to
>>> see what commands would be used to build each configuration and I can see
>>> the cause of the problem:
>>>
>>> Debug:          VCExpress.exe myProj.sln /build Debug          /project
>>> external_proj
>>> DebugOptimised: VCExpress.exe myProj.sln /build DebugOptimised /project
>>> external_proj
>>>
>>> Is there a way to tell cmake how to map my build configurations onto
>>> those that an external project has ?  In this case I would like to build the
>>> external project as Debug when I am building my project as DebugOptimised.
>>>
>>> --
>>> Glenn
>>>
>>>
>>> _______________________________________________
>>> Powered by www.kitware.com
>>>
>>> Visit other Kitware open-source projects at
>>> http://www.kitware.com/opensource/opensource.html
>>>
>>> Please keep messages on-topic and check the CMake FAQ at:
>>> http://www.cmake.org/Wiki/CMake_FAQ
>>>
>>> Follow this link to subscribe/unsubscribe:
>>> http://www.cmake.org/mailman/listinfo/cmake
>>>
>>
>> With Visual Studio builds, external projects get built (by default) with
>> the same configuration as the outer project. If you want a different
>> configuration built, then you will have to customize the BUILD_COMMAND. You
>> can look in the source for ExternalProject.cmake to see how the default
>> BUILD_COMMAND is constructed and go from there.
>>
>> HTH,
>> David
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20110705/6cc27d34/attachment-0001.htm>


More information about the CMake mailing list