diff --git a/lib/mini_portile2/mini_portile_cmake.rb b/lib/mini_portile2/mini_portile_cmake.rb index daaeeef..1009f00 100644 --- a/lib/mini_portile2/mini_portile_cmake.rb +++ b/lib/mini_portile2/mini_portile_cmake.rb @@ -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 [] @@ -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 diff --git a/test/test_cmake.rb b/test/test_cmake.rb index a12a099..8f216b8 100644 --- a/test/test_cmake.rb +++ b/test/test_cmake.rb @@ -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) @@ -87,4 +129,30 @@ 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 + cmake_recipe = MiniPortileCMake.new("test-cmake", "1.0") do |recipe| + recipe.file = File.expand_path("../../tmp/test-cmake-1.0.tar.gz", __FILE__) + end + + cmake_recipe.download + cmake_recipe.extract + + cmake_recipe + 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