`find_package(Ruby)` returns incorrect paths on macOS
- CMake Version: 3.7.0 (installed by
brew install cmake
) - OS: macOS Sierra 10.12.1
Detailed steps to reproduce the problem
- Install Ruby 2.3 by
brew install ruby
. - Create
CMakeLists.txt
with below content.
cmake_minimum_required (VERSION 3.7.0)
project(bug)
set(_RUBY_DEBUG_OUTPUT ON)
find_package(Ruby 2.3 REQUIRED)
- Run
cmake .
What happened
CMake finds Ruby header and libraries, which are all pointed to the system framework (as shown in below log). However, this is incorrect. Because the version of system Ruby is 2.0 instead of 2.3 as required by CMakeLists.txt
.
[snip]
-- --------FindRuby.cmake debug------------
-- _RUBY_POSSIBLE_EXECUTABLE_NAMES: ruby2.3;ruby23;ruby;ruby2.1;ruby21;ruby2.0;ruby20;ruby1.9;ruby19;ruby1.8;ruby18
-- _RUBY_POSSIBLE_LIB_NAMES: ruby;ruby-static;ruby2.3;ruby23;ruby-2.3;ruby-2.3.0
-- RUBY_ARCH_DIR: /usr/local/Cellar/ruby/2.3.1_2/lib/ruby/2.3.0/x86_64-darwin16
-- RUBY_HDR_DIR: /usr/local/Cellar/ruby/2.3.1_2/include/ruby-2.3.0
-- RUBY_POSSIBLE_LIB_DIR: /usr/local/Cellar/ruby/2.3.1_2/lib
-- Found RUBY_VERSION: "2.3.0" , short: "2.3", nodot: "23"
-- _RUBY_REQUIRED_VARS: RUBY_EXECUTABLE;RUBY_INCLUDE_DIR;RUBY_LIBRARY;RUBY_CONFIG_INCLUDE_DIR
-- RUBY_EXECUTABLE: /usr/local/bin/ruby
-- RUBY_LIBRARY: /System/Library/Frameworks/ruby.framework
-- RUBY_INCLUDE_DIR: /System/Library/Frameworks/Ruby.framework/Headers
-- RUBY_CONFIG_INCLUDE_DIR: /System/Library/Frameworks/Ruby.framework/Headers
-- --------------------
-- Found Ruby: /usr/local/bin/ruby (found suitable version "2.3.0", minimum required is "2.3")
[snip]
What should have happened
It should set RUBY_LIBRARY
, RUBY_INCLUDE_DIR
and RUBY_CONFIG_INCLUDE_DIR
to Homebrew Ruby(v2.3) instead of system Ruby(v2.0).
Cause and Workaround
This is caused as by default CMake has CMAKE_FIND_FRAMEWORK
as First
on macOS. In turn, find_library
and find_path
inside FindRuby.cmake
will search macOS Framework before the paths supplied by HINTS
.
Related Codes which trigger the bug:
-
find_path
inFindRuby.cmake
: https://github.com/Kitware/CMake/blob/v3.7.0/Modules/FindRuby.cmake#L182-L187 -
find_library
inFindRuby.cmake
: https://github.com/Kitware/CMake/blob/v3.7.0/Modules/FindRuby.cmake#L238 - Framework path by default has higher priority over
HINTS
: https://github.com/Kitware/CMake/blob/v3.7.0/Source/cmFindPathCommand.cxx#L48-L61
I suspect this bug has wider impact than just Ruby. It may be triggered by other macOS system Framework libraries (e.g. Python). To solve it, I think HINTS
should have the highest priority, regardless the value of CMAKE_FIND_FRAMEWORK
.
To workaround it, set CMAKE_FIND_FRAMEWORK
to Last
in CMakeLists.txt
.