- Home /
How to add compiler or linker flags for il2cpp invocation.
Hi,
is there any way to affect the il2cpp
invocation when building a project? In this particular case it would be for Android but the question probably applies to all players supporting the il2cpp backend.
I basically want to add additional compiler and linker flags:
me@machine /A/U/U/C/i/build> mono ./il2cpp.exe
Options:
...
--compiler-flags=<value> Additional flags to pass to the C++ compiler
--linker-flags=<value> Additional flags to pass to the linker
I was hoping for just a setting somewhere but couldn't find anything. Maybe something with the Scriptable Build Pipeline
? I can find several answers on how to add flags to a generated Xcode project but in the Android case it seems that even if you export to a Gradle project to build in e.g. Android Studio the export contains compiled version of the il2cpp generated code and not ndk scripts to build them over again.
Can you elaborate on what additional flags you would like to pass? Those two arguments are generally used internal by the Unity editor. For the most part we don't support users passing additional C++ compiler arguments, as the current set of arguments should be correct. But I'm interested to learn move about your use case.
@JoshPeterson I'm trying to integrate a tool that does some post-processing based on LLV$$anonymous$$ bitcode. So just like the iOS export I'd like the Android one to have embedded bitcode. Since r16 ND$$anonymous$$ this is possible with the -fembed-bitcode
flag.
Thanks! I don't expect this compiler option to have a negative impact, and we don't currently have a built-in way to provide it. See my answer below for details.
Answer by JoshPeterson · Mar 11, 2019 at 12:24 PM
We do have a "backdoor" (i.e. not documented) ability to pass additional arguments to il2cpp.exe. You can either run the Unity editor with an environment variable set: IL2CPP_ADDITIONAL_ARGS
or you can use an editor script to call PlayerSettings.SetAdditionalIl2CppArgs
. Either option will allow you to pass the command line options you have mentioned.
We don't test with all of possible compiler and linker arguments available, so your mileage may vary, but this should work.
Thanks, both suggestions work. However the flag I pass seems completely ignored without causing any error. The output does not have the desired bitcode sections.
From the editor log I see that it gets to il2cpp but the ELF's don't have the required section.
I wrapped /Applications/Unity/Hub/Editor/2019.1.0b5/PlaybackEngines/AndroidPlayer/ND$$anonymous$$/toolchains/llvm/prebuilt/darwin-x86_64/bin/{clang,clang++}
to inspect the invocations but they are never called. Is il2cpp integrating with clang in a different way? It probably just links agains it and skips the driver? If that's true do you know how the flags are passed?
Can you have a look at the Unity editor log file? It should disable the entire command line passed to il2cpp.exe (search the log file for "il2cpp with"). That should let us know if the command line arguments are getting to il2cpp.exe properly.
@JoshPeterson The flags are passed correctly, I didn't want to paste the whole line here but if it helps:
Invoking il2cpp with arguments:
--convert-to-cpp
--emit-null-checks
--enable-array-bounds-check
--dotnetprofile="legacyunity"
--compile-cpp
--libil2cpp-static
--platform="Android"
--architecture="AR$$anonymous$$64"
--configuration="Release"
--outputpath="/me/$$anonymous$$obilePlaceHolder/Temp/StagingArea/assets/bin/Data/Native/arm64-v8a/libil2cpp.so"
--cachedirectory="/me/$$anonymous$$obilePlaceHolder/Assets/../Library/il2cpp_android_arm64-v8a/il2cpp_cache"
--additional-include-directories="/Applications/Unity/Hub/Editor/2019.1.0b5/PlaybackEngines/AndroidPlayer/Tools/bdwgc/include"
--additional-include-directories="/Applications/Unity/Hub/Editor/2019.1.0b5/PlaybackEngines/AndroidPlayer/Tools/libil2cpp/include"
--tool-chain-path="/Applications/Unity/Hub/Editor/2019.1.0b5/PlaybackEngines/AndroidPlayer/ND$$anonymous$$"
--profiler-report
--map-file-parser="/Applications/Unity/Hub/Editor/2019.1.0b5/Unity.app/Contents/Tools/$$anonymous$$apFileParser/$$anonymous$$apFileParser"
--compiler-flags='-fembed-bitcode'
--directory="/me/$$anonymous$$obilePlaceHolder/Temp/StagingArea/assets/bin/Data/$$anonymous$$anaged"
--generatedcppdir="/me/$$anonymous$$obilePlaceHolder/Temp/StagingArea/Il2Cpp/il2cppOutput"
I've also tried doing this manually with il2cpp on the cmdline but I can't get it to do a simple HelloWorld.cs file. Is there a blog somewhere that shows how to use it like that?
Answer by Warena · Nov 11, 2021 at 02:09 PM
using UnityEditor;
using UnityEngine;
public class BuildHacks {
/// <summary>
/// https://forum.unity.com/threads/android-builds-failing-when-script-debugging-is-enabled.1027357/
/// Android builds failing when 'Script Debugging' is enabled
/// </summary>
[InitializeOnLoadMethod]
static void FixScriptDebuggingBuildFail()
{
Debug.Log("[Build Patch] Android builds failing when 'Script Debugging' is enabled");
PlayerSettings.SetAdditionalIl2CppArgs("--linker-flags=\"-Wl,--stub-group-size=11534360\"");
}
}
put script into "Editor" directory like this.
Answer by morgoth990 · Nov 19, 2021 at 10:17 AM
Just tested it, you have to use double quotes, with single quotes il2cpp ignore the argument: --compiler-flags="-fembed-bitcode"