DataDog/libddwaf

Add cross-compilation to arm64-darwin CI

lloeki opened this issue · 3 comments

This can be achieved on x86_64 macOS runners by adding -arch arm64 to compiler and linker flags.

Executing the tests is another matter but at least the builds would be automated.

Apparently there are dedicated cmake features to handle that:

  • CMAKE_SYSTEM_NAME (macOS? the other is iOS)
  • CMAKE_OSX_ARCHITECTURES (arm64 or x86_64, sets -arch flag)
  • CMAKE_OSX_DEPLOYMENT_TARGET (sets MACOSX_DEPLOYMENT_TARGET, e.g 10.15 or 11.0)
  • CMAKE_OSX_SYSROOT (sets -isysroot, otherwise computed from MACOSX_DEPLOYMENT_TARGET)

And one that apparently needs to be set in a toolchain file:

  • CMAKE_SYSTEM_PROCESSOR (arm64 or x86_64)

Refs:

Adding this:

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4360aeb..c81d311 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -3,6 +3,10 @@ file(READ "version" version)
 project(libddwaf VERSION ${version})
 
 set(CMAKE_OSX_DEPLOYMENT_TARGET "10.12" CACHE STRING "Minimum OS X deployment version")
+if(CMAKE_OSX_ARCHITECTURES MATCHES "x86_64" OR
+  CMAKE_OSX_ARCHITECTURES MATCHES "arm64")
+  set(CPU_TYPE ${CMAKE_OSX_ARCHITECTURES})
+endif()
 
 set(CMAKE_C_STANDARD 99)
 set(CMAKE_CXX_STANDARD 17)

and starting with:

cmake -E make_directory build packages
cd build
cmake -DCMAKE_OSX_ARCHITECTURES=arm64 -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=.. -DCPACK_PACKAGE_DIRECTORY=../packages ..
cmake --build . --config RelWithDebInfo --verbose --target all --target testPowerWAF -j

correctly produced:

/usr/bin/c++ -O3 -DNDEBUG -Wall -Wextra -Wno-narrowing -Wall -Wextra -Wno-narrowing -ggdb -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.0.sdk -dynamiclib -Wl,-headerpad_max_install_names -o libddwaf.dylib -install_name @rpath/libddwaf.dylib CMakeFiles/libddwaf_objects.dir/src/Clock.cpp.o CMakeFiles/libddwaf_objects.dir/src/parameter.cpp.o CMakeFiles/libddwaf_objects.dir/src/PowerWAF.cpp.o CMakeFiles/libddwaf_objects.dir/src/PowerWAFInterface.cpp.o CMakeFiles/libddwaf_objects.dir/src/PWAdditive.cpp.o CMakeFiles/libddwaf_objects.dir/src/object.cpp.o CMakeFiles/libddwaf_objects.dir/src/PWManifest.cpp.o CMakeFiles/libddwaf_objects.dir/src/PWProcessor.cpp.o CMakeFiles/libddwaf_objects.dir/src/PWRet.cpp.o CMakeFiles/libddwaf_objects.dir/src/PWRetriever.cpp.o CMakeFiles/libddwaf_objects.dir/src/PWRule.cpp.o CMakeFiles/libddwaf_objects.dir/src/PWRuleManager.cpp.o CMakeFiles/libddwaf_objects.dir/src/PWTransformer.cpp.o CMakeFiles/libddwaf_objects.dir/src/radixlib.c.o CMakeFiles/libddwaf_objects.dir/src/utils.cpp.o CMakeFiles/libddwaf_objects.dir/src/log.cpp.o CMakeFiles/libddwaf_objects.dir/src/rule_processor/IPWRuleProcessor.cpp.o CMakeFiles/libddwaf_objects.dir/src/rule_processor/perf_match.cpp.o CMakeFiles/libddwaf_objects.dir/src/rule_processor/re2.cpp.o  -Wl,-undefined,error third_party/lib/libre2.a -lpthread third_party/liblib_ac.a third_party/liblib_injection.a 

The problem is now that the dependencies did not get that flag, so they were built as x86_64, and thus the linking phase obviously failed, but we're on the right track.

Alright, #8 should now take care of it.