Skip to content

Commit ff791cf

Browse files
committed
Revamp CmakeBuilder to fix the issues described in #8572. Specifically:
* Correctly pass command line arguments to CMake * Call CMake twice - once to configure a project and a second time to build (which is the standard way to use CMake). This fixes the previously incorrect assumption that CMake generates a Make file. * Update the tests to specify a CMake minimum version of 3.26 (which is already two years old). 3.26 is a bit arbritary but it aligns with Rice, and updates from the ancient 3.5 version being used (which CMake generates a warning message saying stop using it!) * Update the CMake call to use CMAKE_RUNTIME_OUTPUT_DIRECTORY and CMAKE_LIBRARY_OUTPUT_DIRECTORY to tell CMake to copy compiled binaries to the a Gem's lib directory. Note the updated builder took inspiration from the Cargo Builder, meaning you first create an instance of CmakeBuilder versus just calling class methods.
1 parent 72407d4 commit ff791cf

File tree

3 files changed

+122
-28
lines changed

3 files changed

+122
-28
lines changed

lib/rubygems/ext/builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def builder_for(extension) # :nodoc:
165165
@ran_rake = true
166166
Gem::Ext::RakeBuilder
167167
when /CMakeLists.txt/ then
168-
Gem::Ext::CmakeBuilder
168+
Gem::Ext::CmakeBuilder.new
169169
when /Cargo.toml/ then
170170
Gem::Ext::CargoBuilder.new
171171
else

lib/rubygems/ext/cmake_builder.rb

Lines changed: 101 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,114 @@
11
# frozen_string_literal: true
22

3+
# This builder creates extensions defined using CMake. Its is invoked if a Gem's spec file
4+
# sets the `extension` property to a string that contains `CMakeLists.txt`.
5+
#
6+
# In general, CMake projects are built in two steps:
7+
#
8+
# * configure
9+
# * build
10+
#
11+
# The builder follow this convention. First it runs a configuration step and then it runs a build step.
12+
#
13+
# CMake projects can be quite configurable - it is likely you will want to specify options when
14+
# installing a gem. To pass options to CMake specify them after `--` in the gem install command. For example:
15+
#
16+
# gem install <gem_name> -- --preset <preset_name>
17+
#
18+
# Note that options are ONLY sent to the configure step - it is not currently possible to specify
19+
# options for the build step. If this becomes and issue then the CMake builder can be updated to
20+
# support build options.
21+
#
22+
# Useful options to know are:
23+
#
24+
# -G to specify a generator (-G Ninja is recommended)
25+
# -D<CMAKE_VARIABLE> to set a CMake variable (for example -DCMAKE_BUILD_TYPE=Release)
26+
# --preset <preset_name> to use a preset
27+
#
28+
# If the Gem author provides presets, via CMakePresets.json file, you will likely want to us use one of them.
29+
# If not, then you may wish to specify a generator (Ninja is recommended since it can build projects in parallel
30+
# versus a Make generator which build ins serial and thus is *much* slower).
31+
32+
require "fileutils"
33+
334
class Gem::Ext::CmakeBuilder < Gem::Ext::Builder
4-
def self.build(extension, dest_path, results, args=[], lib_dir=nil, cmake_dir=Dir.pwd,
5-
target_rbconfig=Gem.target_rbconfig)
35+
attr_accessor :runner, :profile
36+
37+
def initialize
38+
@runner = self.class.method(:run)
39+
@profile = :release
40+
end
41+
42+
def build(extension, gem_dir, results, args = [], lib_dir = nil, cmake_dir = Dir.pwd, target_rbconfig = Gem.target_rbconfig)
643
if target_rbconfig.path
744
warn "--target-rbconfig is not yet supported for CMake extensions. Ignoring"
845
end
946

10-
unless File.exist?(File.join(cmake_dir, "Makefile"))
11-
require_relative "../command"
12-
cmd = ["cmake", ".", "-DCMAKE_INSTALL_PREFIX=#{dest_path}", *Gem::Command.build_args]
47+
# Make sure lib dir is set
48+
lib_dir ||= File.join(gem_dir, "lib")
1349

14-
run cmd, results, class_name, cmake_dir
15-
end
50+
# Figure the build dir
51+
build_dir = File.join(cmake_dir, "build")
1652

17-
make dest_path, results, cmake_dir, target_rbconfig: target_rbconfig
53+
# Check if the gem defined presets
54+
check_presets(cmake_dir, args)
55+
56+
# Configure
57+
configure(cmake_dir, build_dir, lib_dir, args, results)
58+
59+
# Compile
60+
compile(cmake_dir, build_dir, args, results)
1861

1962
results
2063
end
64+
65+
def configure(cmake_dir, build_dir, lib_dir, args, results)
66+
cmd = ["cmake",
67+
cmake_dir,
68+
"-B",
69+
build_dir,
70+
"-DCMAKE_RUNTIME_OUTPUT_DIRECTORY=#{lib_dir}", # Windows
71+
"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=#{lib_dir}", # Not Windows
72+
*Gem::Command.build_args,
73+
*args]
74+
75+
runner.call(cmd, results, "cmake", cmake_dir)
76+
end
77+
78+
def compile(cmake_dir, build_dir, args, results)
79+
cmd = ["cmake",
80+
"--build",
81+
build_dir.to_s,
82+
"--config",
83+
@profile.to_s]
84+
runner.call(cmd, results, "cmake", cmake_dir)
85+
end
86+
87+
private
88+
89+
def check_presets(cmake_dir, args)
90+
# Return if the user specified a preset
91+
return unless args.grep(/--preset/i).empty?
92+
93+
cmd = ["cmake",
94+
"--list-presets"]
95+
96+
begin
97+
require "open3"
98+
build_env = { "SOURCE_DATE_EPOCH" => Gem.source_date_epoch_string }
99+
stdout, _, _ = Open3.capture3(build_env, *cmd, chdir: cmake_dir)
100+
message = <<~EOS
101+
The gem author provided a list of presets that can be used to build the gem. To use a preset specify it on the command line:
102+
103+
gem install <gem_name> -- --preset <preset_name>
104+
105+
#{stdout}
106+
EOS
107+
108+
STDOUT << message << "\n"
109+
STDOUT.flush
110+
rescue Gem::InstallError
111+
# Do nothing, CMakePresets.json was not included in the Gem
112+
end
113+
end
21114
end

test/rubygems/test_gem_ext_cmake_builder.rb

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def setup
2929
def test_self_build
3030
File.open File.join(@ext, "CMakeLists.txt"), "w" do |cmakelists|
3131
cmakelists.write <<-EO_CMAKE
32-
cmake_minimum_required(VERSION 3.5)
32+
cmake_minimum_required(VERSION 3.26)
3333
project(self_build NONE)
3434
install (FILES test.txt DESTINATION bin)
3535
EO_CMAKE
@@ -39,46 +39,47 @@ def test_self_build
3939

4040
output = []
4141

42-
Gem::Ext::CmakeBuilder.build nil, @dest_path, output, [], nil, @ext
42+
builder = Gem::Ext::CmakeBuilder.new
43+
builder.build nil, @dest_path, output, [], nil, @ext
4344

4445
output = output.join "\n"
4546

46-
assert_match(/^cmake \. -DCMAKE_INSTALL_PREFIX\\=#{Regexp.escape @dest_path}/, output)
47+
assert_match(/^current directory: #{Regexp.escape @ext}/, output)
48+
assert_match(/cmake.*-DCMAKE_RUNTIME_OUTPUT_DIRECTORY\\=#{Regexp.escape @dest_path}/, output)
49+
assert_match(/cmake.*-DCMAKE_LIBRARY_OUTPUT_DIRECTORY\\=#{Regexp.escape @dest_path}/, output)
4750
assert_match(/#{Regexp.escape @ext}/, output)
48-
assert_contains_make_command "", output
49-
assert_contains_make_command "install", output
50-
assert_match(/test\.txt/, output)
5151
end
5252

5353
def test_self_build_fail
5454
output = []
5555

56+
builder = Gem::Ext::CmakeBuilder.new
5657
error = assert_raise Gem::InstallError do
57-
Gem::Ext::CmakeBuilder.build nil, @dest_path, output, [], nil, @ext
58+
builder.build nil, @dest_path, output, [], nil, @ext
5859
end
5960

6061
output = output.join "\n"
61-
62-
shell_error_msg = /(CMake Error: .*)/
63-
64-
assert_match "cmake failed", error.message
65-
66-
assert_match(/^cmake . -DCMAKE_INSTALL_PREFIX\\=#{Regexp.escape @dest_path}/, output)
67-
assert_match(/#{shell_error_msg}/, output)
62+
assert_match(/CMake Error: .*/, output)
6863
end
6964

7065
def test_self_build_has_makefile
71-
File.open File.join(@ext, "Makefile"), "w" do |makefile|
72-
makefile.puts "all:\n\t@echo ok\ninstall:\n\t@echo ok"
66+
File.open File.join(@ext, "CMakeLists.txt"), "w" do |cmakelists|
67+
cmakelists.write <<-EO_CMAKE
68+
cmake_minimum_required(VERSION 3.26)
69+
project(self_build NONE)
70+
install (FILES test.txt DESTINATION bin)
71+
EO_CMAKE
7372
end
7473

7574
output = []
7675

77-
Gem::Ext::CmakeBuilder.build nil, @dest_path, output, [], nil, @ext
76+
builder = Gem::Ext::CmakeBuilder.new
77+
builder.build nil, @dest_path, output, [], nil, @ext
7878

7979
output = output.join "\n"
8080

81-
assert_contains_make_command "", output
82-
assert_contains_make_command "install", output
81+
# The default generator will create a Makefile in the build directory
82+
makefile = File.join(@ext, "build", "Makefile")
83+
assert(File.exist?(makefile))
8384
end
8485
end

0 commit comments

Comments
 (0)