cpack a source archive including a specific file in root

Hey, I am trying to explicitly introduce a cpack step to package a zip-archive with all my sources. I also want to include the root CMakeLists.txt file because the entire archive is supposed to be consumed via FetchContent from other projects.
Previously i had a custom build target that handled this but I wanted to make it explicit that this is a step a developer may want to perform. For reference, this is how I did it before:

set(PACKAGE_ZIP ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-src.zip)
set(package_files MyLibrary/ CMakeLists.txt)
add_custom_target(${PROJECT_NAME}_package ALL
	COMMAND ${CMAKE_COMMAND} -E rm -f ${PACKAGE_ZIP}
	COMMAND ${CMAKE_COMMAND} -E tar c ${PACKAGE_ZIP} --format=zip -- ${package_files}
	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
	COMMENT "Removing old zip and creating new ${PACKAGE_ZIP}"
)

And this is how i tried to replace it:

set(CPACK_SOURCE_PACKAGE_FILE_NAME "${PROJECT_NAME}-src-${PROJECT_VERSION}")
set(CPACK_VERBATIM_VARIABLES YES)
set(CPACK_SOURCE_GENERATOR "ZIP")
set(CPACK_OVERWRITE 1)
set(CPACK_SOURCE_INSTALLED_DIRECTORIES
	"${CMAKE_CURRENT_SOURCE_DIR}/MyLibrary;MyLibrary"
)
set(CPACK_SOURCE_FILES
	"${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt"
)
set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY OFF)
include(CPack)

However, the root CMakeLists.txt is NOT included in the archive when i invoke it via this preset:

"packagePresets": [
	{
		"name": "default-package",
		"configurePreset": "default",
		"configFile": "CPackSourceConfig.cmake",
		"generators": [ "ZIP" ]
	}
],

How can I make the cpack call include the root CMakeLists.txt file in the zip-archive?

Could it be because of CMAKE_CURRENT_SOURCE_DIR tripping you up?

Does it function if, lets say you instead used CMAKE_SOURCE_DIR or PROJECT_SOURCE_DIR?

I’d suggest turning up the verbosity of CMake to see what it’s doing to help narrow down the issue

1 Like

Thanks for the suggestion, I saw i made a mistake because CPACK_SOURCE_FILES does not exist, but I found no other way in the documentation to include a single file for cpack. I also tried --verbose and --debug output with this CPACK_SOURCE_INSTALLED_DIRECTORIES:

set(CPACK_SOURCE_INSTALLED_DIRECTORIES
	"${CMAKE_CURRENT_SOURCE_DIR}/my_library;my_library"
	"${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt;."
)

and while i can see that the single file appears in the logs “CPack: /builds/cmake ci/Source/CPack/cmCPackGenerator.cxx:378 - Install directory: /home/user/Repositories/MyProject/CMakeLists.txt” it is not contained in the resulting zip.