Fix native extension build on M1 macOS (Ruby 3+) by adjusting compiler flags#193
Fix native extension build on M1 macOS (Ruby 3+) by adjusting compiler flags#193restot wants to merge 5 commits intobrianmario:masterfrom
Conversation
|
Please take a look: @tenderlove, @stanhu |
tenderlove
left a comment
There was a problem hiding this comment.
Do we really need to add extra flags anyway? It looks like icu4c installs pkg-config settings, so we should just use the flags stored in pkg config rather than messing with them.
lib/charlock_holmes/version.rb
Outdated
| @@ -1,3 +1,3 @@ | |||
| module CharlockHolmes | |||
| VERSION = "0.7.9" | |||
| VERSION = "0.7.10" | |||
There was a problem hiding this comment.
Please don't change the version. Thanks!
There was a problem hiding this comment.
Changes addressed. If you are talking about compile_options = +"-x c++" I have no idea why it even exists. As you rightly mentioned we are using pkg-config settings. Also, I've changed the code to ignore just this flag and was able to successfully compile the gem with specifying the std version
gem install ./charlock_holmes-0.7.9.gem -- --with-cxxflags="-std=c++17"
|
I wasn't able to reproduce the problem, but maybe diff --git a/ext/charlock_holmes/extconf.rb b/ext/charlock_holmes/extconf.rb
index 903c297..e33ff81 100644
--- a/ext/charlock_holmes/extconf.rb
+++ b/ext/charlock_holmes/extconf.rb
@@ -67,7 +67,7 @@ if icu_requires_version_flag
checking_for("icu that compiles with #{std} standard") do
flags = compile_options + " -std=#{std}"
if try_compile(minimal_program, flags)
- $CPPFLAGS << flags
+ $CFLAGS << flags
true
end |
…ing clang++ and adjusting C++ flags revert version change
b2202f6 to
92a20eb
Compare
Thank you, yes $CFLAGS helped to get rid of defining $CXXFLAGS and $CPPFLAGS but haven't helped to solve the root cause for my case. My initial problem was in this explicit flag So I solved it with conditional application to preserve backward compatibility because I don't have enough competence to justify or disproof its necessity Without an explicit "-x c++" flag for the compiler, I was able to compile source code. |
|
I'm not sue what's going on your system. I have hundreds of team members who have had to compile and install this gem on all sorts of macOS and clang versions. You might want to inspect the output |
When I compile, it uses
To answer my own question, ICU requires certain versions of C++. In order to convince the C or C++ compiler to use that version, we have to specify a For some reason, Strangely, the But it doesn't add The It seems like whatever is generating the |
This change addresses a build failure on M1 MacBooks (macOS 15.3, build 24D60) when compiling the charlock_holmes gem with Ruby 3+. The problem was caused by an explicit hardcoded flag (-x c++) in the ext/charlock_holmes/extconf.rb file, which caused the C compiler to receive a stray "C++" argument.
After some troubleshooting I've come up with fixes:
Tested on macOS 15.3 M1 with Ruby 3.0.2 and 3.3.5 using a custom script for encoding detection/conversion.
A test script was used to verify core functionality (encoding detection and conversion) with different sample strings.
For example:
For the short sample
"Caf\xe9".force_encoding("ISO-8859-1"), the detector returns low confidence (15) and defaults to UTF-8 (this is expected with such a short and ambiguous string).For a longer sample such as
"Caf\xe9 au lait, d\xe9j\xe0 vu, cr\xe8me brûl\xe9e, voilà, à la mode".force_encoding("ISO-8859-1"), the detector correctly identifies the encoding as ISO-8859-1 with higher confidence (41) and the conversion produces a reasonably correct UTF-8 string.I've shared the full test script source and output details below.
Error log I had before fix:
Details
Because of the hardcoded c++ flag in extconf.rb --with-cxx flags have been ignored.
I tried maybe everything to help the compiler recognize the C++ compiler installed in the system but always got the same error.
Test script source:
Details
Test script output:
Details
Thank you for your attention!