The following issue has been SUBMITTED. ====================================================================== http://public.kitware.com/Bug/view.php?id=16026 ====================================================================== Reported By: Stephen Kelly Assigned To: ====================================================================== Project: CMake Issue ID: 16026 Category: (No Category) Reproducibility: have not tried Severity: minor Priority: normal Status: new ====================================================================== Date Submitted: 2016-03-20 19:35 CET Last Modified: 2016-03-20 19:35 CET ====================================================================== Summary: CMake provides no reliable variable for 'the file a function is declared in' Description:
Given $ cat cmake/thefunc.cmake set(somevar "${CMAKE_CURRENT_LIST_DIR}") function(thefunc) message("somevar: ${somevar}") message("listdir: ${CMAKE_CURRENT_LIST_DIR}") endfunction() $ cat dir1/CMakeLists.txt include(thefunc) thefunc() include(GenerateExportHeader) $ cat CMakeLists.txt cmake_minimum_required(VERSION 2.8.12) project(ttt CXX) list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) add_subdirectory(dir1) thefunc() if (COMMAND generate_export_header) add_library(foo foo.cpp) generate_export_header(foo) endif() the build output is somevar: /home/stephen/dev/src/playground/cmake/cmake listdir: /home/stephen/dev/src/playground/cmake/dir1 somevar: listdir: /home/stephen/dev/src/playground/cmake CMake Error: File /exportheader.cmake.in does not exist. CMake Error at /home/stephen/dev/prefix/qtbase/kde/share/cmake-3.5/Modules/GenerateExportHeader.cmake:362 (configure_file): configure_file Problem configuring file Call Stack (most recent call first): /home/stephen/dev/prefix/qtbase/kde/share/cmake-3.5/Modules/GenerateExportHeader.cmake:378 (_do_generate_export_header) CMakeLists.txt:14 (generate_export_header) -- Configuring incomplete, errors occurred! That is: CMake doesn't provide a way to determine the file a macro or function is defined in. The workaround is to determine that outside of the function/macro scope. However, that workaround breaks down because the variable and the function/macro do not follow the same scoping rules. The function/macro is available 'globally' after definition, so users can make the mistake of trying to use it in a scope which does not contain the `include()`. This affects at least the GenerateExportHeader module and perhaps other modules shipped with cmake. One workaround is to check the existence of the variable in the function: https://git.reviewboard.kde.org/r/127432/diff/1 Another would be to use a global property instead of a variable for the workaround. A better solution may be for cmake to provide the 'path of the file this code resides in' in a variable. ====================================================================== Issue History Date Modified Username Field Change ====================================================================== 2016-03-20 19:35 Stephen Kelly New Issue ====================================================================== -- 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-developers