file ARCHIVE_CREATE uses relative paths

I’m trying to create some zip files with cmake. I’m either using the archive creation incorrectly or there’s a bug because the internal paths of my zip files are always relative paths.

With this directory structure:

:: tree
.
├── CMakeLists.txt
└── dir
    ├── a
    │   └── a.txt
    └── b
        └── b.txt

And this CMakeLists.txt file:

cmake_minimum_required(VERSION 3.21)

project(
  test
  LANGUAGES CXX
)

file(GLOB subdirectories LIST_DIRECTORIES true "${CMAKE_CURRENT_SOURCE_DIR}/dir/*")

foreach(subdirectory ${subdirectories})
    if(IS_DIRECTORY ${subdirectory})
        # Extract the directory name from the path
        get_filename_component(directory_name ${subdirectory} NAME)

        # set the output file name
        set(output_archive "${CMAKE_CURRENT_SOURCE_DIR}/${directory_name}.zip")

        message(STATUS "subdirectory: ${subdirectory}")

        # create the archive
        file(ARCHIVE_CREATE
          OUTPUT "${output_archive}"
          PATHS 
              "${subdirectory}"
          FORMAT "zip"
        )
        message(STATUS "Created archive: ${output_archive}")
    endif()
endforeach()

I end up with two zip files that cannot be unzipped without telling unzip to remove the relative paths, unless in the build directory. This is a problem since applications like Finder cannot open archives like this.

:: mkdir build && cd build

:: cmake ..
-- The CXX compiler identification is AppleClang 14.0.3.14030022
...
-- Build files have been written to: ...

:: ls ../
CMakeLists.txt a.zip          b.zip          build          dir

:: cd ..

:: zipinfo a.zip 
Archive:  a.zip
Zip file size: 369 bytes, number of entries: 2
drwxr-xr-x  2.0 unx        0 bx stor 23-Sep-19 16:41 ../dir/a/
-rw-r--r--  2.0 unx        3 bX defN 23-Sep-19 16:41 ../dir/a/a.txt
2 files, 3 bytes uncompressed, 5 bytes compressed:  -66.7%

:: zipinfo b.zip 
Archive:  b.zip
Zip file size: 369 bytes, number of entries: 2
drwxr-xr-x  2.0 unx        0 bx stor 23-Sep-19 16:41 ../dir/b/
-rw-r--r--  2.0 unx        3 bX defN 23-Sep-19 16:41 ../dir/b/b.txt
2 files, 3 bytes uncompressed, 5 bytes compressed:  -66.7%

:: unzip a.zip 
Archive:  a.zip
warning:  skipped "../" path component(s) in ../dir/a/
warning:  skipped "../" path component(s) in ../dir/a/a.txt
replace dir/a/a.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
  inflating: dir/a/a.txt 

:: cd build 

:: unzip ../a.zip 
Archive:  ../a.zip
warning:  skipped "../" path component(s) in ../dir/a/
   creating: dir/a/
warning:  skipped "../" path component(s) in ../dir/a/a.txt
  inflating: dir/a/a.txt             

:: ls
CMakeCache.txt      CMakeFiles          Makefile            cmake_install.cmake dir

The issue is that the archive is created with relative paths, as can be seen by the zipinfo output. How do I create the archive without these relative paths?

Since it may be a possible issue, I filed one.