Skip to content

Commit

Permalink
cmake: only use MSYS/NMake generators when available
Browse files Browse the repository at this point in the history
When cross-compiling Windows targets with `cmake` in a Linux
environment, the MSYS generator may not be available. Supplying `-G
MSYS` will cause the build to fail.

`cmake --help` will output the available generators. Before including
the `-G` option, check that the generator is available.

Closes flavorjones#127
  • Loading branch information
stanhu committed Jul 17, 2023
1 parent 5084a2a commit ffcf78b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/mini_portile2/mini_portile_cmake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ def initialize(name, version, **kwargs)
end

def configure_defaults
if MiniPortile.mswin?
if MiniPortile.mswin? && generator_available?('NMake')
['-G', 'NMake Makefiles']
elsif MiniPortile.mingw?
elsif MiniPortile.mingw? && generator_available?('MSYS')
['-G', 'MSYS Makefiles']
else
[]
Expand Down Expand Up @@ -48,4 +48,16 @@ def make_cmd
def cmake_cmd
(ENV["CMAKE"] || @cmake_command || "cmake").dup
end

private

def generator_available?(generator_type)
action = 'cmake_help'
execute(action, [cmake_cmd, '--help'])
filename = log_file(action)

raise 'Unable to determine whether CMake supports #{generator_type} Makefile generator' unless File.exist?(filename)

File.read(filename).include?("#{generator_type} Makefiles")
end
end
64 changes: 64 additions & 0 deletions test/test_cmake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,48 @@ def test_make_command_configuration
end
end

def test_configure_defaults_with_unix_makefiles
script = write_cmake_help("Unix")

MiniPortile.stub(:mingw?, true) do
with_env("CMAKE"=>script.path) do
test_recipe = cmake_test_recipe

assert_equal([], test_recipe.configure_defaults)
end
end
ensure
script&.unlink
end

def test_configure_defaults_with_msys_makefiles
script = write_cmake_help("MSYS")

MiniPortile.stub(:mingw?, true) do
with_env("CMAKE"=>script.path) do
test_recipe = cmake_test_recipe

assert_equal(['-G', 'MSYS Makefiles'], test_recipe.configure_defaults)
end
end
ensure
script&.unlink
end

def test_configure_defaults_with_nmake_makefiles
script = write_cmake_help("NMake")

MiniPortile.stub(:mswin?, true) do
with_env("CMAKE"=>script.path) do
test_recipe = cmake_test_recipe

assert_equal(['-G', 'NMake Makefiles'], test_recipe.configure_defaults)
end
end
ensure
script&.unlink
end

def test_cmake_command_configuration
without_env("CMAKE") do
assert_equal("cmake", MiniPortileCMake.new("test", "1.0.0").cmake_cmd)
Expand All @@ -87,4 +129,26 @@ def test_cmake_command_configuration
assert_equal("asdf", MiniPortileCMake.new("test", "1.0.0", cmake_command: "xyzzy").cmake_cmd)
end
end

private

def cmake_test_recipe
MiniPortileCMake.new("test-cmake", "1.0") do |recipe|
recipe.file = File.expand_path("../../tmp/test-cmake-1.0.tar.gz", __FILE__)
recipe.cook
end
end

def write_cmake_help(generator_type)
cmake_script = Tempfile.new('cmake_help')
cmake_script.puts <<~SCRIPT
#!/bin/sh
echo "The following generators are available on this platform (* marks default):"
echo "* #{generator_type} Makefiles = Generates standard #{generator_type.upcase} makefiles."
SCRIPT
cmake_script.close

File.chmod(0755, cmake_script)
cmake_script
end
end

0 comments on commit ffcf78b

Please sign in to comment.