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 Godot constants to Mono project builds #28786

Merged
merged 1 commit into from
May 26, 2019

Conversation

ShyRed
Copy link
Contributor

@ShyRed ShyRed commented May 9, 2019

This adds constants to projects build via Godot Mono which allows project to conditionally react to different operating systems and 32/64 Bit architecture. Additionally .NET libraries could support multiple engines like Unity and Godot at the same time when compiled from Godot and reacting to definitions.

How it works

GODOT - Always set (is set in the actual csproj file)

GODOT_REAL_T_IS_DOUBLE - Set, if Godot was compiled with REAL_T_IS_DOUBLE being defined

When building from the edito, the OS defines are created using OS::get_singleton()->get_name().to_upper() so you can find all possible OS defines on the official documentation:
GODOT_WINDOWS - Godot running on Windows
GODOT_X11 - Godot running on X11 compatible system
GODOT_OSX - Godot running on OSX
etc.

The bitness will be defined as GODOT_64 for 64 Bit and GODOT_32 for 32 Bit.

When exporting the entries in the "Feature List" shown in the export dialog will be turned into defines. For example: "Android" will turn into "GODOT_ANDROID" and "arm64-v8a" will turn into "GODOT_ARM64_V8A".

In C# you can write conditional code like

using Godot;
using System;

public class Main : Node2D
{
    public override void _Ready()
    {
		GD.Print("Startup!");
		
		#if (DEBUG)
			GD.Print("DEBUG is defined!");
		#endif 
		
		#if (TOOLS)
			GD.Print("TOOLS is defined!");
		#endif 
		
        	#if (GODOT)
			GD.Print("GODOT is defined!");
		#endif 
		 
		#if (GODOT_WINDOWS)
			GD.Print("GODOT is running on Windows");
		#endif
		
		#if (GODOT_64)
			GD.Print("GODOT is 64Bit");
		#endif
    }
}

Important

  1. Unfortunately defines that are provided to MSBuild via command line will only be used if the project is being rebuild so the build system has been changed to do rebuilds instead of builds. This increases build time a bit...
  2. In order for the defines to show up in the project, existing projects will either have to have their csproj files being recreated or one has to updated them manually. For example:
<DefineConstants>DEBUG;</DefineConstants>

has to be changed into

<DefineConstants>$(GodotDefineConstants);GODOT;DEBUG;</DefineConstants>

Closes #28517
Closes #27227

@aaronfranke
Copy link
Member

GODOT_REAL_T_IS_DOUBLE

Note to self, or anyone else: This will have to be changed in all of the C# source files (I think?)

@neikeq
Copy link
Contributor

neikeq commented May 9, 2019

GODOT_REAL_T_IS_DOUBLE

Note to self, or anyone else: This will have to be changed in all of the C# source files (I think?)

No, that will require passing REAL_T_IS_DOUBLE when building the API solutions. I'm soon going to make SCons build the API solutions (right now, it's a manual step). I will add this at that point.

@neikeq
Copy link
Contributor

neikeq commented May 9, 2019

Unfortunately defines that are provided to MSBuild via command line will only be used if the project is being rebuild so the build system has been changed to do rebuilds instead of builds. This increases build time a bit...

Rebuilding every time is definitely not wanted. I will look into this when I have some time.

@aaronfranke
Copy link
Member

Interesting, though it would probably be better for the API solutions to be consistent with user code.

@ShyRed ShyRed force-pushed the monodefines branch 2 times, most recently from adb71bc to 2667098 Compare May 10, 2019 07:28
@akien-mga
Copy link
Member

GODOT_32BIT - Set, if OS uses 32Bit
GODOT_64BIT - Set, if OS uses 64Bit

Keep in mind that most platforms have several 32 and 64-bit architectures, and this "bitness" distinction is not particularly meaningful. The "bits" setting used by Godot's buildsystem is a legacy parameter but should be obsoleted and replaced by the proper architecture names (x86_64, i686 or x86, armv7, arm64v8 or aarch64, etc.).

@ShyRed
Copy link
Contributor Author

ShyRed commented May 10, 2019

@akien-mga Good point, but I don't think the exact architecture matters for Mono projects. Knowing if it is 32 Bit or 64 Bit is enough as the libraries one references on the target system need to be precompiled. It's just for the correct size mapping (see https://www.mono-project.com/docs/advanced/pinvoke/ for example).

@aaronfranke
Copy link
Member

I don't see why we can't have both. A define for ARM and a define for 64-bit could be combined to detect ARM64, etc, same with OS defines.

@ShyRed
Copy link
Contributor Author

ShyRed commented May 10, 2019

@aaronfranke What would be the use case for detecting ARM64 during Mono compile time? 🤔

If we want to put in all information possible, maybe we can just use the entries of the "Feature List" of the export dialog as our source of constants? Would that be an idea?

For Android, we would end up with "GODOT_ANDROID, GODOT_ARM64_V8A, GODOT_ARMEABI_V7A" etc. That might actually be the easiest solution?

@zEh-
Copy link

zEh- commented May 10, 2019

Is it possible to add custom defines for an export template instance? I want to export client builds without server-side code which is in the same godot project. If this feature exists already I simply missed it otherwise this would be a good point to introduce it.

@ShyRed
Copy link
Contributor Author

ShyRed commented May 10, 2019

@zEh- That's not possible yet. Would be an argument for using the export feature list as a source for defines, though...

@ShyRed
Copy link
Contributor Author

ShyRed commented May 10, 2019

It does seem to make sense to use the export feature list as the source for defines. Now Godot will build using "GODOT_OSNAME" and "GODOT_32" or "GODOT_64" for bits when building from the editor. When exporting it will use the feature list to generate defines like "GODOT_ANDROID". Since you can add custom features to an export this also allows setting up custom defines. 😄

@akien-mga akien-mga added this to the 3.2 milestone May 11, 2019
modules/mono/editor/godotsharp_builds.cpp Outdated Show resolved Hide resolved
modules/mono/editor/godotsharp_export.cpp Outdated Show resolved Hide resolved
modules/mono/editor/mono_bottom_panel.cpp Outdated Show resolved Hide resolved
modules/mono/editor/godotsharp_builds.h Outdated Show resolved Hide resolved
This adds constants to projects build via Godot Mono which allows project to conditionally react to different operating systems and 32/64 Bit architecture. Additionally .NET libraries could support multiple engines like Unity and Godot at the same time when compiled from Godot and reacting to definitions.
@ShyRed
Copy link
Contributor Author

ShyRed commented May 26, 2019

Changed variable and parameters from godotDefines and p_defines to godot_defines and p_godot_defines.

Builds should be ok. The failed build had trouble fetching some remote stuff...

gpg: requesting key BA9EF27F from hkp server keyserver.ubuntu.com
Error: retrieving gpg key timed out.

@neikeq neikeq merged commit 74adfd0 into godotengine:master May 26, 2019
@neikeq
Copy link
Contributor

neikeq commented May 26, 2019

Thanks! :)

@aaronfranke
Copy link
Member

aaronfranke commented Jun 2, 2019

GODOT - Always set (is set in the actual csproj file)

Minor note: This needs to be added manually to existing projects to make it work. Also, adding to a 3.1 project doesn't seem to have any bad effects when opened in 3.1.

@wscalf
Copy link
Contributor

wscalf commented Sep 26, 2020

Does this work with custom features? I'm trying to do the same kind of thing as @zEh-, but I haven't been able to get anything to come through that lets me distinguish between client and server builds (like adding 'Client' or 'Server' as custom features), though I do get the built-in features that show up on the export screen (GODOT, GODOT_64, GODOT_PC, etc.) I can start a new issue if need be (this could totally be something weird about my setup) but thought I'd ask for clarification here first.

@Calinou
Copy link
Member

Calinou commented Sep 26, 2020

@wscalf Custum features currently don't work: #38747

@aaronfranke
Copy link
Member

@wscalf The C# defines work with custom features in Godot 3.2.0, 3.2.1, and 3.2.2, but this was removed in Godot 3.2.3.

@wscalf
Copy link
Contributor

wscalf commented Sep 26, 2020

Ah, I just found it. It was in the same change as adding netstandard support. All right, I have more questions but I'll open a separate thread.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Define constants for the target platform Preprocessor defines for C#/Mono
7 participants