Add Xcode settings to put the DerivedData dir in the project build directory
When building with a CMake-generated Xcode project, files get added to the user's ~/Library/Developer/Xcode/DerivedData
directory. I would expect all files produced by the build process be put in the CMake build directory.
In Xcode, this can be fixed by going to File > Project Settings... and change Derived Data to Project-relative Location. Doing this adds a WorkspaceSettings.xcsettings file to: CMAKE_BINARY_DIR/PROJECT_NAME.xcodeproj/project.xcworkspace/xcuserdata/USER.xcuserdatad/
- CMAKE_BINARY_DIR is the path to the CMake build directory
- PROJECT_NAME is the name of the Xcode project
- USER is the local username in macOS
Here's what the WorkspaceSettings.xcsettings file looks like:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BuildLocationStyle</key>
<string>UseTargetSettings</string>
<key>CustomBuildLocationType</key>
<string>RelativeToDerivedData</string>
<key>DerivedDataCustomLocation</key>
<string>DerivedData</string>
<key>DerivedDataLocationStyle</key>
<string>WorkspaceRelativePath</string>
<key>IssueFilterStyle</key>
<string>ShowActiveSchemeOnly</string>
<key>LiveSourceIssuesEnabled</key>
<true/>
</dict>
</plist>
So this fix can be applied to a user's project with the following CMake code:
if(CMAKE_GENERATOR STREQUAL Xcode)
configure_file(
WorkspaceSettings.xcsettings
${CMAKE_BINARY_DIR}/${PROJECT_NAME}.xcodeproj/project.xcworkspace/xcuserdata/$ENV{USER}.xcuserdatad/WorkspaceSettings.xcsettings
COPYONLY
)
endif()
However, there will still be files in ~/Library/Developer/Xcode/DerivedData with dirs named CMAKE_TRY_COMPILE, CompilerIdC, CompilerIdCXX from test projects that the user cannot control. It would clean up the binary footprint of CMake-generated Xcode projects if WorkspaceSettings.xcsettings were automatically set in CMake such that no files get deposited in this folder.