Referencing file in CMake function shipped via *Config.cmake

Hi

I am planning to ship a convenience CMake function for developers as part of the *Config.cmake file.

That function depends on a python script.

  function(myfunction)
	...
	set(_command Python::Interpreter "WHAT TO PUT HERE"/myscript.py)
	...
	... use ${_command}

and where to place the myscript.py file.

Cheers

Here’s what I usually do:

set(_MyPackage_utils_dir "${CMAKE_CURRENT_FILE_DIR}")

function (MyPackage_some_api)
  if (NOT DEFINED _MyPackage_utils_dir)
    if (CMAKE_VERSION VERSION_LESS "3.17")
      message(FATAL_ERROR "Called outside of `find_package(MyPackage)` scope")
    endif ()
    set(_MyPackage_utils_dir "${CMAKE_CURRENT_FUNCTION_LIST_DIR}")
  endif ()
  …
endfunction ()

If you want to require that consumers of your package have at least 3.17, you can just assume that CMAKE_CURRENT_FUNCTION_LIST_DIR is available.

1 Like