# CPack error copying files

**URL:** https://discourse.cmake.org/t/cpack-error-copying-files/15848
**Category:** Usage
**Tags:** os:windows
**Created:** [September 22, 2026, 10:22am UTC](https://discourse.cmake.org/t/cpack-error-copying-files/15848 "2026-09-22T10:22:42Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Wallby](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/w/4491bb/32.png) [@Wallby](https://discourse.cmake.org/u/Wallby)
#### Post date: [September 22, 2026, 10:22am UTC](https://discourse.cmake.org/t/cpack-error-copying-files/15848/1 "2026-09-22T10:22:42Z")

</div>

I have set up a .cmake file I intend to use as a template/framework. It is included at the beginning of the top level CMakeLists.txt. The actual project contains more than this (and targets not named example), but this is the relevant part.

```cmake
cmake_minimum_required(VERSION 4.4)

project(Example)

include(example_template.cmake)

add_executable(example example.cpp)

set(CPACK_PACKAGE_VENDOR "Example")
set(CPACK_PACKAGE_EXECUTABLES "example" "An example")
add_packages()

```

example\_template.cmake contains:

```cmake
include_guard(DIRECTORY)

function(add_packages)
    # Logic for getting executables from directory targets goes here.
    #...

    install(TARGETS ${executables} RUNTIME DESTINATION bin)
    set(CPACK_POST_BUILD_SCRIPTS "${CMAKE_CURRENT_SOURCE_DIR}/scripts/CopyArtifacts.cmake")
    include(CPack)

endfunction()

```

Then lastly, scripts/CopyArtifacts.cmake contains:

```cmake
list(GET CPACK_BUILD_SOURCE_DIRS 0 SOURCE_DIR)
file(GLOB packageFiles RELATIVE "${CPACK_PACKAGE_DIRECTORIES}" "${CPACK_PACKAGE_FILE_NAME}*")
execute_process(
    COMMAND ${CMAKE_COMMAND} -E make_directory "${SOURCE_DIR}/.artifacts"
    COMMAND ${CMAKE_COMMAND} -E copy "${packageFiles}" "${SOURCE_DIR}/.artifacts"
)

```

This used to work fine when the content of add\_packages was not a function but directly inside of CMakeLists.txt. Though to make it less cluttered, I did this. And now only the first time running `cmake --build .build --target package` an error occurs with output:

```auto
  Error copying file "" to "<path to project>/.artifacts": Invalid argument

```

I output the value of SOURCE\_DIR and packageFiles via message. And the value of SOURCE\_DIR is fine. But the value of packageFiles is empty the first time.

It seems that CPACK\_POST\_BUILD\_SCRIPTS is not running after the packaging is done. Is this a bug in CPack, or am I missing something else?

About the use of CPACK\_BUILD\_SOURCE\_DIRS on non RPM package formats. This was approved in the CMake GitLab: [https://gitlab.kitware.com/cmake/cmake/-/work\_items/21520#note\_1710398](https://gitlab.kitware.com/cmake/cmake/-/work_items/21520#note_1710398)

---

<div class="post-metadata">

### Author: ![Wallby](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/w/4491bb/32.png) [@Wallby](https://discourse.cmake.org/u/Wallby)
#### Post date: [September 28, 2026, 9:45am UTC](https://discourse.cmake.org/t/cpack-error-copying-files/15848/4 "2026-09-28T09:45:46Z")

</div>

I ended up using CPACK\_PACKAGE\_FILES in the artifact copy script. I hadn’t seen that that exists too. The file GLOB operation seems unpredictable still in a post CPack build script. But I didn’t require it anymore.
