Skip to content

Commit 8abdba1

Browse files
authoredJan 4, 2019
Install H3 via native extension if it isn't present. (#45)
1 parent 4d88581 commit 8abdba1

File tree

10 files changed

+67
-28
lines changed

10 files changed

+67
-28
lines changed
 

‎.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "ext/h3/src"]
2+
path = ext/h3/src
3+
url = https://github.com/uber/h3.git

‎.travis.yml

-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
install:
2-
- git clone https://github.com/uber/h3.git h3_build
3-
- pushd h3_build
4-
- git checkout v3.3.0
5-
- cmake . -DBUILD_SHARED_LIBS=true
6-
- make
7-
- sudo make install
8-
- popd
9-
- bundle install
101
language: ruby
112
cache: bundler
123
rvm:

‎README.md

+22-16
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,13 @@ We have also suffixed predicate methods with a question mark, as per the Ruby co
2424

2525
This gem uses FFI to link directly into the H3 library (written in C).
2626

27-
Before using the gem, you will need to install the C lib at https://github.com/uber/h3.
27+
The H3 library is packaged with the gem and is built as a native extension. H3 is not installed system-wide, so it will not interfere with any other versions you may have installed previously.
2828

29-
Install the build dependencies as instructed here: https://github.com/uber/h3#install-build-time-dependencies
30-
31-
Do *not* follow the Compilation Steps. Instead, use the following:
32-
33-
git clone git@github.com:uber/h3.git h3_build
34-
cd h3_build
35-
cmake . -DBUILD_SHARED_LIBS=true
36-
make
37-
sudo make install
38-
39-
The key difference is the `BUILD_SHARED_LIBS` option.
29+
Before installing the gem, please install the build dependencies for your system as instructed here: https://github.com/uber/h3#install-build-time-dependencies
4030

4131
## Installing
4232

43-
You can install the gem directly using
33+
You can install the gem directly from RubyGems.org using
4434

4535
gem install h3
4636

@@ -74,11 +64,27 @@ H3.h3_to_geo_boundary("8819429a9dfffff".to_i(16))
7464

7565
## Documentation
7666

77-
There is a full reference available here: https://www.rubydoc.info/github/StuartApp/h3_ruby/H3
67+
Please read [the Gem Documentation](https://www.rubydoc.info/github/StuartApp/h3_ruby/H3) for a full reference of available methods.
68+
69+
## Development
70+
71+
The development environment requires the H3 library to be compiled from source before tests can be executed.
72+
73+
This is done automatically by the test suite. However, Rake tasks are provided to handle building H3 in a more fine-grained manner.
74+
75+
### Building H3
76+
77+
rake build
78+
79+
You can remove the compiled H3 library with `rake clean`, or rebuild it with `rake rebuild`.
80+
81+
### Running Tests
82+
83+
The test suite exercises all the H3 functions.
7884

79-
## Running Specs
85+
rake spec
8086

81-
rake
87+
Be aware that errors may be encountered if you have a locally cached H3 binary that's older than the version targeted. Try `rake rebuild` and re-run `rake spec` if this occurs.
8288

8389
## Contributing
8490

‎Rakefile

+24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
11
require "rspec/core/rake_task"
22
RSpec::Core::RakeTask.new(:spec)
3+
4+
desc "Build H3 C library"
5+
task :build do
6+
unless File.exists?("ext/h3/src/Makefile")
7+
`git submodule update --init --recursive`
8+
print "Building h3..."
9+
`cd ext/h3; make > /dev/null 2>&1`
10+
puts " done."
11+
end
12+
end
13+
14+
desc "Remove compiled H3 library"
15+
task :clean do
16+
File.delete("ext/h3/src/Makefile") if File.exists?("ext/h3/src/Makefile")
17+
FileUtils.remove_dir("ext/h3/src/bin") if Dir.exists?("ext/h3/src/bin")
18+
FileUtils.remove_dir("ext/h3/src/generated") if Dir.exists?("ext/h3/src/generated")
19+
FileUtils.remove_dir("ext/h3/src/lib") if Dir.exists?("ext/h3/src/lib")
20+
end
21+
22+
task spec: :build
23+
24+
desc "Recompile the H3 C library"
25+
task rebuild: [:clean, :build]
26+
327
task default: :spec

‎ext/h3/Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
make:
2+
cd src; cmake . -DBUILD_SHARED_LIBS=true; make
3+
install:
4+
: # do nothing, we'll load the lib directly
5+
clean:
6+
: # do nothing, cleanup happens when gem uninstalled

‎ext/h3/extconf.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Do nothing, Makefile is already made.
2+
# This file keeps `gem install` happy.

‎ext/h3/src

Submodule src added at 6af4914

‎h3.gemspec

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
1010
spec.email = "l.laycock@stuart.com"
1111

1212
spec.required_ruby_version = "> 2.3"
13-
spec.files = `git ls-files`.split("\n")
13+
spec.files = `git ls-files --recurse-submodules`.split("\n")
1414

1515
spec.add_runtime_dependency "ffi", "~> 1.9"
1616
spec.add_runtime_dependency "rgeo-geojson", "~> 2.1"
@@ -19,4 +19,6 @@ Gem::Specification.new do |spec|
1919
spec.add_development_dependency "rspec", "~> 3.8"
2020
spec.add_development_dependency "yard", "~> 0.9"
2121
spec.add_development_dependency "coveralls", "~> 0.8"
22+
23+
spec.extensions << "ext/h3/extconf.rb"
2224
end

‎lib/h3/bindings/base.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def self.extended(base)
88
base.extend FFI::Library
99
base.include Structs
1010
base.include Types
11-
base.ffi_lib ["libh3", "libh3.1"]
11+
base.ffi_lib ["ext/h3/src/lib/libh3.dylib", "ext/h3/src/lib/libh3.so"]
1212
base.typedef :ulong_long, :h3_index
1313
base.typedef :int, :size
1414
base.typedef :int, :k_distance

‎spec/spec_helper.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
require "coveralls"
22
Coveralls.wear!
33

4-
require "h3"
4+
begin
5+
require "h3"
6+
rescue LoadError
7+
raise "Could not load h3. Run `rake rebuild` to recompile."
8+
end
59

610
# This file was generated by the `rspec --init` command. Conventionally, all
711
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.

0 commit comments

Comments
 (0)
Please sign in to comment.