|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +require 'optparse' |
| 4 | +require 'fileutils' |
| 5 | +require 'tmpdir' |
| 6 | +require 'shellwords' |
| 7 | + |
| 8 | +$prefix = $cflags = $ldflags = $cc = $makeopts = nil |
| 9 | +OptionParser.new do |opts| |
| 10 | + opts.banner = "Usage: asan_libs.rb --prefix=$RUBY_PREFIX" |
| 11 | + opts.on('--prefix=PREFIX') { $prefix = _1 } |
| 12 | + opts.on('--cflags=CFLAGS') { $cflags = _1 } |
| 13 | + opts.on('--ldflags=LDFLAGS') { $ldflags= _1 } |
| 14 | + opts.on('--cc=CC') { $cc = _1 } |
| 15 | + opts.on('--makeopts=MAKEOPTS') { $makeopts = _1 } |
| 16 | +end.parse! |
| 17 | +raise "--prefix must be specified" if $prefix.nil? |
| 18 | + |
| 19 | + |
| 20 | +OPENSSL_VERSION = "3.3.0" |
| 21 | +LIBYAML_VERSION = "0.2.5" |
| 22 | + |
| 23 | +$cflags_args = [] |
| 24 | +$cflags_args << "CC=#{$cc}" if $cc |
| 25 | +$cflags_args << "CFLAGS=#{$cflags}" if $cflags |
| 26 | +$cflags_args << "LDFLAGS=#{$ldflags}" if $ldflags |
| 27 | +$makeopts = Shellwords.split($makeopts || '') |
| 28 | + |
| 29 | +def sh!(*args, **kwargs) |
| 30 | + puts "==> #{Shellwords.join(args)}" |
| 31 | + system(*args, **kwargs, exception: true) |
| 32 | +end |
| 33 | + |
| 34 | +def chdir!(dir, &block) |
| 35 | + puts "==> cd #{File.realpath dir}" |
| 36 | + Dir.chdir dir, &block |
| 37 | +end |
| 38 | + |
| 39 | +def rmglob!(what) |
| 40 | + Dir.glob(what).each do |file| |
| 41 | + puts "==> rm #{file}" |
| 42 | + FileUtils.rm_f file |
| 43 | + end |
| 44 | +end |
| 45 | + |
| 46 | +def compile_openssl |
| 47 | + # Make sure we compile OpenSSL to look for certificates in the same place that the |
| 48 | + # distribution provided OpenSSl would. |
| 49 | + openssldir = Shellwords.split(`openssl version -d`.match(/^\s*OPENSSLDIR:(.*)$/)[1].strip).first |
| 50 | + |
| 51 | + sh! *%w[curl -fsSLO], "https://www.openssl.org/source/openssl-#{OPENSSL_VERSION}.tar.gz" |
| 52 | + sh! *%w[tar -xf], "openssl-#{OPENSSL_VERSION}.tar.gz" |
| 53 | + chdir!("openssl-#{OPENSSL_VERSION}") do |
| 54 | + sh! './Configure', "--prefix=#{$prefix}", '--libdir=lib', "--openssldir=#{openssldir}", |
| 55 | + *%w[shared no-tests no-apps], *$cflags_args |
| 56 | + sh! 'make', *$makeopts |
| 57 | + # make install_sw will try and write config to OPENSSLDIR, which we don't want to do. |
| 58 | + sh! *%w[make install_dev], exception: true |
| 59 | + # OpenSSL make install_dev will also install static libraries, which we don't need |
| 60 | + rmglob! File.join($prefix, "lib/*.a") |
| 61 | + end |
| 62 | +end |
| 63 | + |
| 64 | +def compile_libyaml |
| 65 | + sh! *%w[curl -fsSLO], "http://pyyaml.org/download/libyaml/yaml-#{LIBYAML_VERSION}.tar.gz" |
| 66 | + sh! *%w[tar -xf], "yaml-#{LIBYAML_VERSION}.tar.gz" |
| 67 | + chdir!("yaml-#{LIBYAML_VERSION}") do |
| 68 | + sh! './configure', "--prefix=#{$prefix}", *%w[--disable-static --enable-shared], *$cflags_args |
| 69 | + sh! 'make', *$makeopts |
| 70 | + sh! 'make', 'install' |
| 71 | + end |
| 72 | +end |
| 73 | + |
| 74 | +Dir.mktmpdir('asan_libs') do |build_dir| |
| 75 | + Dir.chdir(build_dir) do |
| 76 | + compile_openssl |
| 77 | + compile_libyaml |
| 78 | + end |
| 79 | +end |
0 commit comments