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

Fix the builds on macOS (universal). #26

Merged
merged 1 commit into from
Oct 28, 2024

Conversation

Ivorforce
Copy link
Contributor

@Ivorforce Ivorforce commented Oct 24, 2024

A few comments:

std::quick_exit

std::quick_exit is not supported on macOS. For the replacement call I referenced this change:
wesnoth/wesnoth@6ce218f#diff-fc3beea58bc7268f70c24a27ee7f556f6440c92ebb72d7db4ff321b82cec4df4R182

(Oh, i didn't even realize it was from wesnoth. Nice!)

libs

The line (v.removeprefix('-l') for v in value.split()) failed for the following error:

❯ scons
scons: Reading SConscript files ...
Auto-detected 16 CPU cores available for build parallelism. Using 15 cores by default. You can override it with the -j argument.
Building for architecture universal on platform macos
scons: done reading SConscript files.
scons: Building targets ...
clang++ -o bin/macos-universal/libgodot-python.macos.universal.dylib -arch x86_64 -arch arm64 -framework Cocoa -Wl,-undefined,dynamic_lookup -fvisibility=hidden -Wl,-S -Wl,-x -Wl,-dead_strip -Wl,-S -Wl,-x -Wl,-dead_strip -fvisibility=hidden -ldl -framework CoreFoundation -shared src/extension/.extension.os src/module/.class_creation_info.os src/module/.class_method_info.os src/module/.module.os src/module/.property_info.os src/module/.property_list.os src/module/.script_instance_info.os src/util/.exceptions.os src/util/.python_utils.os src/util/.system.os src/variant/.callable.os src/variant/.object.os src/variant/.string.os src/variant/.string_name.os src/variant/.variant.os src/.generated/.godot_module_archive.os -Lsrc/.generated/python/macos-universal/python/lib -ldl -l-framework -lCoreFoundation -lpython3.12
ld: warning: ignoring duplicate libraries: '-ldl'
ld: library '-framework' not found
clang: error: linker command failed with exit code 1 (use -v to see invocation)
scons: *** [bin/macos-universal/libgodot-python.macos.universal.dylib] Error 1
scons: building terminated because of errors.

I printed print(config_vars.link_libs) to get the following result:

['dl', '-framework', 'CoreFoundation', 'python3.12']

CoreFoundation is technically a library, but it doesn't compile with it:

clang++ -o bin/macos-universal/libgodot-python.macos.universal.dylib -arch x86_64 -arch arm64 -framework Cocoa -Wl,-undefined,dynamic_lookup -fvisibility=hidden -Wl,-S -Wl,-x -Wl,-dead_strip -Wl,-S -Wl,-x -Wl,-dead_strip -fvisibility=hidden -ldl -framework CoreFoundation -shared src/extension/.extension.os src/module/.class_creation_info.os src/module/.class_method_info.os src/module/.module.os src/module/.property_info.os src/module/.property_list.os src/module/.script_instance_info.os src/util/.exceptions.os src/util/.python_utils.os src/util/.system.os src/variant/.callable.os src/variant/.object.os src/variant/.string.os src/variant/.string_name.os src/variant/.variant.os src/.generated/.godot_module_archive.os -Lsrc/.generated/python/macos-universal/python/lib -lCoreFoundation
ld: library 'CoreFoundation' not found

Since it compiles fine without it, I conclude none of my items are libraries. I'm sure there's a reason the line is there, so I opted to just include the libraries if they actually do start with -l (even if there aren't any that do on my end).

strip -s

strip -s is not supported as that in macOS. But to strip dylibs, -x is needed. I opted to just separate the 2 lines, it's likely more differences may emerge in the future.

dylib vs framework

On macOS, both dylibs and frameworks are supported. On iOS, only .framework is supported. .framework and .dylib have the same binary but slightly different directory structures. The reason godot-cpp defaults to .framework is for consistency to iOS, as well as for signing the binary later. This is not important for now since the python extension supports neither iOS nor signing currently. Besides, godot's loading of .framework remains spotty, and I don't want to fiddle with SConstruct too much for now. We can switch over later .framework later when it becomes relevant.

More work

The current build still has 3 warnings:

ld: warning: ignoring duplicate libraries: '-ldl'
ld: warning: ignoring duplicate libraries: '-ldl'
ld: warning: ignoring file '/Users/lukas/dev/godot/godot-python-extension/src/.generated/python/macos-universal/python/lib/libpython3.12.dylib': found architecture 'x86_64', required architecture 'arm64'

At least the final warning is probably relevant for arm macs, but we can address it later. For now, libgodot-python.macos.universal.dylib is built, and that's better than before.

I have not yet tested the library in action.

Edit: I have now, it works.

@Ivorforce Ivorforce force-pushed the macos-build-config branch 3 times, most recently from 09bc08b to b4123c2 Compare October 24, 2024 18:23
@maiself maiself added bug Something isn't working build related to the build system labels Oct 24, 2024
@Ivorforce
Copy link
Contributor Author

Looks like the macOS build is passing, though Windows is failing. Curious, as I didn't touch the Windows builds. Maybe it's affected by the if v.startswith('-l') change? Or somehow something else broke with a windows-latest update.

src/util/system.cpp Show resolved Hide resolved
@@ -5,7 +5,7 @@ entry_symbol = "python_extension_init"

[libraries]

macos = "res://bin/macos/libgodot-python.macos.framework"
macos = "res://bin/macos/libgodot-python.macos.universal.dylib"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know the difference here, but maybe there's a tag to be added after macos so that both can be listed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The difference between the dylib is explained in my PR text. TL;DR dylib and framework use the same binary but with a slightly different folder structure. Both cannot be supported since the gdextension must choose one path for the tags at hand.

tools/build/python_config.py Show resolved Hide resolved
@maiself
Copy link
Owner

maiself commented Oct 24, 2024

Looks like the macOS build is passing, though Windows is failing. Curious, as I didn't touch the Windows builds. Maybe it's affected by the if v.startswith('-l') change? Or somehow something else broke with a windows-latest update.

I don't think anything you did broke anything, my guess is windows-latest changed somehow. The compiler output doesn't really help...

@Ivorforce
Copy link
Contributor Author

I do have one tip for you.

Windows builds were very stubborn in my own project, until I adopted include-what-you-use for my includes. Being verbose with includes stabilizes include order across systems and was the final thing I needed to compile on all systems reliably. Since Windows is failing around a reference to GDExtensionTypePtr, included from another file, I can imagine it may help here too.

@Ivorforce
Copy link
Contributor Author

Ivorforce commented Oct 25, 2024

This PR includes #29 now so that the runners can pass.

@Ivorforce Ivorforce mentioned this pull request Oct 25, 2024
@Ivorforce
Copy link
Contributor Author

Ivorforce commented Oct 25, 2024

I managed to run Godot and execute some python code! 🥳

The binary needs to be patched after build like so:

scons
install_name_tool -add_rpath @loader_path bin/macos-universal/libgodot-python.macos.universal.dylib
install_name_tool -change /install/lib/libpython3.12.dylib @rpath/libpython3.12.dylib bin/macos-universal/libgodot-python.macos.universal.dylib

I added that to SConstruct to be performed automatically. It's probably not the cleanest solution to do it as a post-action (the expected path may be changeable elsehow), but it works.

I get the icons and can move them with the arrow keys:

SCR-20241025-peyf

@fire
Copy link
Contributor

fire commented Oct 25, 2024

@maiself Are you following the godot engine policy of 1 commit squashed pull request?

@maiself
Copy link
Owner

maiself commented Oct 25, 2024

@fire Out of habit I've been following Blender's policy of a linear rebased history.

@fire
Copy link
Contributor

fire commented Oct 25, 2024

My personal preference is to have a merge commit per pull request. I am not picky about single commit or sequence of commits, but it helps me a lot to have the merge commit.

@maiself
Copy link
Owner

maiself commented Oct 25, 2024

I'm the opposite, I've always found it easier if merge commits are avoided so that the history is easier to navigate, bisection is easier, rebasing if needed is easier etc. Can you give insight into how having merge commits helps you?

@Ivorforce
Copy link
Contributor Author

Ivorforce commented Oct 26, 2024

I just realized the 'universal' macOS build was a lie: The libpython is single-arch, so we cannot have a proper universal build. I updated everything to match. aarch64 builds should work fine though when we add a target for it.

Besides, Godot also uses two separate builds for universal, joining them after the fact. Godot-CPP is inconsistent with Godot upstream in this right now (see: godotengine/godot-cpp#1613).

if platform == 'macos':
# Rename the library id (which we depend on) to be in @rpath.
# (it defaults to /install/lib/)
subprocess.run(['install_name_tool', '-id', f'@rpath/{lib_filename}', src_lib_path], check=True)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should maybe be performed on the copy instead, but that doesn't work because the copy isn't what is being linked, rendering the id change useless, and the final binary links to /install/lib/.

@maiself maiself merged commit 4b2cc53 into maiself:master Oct 28, 2024
4 checks passed
@Ivorforce Ivorforce deleted the macos-build-config branch October 28, 2024 01:18
@maiself
Copy link
Owner

maiself commented Oct 28, 2024

Horray! Thanks a bunch for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working build related to the build system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants