[ruby/json] Reduce duplication in extconf.rb

https://github.com/ruby/json/commit/3ae3eeb9d3
This commit is contained in:
Jean Boussier 2025-06-29 12:04:34 +02:00 committed by Hiroshi SHIBATA
parent 50b6cd409a
commit bc334be4db
4 changed files with 29 additions and 52 deletions

25
ext/json/ext/simd/conf.rb Normal file
View File

@ -0,0 +1,25 @@
if RbConfig::CONFIG['host_cpu'] =~ /^(arm.*|aarch64.*)/
# Try to compile a small program using NEON instructions
if have_header('arm_neon.h')
have_type('uint8x16_t', headers=['arm_neon.h']) && try_compile(<<~'SRC')
#include <arm_neon.h>
int main() {
uint8x16_t test = vdupq_n_u8(32);
return 0;
}
SRC
$defs.push("-DJSON_ENABLE_SIMD")
end
end
if have_header('x86intrin.h') && have_type('__m128i', headers=['x86intrin.h']) && try_compile(<<~'SRC')
#include <x86intrin.h>
int main() {
__m128i test = _mm_set1_epi8(32);
return 0;
}
SRC
$defs.push("-DJSON_ENABLE_SIMD")
end
have_header('cpuid.h')

View File

@ -9,31 +9,7 @@ else
$defs << "-DJSON_DEBUG" if ENV["JSON_DEBUG"]
if enable_config('generator-use-simd', default=!ENV["JSON_DISABLE_SIMD"])
if RbConfig::CONFIG['host_cpu'] =~ /^(arm.*|aarch64.*)/
# Try to compile a small program using NEON instructions
if have_header('arm_neon.h')
have_type('uint8x16_t', headers=['arm_neon.h']) && try_compile(<<~'SRC')
#include <arm_neon.h>
int main() {
uint8x16_t test = vdupq_n_u8(32);
return 0;
}
SRC
$defs.push("-DJSON_ENABLE_SIMD")
end
end
if have_header('x86intrin.h') && have_type('__m128i', headers=['x86intrin.h']) && try_compile(<<~'SRC')
#include <x86intrin.h>
int main() {
__m128i test = _mm_set1_epi8(32);
return 0;
}
SRC
$defs.push("-DJSON_ENABLE_SIMD")
end
have_header('cpuid.h')
require_relative "../simd/conf.rb"
end
create_makefile 'json/ext/generator'

View File

@ -52,7 +52,7 @@ spec = Gem::Specification.new do |s|
s.files += Dir["lib/json/ext/**/*.jar"]
else
s.extensions = Dir["ext/json/**/extconf.rb"]
s.files += Dir["ext/json/**/*.{c,h}"]
s.files += Dir["ext/json/**/*.{c,h,rb}"]
end
end

View File

@ -9,31 +9,7 @@ have_func("strnlen", "string.h") # Missing on Solaris 10
append_cflags("-std=c99")
if enable_config('parser-use-simd', default=!ENV["JSON_DISABLE_SIMD"])
if RbConfig::CONFIG['host_cpu'] =~ /^(arm.*|aarch64.*)/
# Try to compile a small program using NEON instructions
if have_header('arm_neon.h')
have_type('uint8x16_t', headers=['arm_neon.h']) && try_compile(<<~'SRC')
#include <arm_neon.h>
int main() {
uint8x16_t test = vdupq_n_u8(32);
return 0;
}
SRC
$defs.push("-DJSON_ENABLE_SIMD")
end
end
if have_header('x86intrin.h') && have_type('__m128i', headers=['x86intrin.h']) && try_compile(<<~'SRC')
#include <x86intrin.h>
int main() {
__m128i test = _mm_set1_epi8(32);
return 0;
}
SRC
$defs.push("-DJSON_ENABLE_SIMD")
end
have_header('cpuid.h')
end
require_relative "../simd/conf.rb"
end
create_makefile 'json/ext/parser'