From e241f086ee08eb8350c2b74f58e24f0e6d9b39c3 Mon Sep 17 00:00:00 2001 From: "R. Tyler Croy" Date: Fri, 29 Aug 2014 14:01:42 -0700 Subject: [PATCH] Restructure the Hermann gem extension build process to use rake-compiler This is a little bit better and cleaner IMO than the hand-crafted rake tasks. Would you like to know more? --- .gitignore | 6 ++++-- Gemfile | 1 + Gemfile.lock | 3 +++ Rakefile | 38 ++++++++------------------------- ext/{ => hermann}/extconf.rb | 8 ++++--- ext/{ => hermann}/hermann_lib.c | 0 ext/{ => hermann}/hermann_lib.h | 0 hermann.gemspec | 18 ++++++++++------ 8 files changed, 33 insertions(+), 41 deletions(-) rename ext/{ => hermann}/extconf.rb (67%) rename ext/{ => hermann}/hermann_lib.c (100%) rename ext/{ => hermann}/hermann_lib.h (100%) diff --git a/.gitignore b/.gitignore index 44b67d9..d8b81cf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,9 @@ hermann.iml .ruby-* -ext/*.so -ext/*.o +*.so +*.o ext/mkmf.log *.sw* ext/Makefile +pkg/ +tmp/ diff --git a/Gemfile b/Gemfile index 1c359da..9400786 100644 --- a/Gemfile +++ b/Gemfile @@ -2,6 +2,7 @@ source "https://rubygems.org" gem 'rake' +gem 'rake-compiler' group :test do gem 'rspec', '~> 3.0.0' diff --git a/Gemfile.lock b/Gemfile.lock index f2cb6e1..68fb550 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,6 +3,8 @@ GEM specs: diff-lcs (1.2.5) rake (10.3.2) + rake-compiler (0.9.3) + rake rspec (3.0.0) rspec-core (~> 3.0.0) rspec-expectations (~> 3.0.0) @@ -24,5 +26,6 @@ PLATFORMS DEPENDENCIES rake + rake-compiler rspec (~> 3.0.0) rspec-its diff --git a/Rakefile b/Rakefile index c5d400a..1858e88 100644 --- a/Rakefile +++ b/Rakefile @@ -1,37 +1,17 @@ - require 'rubygems' -require 'rake/clean' +require "bundler/gem_tasks" require 'rspec/core/rake_task' +require 'rake/extensiontask' -EXT_CONF = "ext/extconf.rb" -MAKEFILE = 'ext/Makefile' -MODULE = 'ext/hermann_lib.so' -SRC = Dir.glob('ext/*.c') -SRC << MAKEFILE -CLEAN.include [ 'ext/*.o', 'ext/depend', 'ext/hermann_lib.bundle', MODULE ] -CLOBBER.include [ 'config.save', 'ext/mkmf.log', 'ext/hermann_lib.bundle', MAKEFILE ] - -file MAKEFILE => EXT_CONF do |t| - Dir::chdir(File::dirname(EXT_CONF)) do - unless sh "ruby #{File::basename(EXT_CONF)}" - $stderr.puts "Failed to run extconf" - break - end - end +Rake::ExtensionTask.new do |t| + t.name = 'hermann_lib' + t.ext_dir = 'ext/hermann' + t.gem_spec = Gem::Specification.load('hermann.gemspec') end -file MODULE => SRC do |t| - Dir::chdir(File::dirname(EXT_CONF)) do - unless sh "make" - $stderr.puts "make failed" - end - end -end - -desc "Build the native library" -task :build => MODULE - RSpec::Core::RakeTask.new(:spec) -task :default => [:clean, :build, :spec] +task :build => [:compile] +task :default => [:compile, :spec] + diff --git a/ext/extconf.rb b/ext/hermann/extconf.rb similarity index 67% rename from ext/extconf.rb rename to ext/hermann/extconf.rb index f3562d8..1e460cf 100644 --- a/ext/extconf.rb +++ b/ext/hermann/extconf.rb @@ -12,15 +12,17 @@ puts "Include Dir: #{INCLUDEDIR}" HEADER_DIRS = [INCLUDEDIR] -LIB_DIRS = [LIBDIR] +LIB_DIRS = [LIBDIR, './librdkafka'] dir_config('rdkafka', HEADER_DIRS, LIB_DIRS) -unless find_header('librdkafka/rdkafka.h') +ROOTDIR = File.expand_path(File.dirname(__FILE__) + '/../../') +RDK = File.join(ROOTDIR, 'librdkafka/target') +unless find_header('librdkafka/rdkafka.h', File.join(RDK, 'include')) abort "librdkafka not installed" end -unless find_library('rdkafka', 'rd_kafka_conf_new') +unless find_library('rdkafka', 'rd_kafka_conf_new', File.join(RDK, 'lib')) abort "librdkafka not installed" end diff --git a/ext/hermann_lib.c b/ext/hermann/hermann_lib.c similarity index 100% rename from ext/hermann_lib.c rename to ext/hermann/hermann_lib.c diff --git a/ext/hermann_lib.h b/ext/hermann/hermann_lib.h similarity index 100% rename from ext/hermann_lib.h rename to ext/hermann/hermann_lib.h diff --git a/hermann.gemspec b/hermann.gemspec index ed87d90..6de5073 100644 --- a/hermann.gemspec +++ b/hermann.gemspec @@ -9,15 +9,19 @@ SPEC = Gem::Specification.new do |s| s.authors = ["Stan Campbell", 'R. Tyler Croy'] s.description = 'Ruby gem wrapper for librdkafka' - s.email = ['stan.campbell3@gmail.com', 'rtyler.croy@lookout.com'] - s.files = [ "Rakefile", "ext/hermann_lib.h", "ext/hermann_lib.c", "ext/extconf.rb"] - s.files += `git ls-files -- lib`.split($\) - s.extensions = [ "ext/extconf.rb"] - s.homepage = 'https://github.com/lookout/Hermann' - s.require_paths = ["lib", "ext"] - s.rubygems_version = '2.2.2' s.summary = 'A Kafka consumer/producer gem based on the librdkafka C library.' + s.email = ['stan.campbell3@gmail.com', 'rtyler.croy@lookout.com'] + s.homepage = 'https://github.com/lookout/Hermann' s.licenses = ['MIT'] + s.files = [ "Rakefile"] + s.files += `git ls-files -- lib`.split($\) + s.files += `git ls-files -- ext`.split($\) + + s.extensions = Dir['ext/**/extconf.rb'] + + s.require_paths = ["lib", "ext/hermann"] + s.rubygems_version = '2.2.2' + s.specification_version = 3 if s.respond_to?(:specification_version) end