summaryrefslogtreecommitdiff
path: root/ext/libyajl2/extconf.rb
blob: 52202d1b47824ccf80daa29e0903c676a75e33ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
exit(0) if ENV["USE_SYSTEM_LIBYAJL2"]

require 'pp'

module Libyajl2Build
  class BuildError < StandardError; end

  LIBYAJL2_VENDOR_DIR = File.expand_path("../vendor/yajl", __FILE__).freeze

  PREFIX = File.expand_path("../../../lib/libyajl2/vendored-libyajl2", __FILE__).freeze

  def self.windows?
    !!(RUBY_PLATFORM =~ /mswin|mingw|windows/)
  end

  def self.libyajl2_vendor_dir
    LIBYAJL2_VENDOR_DIR
  end

  def self.configure
    File.join(LIBYAJL2_VENDOR_DIR, "configure")
  end

  def self.prefix
    PREFIX
  end

  def self.configure_cmd
    # NB: this is not a gnu configure command
    %W[
      sh
      #{configure}
      -p
      #{prefix}
    ]
  end

  def self.setup_env
    if windows?
      ENV['CC'] = 'gcc.exe'
      ENV['CXX'] = 'g++.exe'
    end
  end

  def self.system(*args)
    print("-> #{args.join(' ')}\n")
    super(*args)
  end

  def self.run_build_commands
    setup_env
    puts `pwd`
    puts `env`
    puts configure_cmd
    system(*configure_cmd) &&
      system("make", "-j", "5", "install")
  end

  def self.run
    Dir.chdir(libyajl2_vendor_dir) do
      run_build_commands or raise BuildError, "Failed to build libyajl2 library."
    end
  end
end

Libyajl2Build.run