Class: MilkTea::VendoredCLibrary::Archive

Inherits:
Base
  • Object
show all
Defined in:
lib/milk_tea/bindings/vendored_c_library.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#include_roots, #name, #source_root

Instance Method Summary collapse

Methods inherited from Base

#include_flags

Constructor Details

#initialize(name:, source_root:, build_root:, archive_name:, sources:, include_roots: [], defines: [], system_link_flags: [], cc_env_var: nil, c_flags: [], cxx_env_var: nil, cxx_flags: [], ar_env_var: nil, default_ar: "ar") ⇒ Archive

Returns a new instance of Archive.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/milk_tea/bindings/vendored_c_library.rb', line 81

def initialize(name:, source_root:, build_root:, archive_name:, sources:, include_roots: [], defines: [], system_link_flags: [], cc_env_var: nil, c_flags: [], cxx_env_var: nil, cxx_flags: [], ar_env_var: nil, default_ar: "ar")
  resolved_include_roots = include_roots.empty? ? [source_root] : include_roots
  super(name:, source_root:, include_roots: resolved_include_roots, cc_env_var:)
  @build_root = Pathname.new(File.expand_path(build_root.to_s))
  @archive_path = @build_root.join(archive_name)
  @sources = sources.dup.freeze
  @defines = defines.dup.freeze
  @system_link_flags = system_link_flags.dup.freeze
  @c_flags = c_flags.dup.freeze
  @cxx_env_var = cxx_env_var
  @cxx_flags = cxx_flags.dup.freeze
  @ar_env_var = ar_env_var
  @default_ar = default_ar
end

Instance Attribute Details

#archive_pathObject (readonly)

Returns the value of attribute archive_path.



79
80
81
# File 'lib/milk_tea/bindings/vendored_c_library.rb', line 79

def archive_path
  @archive_path
end

#build_rootObject (readonly)

Returns the value of attribute build_root.



79
80
81
# File 'lib/milk_tea/bindings/vendored_c_library.rb', line 79

def build_root
  @build_root
end

#definesObject (readonly)

Returns the value of attribute defines.



79
80
81
# File 'lib/milk_tea/bindings/vendored_c_library.rb', line 79

def defines
  @defines
end

#sourcesObject (readonly)

Returns the value of attribute sources.



79
80
81
# File 'lib/milk_tea/bindings/vendored_c_library.rb', line 79

def sources
  @sources
end

Instance Method Details

#build_flags(platform: nil) ⇒ Object



100
101
102
# File 'lib/milk_tea/bindings/vendored_c_library.rb', line 100

def build_flags(platform: nil)
  [*include_flags, *defines.map { |define| "-D#{define}" }].uniq
end


96
97
98
# File 'lib/milk_tea/bindings/vendored_c_library.rb', line 96

def link_flags(platform: nil)
  ["-L#{build_root}", *@system_link_flags]
end

#prepare!(env: ENV, cc: ENV.fetch("CC", "cc"), cxx: ENV.fetch("CXX", "c++"), platform: nil) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/milk_tea/bindings/vendored_c_library.rb', line 104

def prepare!(env: ENV, cc: ENV.fetch("CC", "cc"), cxx: ENV.fetch("CXX", "c++"), platform: nil)
  resolved_cc = resolved_cc(env, cc)
  resolved_cxx = resolved_cxx(env, cxx)
  resolved_ar = env.fetch(@ar_env_var || "AR", @default_ar)
  signature = configuration_signature(cc: resolved_cc, cxx: resolved_cxx, ar: resolved_ar)
  signature_changed = signature != read_signature(build_root)

  FileUtils.mkdir_p(build_root)
  object_paths = sources.map do |source|
    build_object(source, cc: resolved_cc, cxx: resolved_cxx, force_rebuild: signature_changed)
  end
  if !signature_changed && archive_up_to_date?(object_paths)
    ensure_archive_index(ar: resolved_ar)
    return archive_path.to_s
  end

  stdout, stderr, status = Open3.capture3(resolved_ar, "rcs", archive_path.to_s, *object_paths)
  unless status.success?
    raise Error, command_error("failed to archive vendored #{name}", stdout, stderr)
  end
  ensure_archive_index(ar: resolved_ar)

  write_signature(build_root, signature)
  archive_path.to_s
rescue Errno::ENOENT => e
  raise tool_not_found(e)
end