README not packaged in tar.gz or .pkg file in CPack_install_scripts

Somewhat related to - CPack script not recognizing CMAKE_BINARY_DIR

These are some steps in my CPack configuration file -

....
configure_file(README.md README.md COPYONLY)
set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_BINARY_DIR}/README.md")
set(CPACK_RESOURCE_FILE_README "${CMAKE_BINARY_DIR}/README.md")
configure_file(${CMAKE_SOURCE_DIR}/cmake/modules/CPackfoo.cmake.in ${CMAKE_SOURCE_DIR}/cmake/modules/CPackfoo.cmake @ONLY)
set(CPACK_INSTALL_SCRIPTS ${CMAKE_SOURCE_DIR}/cmake/modules/CPackfoo.cmake)
....

And these are some steps inside CPackfoo.cmake.in -

....
if (APPLE)
  ....
  execute_process(COMMAND textutil -convert html README.md -output "@CMAKE_BINARY_DIR@/README.html")
  set(CPACK_PACKAGE_DESCRIPTION_FILE "@CMAKE_BINARY_DIR@/README.html") # Trying to overwrite the variable
  set(CPACK_RESOURCE_FILE_README "@CMAKE_BINARY_DIR@/README.html") # Trying to overwrite the variable
  ....
endif()
....

But when packaging for APPLE, the README.html file is not present either in the packaged tarball or the .pkg file.
I think the variables set in CPackfoo.cmake are not carried over to the original CPack configuration file.
How could I fix this?

Try using absolute paths for any files you reference, a relative path is fairly ambiguous when tools that use those paths could be running in different directories other than the source directory. This may require you to wire through variables like CMAKE_BINARY_DIR as mentioned by @ben.boeckel in another forum discussion thread.

Ah, sorry I am using absolute paths, but it seems I removed a lot of important detail when trying to produce a minimal example.

I have updated the top post.

Could you please tell me what I could be doing wrong?

Pretty sure the if(APPLE) expression will always evaluate to false inside the CPackfoo.cmake file. The APPLE variable is defined at configure time, but I don’t think it is defined when cpack is running.

I think the APPLE check works, because this - execute_process(COMMAND textutil -convert html README.md -output "@CMAKE_BINARY_DIR@/README.html") is correctly executed and I am able to see a README.html in ${CMAKE_BINARY_DIR}.

What package generator are you using? Note that the docs for CPACK_RESOURCE_FILE_README mention that not all CPack generators support it.

I suggest that you add some message(...) commands around the commands in CPackfoo.cmake.in to print out values you are expecting to be set. Run cpack with the -V option for more verbose output. This should confirm that things are set and executed how and when you expected.