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.

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.

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.

Just tested it, you have to use double quotes, with single quotes il2cpp ignore the argument:
–compiler-flags=“-fembed-bitcode”