|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
| 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 | + |
3 | 34 | 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) |
6 | 43 | if target_rbconfig.path |
7 | 44 | warn "--target-rbconfig is not yet supported for CMake extensions. Ignoring" |
8 | 45 | end |
9 | 46 |
|
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") |
13 | 49 |
|
14 | | - run cmd, results, class_name, cmake_dir |
15 | | - end |
| 50 | + # Figure the build dir |
| 51 | + build_dir = File.join(cmake_dir, "build") |
16 | 52 |
|
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) |
18 | 61 |
|
19 | 62 | results |
20 | 63 | 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 |
21 | 114 | end |
0 commit comments