[CMake] What is the correct way to manage a possibly user-defined directory?

Cedric Doucet cedric.doucet at inria.fr
Mon May 18 06:05:07 EDT 2015


Ok, I found the error: 
IF(NOT EXISTS FOO_DIR) 
should be replaced by 
IF(NOT EXISTS ${FOO_DIR}) 

However, it seems I don't need to define an option. 
I just have to type 
cmake -D FOO_DIR=path .. 

Should I define an option? 
When does one need to define an option? 

Cédric 

----- Mail original -----

> De: "Cedric Doucet" <cedric.doucet at inria.fr>
> À: cmake at cmake.org
> Envoyé: Lundi 18 Mai 2015 11:48:52
> Objet: [CMake] What is the correct way to manage a possibly user-defined
> directory?

> Hello,

> I would like to let users choose between providing third party libraries or
> let CMake download them.
> To do that, I try to write a simple code like this to manage a third party
> library FOO of an executable MY_EXEC:
> =================================================
> IF(NOT EXISTS FOO_DIR)
> MESSAGE(STATUS "FOO LIBRARY WILL BE DOWNLOADED AND INSTALLED")
> # download and install FOO, define FOO_DIR
> ENDIF()
> INCLUDE_DIRECTORIES(MY_EXEC ${FOO_DIR}/include)
> TARGET_LINK_LIBRARIES(MY_EXEC ${FOO_DIR}/include)
> =================================================
> To do that, I think the user should have the possibility to define FOO_DIR
> with an option.
> So, I put the line
> OPTION(FOO_DIR)
> before the code below.

> However, it doesn't seem to work because FOO_DIR does not seem to be defined.
> The message "FOO LIBRARY WILL BE DOWNLOADED AND INSTALLED" always appears,
> - even if I type "cmake -D FOO_DIR=path", where path is a user-defined path
> to FOO library,
> - even if I put a default value to FOO_DIR with OPTION(FOO_DIR path), where
> path is a hard-coded path to FOO.

> Where does I make a mistake here?
> What is the correct way to manage FOO_DIR when it can be defined by a user?

> Note: the next step is to define FOO_DIR in a FindFoo.cmake file and use
> find_package(Foo), but it's not my goal yet.

> Thank you very much for your help!

> Cédric

> --

> Powered by www.kitware.com

> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ

> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:

> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html

> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html

> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20150518/23cc967d/attachment.html>


More information about the CMake mailing list