void CompileNDK(IEnumerable <string> files) { RefreshAndroidSdk(); var monoPath = ManagedToolchain.FindMonoPath(); var name = Path.GetFileNameWithoutExtension(Project.Assemblies[0]); var libName = $"lib{name}.so"; var ndkPath = AndroidSdk.AndroidNdkPath; //NOTE: "arm64-v8a" doesn't compile at the moment foreach (var abi in new[] { "armeabi", "armeabi-v7a", "x86", "x86_64" }) { string extra = string.Empty; AndroidTargetArch targetArch; switch (abi) { case "armeabi": targetArch = AndroidTargetArch.Arm; break; case "armeabi-v7a": targetArch = AndroidTargetArch.Arm; extra = " -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16"; break; case "arm64-v8a": targetArch = AndroidTargetArch.Arm64; break; case "x86": targetArch = AndroidTargetArch.X86; extra = " -march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32"; break; case "x86_64": targetArch = AndroidTargetArch.X86_64; extra = " -march=x86-64 -mtune=intel -msse4.2 -mpopcnt -m64"; break; default: throw new NotImplementedException(); } var clangBin = NdkUtil.GetNdkClangBin(Path.Combine(ndkPath, "toolchains"), targetArch); var systemInclude = NdkUtil.GetNdkPlatformIncludePath(ndkPath, targetArch, 24); //NOTE: 24 should be an option? var monoDroidPath = Path.Combine(MonoDroidSdk.BinPath, "..", "lib", "xbuild", "Xamarin", "Android", "lib", abi); var abiDir = Path.Combine(Options.OutputDir, "android", "jni", abi); var output = Path.Combine(abiDir, libName); if (!Directory.Exists(abiDir)) { Directory.CreateDirectory(abiDir); } var args = new List <string> { $"--sysroot=\"{systemInclude}\"{extra}", $"-D{DLLExportDefine}", $"-I\"{monoPath}/include/mono-2.0\"", $"-L\"{monoDroidPath}\" -lmonosgen-2.0", string.Join(" ", files.ToList()), "--std=c99", $"-shared -o {output}", }; var invocation = string.Join(" ", args); Invoke(clangBin, invocation); } }
bool CompileNDK(IEnumerable <string> files) { var monoPath = Path.Combine(MonoSdkPath, "include", "mono-2.0"); var name = Path.GetFileNameWithoutExtension(Project.Assemblies[0]); var libName = $"lib{name}.so"; var ndkPath = AndroidSdk.AndroidNdkPath; foreach (var abi in new[] { "armeabi", "armeabi-v7a", "arm64-v8a", "x86", "x86_64" }) { string extra = string.Empty; AndroidTargetArch targetArch; switch (abi) { case "armeabi": targetArch = AndroidTargetArch.Arm; break; case "armeabi-v7a": targetArch = AndroidTargetArch.Arm; extra = " -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16"; break; case "arm64-v8a": targetArch = AndroidTargetArch.Arm64; break; case "x86": targetArch = AndroidTargetArch.X86; extra = " -march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32"; break; case "x86_64": targetArch = AndroidTargetArch.X86_64; extra = " -march=x86-64 -mtune=intel -msse4.2 -mpopcnt -m64"; break; default: throw new NotImplementedException(); } var clangBin = NdkUtil.GetNdkClangBin(Path.Combine(ndkPath, "toolchains"), targetArch); var systemInclude = NdkUtil.GetNdkPlatformIncludePath(ndkPath, targetArch, XamarinAndroid.ApiLevel); var monoDroidPath = Path.Combine(XamarinAndroid.LibraryPath, abi); var abiDir = Path.Combine(Options.OutputDir, "android", "jni", abi); var outputPath = Path.Combine(abiDir, libName); if (!Directory.Exists(abiDir)) { Directory.CreateDirectory(abiDir); } var args = new List <string> { $"--sysroot=\"{systemInclude}\"{extra}", "-fdiagnostics-color", $"-D{DLLExportDefine}", $"-I\"{monoPath}\"", $"-L\"{monoDroidPath}\" -lmonosgen-2.0 -lmono-android.release", string.Join(" ", files.ToList()), "--std=c99", $"-shared -o {outputPath}", }; var invocation = string.Join(" ", args); var output = Invoke(clangBin, invocation); if (output.ExitCode != 0) { return(false); } } return(true); }
bool CompileNDK(IEnumerable <string> files) { var monoPath = Path.Combine(MonoSdkPath, "include", "mono-2.0"); var name = Path.GetFileNameWithoutExtension(Project.Assemblies[0]); var libName = $"lib{name}.so"; var ndkPath = XamarinAndroid.AndroidSdk.AndroidNdkPath; foreach (var abi in new[] { "armeabi-v7a", "arm64-v8a", "x86", "x86_64" }) { string extra = string.Empty; AndroidTargetArch targetArch; switch (abi) { case "armeabi-v7a": targetArch = AndroidTargetArch.Arm; extra = " -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16"; break; case "arm64-v8a": targetArch = AndroidTargetArch.Arm64; break; case "x86": targetArch = AndroidTargetArch.X86; extra = " -march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32"; break; case "x86_64": targetArch = AndroidTargetArch.X86_64; extra = " -march=x86-64 -mtune=intel -msse4.2 -mpopcnt -m64"; break; default: throw new NotImplementedException(); } bool isLLVM = false; var clangBin = NdkUtil.GetNdkClangBin(Path.Combine(ndkPath, "toolchains"), targetArch); if (string.IsNullOrEmpty(clangBin)) { clangBin = NdkUtil.GetNdkClangBin(Path.Combine(ndkPath, "toolchains", "llvm"), targetArch); isLLVM = true; } if (string.IsNullOrEmpty(clangBin)) { throw new Exception($"Unable to find NDK toolchain for {abi}!"); } var systemInclude = NdkUtil.GetNdkPlatformIncludePath(ndkPath, targetArch, XamarinAndroid.ApiLevel, isLLVM); //var newPath = "D:\\dev\\githubMP\\flutter\\embeddinatorfork\\external\\Xamarin.Android\\lib\\xbuild\\Xamarin\\Android\\lib"; //var monoDroidPath = Path.Combine(newPath, abi); // XamarinAndroid.LibraryPath, abi); var monoDroidPath = Path.Combine(XamarinAndroid.LibraryPath, abi); var abiDir = Path.Combine(Options.OutputDir, "android", "jni", abi); var outputPath = Path.Combine(abiDir, libName); if (!Directory.Exists(abiDir)) { Directory.CreateDirectory(abiDir); } var args = new List <string> { $"--sysroot=\"{systemInclude}\"{extra}", "-fdiagnostics-color", $"-D{DLLExportDefine}", $"-I\"{monoPath}\"", $"-L\"{monoDroidPath}\" -lmonosgen-2.0 -lmono-android.release", string.Join(" ", files.ToList()), "--std=c11", "-fPIC", $"-shared -o {outputPath}", }; if (isLLVM) { args.Add($"--target={NdkUtil.GetLlvmToolchainTarget(targetArch, XamarinAndroid.ApiLevel)}"); } var invocation = string.Join(" ", args); var output = Invoke(clangBin, invocation); if (output.ExitCode != 0) { return(false); } } return(true); }