Module: GoGem::Mkmf

Defined in:
_gem/lib/go_gem/mkmf.rb

Overview

Helper module for creating Go Makefiles

Instance Method Summary collapse

Instance Method Details

#create_go_makefile(target, srcprefix: nil, go_build_args: nil) ⇒ Object

Create Makefile for go-gem

Examples:

require "mkmf"
require "go_gem/mkmf" # Append this

# Use create_go_makefile instead of create_makefile
# create_makefile("example/example")
create_go_makefile("example/example")

Pass debug flags to go build

create_go_makefile("example/example", go_build_args: "-gcflags='all=-N -l'")

Parameters:

  • target (String)
  • srcprefix (String, nil) (defaults to: nil)
  • go_build_args (String, nil) (defaults to: nil)

    Arguments passed to go build



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
# File '_gem/lib/go_gem/mkmf.rb', line 24

def create_go_makefile(target, srcprefix: nil, go_build_args: nil)
  find_executable("go")

  # rubocop:disable Style/GlobalVars
  $objs = []
  # @private
  def $objs.empty?; false; end
  # rubocop:enable Style/GlobalVars

  create_makefile(target, srcprefix)

  ldflags = GoGem::Util.generate_ldflags
  current_dir = File.expand_path(".")

  goflags = "-tags=#{GoGem::Util.ruby_minor_version_build_tag}"

  File.open("Makefile", "a") do |f|
    f.write <<~MAKEFILE.gsub(/^ {8}/, "\t")
      $(DLLIB): Makefile $(srcdir)/*.go
              cd $(srcdir); \
              CGO_CFLAGS='$(INCFLAGS)' CGO_LDFLAGS='#{ldflags}' GOFLAGS='#{goflags}' \
                go build -p 4 -buildmode=c-shared -o #{current_dir}/$(DLLIB) #{go_build_args}
    MAKEFILE
  end
end