|
| 1 | +require "rake" |
| 2 | +require "rake/tasklib" |
| 3 | + |
| 4 | +module GoGem |
| 5 | + # Provides rake tasks for `go test` with CRuby |
| 6 | + # |
| 7 | + # @example Without config |
| 8 | + # # Rakefile |
| 9 | + # require "go_gem/rake_task" |
| 10 | + # |
| 11 | + # GoGem::RakeTask.new("gem_name") |
| 12 | + # |
| 13 | + # @example With config |
| 14 | + # # Rakefile |
| 15 | + # require "go_gem/rake_task" |
| 16 | + # |
| 17 | + # GoGem::RakeTask.new("gem_name") do |config| |
| 18 | + # config.task_namespace = "go5" |
| 19 | + # config.go_bin_path = "/path/to/go" |
| 20 | + # config.go_test_args = "#{GoGem::RakeTask::DEFAULT_GO_TEST_ARGS} -race" |
| 21 | + # end |
| 22 | + class RakeTask < ::Rake::TaskLib |
| 23 | + # @!attribute [r] gem_name |
| 24 | + # @return [String] |
| 25 | + attr_reader :gem_name |
| 26 | + |
| 27 | + # @!attribute task_namespace |
| 28 | + # @return [Symbol] task namespace (default: `:go`) |
| 29 | + attr_accessor :task_namespace |
| 30 | + |
| 31 | + # @!attribute go_bin_path |
| 32 | + # @return [String] path to go binary (default: `"go"`) |
| 33 | + attr_accessor :go_bin_path |
| 34 | + |
| 35 | + # @!attribute go_test_args |
| 36 | + # @return [String] argument passed to `go test` (default: `"-mod=readonly -count=1"`) |
| 37 | + attr_accessor :go_test_args |
| 38 | + |
| 39 | + # @param gem_name [String] |
| 40 | + # @yield configuration of {RakeTask} |
| 41 | + # @yieldparam config [RakeTask] |
| 42 | + def initialize(gem_name) |
| 43 | + super() |
| 44 | + |
| 45 | + @gem_name = gem_name |
| 46 | + |
| 47 | + @task_namespace = :go |
| 48 | + @go_bin_path = "go" |
| 49 | + @go_test_args = "-mod=readonly -count=1" |
| 50 | + |
| 51 | + yield(self) if block_given? |
| 52 | + |
| 53 | + namespace(task_namespace) do |
| 54 | + desc "Run go test" |
| 55 | + task(:test) do |
| 56 | + within_ext_dir do |
| 57 | + sh RakeTask.build_env_vars, "#{go_bin_path} test #{go_test_args} ./..." |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + # Generate environment variables to build go programs in the Go gem |
| 64 | + # |
| 65 | + # @return [Hash<String, String>] |
| 66 | + def self.build_env_vars |
| 67 | + ldflags = "-L#{RbConfig::CONFIG["libdir"]} -l#{RbConfig::CONFIG["RUBY_SO_NAME"]}" |
| 68 | + |
| 69 | + case `#{RbConfig::CONFIG["CC"]} --version` # rubocop:disable Lint/LiteralAsCondition |
| 70 | + when /Free Software Foundation/ |
| 71 | + ldflags << " -Wl,--unresolved-symbols=ignore-all" |
| 72 | + when /clang/ |
| 73 | + ldflags << " -undefined dynamic_lookup" |
| 74 | + end |
| 75 | + |
| 76 | + cflags = "#{RbConfig::CONFIG["CFLAGS"]} -I#{RbConfig::CONFIG["rubyarchhdrdir"]} -I#{RbConfig::CONFIG["rubyhdrdir"]}" |
| 77 | + |
| 78 | + # FIXME: Workaround for GitHub Actions |
| 79 | + if ENV["GITHUB_ACTIONS"] |
| 80 | + cflags.gsub!("-Wno-self-assign", "") |
| 81 | + cflags.gsub!("-Wno-parentheses-equality", "") |
| 82 | + cflags.gsub!("-Wno-constant-logical-operand", "") |
| 83 | + cflags.gsub!("-Wsuggest-attribute=format", "") |
| 84 | + cflags.gsub!("-Wold-style-definition", "") |
| 85 | + cflags.gsub!("-Wsuggest-attribute=noreturn", "") |
| 86 | + ldflags.gsub!("-Wl,--unresolved-symbols=ignore-all", "") |
| 87 | + end |
| 88 | + |
| 89 | + ld_library_path = RbConfig::CONFIG["libdir"] |
| 90 | + |
| 91 | + { |
| 92 | + "CGO_CFLAGS" => cflags, |
| 93 | + "CGO_LDFLAGS" => ldflags, |
| 94 | + "LD_LIBRARY_PATH" => ld_library_path, |
| 95 | + } |
| 96 | + end |
| 97 | + |
| 98 | + private |
| 99 | + |
| 100 | + # @yield |
| 101 | + def within_ext_dir |
| 102 | + Dir.chdir(ext_dir) do |
| 103 | + yield |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + # @return [String] |
| 108 | + def ext_dir |
| 109 | + File.join("ext", gem_name) |
| 110 | + end |
| 111 | + end |
| 112 | +end |
0 commit comments