Skip to content

Commit 8b7154a

Browse files
committed
Implement proof of concept
0 parents  commit 8b7154a

19 files changed

+333
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/pkg
2+
/src
3+
/vendor
4+
/Gemfile.lock
5+
*.gem
6+
/Makefile

CHANGELOG.md

Whitespace-only changes.

Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source "https://rubygems.org"
2+
3+
# Specify your gem's dependencies in libv8.gemspec
4+
gemspec

LICENSE

Whitespace-only changes.

README.md

Whitespace-only changes.

Rakefile

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require 'bundler/setup'
2+
3+
Bundler::GemHelper.install_tasks
4+
5+
module Helpers
6+
module_function
7+
8+
def binary_gemspec(platform = Gem::Platform.local)
9+
gemspec = eval(File.read('libv8-node.gemspec'))
10+
gemspec.platform = platform
11+
gemspec
12+
end
13+
14+
def binary_gem_name(platform = Gem::Platform.local)
15+
File.basename(binary_gemspec(platform).cache_file)
16+
end
17+
end
18+
19+
task :compile do
20+
#sh 'ruby ext/libv8-node/extconf.rb'
21+
end
22+
23+
task :binary => :compile do
24+
gemspec = Helpers.binary_gemspec
25+
gemspec.extensions.clear
26+
27+
# We don't need most things for the binary
28+
gemspec.files = []
29+
gemspec.files += ['lib/libv8-node.rb', 'lib/libv8_node.rb', 'lib/libv8_node/version.rb']
30+
gemspec.files += ['ext/libv8-node/location.rb', 'ext/libv8-node/paths.rb']
31+
gemspec.files += ['ext/libv8-node/.location.yml']
32+
33+
# V8
34+
gemspec.files += Dir['vendor/v8/include/**/*.h']
35+
gemspec.files += Dir['vendor/v8/out.gn/**/*.a']
36+
37+
FileUtils.chmod(0o0644, gemspec.files)
38+
FileUtils.mkdir_p('pkg')
39+
40+
package = if Gem::VERSION < '2.0.0'
41+
Gem::Builder.new(gemspec).build
42+
else
43+
require 'rubygems/package'
44+
Gem::Package.build(gemspec)
45+
end
46+
47+
FileUtils.mv(package, 'pkg')
48+
end

build-libv8

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/sh
2+
3+
set -e
4+
set -u
5+
6+
version="${1}"
7+
8+
cd "src/node-${version}"
9+
10+
# taken from
11+
# ./configure
12+
# make
13+
# make -C out BUILDTYPE=Release V=1
14+
15+
python configure.py --openssl-no-asm --without-npm
16+
make BUILDTYPE=Release out/Makefile
17+
make -C out BUILDTYPE=Release V=1 v8
18+
make -C out BUILDTYPE=Release V=1 v8_libbase
19+
make -C out BUILDTYPE=Release V=1 v8_libplatform
20+
make -C out BUILDTYPE=Release V=1 v8_libsampler
21+

build-monolith

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/sh
2+
3+
set -e
4+
set -u
5+
6+
version="${1}"
7+
8+
cd "src/node-${version}"
9+
10+
BASEDIR="${PWD}"
11+
BUILDTYPE="${BUILDTYPE:-Release}"
12+
LIBV8_MONOLITH="libv8_monolith.a"
13+
14+
cd "out/${BUILDTYPE}/obj.target"
15+
16+
platform=$(uname)
17+
18+
rm -vf "${LIBV8_MONOLITH}"
19+
case "${platform}" in
20+
"SunOS")
21+
/usr/xpg4/bin/find . -path "**/v8*/**/*.o" | xargs ar cqs "${LIBV8_MONOLITH}"
22+
/usr/xpg4/bin/find . -path "**/icu*/**/*.o" | xargs ar cqs "${LIBV8_MONOLITH}"
23+
;;
24+
"Darwin")
25+
#/usr/bin/find . -path "**/v8*/**/*.o" | xargs ar -q "${LIBV8_MONOLITH}"
26+
#/usr/bin/find . -path "**/icu*/**/*.o" | xargs ar -q "${LIBV8_MONOLITH}"
27+
/usr/bin/find . -path "**/v8*/**/*.o" -or -path "**/icu*/**/*.o" | sort | uniq | xargs ar -q "${LIBV8_MONOLITH}"
28+
;;
29+
"Linux")
30+
find . -path './deps/v8/gypfiles/*.a' | while read -r lib; do
31+
ar -t "${lib}" | xargs ar -q "${LIBV8_MONOLITH}"
32+
done
33+
find . -path './tools/icu/*.a' | while read -r lib; do
34+
ar -t "${lib}" | xargs ar -q "${LIBV8_MONOLITH}"
35+
done
36+
;;
37+
*)
38+
echo "Unsupported platform: ${platform}"
39+
exit 1
40+
;;
41+
esac
42+
43+
mv -vf "${LIBV8_MONOLITH}" "${BASEDIR}/out/${BUILDTYPE}/${LIBV8_MONOLITH}"

download-node

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
3+
set -e
4+
set -u
5+
6+
version="${1}"
7+
8+
mkdir -p src
9+
10+
curl -L -o "src/node-${version}.tar.gz" "https://github.com/nodejs/node/archive/v${version}.tar.gz"

ext/libv8-node/.location.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--- !ruby/object:Libv8Node::Location::Vendor {}
2+

ext/libv8-node/extconf.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'mkmf'
2+
create_makefile('libv8-node')
3+
4+
#require File.expand_path('../location', __FILE__)
5+
#location = Libv8Node::Location::Vendor.new
6+
7+
#exit location.install!
8+
exit 0

ext/libv8-node/location.rb

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
require 'yaml'
2+
require 'pathname'
3+
require File.expand_path '../paths', __FILE__
4+
5+
module Libv8Node
6+
class Location
7+
def install!
8+
File.open(Pathname(__FILE__).dirname.join('.location.yml'), "w") do |f|
9+
f.write self.to_yaml
10+
end
11+
return 0
12+
end
13+
14+
def self.load!
15+
File.open(Pathname(__FILE__).dirname.join('.location.yml')) do |f|
16+
YAML.load f
17+
end
18+
end
19+
20+
class Vendor < Location
21+
def install!
22+
require File.expand_path '../builder', __FILE__
23+
builder = Libv8Node::Builder.new
24+
exit_status = builder.build_libv8!
25+
super if exit_status == 0
26+
verify_installation!
27+
return exit_status
28+
end
29+
30+
def configure(context = MkmfContext.new)
31+
context.incflags.insert 0, Libv8Node::Paths.include_paths.map{ |p| "-I#{p}" }.join(" ") + " "
32+
context.ldflags.insert 0, Libv8Node::Paths.object_paths.join(" ") + " "
33+
end
34+
35+
def verify_installation!
36+
include_paths = Libv8Node::Paths.include_paths
37+
unless include_paths.detect { |p| Pathname(p).join('v8.h').exist? }
38+
fail HeaderNotFound, "Unable to locate 'v8.h' in the libv8 header paths: #{include_paths.inspect}"
39+
end
40+
Libv8Node::Paths.object_paths.each do |p|
41+
fail ArchiveNotFound, p unless File.exist? p
42+
end
43+
end
44+
45+
class HeaderNotFound < StandardError; end
46+
47+
class ArchiveNotFound < StandardError
48+
def initialize(filename)
49+
super "libv8 did not install properly, expected binary v8 archive '#{filename}'to exist, but it was not found"
50+
end
51+
end
52+
end
53+
54+
class System < Location
55+
def configure(context = MkmfContext.new)
56+
context.send(:dir_config, 'v8')
57+
context.send(:find_header, 'v8.h') or fail NotFoundError
58+
context.send(:find_header, 'libplatform/libplatform.h') or fail NotFoundError
59+
context.send(:have_library, 'v8') or fail NotFoundError
60+
end
61+
62+
class NotFoundError < StandardError
63+
def initialize(*args)
64+
super(<<-EOS)
65+
By using --with-system-v8, you have chosen to use the version
66+
of V8 found on your system and *not* the one that is bundled with
67+
the libv8 rubygem.
68+
69+
However, your system version of v8 could not be located.
70+
71+
Please make sure your system version of v8 that is compatible
72+
with #{Libv8Node::VERSION} installed. You may need to use the
73+
--with-v8-dir option if it is installed in a non-standard location
74+
EOS
75+
end
76+
end
77+
end
78+
79+
class MkmfContext
80+
def incflags
81+
$INCFLAGS
82+
end
83+
84+
def ldflags
85+
$LDFLAGS
86+
end
87+
end
88+
end
89+
end

ext/libv8-node/paths.rb

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'rbconfig'
2+
require 'shellwords'
3+
4+
module Libv8Node
5+
module Paths
6+
module_function
7+
8+
def include_paths
9+
[Shellwords.escape(File.join(vendored_source_path, 'include'))]
10+
end
11+
12+
def object_paths
13+
[Shellwords.escape(File.join(vendored_source_path,
14+
'out.gn',
15+
'libv8',
16+
'obj',
17+
"libv8_monolith.#{config['LIBEXT']}"))]
18+
end
19+
20+
def config
21+
RbConfig::MAKEFILE_CONFIG
22+
end
23+
24+
def vendored_source_path
25+
File.expand_path "../../../vendor/v8", __FILE__
26+
end
27+
end
28+
end

extract-node

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
3+
set -e
4+
set -u
5+
6+
version="${1}"
7+
8+
tar -C src -xz -f "src/node-${version}.tar.gz"

inject-libv8

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/sh
2+
3+
set -e
4+
set -u
5+
6+
version="${1}"
7+
top="${PWD}"
8+
9+
cd "src/node-${version}"
10+
11+
BASEDIR="${PWD}"
12+
BUILDTYPE="${BUILDTYPE:-Release}"
13+
14+
cd "${BASEDIR}/deps/v8/include"
15+
16+
find . -name '*.h' | while read -r header; do
17+
dir="${top}/vendor/v8/include/$(dirname "${header}")"
18+
mkdir -p "${dir}"
19+
cp "${header}" "${dir}"
20+
done
21+
22+
cd "${BASEDIR}/out/${BUILDTYPE}"
23+
24+
for lib in libv8_libbase.a libv8_libplatform.a libv8_monolith.a; do
25+
dir="${top}/vendor/v8/out.gn/libv8/obj/$(dirname "${lib}")"
26+
mkdir -p "${dir}"
27+
rm -fv "${dir}/${lib}"
28+
strip -S -x -o "${dir}/${lib}" "${lib}"
29+
done

lib/libv8-node.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'libv8_node'

lib/libv8_node.rb

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require 'libv8_node/version'
2+
require 'libv8-node/location'
3+
4+
module Libv8Node
5+
def self.configure_makefile
6+
location = Location.load!
7+
location.configure
8+
end
9+
end
10+

lib/libv8_node/version.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Libv8Node
2+
VERSION = "12.18.4.0.beta1"
3+
NODE_VERSION = "12.18.4"
4+
LIBV8_VERSION = "7.8.279.23" # v8/include/v8-version.h
5+
end

libv8-node.gemspec

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
2+
require 'libv8_node/version'
3+
4+
Gem::Specification.new do |s|
5+
s.name = 'libv8-node'
6+
s.version = Libv8Node::VERSION
7+
s.platform = Gem::Platform::RUBY
8+
s.authors = ['']
9+
s.email = ['']
10+
s.homepage = 'https://github.com/sqreen/libv8-node'
11+
s.summary = "Node.JS's V8 JavaScript engine"
12+
s.description = "Node.JS's V8 JavaScript engine for multiplatform goodness"
13+
s.license = 'MIT'
14+
15+
s.files = `git ls-files`.split("\n")
16+
17+
s.extensions = ['ext/libv8-node/extconf.rb']
18+
s.require_paths = ['lib', 'ext']
19+
20+
s.add_development_dependency 'rake', '~> 12'
21+
end

0 commit comments

Comments
 (0)