Ninja/ASM/Apple: Fat library fails to rebuild after assembly source file is updated
Issue
When ninja tries to build a fat library that contains an assembly source, it will fail to build on a consecutive run when the assembly source has been updated.
[2/2] : && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar cr libdylib1_10_5.a CMakeFiles/dylib1_10_5.dir/dyld_glue.S.o && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib libdylib1_10_5.a && :
FAILED: libdylib1_10_5.a
: && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar cr libdylib1_10_5.a CMakeFiles/dylib1_10_5.dir/dyld_glue.S.o && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib libdylib1_10_5.a && :
ar: libdylib1_10_5.a is a fat file (use libtool(1) or lipo(1) and ar(1) on it)
ar: libdylib1_10_5.a: Inappropriate file type or format
ninja: build stopped: subcommand failed.
From my testing, the following two conditions need to be met for this bug to occur:
- the object built must be universal binary (ex:
set(CMAKE_OSX_ARCHITECTURES "i386;x86_64")
). - The source file needs to be an assembly file.
This issue does not occur when using make
. This has been tested on macOS (this issue can also happen on Linux when cross-building macOS executables).
How to reproduce
-
Clone
https://github.com/apple-oss-distributions/Csu.git
. CheckoutCsu-88
. -
Add the following
CMakeLists.txt
file & renamedyld_glue.s
todyld_glue.S
:
cmake_minimum_required(VERSION 3.24.2)
project(csu)
enable_language(ASM)
set(CMAKE_OSX_ARCHITECTURES "i386;x86_64")
ADD_LIBRARY(dylib1_10_5 STATIC dyld_glue.S)
target_compile_options(dylib1_10_5
PRIVATE
-r
-Os
-mmacosx-version-min=10.5
-nostdlib
-keep_private_externs
-DCFM_GLUE
)
- Create a
build
folder and build the library as you normally would:
mkdir build
cd build
cmake .. -GNinja
ninja -v
- Use
touch
to simulate file being updated and do anotherninja
build:
touch ../dyld_glue.S
ninja -v
- You should get a build failure.
Version details
% sw_vers
ProductName: macOS
ProductVersion: 13.4
BuildVersion: 22F66
% cmake --version
cmake version 3.26.4
CMake suite maintained and supported by Kitware (kitware.com/cmake).