buildtool icon indicating copy to clipboard operation
buildtool copied to clipboard

Preprocessor directives forced to be upper case

Open ValentinChareyre opened this issue 1 year ago • 1 comments

Hello! 👋

I am working on a project using third-party assets using #define preprocessor directives.

Currently, BuildProject.GenerateDefaultDefines (see here) collects the defines of the given release type and calls the string extension method SanitizeDefine (see here) which transforms the define to uppercase. Because preprocessor directives are case-sensitive, the third-party code between the directives is stripped, which causes issues in the build.

        // Editor/Build/BuildProject.cs
        public static string GenerateDefaultDefines(BuildReleaseType releaseType, BuildPlatform platform, BuildArchitecture arch,
            BuildScriptingBackend scriptingBackend, BuildDistribution dist)
        {
            // ...

            if (releaseType != null && !string.IsNullOrEmpty(releaseType.customDefines))
            {
                string[] customDefines = releaseType.customDefines.Split(';', ',');
                for (int i = 0; i < customDefines.Length; i++)
                {
                    defines.Add(customDefines[i].SanitizeDefine());
                }
            }

            return string.Join(";", defines.ToArray());
        }
        // Editor/Generic/ExtensionMethods.cs
        public static string SanitizeDefine(this string input)
        {
            return input.ToUpperInvariant().Replace(" ", "").SanitizeCodeString();
        }

Is there any reason to force the defines to be uppercase?

Thanks in advance.


Info

Unity version: Unity 2021.3.21f1 Platform: Windows Target build platform tested: Windows


Reproduction steps:

  1. Create a new scene, leave the camera and light unchanged
  2. Create a new script called NewBehaviourScript with the following content, and attach it to an empty object you place in front of the default camera
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    private GameObject _gameObject;

    void Start()
    {
        _gameObject = UnityEngine.GameObject.CreatePrimitive(PrimitiveType.Cube);
    }

    void Update()
    {
#if MyDefine
        _gameObject.transform.localScale = Vector3.one * Mathf.Sin(Time.time) * 5;
#else
        _gameObject.transform.position = new Vector3(Mathf.Cos(Time.time), Mathf.Sin(Time.time), 0) * 4;
#endif
    }
}
  1. In SuperUnityBuild, define a new release type with the following define: MyDefine, add your empty scene to the scene list
  2. Create a new build platform you can build to
  3. Click on the build configuration you've created from the Build Configurations options at the bottom of the window, and click on Selected Build Info
  4. Observe in the Defines section that your define has been transformed to upper-case
  5. Build the game
  6. Observe that the cube is moving in circles (code outside of the MyDefine) instead of growing and shrinking (code between MyDefine)

ValentinChareyre avatar Aug 27 '24 17:08 ValentinChareyre

Note that the issue can be resolved by doing the following:

  1. Remove the define(s) with lower case characters
  2. Add an OverrideDefines BuildAction (see BuildActions repository)
  3. Add your define in the Add Defines field

There is discrepancy between the "core" code and that build action.

ValentinChareyre avatar Aug 28 '24 11:08 ValentinChareyre