Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cross-compilation to arm64-darwin CI #7

Closed
lloeki opened this issue Sep 21, 2021 · 3 comments · Fixed by #8
Closed

Add cross-compilation to arm64-darwin CI #7

lloeki opened this issue Sep 21, 2021 · 3 comments · Fixed by #8

Comments

@lloeki
Copy link
Collaborator

lloeki commented Sep 21, 2021

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.

@lloeki
Copy link
Collaborator Author

lloeki commented Sep 21, 2021

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:

@lloeki
Copy link
Collaborator Author

lloeki commented Sep 21, 2021

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.

@lloeki
Copy link
Collaborator Author

lloeki commented Sep 21, 2021

Alright, #8 should now take care of it.

@lloeki lloeki closed this as completed in #8 Sep 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant