Skip to content

Commit

Permalink
MacOS: Add a Homebrew OpenSSL directory configuration option.
Browse files Browse the repository at this point in the history
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/[email protected]/1.1.1s
lrwxr-xr-x  1 runner  admin  28 Dec 16 01:19 /usr/local/opt/[email protected] -> ../Cellar/[email protected]/1.1.1s
/usr/local/opt/[email protected]
```

== How to use ==

In the development, you can run like this.

```
$ RUBY_MYSQL2_BREW_SSL_DIR=/usr/local/opt/[email protected] rake
```

In the installation, you can run like this.

```
$ gem install mysql2 -- --with-brew-ssl-dir=/usr/local/opt/[email protected]
```

== 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
```
  • Loading branch information
junaruga committed Jan 16, 2023
1 parent d4469e4 commit 97e2cbf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
34 changes: 23 additions & 11 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -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: >-
Expand Down Expand Up @@ -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: '[email protected]'" 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/[email protected]'
# 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/[email protected]'
# 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
Expand Down
12 changes: 10 additions & 2 deletions ext/mysql2/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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+
Expand Down
3 changes: 3 additions & 0 deletions tasks/compile.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 97e2cbf

Please sign in to comment.