[CMake] set CMAKE_INSTALL_PREFIX in CMakeLists

Michael Wild themiwi at gmail.com
Thu Dec 2 04:35:22 EST 2010


On 12/02/2010 10:18 AM, Marcel Loose wrote:
>>>> On 30-11-2010 at 18:48, in message
> <20101130174852.GC10257 at cryptio.net>, Tyler
> Roscoe <tyler at cryptio.net> wrote: 
>> On Thu, Nov 25, 2010 at 02:01:31PM +0100, Marcel Loose wrote:
>>>>>> On 24-11-2010 at 17:45, in message
>>> <20101124164507.GG23449 at cryptio.net>, Tyler
>>> Roscoe <tyler at cryptio.net> wrote: 
>>>> On Wed, Nov 24, 2010 at 12:11:56PM +0100, Micha Renner wrote:
>>>>>
>>>>>>   SET(CMAKE_INSTALL_PREFIX "/foo/bar" CACHE PATH "Foo install
>>> prefix")
>>>>>>
>>>>>> So, without the test to
>>> CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT,
>>>>>> and without the FORCE option.
>>>>>
>>>>> No, as I mentioned, there was an article of one the
>>> CMake-maintainers
>>>>> who recommended this.
>>>>
>>>> Micha is correct. CMAKE_INSTALL_PREFIX is set before your
>>> CMakeLists.txt
>>>> is processed, so the above will never do anything.
>>>>
>>>> tyler
>>>
>>> Well, I tested this before I posted my reply. It does work the way
> I
>>> describe it. Try it yourself.
>>
>> It doesn't work for me:
>>
>> [tylermr at tpb006:~/cmake-test-install-prefix]$ cmake --version
>> cmake version 2.8.3
>>
>> [tylermr at tpb006:~/cmake-test-install-prefix]$ cat CMakeLists.txt
>> cmake_minimum_required(VERSION 2.8)
>> project(p)
>>
>> set (CMAKE_INSTALL_PREFIX "foo" CACHE PATH "docstring")
>> message ("CMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}")
>>
>> [tylermr at tpb006:~/cmake-test-install-prefix]$ mkdir b && cd b &&
> cmake ..
>> -- The C compiler identification is GNU
>> -- The CXX compiler identification is GNU
>> -- Check for working C compiler: /usr/bin/gcc
>> -- Check for working C compiler: /usr/bin/gcc -- works
>> -- Detecting C compiler ABI info
>> -- Detecting C compiler ABI info - done
>> -- Check for working CXX compiler: /usr/bin/c++
>> -- Check for working CXX compiler: /usr/bin/c++ -- works
>> -- Detecting CXX compiler ABI info
>> -- Detecting CXX compiler ABI info - done
>> CMAKE_INSTALL_PREFIX = /usr/local
>> -- Configuring done
>> -- Generating done
>> -- Build files have been written to:
> /tpb006/tylermr/cmake-test-install-prefix/b
> 
> Hi Tyler,
> 
> I now see why it works for me and not for you. Maybe I didn't write it
> clearly, but I do SET(CMAKE_INSTALL_PREFIX ...) *before* the call to
> PROJECT(). You are right that it doesn't work when you do it after the
> PROJECT() call. Unfortunately, the solution with the IF-test doesn't
> work when you use it before the PROJECT() call, for obvious reasons I
> would say.
> 
> So, there are two solutions that work, but you have to choose carefully
> among them.
> 
> 1) Use this snippet *before* PROJECT(xxx):
>   SET(CMAKE_INSTALL_PREFIX <path> CACHE PATH <comment>)
> 
> 2) Use this snippet *after* PROJECT(xxx):
>   IF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
>     SET(CMAKE_INSTALL_PREFIX <path> CACHE PATH <comment> FORCE)
>   ENDIF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
> 
> Best regards,
> Marcel Loose.

Considering what David said, the first solution depends on the
implementation details of the PROJECT command and is very fragile since
it works "accidentally" for some versions of CMake. I don't consider it
to be an option at all. I would suggest to always use the second option.

My 2c.

Michael



More information about the CMake mailing list