How to pass the CMAKE_OSX_ARCHITECTURES with multiple values in the ExternalProject_Add's CMAKE_ARGS

May 2024 · 2 minute read

I cannot find out a way how to create a fat library of my CMake ExternalProjects. The merit of the problem is how one can pass the -DCMAKE_OSX_ARCHITECTURES:STRING="x86_64;arm64" to the CMAKE_ARGS within the ExternalProject_Add definition.

One would expect that the following definition will work flawlessly. However, it seems that the content of the CMAKE_ARGS is being parsed somehow and this results in the malformed value in the resulting CMakeCache.txt.

CMake Definition

ExternalProject_Add( ext_libexiv2 GIT_REPOSITORY https://github.com/Exiv2/exiv2.git GIT_TAG v0.27.6 GIT_SHALLOW true CMAKE_ARGS -DCMAKE_OSX_ARCHITECTURES:STRING="x86_64;arm64" -DBUILD_SHARED_LIBS=OFF -DEXIV2_BUILD_SAMPLES=OFF -DEXIV2_BUILD_EXIV2_COMMAND=OFF UPDATE_COMMAND "" ) 

CMakeCache.txt

//No help, variable specified on the command line. CMAKE_OSX_ARCHITECTURES:STRING="x86_64 

I tried to escape the double quotes, use single quotes, escape the semicolon, but none of that worked out.

Certainly, I could do the fat library in the multiple steps, but I would prefer the cleaner solution. I will appreciate any direction you give me.

5

2 Answers

The solution is to use the LIST_SEPARATOR option in the ExternalProject_Add definition.

ExternalProject_Add( ext_libexiv2 GIT_REPOSITORY https://github.com/Exiv2/exiv2.git GIT_TAG v0.27.6 GIT_SHALLOW true LIST_SEPARATOR | CMAKE_ARGS -DCMAKE_OSX_ARCHITECTURES:STRING=x86_64|arm64 -DBUILD_SHARED_LIBS=OFF -DEXIV2_BUILD_SAMPLES=OFF -DEXIV2_BUILD_EXIV2_COMMAND=OFF UPDATE_COMMAND "" ) 

Then, the resulting cache value is the correct one.

//No help, variable specified on the command line. CMAKE_OSX_ARCHITECTURES:STRING=x86_64;arm64 

Resulting libraries are really fat now.

➜ lib git:(prebuilt-components-replacement) ✗ lipo -info *.a Architectures in the fat file: libexiv2-xmp.a are: x86_64 arm64 Architectures in the fat file: libexiv2.a are: x86_64 arm64 
1

The issue goes away if you switch to using CMAKE_CACHE_ARGS instead of CMAKE_ARGS. Like CMAKE_CACHE_ARGS "-DCMAKE_OSX_ARCHITECTURES:STRING=x86_64;arm64". From the docs:

This is an alternate way of specifying cache variables where command line length issues may become a problem. The arguments are expected to be in the form -Dvar:STRING=value, which are then transformed into CMake set() commands with the FORCE option used. These set() commands are written to a pre-load script which is then applied using the cmake -C command line option.

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJobm5wZmV9coCOoaawZaSkerGt0qxkraCVYrCurcqeZKirqGKus6%2FHoquem6Sqv6a%2FjLCgraBdosKtwMipo55lppa5trHSZqCnZaSdsm6x162cq6aRob2zu8k%3D