MantisBT - CMake
View Issue Details
0016026CMake(No Category)public2016-03-20 14:352016-06-10 14:31
Stephen Kelly 
Kitware Robot 
normalminorhave not tried
closedmoved 
 
 
0016026: CMake provides no reliable variable for 'the file a function is declared in'

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.
No tags attached.
Issue History
2016-03-20 14:35Stephen KellyNew Issue
2016-06-10 14:29Kitware RobotNote Added: 0042967
2016-06-10 14:29Kitware RobotStatusnew => resolved
2016-06-10 14:29Kitware RobotResolutionopen => moved
2016-06-10 14:29Kitware RobotAssigned To => Kitware Robot
2016-06-10 14:31Kitware RobotStatusresolved => closed

Notes
(0042967)
Kitware Robot   
2016-06-10 14:29   
Resolving issue as `moved`.

This issue tracker is no longer used. Further discussion of this issue may take place in the current CMake Issues page linked in the banner at the top of this page.