Class: XCCache::SPM::Macro

Inherits:
Buildable show all
Defined in:
lib/xccache/spm/macro.rb

Instance Attribute Summary collapse

Attributes inherited from Buildable

#config, #library_evolution, #module_name, #name, #path, #pkg_desc, #pkg_dir, #sdk, #sdks, #tmpdir

Instance Method Summary collapse

Methods inherited from Buildable

#pkg_target, #swift_build, #swift_build_args

Constructor Details

#initialize(options = {}) ⇒ Macro

Returns a new instance of Macro.



8
9
10
11
12
# File 'lib/xccache/spm/macro.rb', line 8

def initialize(options = {})
  super
  @library_evolution = false # swift-syntax is not compatible with library evolution
  @macosx_sdk = Swift::Sdk.new(:macosx)
end

Instance Attribute Details

#macosx_sdkObject (readonly)

Returns the value of attribute macosx_sdk.



6
7
8
# File 'lib/xccache/spm/macro.rb', line 6

def macosx_sdk
  @macosx_sdk
end

Instance Method Details

#build(_options = {}) ⇒ Object

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/xccache/spm/macro.rb', line 14

def build(_options = {})
  # NOTE: Building macro binary is tricky...
  # --------------------------------------------------------------------------------
  # Consider this manifest config: .target(Macro) -> .macro(MacroImpl)
  #   where `.target(Macro)` contains the interfaces
  #   and `.target(MacroImpl)` contains the implementation
  # --------------------------------------------------------------------------------
  # Building `.macro(MacroImpl)` does not produce the tool binary (MacroImpl-tool)... Only `.o` files.
  # Yet, linking those files are exhaustive due to many dependencies in swift-syntax
  # Luckily, building `.target(Macro)` does produce the tool binary.
  # -> WORKAROUND: Find the associated regular target and build it, then collect the tool binary
  # ---------------------------------------------------------------------------------
  associated_target = pkg_desc.targets.find { |t| t.direct_dependency_targets.include?(pkg_target) }
  UI.message(
    "#{name.yellow.dark} is a macro target. " \
    "Will build the associated target #{associated_target.name.dark} to get the tool binary."
  )
  swift_build(target: associated_target.name)
  binary_path = products_dir / "#{module_name}-tool"
  raise GeneralError, "Tool binary not exist at: #{binary_path}" unless binary_path.exist?
  binary_path.copy(to: path)
  FileUtils.chmod("+x", path)
  UI.info("-> Macro binary: #{path.to_s.dark}")
end

#products_dirObject



39
40
41
# File 'lib/xccache/spm/macro.rb', line 39

def products_dir
  @products_dir ||= pkg_dir / ".build" / macosx_sdk.triple / config
end