[cmake-developers] [CMake 0016026]: CMake provides no reliable variable for 'the file a function is declared in'

Mantis Bug Tracker mantis at public.kitware.com
Sun Mar 20 14:35:20 EDT 2016


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                                    
======================================================================



More information about the cmake-developers mailing list