From 7dc1ffaa0a0ef085265f560f23f8bcb5160cbac0 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Mon, 17 Jul 2023 00:52:48 -0700 Subject: [PATCH 1/3] cmake: only use MSYS/NMake generators when available 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 #127 --- lib/mini_portile2/mini_portile_cmake.rb | 15 +++++++-- test/test_cmake.rb | 42 ++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/lib/mini_portile2/mini_portile_cmake.rb b/lib/mini_portile2/mini_portile_cmake.rb index daaeeef..bb7f3e4 100644 --- a/lib/mini_portile2/mini_portile_cmake.rb +++ b/lib/mini_portile2/mini_portile_cmake.rb @@ -1,4 +1,5 @@ require 'mini_portile2/mini_portile' +require 'open3' class MiniPortileCMake < MiniPortile def configure_prefix @@ -11,9 +12,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 +49,14 @@ def make_cmd def cmake_cmd (ENV["CMAKE"] || @cmake_command || "cmake").dup end + + private + + def generator_available?(generator_type) + stdout_str, status = Open3.capture2("#{cmake_cmd} --help") + + raise 'Unable to determine whether CMake supports #{generator_type} Makefile generator' unless status.success? + + stdout_str.include?("#{generator_type} Makefiles") + end end diff --git a/test/test_cmake.rb b/test/test_cmake.rb index a12a099..c64f3f3 100644 --- a/test/test_cmake.rb +++ b/test/test_cmake.rb @@ -59,7 +59,7 @@ def test_install end end -class TestCMakeConfig < TestCase +class TestCMakeConfig < TestCMake def test_make_command_configuration MiniPortile.stub(:mswin?, false) do without_env("MAKE") do @@ -77,6 +77,30 @@ def test_make_command_configuration end end + def test_configure_defaults_with_unix_makefiles + Open3.stub(:capture2, cmake_help_mock('Unix')) do + MiniPortile.stub(:mingw?, true) do + assert_equal([], @recipe.configure_defaults) + end + end + end + + def test_configure_defaults_with_msys_makefiles + Open3.stub(:capture2, cmake_help_mock('MSYS')) do + MiniPortile.stub(:mingw?, true) do + assert_equal(['-G', 'MSYS Makefiles'], @recipe.configure_defaults) + end + end + end + + def test_configure_defaults_with_nmake_makefiles + Open3.stub(:capture2, cmake_help_mock('NMake')) do + MiniPortile.stub(:mswin?, true) do + assert_equal(['-G', 'NMake Makefiles'], @recipe.configure_defaults) + end + end + end + def test_cmake_command_configuration without_env("CMAKE") do assert_equal("cmake", MiniPortileCMake.new("test", "1.0.0").cmake_cmd) @@ -87,4 +111,20 @@ def test_cmake_command_configuration assert_equal("asdf", MiniPortileCMake.new("test", "1.0.0", cmake_command: "xyzzy").cmake_cmd) end end + + private + + def cmake_help_mock(generator_type) + open3_mock = MiniTest::Mock.new + cmake_script = <<~SCRIPT + echo "The following generators are available on this platform (* marks default):" + echo "* #{generator_type} Makefiles = Generates standard #{generator_type.upcase} makefiles." + SCRIPT + + exit_status = MiniTest::Mock.new + exit_status.expect(:success?, true) + expected_output = [cmake_script, exit_status] + open3_mock.expect(:call, expected_output, ['cmake --help']) + open3_mock + end end From 954313b4c24c8ba377217559812b4228e9378afe Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Mon, 17 Jul 2023 12:17:11 -0700 Subject: [PATCH 2/3] Add CHANGELOG.md for CMake fix --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53c5bc3..9dca93e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ ## mini_portile changelog +### 2.9.0 / 2023-07-17 + +#### Fixed + +- cmake: only use MSYS/NMake generators when available. [#129] + + ### 2.8.2 / 2023-04-30 #### Fixed From d39c26f42e479da7dcbe1cae79610019b45a2c59 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Mon, 17 Jul 2023 17:55:40 -0400 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9dca93e..95f8ab0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ ## mini_portile changelog -### 2.9.0 / 2023-07-17 +### next / unreleased #### Fixed