Skip to content

Commit 7ba25e2

Browse files
authored
Merge pull request #763 from libgit2/cmn/cmake-timeout
Give CMake a timeout for when it hangs
2 parents e7c3d3c + 790755c commit 7ba25e2

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

ext/rugged/extconf.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# For full terms see the included LICENSE file.
55

66
require 'mkmf'
7+
require 'timeout'
78

89
RbConfig::MAKEFILE_CONFIG['CC'] = ENV['CC'] if ENV['CC']
910

@@ -23,6 +24,26 @@ def sys(cmd)
2324
ret
2425
end
2526

27+
# Thrown when we detect CMake is taking too long and we killed it
28+
class CMakeTimeout < StandardError
29+
end
30+
31+
def self.run_cmake(timeout, args)
32+
# Set to process group so we can kill it and its children
33+
pid = Process.spawn("cmake #{args}", pgroup: true)
34+
35+
Timeout.timeout(timeout) do
36+
Process.waitpid(pid)
37+
end
38+
39+
rescue Timeout::Error
40+
# Kill it, #detach is essentially a background wait, since we don't actually
41+
# care about waiting for it now
42+
Process.kill(-9, pid)
43+
Process.detach(pid)
44+
raise CMakeTimeout.new("cmake has exceeded its timeout of #{timeout}s")
45+
end
46+
2647
MAKE = if Gem.win_platform?
2748
# On Windows, Ruby-DevKit only has 'make'.
2849
find_executable('make')
@@ -80,7 +101,7 @@ def sys(cmd)
80101
Dir.chdir("build") do
81102
# On Windows, Ruby-DevKit is MSYS-based, so ensure to use MSYS Makefiles.
82103
generator = "-G \"MSYS Makefiles\"" if Gem.win_platform?
83-
sys("cmake .. -DBUILD_CLAR=OFF -DTHREADSAFE=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_FLAGS=-fPIC -DCMAKE_BUILD_TYPE=RelWithDebInfo #{cmake_flags.join(' ')} #{generator}")
104+
run_cmake(5 * 60, ".. -DBUILD_CLAR=OFF -DTHREADSAFE=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_FLAGS=-fPIC -DCMAKE_BUILD_TYPE=RelWithDebInfo #{cmake_flags.join(' ')} #{generator}")
84105
sys(MAKE)
85106

86107
# "normal" libraries (and libgit2 builds) get all these when they build but we're doing it

0 commit comments

Comments
 (0)