From 97e2cbfb5d226d2640ed34013bd8e82691fd9c4f Mon Sep 17 00:00:00 2001 From: Jun Aruga Date: Fri, 13 Jan 2023 13:24:08 +0100 Subject: [PATCH] MacOS: Add a Homebrew OpenSSL directory configuration option. This commit is to enable users to set a custom Homebrew openssl directory configuration option to set the openssl library path on MacOS. Previously we got the openssl library path by `brew --prefix openssl` on MacOS. However, there is a case where the directory actually doesn't exist. And this option enables users to build mysql2 in the case. ``` + brew --prefix openssl /usr/local/opt/openssl@3 + ls -ld /usr/local/opt/openssl* lrwxr-xr-x 1 runner admin 36 Dec 16 01:28 /usr/local/opt/openssl -> /usr/local/Cellar/openssl@1.1/1.1.1s lrwxr-xr-x 1 runner admin 28 Dec 16 01:19 /usr/local/opt/openssl@1.1 -> ../Cellar/openssl@1.1/1.1.1s /usr/local/opt/openssl@1.1 ``` == How to use == In the development, you can run like this. ``` $ RUBY_MYSQL2_BREW_SSL_DIR=/usr/local/opt/openssl@1.1 rake ``` In the installation, you can run like this. ``` $ gem install mysql2 -- --with-brew-ssl-dir=/usr/local/opt/openssl@1.1 ``` == Note == In this commit, the directory existing check is added in the `extconf.rb`. If the openssl directory doesn't exist, the build fails with the error message below. ``` Cannot find library dir(s) /usr/local/opt/openssl@3/lib ``` --- .github/workflows/build.yml | 34 +++++++++++++++++++++++----------- ext/mysql2/extconf.rb | 12 ++++++++++-- tasks/compile.rake | 3 +++ 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9136cf965..65dfe8b6a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,5 +1,10 @@ name: Build on: [push, pull_request] +env: + BUNDLE_WITHOUT: development + # Reduce MacOS CI time, don't need to clean a runtime that isn't saved + HOMEBREW_NO_INSTALL_CLEANUP: 1 + HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK: 1 jobs: build: name: >- @@ -27,30 +32,37 @@ jobs: - '2.2' - '2.1' db: [''] + env: [{}] include: # Comment out due to ci/setup.sh stucking. # - {os: ubuntu-18.04, ruby: 2.4, db: mariadb10.1} - - {os: ubuntu-20.04, ruby: '2.4', db: mariadb10.3} - - {os: ubuntu-18.04, ruby: '2.4', db: mysql57} - - {os: ubuntu-20.04, ruby: '2.4', db: mysql80} - - {os: ubuntu-18.04, ruby: 'head', db: ''} + - {os: ubuntu-20.04, ruby: '2.4', db: mariadb10.3, env: {}} + - {os: ubuntu-18.04, ruby: '2.4', db: mysql57, env: {}} + - {os: ubuntu-20.04, ruby: '2.4', db: mysql80, env: {}} + - {os: ubuntu-18.04, ruby: 'head', db: '', env: {}} # db: A DB's brew package name in macOS case. # Set a name "db: 'name@X.Y'" when using an old version. # MariaDB lastet version # Allow failure due to the following test failures that rarely happens. # https://github.com/brianmario/mysql2/issues/1194 - - {os: macos-latest, ruby: '2.6', db: mariadb, allow-failure: true} + - os: macos-latest + ruby: '2.6' + db: mariadb + allow-failure: true + env: + RUBY_MYSQL2_HOMEBREW_SSL_DIR: '/usr/local/opt/openssl@1.1' # MySQL latest version # Allow failure due to the issue #1194. - - {os: macos-latest, ruby: '2.6', db: mysql, allow-failure: true} + - os: macos-latest + ruby: '2.6' + db: mysql + allow-failure: true + env: + RUBY_MYSQL2_HOMEBREW_SSL_DIR: '/usr/local/opt/openssl@1.1' # On the fail-fast: true, it cancels all in-progress jobs # if any matrix job fails unlike Travis fast_finish. fail-fast: false - env: - BUNDLE_WITHOUT: development - # reduce MacOS CI time, don't need to clean a runtime that isn't saved - HOMEBREW_NO_INSTALL_CLEANUP: 1 - HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK: 1 + env: ${{ matrix.env }} steps: - uses: actions/checkout@v3 # https://github.com/ruby/setup-ruby diff --git a/ext/mysql2/extconf.rb b/ext/mysql2/extconf.rb index 592e02486..61b9d527b 100644 --- a/ext/mysql2/extconf.rb +++ b/ext/mysql2/extconf.rb @@ -28,8 +28,16 @@ def add_ssl_defines(header) # Homebrew openssl if RUBY_PLATFORM =~ /darwin/ && system("command -v brew") - openssl_location = `brew --prefix openssl`.strip - $LDFLAGS << " -L#{openssl_location}/lib" if openssl_location + _, lib = dir_config('brew-ssl') + unless lib + openssl_location = `brew --prefix openssl`.strip + lib = "#{openssl_location}/lib" if openssl_location + end + + if lib + abort "-----\nCannot find library dir(s) #{lib}\n-----" unless lib && lib.split(File::PATH_SEPARATOR).any? { |dir| File.directory?(dir) } + $LDFLAGS << " -L#{lib}" + end end # 2.1+ diff --git a/tasks/compile.rake b/tasks/compile.rake index 07aa1abea..6d4a2bb3b 100644 --- a/tasks/compile.rake +++ b/tasks/compile.rake @@ -51,6 +51,9 @@ Rake::ExtensionTask.new("mysql2", Mysql2::GEMSPEC) do |ext| POST_INSTALL_MESSAGE end end + + brew_ssl_dir = ENV['RUBY_MYSQL2_HOMEBREW_SSL_DIR'] + ext.config_options << "--with-brew-ssl-dir=#{brew_ssl_dir}" if brew_ssl_dir end Rake::Task[:spec].prerequisites << :compile