[CMake] Force out of source build

Mathieu Malaterre mathieu.malaterre at gmail.com
Fri Jun 6 10:25:05 EDT 2008


On Fri, Jun 6, 2008 at 4:18 PM, Horacio Sanson <hsanson at gmail.com> wrote:
> Thanks,
>
> This is what I have now
>
>
> MACRO(MACRO_ENSURE_OUT_OF_SOURCE_BUILD)
>    STRING(COMPARE EQUAL "${${PROJECT_NAME}_SOURCE_DIR}"
> "${${PROJECT_NAME}_BINARY_DIR}" insource)
>    GET_FILENAME_COMPONENT(PARENTDIR ${${PROJECT_NAME}_SOURCE_DIR} PATH)
>    STRING(COMPARE EQUAL "${${PROJECT_NAME}_SOURCE_DIR}"
> "${PARENTDIR}" insourcesubdir)
>    IF(insource OR insourcesubdir)
>        MESSAGE(FATAL_ERROR "${PROJECT_NAME} requires an out of source build.")
>    ENDIF(insource OR insourcesubdir)
> ENDMACRO(MACRO_ENSURE_OUT_OF_SOURCE_BUILD)
>
>
> Then I simply call MACRO_ENSURE_OUT_OF_SOURCE_BUILD() just after I
> define the project name with PROJECT().
>
> I only have a minor problem that this macro still creates a
> CMakeCache.txt file and a CMakeFiles directory. How would be the best
> way to avoid these to be created or to delete them (in a portable way)
> from the source directory??


Then you should avoid the call to  GET_FILENAME_COMPONENT and do something like:

STRING(LENGTH src len_src)
STRING(SUBSTRING bin 0 ${len_src} my_bin)
STRING(COMPARE EQUAL src my_bin out)

On one hand, this should even work when someone create a sub-sub dir
of source and start cmake from there. But on the other hand this is
not robust to symlink and UNIX vs WIN32 path (/ vs \ separator).

HTH

-- 
Mathieu


More information about the CMake mailing list