Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit a118bb3

Browse files
committed
Merge branch 'backport-compiler-detection'
2 parents 8fc09a4 + ca0aa4a commit a118bb3

7 files changed

Lines changed: 109 additions & 36 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### Unreleased
22

3+
* Enhance compiler version detection and architecture detection #212
34
* Introduce darwin13-15 binary building #211
45
* Disable errors on warning for OS X #210
56
* Improve --with-system-v8 error message #200

ext/libv8/builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def setup_build_deps!
102102
def choose_compiler
103103
compiler_names = if with_config('cxx') then [with_config('cxx')]
104104
elsif ENV['CXX'] then [ENV['CXX']]
105-
else Compiler::KNOWN_COMPILERS
105+
else Compiler::well_known_compilers
106106
end
107107

108108
available_compilers = Compiler.available_compilers(*compiler_names)

ext/libv8/compiler.rb

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,63 @@
1-
require 'mkmf'
2-
require 'open3'
1+
require 'rbconfig'
32
require File.expand_path '../compiler/generic_compiler', __FILE__
43
require File.expand_path '../compiler/gcc', __FILE__
54
require File.expand_path '../compiler/clang', __FILE__
5+
require File.expand_path '../compiler/apple_llvm', __FILE__
66

77
module Libv8
88
module Compiler
9-
KNOWN_COMPILERS = [
10-
'c++',
11-
'g++48', 'g++46', 'g++44', 'g++',
12-
'clang++',
13-
]
14-
159
module_function
1610

17-
def system_compilers
18-
available_compilers *Compiler::KNOWN_COMPILERS
11+
def well_known_compilers
12+
compilers = []
13+
14+
# The default system compiler
15+
compilers << 'c++'
16+
17+
# FreeBSD GCC command names
18+
compilers += ['g++48', 'g++49', 'g++5', 'g++6', 'g++7']
19+
20+
# Default compiler names
21+
compilers += ['clang++', 'g++']
22+
23+
compilers.uniq
1924
end
2025

2126
def available_compilers(*compiler_names)
22-
compiler_paths = compiler_names.map { |name| find name }.reject &:nil?
27+
available = compiler_names.select { |compiler_name| available? compiler_name }
28+
available.map { |compiler_name| type_of(compiler_name).new compiler_name }
2329
end
2430

25-
def find(name)
26-
return nil if name.empty?
27-
path, _, status = Open3.capture3 "which #{name}"
28-
path.chomp!
29-
determine_type(path).new(path) if status.success?
31+
def type_of(compiler_name)
32+
case version_string_of(compiler_name)
33+
when /^Apple LLVM\b/ then AppleLLVM
34+
when /\bclang\b/i then Clang
35+
when /^gcc/i then GCC
36+
else GenericCompiler
37+
end
3038
end
3139

32-
def determine_type(compiler_path)
33-
compiler_version = Open3.capture3("#{compiler_path} -v")[0..1].join
40+
def version_string_of(compiler_name)
41+
command_result = execute_command "#{compiler_name} -v 2>&1"
3442

35-
case compiler_version
36-
when /\bclang\b/i then Clang
37-
when /^gcc/i then GCC
38-
else GenericCompiler
43+
unless command_result.status.success?
44+
raise "Could not get version string of compiler #{compiler_name}"
3945
end
46+
47+
command_result.output
48+
end
49+
50+
def available?(command)
51+
execute_command("which #{command} 2>&1").status.success?
52+
end
53+
54+
def execute_command(command)
55+
output = `env LC_ALL=C LANG=C #{command}`
56+
status = $?
57+
ExecutionResult.new output, status
58+
end
59+
60+
class ExecutionResult < Struct.new(:output, :status)
4061
end
4162
end
4263
end

ext/libv8/compiler/apple_llvm.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Libv8
2+
module Compiler
3+
class AppleLLVM < Clang
4+
LLVM_VERSION_REGEXP = /Apple LLVM version (\d+\.\d+(\.\d+)*) \(/i
5+
6+
def name
7+
'Apple LLVM'
8+
end
9+
10+
def compatible?
11+
version >= '4.3' unless version.nil?
12+
end
13+
14+
private
15+
16+
def version_regexp
17+
LLVM_VERSION_REGEXP
18+
end
19+
end
20+
end
21+
end

ext/libv8/compiler/clang.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
module Libv8
22
module Compiler
33
class Clang < GenericCompiler
4-
VERSION_REGEXP = /clang version (\d+\.\d+(\.\d+)*) \(/i
4+
CLANG_VERSION_REGEXP = /clang version (\d+\.\d+(\.\d+)*) \(/i
55

66
def name
77
'clang'
88
end
99

1010
def compatible?
11-
version >= '3.1'
11+
version >= '3.1' unless version.nil?
12+
end
13+
14+
private
15+
16+
def version_regexp
17+
CLANG_VERSION_REGEXP
1218
end
1319
end
1420
end

ext/libv8/compiler/gcc.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
module Libv8
22
module Compiler
33
class GCC < GenericCompiler
4-
VERSION_REGEXP = /gcc version (\d+\.\d+(\.\d+)*)/i
4+
GCC_VERSION_REGEXP = /gcc version (\d+\.\d+(\.\d+)*)/i
55

66
def name
77
'GCC'
88
end
99

1010
def compatible?
11-
version > '4.3' and version < '5'
11+
version > '4.3' unless version.nil?
12+
end
13+
14+
private
15+
16+
def version_regexp
17+
GCC_VERSION_REGEXP
1218
end
1319
end
1420
end

ext/libv8/compiler/generic_compiler.rb

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
module Libv8
22
module Compiler
33
class GenericCompiler
4-
VERSION_REGEXP = /(\d+\.\d+(\.\d+)*)/
5-
TARGET_REGEXP = /Target: ([a-z0-9\-_.]*)/
4+
GENERIC_VERSION_REGEXP = /(\d+\.\d+(\.\d+)*)/
5+
GENERIC_TARGET_REGEXP = /Target: ([a-z0-9\-_.]*)/
66

7-
def initialize(path)
8-
@path = path
7+
def initialize(command)
8+
@command = command
99
end
1010

1111
def name
12-
File.basename @path
12+
File.basename @command
1313
end
1414

1515
def to_s
16-
@path
16+
@command
1717
end
1818

1919
def version
20-
call('-v')[0..1].join =~ VERSION_REGEXP
20+
version_string =~ version_regexp
2121
$1
2222
end
2323

2424
def target
25-
call('-v')[0..1].join =~ TARGET_REGEXP
25+
version_string =~ target_regexp
2626
$1
2727
end
2828

@@ -31,7 +31,25 @@ def compatible?
3131
end
3232

3333
def call(*arguments)
34-
Open3.capture3 arguments.unshift('env LC_ALL=en', @path).join(' ')
34+
Compiler::execute_command arguments.unshift(@command).join(' ')
35+
end
36+
37+
private
38+
39+
def version_string
40+
begin
41+
Compiler::version_string_of @command
42+
rescue StandardError
43+
nil
44+
end
45+
end
46+
47+
def version_regexp
48+
GENERIC_VERSION_REGEXP
49+
end
50+
51+
def target_regexp
52+
GENERIC_TARGET_REGEXP
3553
end
3654
end
3755
end

0 commit comments

Comments
 (0)