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 19d9913
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 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
57 changes: 56 additions & 1 deletion test/test_cmake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ def test_install
end
end

class TestCMakeConfig < TestCase
class TestCMakeConfig < TestCMake
def setup
FileUtils.rm_rf(Dir[File.join("tmp", "**", "*.log")])
end

def test_make_command_configuration
MiniPortile.stub(:mswin?, false) do
without_env("MAKE") do
Expand All @@ -77,6 +81,42 @@ 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
assert_equal([], @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
assert_equal(['-G', 'MSYS Makefiles'], @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
assert_equal(['-G', 'NMake Makefiles'], @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 +127,19 @@ def test_cmake_command_configuration
assert_equal("asdf", MiniPortileCMake.new("test", "1.0.0", cmake_command: "xyzzy").cmake_cmd)
end
end

private

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 19d9913

Please sign in to comment.