mysql2 icon indicating copy to clipboard operation
mysql2 copied to clipboard

Refactor extconf.rb

Open lowjoel opened this issue 10 years ago • 34 comments

Hello there.

I've made a pretty major rewrite for extconf.rb. It was generating bad makefiles for Windows (since it is not commonly tested) and while trying to fix that I felt that the flow of logic could be much better.

I've split the path detection logic into the detect_* functions, and the configuration logic to configure_* functions. These are different depending on the platform.

I've tried to be as faithful to the original implementation as possible, however I have not tested this on platforms other than Windows. Comments much appreciated.

lowjoel avatar Apr 01 '15 11:04 lowjoel

Hmm, the breakage is on Ruby 1.9.1, 1.8.7, and Ruby EE because of require 'rake'. All are no longer supported. Should they be removed from the build matrix?

lowjoel avatar Apr 01 '15 11:04 lowjoel

Thanks for the effort on this! Will review it shortly. There's a ton of material in the merge queue at the moment.

I am surprised that the Makefiles aren't correct on Windows - I spent a lot of time updating the Windows support for the 0.3.18 release, and added AppVeyor tests. What version of Windows and what compiler environment are you using?

We're not removing any older Rubies just yet - the error is actually in the rm_f usage:

...  `rm_f': wrong number of arguments (3 for 2) (ArgumentError)
... from extconf.rb line 300

sodabrew avatar Apr 01 '15 15:04 sodabrew

Ah yes, Windows. extconf.rb currently should work for MingW, but I compile mine using VC++. There's a ton of conditionals in extconf that test for Unixy environments (that MingW is more than VC++), and also logic for generating libmysql.a from libmysql.dll (that VC++ doesn't need, and errors out because of missing dlltool and friends).

Also, because mysqlclient.lib is a static linkage version of MySQL, and that doesn't work too well with VC++ (the correct compiler version must be used -- the MySQL C connector comes with 1 mysqlclient.lib per VC++ version). libmysql.lib works better because it's a DLL and any version of VC++ (and MingW) should be able to link to it.

line 300 is have_func('rb_thread_blocking_region')... I think rake has overridden rm_f, I don't directly use it.

lowjoel avatar Apr 01 '15 21:04 lowjoel

Oh, that's great. I really appreciate that you're a VC++ user and dug into the build system to make it better across all platforms / Rubies (...gotta get those 1.8.7 and 1.9.3 Rubies working though...). Very odd error with rm_f.

sodabrew avatar Apr 02 '15 00:04 sodabrew

Hopefully with the platform code split up to various classes debugging the build system and improving it should be better in future.

I'm not sure how to start fixing the old Rubies though.

lowjoel avatar Apr 02 '15 01:04 lowjoel

@lowjoel looks like this patch fixes it:

diff --git a/ext/mysql2/extconf.rb b/ext/mysql2/extconf.rb
index 8320218..d6505e4 100644
--- a/ext/mysql2/extconf.rb
+++ b/ext/mysql2/extconf.rb
@@ -1,6 +1,12 @@
 # encoding: UTF-8
 require 'mkmf'
-require 'rake'
+
+# rake fucks with rm_f
+class << self
+  alias_method :rm_f_original, :rm_f
+  require 'rake'
+  alias_method :rm_f, :rm_f_original
+end

 class Platform
   # Gets the proper platform object for the current platform.

Maybe that should be wrapped in some condition on RUBY_VERSION.

tamird avatar Apr 15 '15 19:04 tamird

Strikes me as either a) a bug in rake, or b) newer rake doesn't work with 1.8.7 anymore. Thanks for looking at this, @tamird. Let's get that patch into this branch and it should be mergeable in the 0.4.0 queue.

sodabrew avatar Apr 15 '15 23:04 sodabrew

@tamird Thanks for the patch, I've rebased and hope it passes! @sodabrew

lowjoel avatar May 17 '15 09:05 lowjoel

Thanks for updating the PR! Logged in my (slow) review queue.

sodabrew avatar May 20 '15 21:05 sodabrew

Thanks!

lowjoel avatar May 21 '15 00:05 lowjoel

Would you do another rebase to master now that prepared statements have landed?

sodabrew avatar Jun 01 '15 19:06 sodabrew

Okay, done!

I'm not sure if I'm expecting merge conflicts though? It appears that this shouldn't affect what happens in the library, unless I'm missing something...

lowjoel avatar Jun 02 '15 00:06 lowjoel

Mostly a matter of triggering the tests to ensure that the combined code runs tests cleanly.

sodabrew avatar Jun 02 '15 02:06 sodabrew

Okay, it seems to pass :)

lowjoel avatar Jun 02 '15 04:06 lowjoel

@sodabrew I've added the check and rebased. Have a look.

lowjoel avatar Jun 06 '15 03:06 lowjoel

Thanks for updating! Please rebase again when you get a chance, and I think there may be one more rebase next before this PR is up for final review.

sodabrew avatar Jun 07 '15 22:06 sodabrew

Done!

lowjoel avatar Jun 07 '15 22:06 lowjoel

Wait, @sodabrew: this was recently added:

# This is our wishlist. We use whichever flags work on the host.
  # -Wall and -Wextra are included by default.
  # TODO: fix statement.c and remove -Wno-error=declaration-after-statement
  %w(
    -Werror
    -Weverything
    -fsanitize=address
    -fsanitize=integer
    -fsanitize=thread
    -fsanitize=memory
    -fsanitize=undefined
    -fsanitize=cfi
    -Wno-error=declaration-after-statement
  ).each do |flag|
    if try_link('int main() {return 0;}', flag)
      $CFLAGS << ' ' << flag
    end
  end

That only applies for GCC/clang? VC most definitely do not support those.

My latest rebase were to pick only my changes.

lowjoel avatar Jun 07 '15 22:06 lowjoel

Could you rebase once more? Sorry about the ever-changing whitespace in extconf.rb this week!

sodabrew avatar Jun 10 '15 06:06 sodabrew

@sodabrew no problem. I've added the compile flag wishlist back to the same one as current master. Also I saw that mariadb was added to the paths to check, my branch has been fixed to support that too.

lowjoel avatar Jun 10 '15 23:06 lowjoel

Apologies in advance; #634 is about to drop another conflict on this

tamird avatar Jun 11 '15 00:06 tamird

@tamird Ping me when that's merged. The only thing I'd preserve from #634 would be to add another directory to check for mysql's dev files (/usr/local/opt/mysql5*, which I think is for homebrew-like installs?)

lowjoel avatar Jun 11 '15 00:06 lowjoel

You should preserve the entire diff from #634 because the current code results in an infinite loop if it can't find the mysql client headers.

tamird avatar Jun 11 '15 00:06 tamird

@tamird OK -- I see how the loop could continue forever. Does b492763e5d4a595cb2e5bbb1d64da8f508e343a7 solve the problem?

lowjoel avatar Jun 11 '15 01:06 lowjoel

Why do you insist on keeping this cruft?

tamird avatar Jun 11 '15 01:06 tamird

Well, I'd rather not break things unnecessarily. If the decision is that we don't need this, then I'd be happy to omit it.

lowjoel avatar Jun 11 '15 01:06 lowjoel

I traced the history of that part of extconf.rb, and the extra library search block was borrowed from mysqlplus, going back to the original commit to mysqlplus, https://github.com/oldmoe/mysqlplus/commit/d7e47a3f2da222d1923bf7d1a6077e32621ac46d, which comes from an earlier gem called neverblock-mysql, https://rubygems.org/gems/oldmoe-neverblock-mysql, the sources for which are no longer online. I think we can let it go :)

sodabrew avatar Jun 11 '15 16:06 sodabrew

@sodabrew rebased, and removed that block of code.

lowjoel avatar Jun 12 '15 00:06 lowjoel

looks ok to me, but the commit history is a mess :(

tamird avatar Jun 12 '15 00:06 tamird

I'll squash it once this is ready to be merged. This PR has been going on for 3 months and rebased many times.

lowjoel avatar Jun 12 '15 00:06 lowjoel