[CMake] CMAKE_INCLUDE_CURRENT_DIR on a function

Craig Scott craig.scott at crascit.com
Tue Oct 25 07:50:16 EDT 2016


Thinking about this some more, I suspect Petr's comments may be on the
right track. What matters is the value of this variable in the scope of the
*directory* being processed. You need the CMAKE_INCLUDE_CURRENT_DIR
variable to be set in that directory scope, if I'm understanding the docs
correctly. If you create an executable target inside the function, I would
hazard a guess that it is that directory scope's variable that will be
consulted to determine whether to include the current source and binary
dirs in the include path.

To be honest, when I've used the automoc feature in the past, I've always
set this variable directly in the CMakeLists.txt file where I want it to
apply, never inside a function. This has always Just Worked for me. If you
really want to get those two paths included in the search path without
having to rely on CMAKE_INCLUDE_CURRENT_DIR being set in the directory
scope, you can always just add them manually to your target inside the
function. For example:

function(AddTest)
    ...
    add_executable(${FILE_RAW} ${TEST_FILE})
    target_include_directories(${FILE_RAW} PRIVATE
        "${CMAKE_CURRENT_BINARY_DIR}"
        "${CMAKE_CURRENT_SOURCE_DIR}"
    )
    ...
endfunction()

Pretty sure that would work and it avoids the whole question of where
CMAKE_INCLUDE_CURRENT_DIR needs to be set. One could also argue that it
makes your function more self-contained with fewer side effects.


On Tue, Oct 25, 2016 at 4:20 PM, Petr Kmoch <petr.kmoch at gmail.com> wrote:

> Hi Tiago.
>
> Yes, Craig's original comment applies. Targets do not have scope,
> variables do. Because you're in a function, you'd need to set the variable
> using PARENT_SCOPE to have it apply outside the function:
>
> function(AddTest)
>   #...
>   set(CMAKE_INCLUDE_CURRENT_DIR ON PARENT_SCOPE)
>   #...
> endfunction()
>
> Note that this will only help if the funciton is called directly; if
> called from another function, it will fail again (since the variable would
> just be set in the calling function's scope and not at global level).
>
> However, taking a step back, I believe setting the variable doesn't belong
> into the `AddTest` function at all. Looking at it, it seems to be concerned
> with creating and setting up one target. IMO, such a function should not
> also modify global state. Do not forget that CMAKE_INCLUDE_CURRENT_DIR is
> not target-specific in any way; it affects *all* targets in the current
> directory.
>
> Therefore, my suggestion would be to move setting it out of the function
> altogether and perform it at CMakeList scope. Alternatively, put the
> function's declaration into a separate CMake file, along with the set()
> command. Then, whoever wants to use the function has to include() that
> file, which will also cause them to have CMAKE_INCLUDE_CURRENT_DIR set
> accordingly.
>
> Petr
>
>
> On 25 October 2016 at 00:55, Tiago Macarios <tiagomacarios at gmail.com>
> wrote:
>
>> Hi Craig,
>>
>> Maybe my problem description was lacking. Below is the function I have.
>> Both CMAKE_INCLUDE_CURRENT_DIR and the target are defined on the same
>> function scope, but this does not seem to work. I need to define
>> CMAKE_INCLUDE_CURRENT_DIR on the parent CMakeLists file.
>>
>> function(AddTest)
>>     set(options)
>>     set(oneValueArgs FILE FOLDER)
>>     set(multiValueArgs LIBRARIES)
>>     cmake_parse_arguments(TEST
>>         "${options}"
>>         "${oneValueArgs}"
>>         "${multiValueArgs}"
>>         ${ARGN}
>>     )
>>
>>     # THIS DOES NOT WORK HERE I NEED TO SET IT IN THE PARENT FOLDER
>>     set(CMAKE_INCLUDE_CURRENT_DIR ON)
>>
>>     get_filename_component(FILE_RAW ${TEST_FILE} NAME_WE)
>>     add_executable(${FILE_RAW} ${TEST_FILE})
>>
>>     set_target_properties(${FILE_RAW}
>>         PROPERTIES
>>         CXX_STANDARD 14
>>         CXX_EXTENSIONS OFF
>>         AUTOMOC ON
>>         AUTOUIC ON
>>         FOLDER ${TEST_FOLDER}
>>     )
>>
>>     find_package(Qt5Test)
>>     target_link_libraries(${FILE_RAW} ${TEST_LIBRARIES})
>>
>>     add_test(NAME ${FILE_RAW} COMMAND ${FILE_RAW})
>> endfunction()
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> On Mon, Oct 24, 2016 at 3:48 PM, Craig Scott <craig.scott at crascit.com>
>> wrote:
>>
>>> function() introduces a new scope, so if you want changes you make to
>>> variables inside the function to be visible outside the function, you need
>>> to use set(... PARENT_SCOPE). Alternatively, a macro() does not introduce a
>>> new scope, so replacing your function() with a macro() may also yield the
>>> behaviour you want (but changing to a macro has other effects, so make sure
>>> you read the docs before going down that path). Also note that setting it
>>> in one directory does not make it apply to subdirectories as well, in case
>>> that matters in your situation.
>>>
>>>
>>>
>>> On Tue, Oct 25, 2016 at 9:42 AM, Tiago Macarios <tiagomacarios at gmail.com
>>> > wrote:
>>>
>>>> Hi,
>>>>
>>>> Does CMAKE_INCLUDE_CURRENT_DIR need to be set outside of a function?
>>>>
>>>> I have a function where I define an executable "add_executable". This
>>>> executable uses moc'ed Qt clasees, so I need to set
>>>> CMAKE_INCLUDE_CURRENT_DIR. It seems like I have to set it from the top
>>>> level script calling the function. If I set it inside the function the
>>>> compilation fails with a missing moc file.
>>>>
>>>> Any ideas?
>>>>
>>>> Tiago
>>>>
>>>> --
>>>>
>>>> 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
>>>>
>>>
>>>
>>>
>>> --
>>> Craig Scott
>>> Melbourne, Australia
>>> https://crascit.com
>>>
>>
>>
>> --
>>
>> 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
>>
>
>


-- 
Craig Scott
Melbourne, Australia
https://crascit.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20161025/c181cc80/attachment-0001.html>


More information about the CMake mailing list