cmake add_custom_command + Xcode: multi output = multi command invocation

cross posting

I have custom command that generate several headers at once.
All works fine with make/ninja files generated by cmake.
But if I generate Xcode project via cmake -GXCode,
then instead of once gen.sh was invoked 10 times
and not only that, it also will be invoked every build,
even if timestamps of generated file are younger then gen_in.txt.

How can I fix this?

project(multi_output)
cmake_minimum_required(VERSION 3.17)

set(MANY_HEADERS test0.h test1.h  test2.h  test3.h  test4.h  test5.h  test6.h  test7.h  test8.h  test9.h)

add_custom_command(
  OUTPUT ${MANY_HEADERS}
  COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/gen.sh
  DEPENDS gen_in.txt
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  )

add_executable(foo main.cpp ${MANY_HEADERS})

where gen.sh is

#!/bin/sh

echo "gen.sh: start"
sleep 1
echo "gen.sh: hard work done"
for i in `seq 0 9`; do
    cat gen_in.txt > test$i.h
    sed -i bak s/placeholder/$i/g test$i.h
done

You have a working directory of the source directory, but I believe relative paths in OUTPUT are computed to be relative to the binary directory. I recommend using absolute paths for MANY_HEADERS items.