RubyGateway icon indicating copy to clipboard operation
RubyGateway copied to clipboard

Can't setup Custom Ruby when writing a mac App.

Open mlch911 opened this issue 1 year ago • 8 comments

I'm writing a macOS App, not a executable swift package. Is there a way to use custom Ruby rather than the system one.

mlch911 avatar Jan 25 '25 07:01 mlch911

You mean using Xcode -> Create Project? It should be doable - probably need to clone a separate copy of CRuby and configure that to point to whatever version of Ruby you want to us, then persuade Xcode to use that somehow -- at worst you'll need to clone RubyGateway (a la Carthage...) if Xcode won't do the right thing with the package dependencies.

johnfairh avatar Jan 25 '25 10:01 johnfairh

I'm working with a xcodeproj. And I write a custom Package inside the project. I can add RubyGateway into the Package.swift to use system ruby. But any other way can't build. I tried clone CRuby, and add it to the Package.swift. But after I run cfg-cruby, it can't build.

mlch911 avatar Jan 25 '25 11:01 mlch911

You'll probably need to ask for some Xcode help on how to best route the flags through - not something I know much about.

johnfairh avatar Jan 25 '25 11:01 johnfairh

You'll probably need to ask for some Xcode help on how to best route the flags through - not something I know much about.

Is there a way to write the path of ruby inside CRuby?

mlch911 avatar Jan 25 '25 11:01 mlch911

The 'path of Ruby' is set by the -I & -L C compiler flags so that when the CRuby module does its #include <ruby.h> it gets the right thing. The CRuby package setup (cfg_cruby) is supposed to generate the right values for those flags so the build system sets them before / as part of importing the CRuby module.

For SwiftPM, this happens via the pkgconfig thing - the CRuby Package.swift refs that file, which includes the -I and -L values, that the build system sets so the import works.

johnfairh avatar Jan 25 '25 11:01 johnfairh

After I setup cfg_cruby, it wouldn't build. Could not find <ruby.h>. And there is a warning tha can't find the pc file.

mlch911 avatar Jan 25 '25 13:01 mlch911

You need to help Xcode find the pc file -- I don't know how that works; could maybe use the PKG_CONFIG_PATH env var in xcconfig?

Or... try adding the xcconfig that cfg_cruby generated into the project - should have the -I and -L settings in it?

johnfairh avatar Jan 26 '25 09:01 johnfairh

Did you get anywhere with this? I had a play today; it does look like an Xcode problem, that Xcode is not dealing properly with the pkgConfig setting in Package.swift.

macOS 15.3, Intel, Xcode 16.2.

Create new project "mac App", add RubyGateway as dependency, update default ContentView to Text("Hello, world from Ruby \(Ruby.version)!"). Preview works, shows system Ruby 2.6.10. OK.

Use apple instructions to create local copy of CRuby that I guess is their version of swift package edit. Xcode is happy, acknowledges local dependency: Image

Configure Ruby, ./cfg-cruby --mode rbenv --name 3.4.1. Everything breaks: Image This is expected because we have not told Xcode where to find the files - but it does prove Xcode is using the right copy of CRuby. Suspiciously there is no mention of a missing pkgconfig file.

And... I can't get past this. I tried:

  1. Put CRuby.pc into /usr/local/lib - no change. Who knows where Xcode is searching for it.
  2. Add the CRuby-generated CRuby.xcconfig into the project. Nothing. 2a) Add a PKG_CONFIG_PATH into that xcconfig. Nothing.
  3. Manually add the correct HEADER_SEARCH_PATHS in Project -> Build Settings. Still fails in the same way; these must be ignored for 'dependencies'.

I think a workaround would be to clone the entire RubyGateway project and create your App target in there -- then there are no dependencies to confuse Xcode. I had a quick go at this but the build failed with a "Type 'bundle' has no member 'module'" error before I could really start customising anything...

johnfairh avatar Feb 10 '25 12:02 johnfairh