Skip to content

Commit

Permalink
Add 'ruby' from-source platform
Browse files Browse the repository at this point in the history
  • Loading branch information
lloeki committed Feb 22, 2021
1 parent f7fec49 commit cf6b17b
Show file tree
Hide file tree
Showing 18 changed files with 187 additions and 78 deletions.
52 changes: 46 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,48 @@ on:
- push

jobs:
build-ruby:
name: Build (ruby)
runs-on: ubuntu-20.04
container: ruby:2.3
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Bundle
run: bundle install
- name: Set metadata
id: set-metadata
run: |
bundle exec ruby -e 'puts RUBY_PLATFORM' | tee ruby_platform
echo "::set-output name=RUBY_PLATFORM::$(cat ruby_platform)"
bundle exec ruby -e 'puts Gem.platforms.last.to_s' | tee gem_platform
echo "::set-output name=GEM_PLATFORM::$(cat gem_platform)"
bundle exec ruby -e 'puts Libv8::Node::VERSION' | tee gem_version
echo "::set-output name=GEM_VERSION::$(cat gem_version)"
bundle exec ruby -e 'puts Libv8::Node::NODE_VERSION' | tee node_version
echo "::set-output name=NODE_VERSION::$(cat node_version)"
bundle exec ruby -e 'puts Libv8::Node::LIBV8_VERSION' | tee libv8_version
echo "::set-output name=LIBV8_VERSION::$(cat libv8_version)"
- name: Download Node.js
run: |
./libexec/download-node ${{ steps.set-metadata.outputs.NODE_VERSION }}
- name: Extract Node.js
run: |
./libexec/extract-node ${{ steps.set-metadata.outputs.NODE_VERSION }}
- name: Validate V8 version
run: |
ruby -e 'h = File.read("src/node-${{ steps.set-metadata.outputs.NODE_VERSION }}/deps/v8/include/v8-version.h"); eval h.each_line.with_object([]) { |l, e| l =~ /#define (V8_[A-Z_]+)\s(\d+)/ && e << "#{$1} = #{$2}" }.join(";"); puts "#{V8_MAJOR_VERSION}.#{V8_MINOR_VERSION}.#{V8_BUILD_NUMBER}.#{V8_PATCH_LEVEL}"' | tee libv8_version_h
diff libv8_version_h libv8_version
- name: Build gem
run: |
bundle exec rake build
- name: Upload gem
uses: actions/upload-artifact@v1
with:
name: gem-${{ steps.set-metadata.outputs.GEM_VERSION }}-ruby
path: pkg
build-macos:
name: Build (macOS)
name: Build (x86_64-darwin)
runs-on: macos-10.15
steps:
- name: Checkout
Expand Down Expand Up @@ -43,7 +83,7 @@ jobs:
- name: Inject V8
run: |
./libexec/inject-libv8 ${{ steps.set-metadata.outputs.NODE_VERSION }}
- name: Build gem
- name: Build binary gems
run: |
bundle exec rake binary:all
- name: Upload V8
Expand All @@ -57,7 +97,7 @@ jobs:
name: gem-${{ steps.set-metadata.outputs.GEM_VERSION }}-${{ steps.set-metadata.outputs.GEM_PLATFORM }}
path: pkg
build-linux-glibc:
name: Build (Linux glibc)
name: Build (x86_64-linux-gnu)
runs-on: ubuntu-20.04
container: ruby:2.3
steps:
Expand Down Expand Up @@ -97,7 +137,7 @@ jobs:
- name: Inject V8
run: |
./libexec/inject-libv8 ${{ steps.set-metadata.outputs.NODE_VERSION }}
- name: Build gem
- name: Build binary gem
run: |
bundle exec rake binary
- name: Upload V8
Expand All @@ -111,7 +151,7 @@ jobs:
name: gem-${{ steps.set-metadata.outputs.GEM_VERSION }}-${{ steps.set-metadata.outputs.GEM_PLATFORM }}
path: pkg
build-linux-musl:
name: Build (Linux musl)
name: Build (x86_64-linux-musl)
runs-on: ubuntu-20.04
container: ruby:2.3-alpine
steps:
Expand Down Expand Up @@ -153,7 +193,7 @@ jobs:
- name: Inject V8
run: |
./libexec/inject-libv8 ${{ steps.set-metadata.outputs.NODE_VERSION }}
- name: Build gem
- name: Build binary gem
run: |
bundle exec rake binary
- name: Prepare artifacts
Expand Down
16 changes: 16 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
AllCops:
TargetRubyVersion: 2.0
Exclude:
- src/**

Style/Documentation:
Enabled: false

Style/ClassAndModuleChildren:
Enabled: false

Style/NumericPredicate:
Enabled: false

Metrics/LineLength:
Enabled: false
3 changes: 1 addition & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
source "https://rubygems.org"
source 'https://rubygems.org'

# Specify your gem's dependencies in libv8.gemspec
gemspec
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) 2009,2010 Charles Lowell

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 changes: 10 additions & 8 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Helpers
def binary_gemspec(platform: Gem::Platform.local, str: RUBY_PLATFORM)
platform.instance_eval { @version = 'musl' } if str =~ /-musl/ && platform.version.nil?

gemspec = eval(File.read('libv8-node.gemspec'))
gemspec = eval(File.read('libv8-node.gemspec')) # rubocop:disable Security/Eval
gemspec.platform = platform
gemspec
end
Expand All @@ -19,7 +19,7 @@ module Helpers
end

task :compile do
#sh 'ruby ext/libv8-node/extconf.rb'
sh 'ruby ext/libv8-node/extconf.rb'
end

task :binary, [:platform] => [:compile] do |_, args|
Expand Down Expand Up @@ -50,15 +50,17 @@ task :binary, [:platform] => [:compile] do |_, args|
end

namespace :binary do
task :all => :binary do
task all: :binary do
return unless RUBY_PLATFORM =~ /darwin-?(\d+)/

Helpers.binary_gemspec # loads NODE_VERSION

current = Integer($1)

Helpers.binary_gemspec # loads NODE_VERSION
major, minor = File.read(Dir["src/node-#{Libv8::Node::NODE_VERSION}/common.gypi"].last).lines.find { |l| l =~ /-mmacosx-version-min=(\d+).(\d+)/ } && [Integer($1), Integer($2)]
first = minor + 4
max = 20

first = minor + 4 # macos 10.X => darwinY offset
first = 20 if RUBY_PLATFORM =~ /^arm46/
max = 20 # current known max

(first..max).each do |version|
next if version == current
Expand All @@ -67,7 +69,7 @@ namespace :binary do
platform.instance_eval { @version = version }
puts "> building #{platform}"

Rake::Task["binary"].execute(Rake::TaskArguments.new([:platform], [platform]))
Rake::Task['binary'].execute(Rake::TaskArguments.new([:platform], [platform]))
end
end
end
4 changes: 1 addition & 3 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Vagrant.configure("2") do |config|
Vagrant.configure('2') do |config|
config.vm.box = 'secretescapes/smartos-base64'
config.vm.box_version = '1.0.0'
config.vm.synced_folder './vagrant', '/usbkey/user_home/vagrant/vagrant',
Expand All @@ -19,5 +19,3 @@ Vagrant.configure("2") do |config|
v.customize ['modifyvm', :id, '--cpus', 2]
end
end


2 changes: 0 additions & 2 deletions ext/libv8-node/.location.yml

This file was deleted.

47 changes: 47 additions & 0 deletions ext/libv8-node/builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
unless $LOAD_PATH.include?(File.expand_path('../../lib', __dir__))
$LOAD_PATH.unshift(File.expand_path('../../lib', __dir__))
end
require 'libv8/node/version'

module Libv8::Node
class BuilderError < StandardError; end

class Builder
def build_libv8!
version = Libv8::Node::NODE_VERSION
download_node(version) || raise(BuilderError, "failed to download node #{NODE_VERSION}")
extract_node(version) || raise(BuilderError, "failed to extract node #{NODE_VERSION}")
build_libv8(version) || raise(BuilderError, "failed to build libv8 #{NODE_VERSION}")
build_monolith(version) || raise(BuilderError, "failed to build monolith #{NODE_VERSION}")
inject_libv8(version) || raise(BuilderError, "failed to inject libv8 #{NODE_VERSION}")

0
end

private

def download_node(version)
system(script('download-node'), version)
end

def extract_node(version)
system(script('extract-node'), version)
end

def build_libv8(version)
system(script('build-libv8'), version)
end

def build_monolith(version)
system(script('build-monolith'), version)
end

def inject_libv8(version)
system(script('inject-libv8'), version)
end

def script(name)
File.expand_path("../../libexec/#{name}", __dir__).tap { |v| p Dir.pwd; p v }
end
end
end
9 changes: 5 additions & 4 deletions ext/libv8-node/extconf.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# frozen_string_literal: true

require 'mkmf'
create_makefile('libv8-node')

#require File.expand_path('../location', __FILE__)
#location = Libv8::Node::Location::Vendor.new
require File.expand_path('location', __dir__)
location = Libv8::Node::Location::Vendor.new

#exit location.install!
exit 0
exit location.install!
60 changes: 22 additions & 38 deletions ext/libv8-node/location.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,55 @@
# frozen_string_literal: true

require 'yaml'
require 'pathname'
require File.expand_path '../paths', __FILE__
require File.expand_path('paths', __dir__)

module Libv8; end

module Libv8::Node
class Location
def install!
File.open(Pathname(__FILE__).dirname.join('.location.yml'), "w") do |f|
f.write self.to_yaml
File.open(Pathname(__FILE__).dirname.join('.location.yml'), 'w') do |f|
f.write(to_yaml)
end
return 0

0
end

def self.load!
File.open(Pathname(__FILE__).dirname.join('.location.yml')) do |f|
YAML.load f
YAML.load(f) # rubocop:disable Security/YAMLLoad
end
end

class Vendor < Location
def install!
require File.expand_path '../builder', __FILE__
require File.expand_path('builder', __dir__)

builder = Libv8::Node::Builder.new
exit_status = builder.build_libv8!

super if exit_status == 0

verify_installation!
return exit_status

exit_status
end

def configure(context = MkmfContext.new)
context.incflags.insert 0, Libv8::Node::Paths.include_paths.map{ |p| "-I#{p}" }.join(" ") + " "
context.ldflags.insert 0, Libv8::Node::Paths.object_paths.join(" ") + " "
context.incflags.insert(0, Libv8::Node::Paths.include_paths.map { |p| "-I#{p}" }.join(' ') << ' ')
context.ldflags.insert(0, Libv8::Node::Paths.object_paths.join(' ') << ' ')
end

def verify_installation!
include_paths = Libv8::Node::Paths.include_paths

unless include_paths.detect { |p| Pathname(p).join('v8.h').exist? }
fail HeaderNotFound, "Unable to locate 'v8.h' in the libv8 header paths: #{include_paths.inspect}"
raise(HeaderNotFound, "Unable to locate 'v8.h' in the libv8 header paths: #{include_paths.inspect}")
end

Libv8::Node::Paths.object_paths.each do |p|
fail ArchiveNotFound, p unless File.exist? p
raise(ArchiveNotFound, p) unless File.exist?(p)
end
end

Expand All @@ -53,38 +62,13 @@ def initialize(filename)
end
end

class System < Location
def configure(context = MkmfContext.new)
context.send(:dir_config, 'v8')
context.send(:find_header, 'v8.h') or fail NotFoundError
context.send(:find_header, 'libplatform/libplatform.h') or fail NotFoundError
context.send(:have_library, 'v8') or fail NotFoundError
end

class NotFoundError < StandardError
def initialize(*args)
super(<<-EOS)
By using --with-system-v8, you have chosen to use the version
of V8 found on your system and *not* the one that is bundled with
the libv8 rubygem.
However, your system version of v8 could not be located.
Please make sure your system version of v8 that is compatible
with #{Libv8::Node::VERSION} installed. You may need to use the
--with-v8-dir option if it is installed in a non-standard location
EOS
end
end
end

class MkmfContext
def incflags
$INCFLAGS
$INCFLAGS # rubocop:disable Style/GlobalVars
end

def ldflags
$LDFLAGS
$LDFLAGS # rubocop:disable Style/GlobalVars
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion ext/libv8-node/paths.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def config
end

def vendored_source_path
File.expand_path "../../../vendor/v8", __FILE__
File.expand_path('../../vendor/v8', __dir__)
end
end
end
6 changes: 3 additions & 3 deletions lib/libv8/node/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Libv8; end

module Libv8::Node
VERSION = "15.5.1.0.beta1"
NODE_VERSION = "15.5.1"
LIBV8_VERSION = "8.6.395.17" # v8/include/v8-version.h
VERSION = '15.5.1.0.beta1'.freeze
NODE_VERSION = '15.5.1'.freeze
LIBV8_VERSION = '8.6.395.17'.freeze # from v8/include/v8-version.h
end
5 changes: 4 additions & 1 deletion libexec/build-libv8
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ set -e
set -u

version="${1}"
libexec="$(cd "$(dirname "$0")"; pwd)"
top="${libexec}/.."
src="${2:-"${top}/src"}"

platform=$(uname)

Expand All @@ -12,7 +15,7 @@ NJOBS="${NJOBS:-1}"

echo "parallel job count: ${NJOBS}"

cd "src/node-${version}"
cd "${src}/node-${version}"

if which python3 >/dev/null 2>&1; then
PYTHON="${PYTHON:-python3}"
Expand Down
Loading

0 comments on commit cf6b17b

Please sign in to comment.