Skip to content

Commit 719dc4b

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 719dc4b

File tree

3 files changed

+110
-38
lines changed

3 files changed

+110
-38
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: 100 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,113 @@
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 #{build_dir}",
69+
"-DCMAKE_RUNTIME_OUTPUT_DIRECTORY=#{lib_dir}", # Windows
70+
"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=#{lib_dir}", # Not Windows
71+
*Gem::Command.build_args,
72+
*args]
73+
74+
runner.call(cmd, results, "cmake", cmake_dir)
75+
end
76+
77+
def compile(cmake_dir, build_dir, args, results)
78+
cmd = ["cmake",
79+
"--build",
80+
build_dir.to_s,
81+
"--config",
82+
@profile.to_s]
83+
runner.call(cmd, results, "cmake", cmake_dir)
84+
end
85+
86+
private
87+
88+
def check_presets(cmake_dir, args)
89+
# Return if the user specified a preset
90+
return unless args.grep(/--preset/i).empty?
91+
92+
cmd = ["cmake",
93+
"--list-presets"]
94+
95+
begin
96+
require "open3"
97+
build_env = { "SOURCE_DATE_EPOCH" => Gem.source_date_epoch_string }
98+
stdout, _, _ = Open3.capture3(build_env, *cmd, chdir: cmake_dir)
99+
message = <<~EOS
100+
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:
101+
102+
gem install <gem_name> -- --preset <preset_name>
103+
104+
#{stdout}
105+
EOS
106+
107+
STDOUT << message << "\n"
108+
STDOUT.flush
109+
rescue Gem::InstallError
110+
# Do nothing, CMakePresets.json was not included in the Gem
111+
end
112+
end
21113
end

test/rubygems/test_gem_ext_cmake_builder.rb

Lines changed: 9 additions & 29 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,26 @@ 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)
68-
end
69-
70-
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"
73-
end
74-
75-
output = []
76-
77-
Gem::Ext::CmakeBuilder.build nil, @dest_path, output, [], nil, @ext
78-
79-
output = output.join "\n"
80-
81-
assert_contains_make_command "", output
82-
assert_contains_make_command "install", output
62+
assert_match(/CMake Error: .*/, output)
8363
end
8464
end

0 commit comments

Comments
 (0)