Class: Pod::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-packager-ext/ext/builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(platform, static_installer, source_dir, static_sandbox_root, dynamic_sandbox_root, public_headers_root, spec, embedded, mangle, dynamic, config, bundle_identifier, exclude_deps, exclude_dep_items = [], select_archs = []) ⇒ Builder

Returns a new instance of Builder.



290
291
292
293
294
295
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 290

def initialize(platform, static_installer, source_dir, static_sandbox_root, dynamic_sandbox_root, public_headers_root, spec, embedded, mangle, dynamic, config, bundle_identifier, exclude_deps,exclude_dep_items=[],select_archs=[])
  @exclude_dep_items = ['libPods-packager.a']
  @exclude_dep_items.concat(exclude_dep_items) if exclude_dep_items
  @select_archs = select_archs
  initialize_t(platform, static_installer,source_dir, static_sandbox_root, dynamic_sandbox_root, public_headers_root, spec, embedded, mangle, dynamic, config, bundle_identifier, exclude_deps)
end

Instance Method Details

#build_dynamic_framework_for_ios(defines, output) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 123

def build_dynamic_framework_for_ios(defines, output)
  # Specify frameworks to link and search paths
  linker_flags = static_linker_flags_in_sandbox
  defines = "#{defines} OTHER_LDFLAGS='$(inherited) #{linker_flags.join(' ')}'"

  # Build Target Dynamic Framework for both device and Simulator
  device_defines = "#{defines} LIBRARY_SEARCH_PATHS=\"#{static_linker_defines}\""
  device_options = ios_build_options << ' -sdk iphoneos'
  xcodebuild(device_defines, device_options, 'build', @spec.name.to_s, @dynamic_sandbox_root.to_s)
  if @select_archs.size == 0 || @select_archs.include?('i386') || @select_archs.include?('x86_64')
    sim_defines = "#{defines} LIBRARY_SEARCH_PATHS=\"#{Dir.pwd}/#{@static_sandbox_root}/build-sim\" ONLY_ACTIVE_ARCH=NO"
    xcodebuild(sim_defines, '-sdk iphonesimulator', 'build-sim', @spec.name.to_s, @dynamic_sandbox_root.to_s)
    # Combine architectures
    `lipo #{@dynamic_sandbox_root}/build/#{@spec.name}.framework/#{@spec.name} #{@dynamic_sandbox_root}/build-sim/#{@spec.name}.framework/#{@spec.name} -create -output #{output}`
  else
    `mv #{@dynamic_sandbox_root}/build/#{@spec.name}.framework/#{@spec.name} #{output}`
  end

  FileUtils.mkdir(@platform.name.to_s)
  `mv #{@dynamic_sandbox_root}/build/#{@spec.name}.framework #{@platform.name}`
  `mv #{@dynamic_sandbox_root}/build/#{@spec.name}.framework.dSYM #{@platform.name}`
end

#build_dynamic_framework_for_ios_tObject



122
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 122

alias build_dynamic_framework_for_ios_t build_dynamic_framework_for_ios

#build_sim_libraries(defines) ⇒ Object



250
251
252
253
254
255
256
257
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 250

def build_sim_libraries(defines)
    if @select_archs.size != 0 && !@select_archs.include?('i386') && !@select_archs.include?('x86_64')
      return 
    else
      UI.puts 'start build sim'
      build_sim_libraries_t defines
    end
end

#build_sim_libraries_tObject



249
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 249

alias build_sim_libraries_t build_sim_libraries

#build_static_libraryObject



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
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 7

def build_static_library

  UI.puts("Building static library #{@spec} with configuration #{@config}")

  defines = compile
  build_sim_libraries(defines)

  platform_path = Pathname.new(@platform.name.to_s)
  platform_path.mkdir unless platform_path.exist?

  output = platform_path + "lib#{@spec.name}-#{@platform.name.to_s}.a"

  if @platform.name == :ios
    build_static_library_for_ios(output)
  else
    build_static_library_for_mac(output)
  end

  platform_path = Pathname.new(@platform.name.to_s)
  header_path = platform_path+'Headers'
  header_path.mkpath unless header_path.exist?

  module_map_path = platform_path+'Modules'
  module_map_path.mkpath unless module_map_path.exist?
  copy_headers_to_target header_path,module_map_path

  resources_path = platform_path+'Resources'
  resources_path.mkpath unless resources_path.exist?
  copy_resources_to_target resources_path
end

#build_static_library_for_ios(output) ⇒ Object



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
67
68
69
70
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 38

def build_static_library_for_ios(output)
  if @exclude_dep_items.length > 0
    static_libs = static_libs_in_sandbox('build') + static_libs_in_sandbox('build-sim') + vendored_libraries
    static_libs.reject! {|lib| @exclude_dep_items.any? {|item| lib.to_s.include? item}}
    UI.section 'Fetch output libs' do
      static_libs.each do |lib|
        UI.message lib
      end
    end
    libs = ios_architectures.map do |arch|
      library = "#{@static_sandbox_root}/build/package-#{arch}.a"
      `libtool -arch_only #{arch} -static -o #{library} #{static_libs.join(' ')}`
      library
    end

    `lipo -create -output #{output} #{libs.join(' ')}`

    # static_libs = static_libs_in_sandbox('build')
    # sim_libs = static_libs_in_sandbox('build-sim')
    # for item in @black_deps
    #   static_libs.delete_if do |obj|
    #     obj.include? item
    #   end
    #   sim_libs.delete_if do |obj|
    #     obj.include? item
    #   end
    # end
    # UI.puts "links statics:#{static_libs.join(' ')}, sim_libs:#{sim_libs.join(' ')}"
    # `xcrun -r libtool -no_warning_for_no_symbols -static -o #{output} #{static_libs.join(' ')} #{sim_libs.join(' ')}`
  else
    build_static_library_for_ios_t output
  end
end

#build_static_library_for_ios_tObject



37
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 37

alias build_static_library_for_ios_t build_static_library_for_ios

#build_static_library_tObject



6
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 6

alias build_static_library_t build_static_library

#compileObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 93

def compile
  if ENV['GCC_PREPROCESSOR_DEFINITIONS']
    defines = "GCC_PREPROCESSOR_DEFINITIONS='$(inherited) PodsDummy_Pods_#{@spec.name}=PodsDummy_PodPackage_#{@spec.name} #{ENV['GCC_PREPROCESSOR_DEFINITIONS']}'"
    UI.puts "Set GCC_PREPROCESSOR_DEFINITIONS:#{defines}"
  else
    defines = "GCC_PREPROCESSOR_DEFINITIONS='$(inherited) PodsDummy_Pods_#{@spec.name}=PodsDummy_PodPackage_#{@spec.name}'"
  end
  defines << ' ' << @spec.consumer(@platform).compiler_flags.join(' ')

  if @platform.name == :ios
    options = ios_build_options
  end

  xcodebuild(defines, options)

  if @mangle
    return build_with_mangling(options)
  end

  defines
end

#compile_tObject



92
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 92

alias compile_t compile

#copy_headersObject



156
157
158
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 156

def copy_headers
  copy_headers_to_target @fwk.headers_path
end

#copy_headers_tObject



155
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 155

alias copy_headers_t copy_headers

#copy_headers_to_target(headers_target_path = nil, module_map_target_path = nil) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 160

def copy_headers_to_target(headers_target_path = nil ,module_map_target_path = nil )
  headers_target_path = @fwk.headers_path if headers_target_path.nil?
  module_map_target_path = @fwk.module_map_path if module_map_target_path.nil?

  headers_source_root = "#{@public_headers_root}/#{@spec.name}"

  Dir.glob("#{headers_source_root}/**/*.h").
    each { |h| `ditto #{h} #{headers_target_path}/#{h.sub(headers_source_root, '')}` }

  # If custom 'module_map' is specified add it to the framework distribution
  # otherwise check if a header exists that is equal to 'spec.name', if so
  # create a default 'module_map' one using it.
  if !@spec.module_map.nil?
    module_map_file = @file_accessors.flat_map(&:module_map).first
    module_map = File.read(module_map_file) if Pathname(module_map_file).exist?
  elsif File.exist?("#{@public_headers_root}/#{@spec.name}/#{@spec.name}.h")
    module_map = <<MAP
framework module #{@spec.name} {
  umbrella header "#{@spec.name}.h"

  export *
  module * { export * }
}
MAP
  else

    f = File.new(File.join("#{headers_target_path}","#{@spec.name}.h"), "w+")
    f.puts("#import <UIKit/UIKit.h>")
    f.puts("#import <Foundation/Foundation.h>")
    Dir.foreach(headers_target_path) do |filename|
      if filename != "." and filename != ".."
        f.puts("#import \"#{filename}\"")
      end
    end
    f.close

    module_map = <<MAP
framework module #{@spec.name} {
  umbrella header "#{@spec.name}.h"

  export *
  module * { export * }
}
MAP
  end

  unless module_map.nil?
    module_map_target_path.mkpath unless module_map_target_path.exist?
    File.write("#{module_map_target_path}/module.modulemap", module_map)
  end
end

#copy_resourcesObject



213
214
215
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 213

def copy_resources
  copy_resources_to_target @fwk.resources_path unless @dynamic
end

#copy_resources_tObject



212
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 212

alias copy_resources_t copy_resources

#copy_resources_to_target(resources_target_path = nil) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 216

def copy_resources_to_target(resources_target_path = nil )
  resources_target_path = @fwk.resources_path if resources_target_path.nil?
  if @exclude_deps
    bundles = @static_installer.pod_targets.flat_map(&:file_accessors).flat_map{|item|item.resources}
  else
    bundles = @static_installer.pod_targets.reject { |t| @exclude_dep_items.any?{|item|t.pod_name == item}}.flat_map(&:file_accessors).flat_map{|item|item.resources}
  end
  if @dynamic
    resources_path = "ios/#{@spec.name}.framework"
    bundles.tap{|path| FileUtils.cp_r path,resources_path}
    `cp -rp #{@static_sandbox_root}/build/*.bundle #{resources_path} 2>&1`
  else
    `cp -rp #{@static_sandbox_root}/build/*.bundle #{resources_path} 2>&1`
    resources = expand_paths(@spec.consumer(@platform).resources)
    if resources.count == 0 && bundles.count == 0
      if @fwk
        @fwk.delete_resources
      else
        if resources_target_path.exist?
          FileUtils.rm_rf resources_target_path
        end
      end
      return
    end
    if resources.count > 0
      `cp -rp #{resources.join(' ')} #{resources_target_path}`
    end
    if bundles.count > 0
      `cp -rp #{bundles.join(' ')} #{resources_target_path}`
    end
  end
end

#initialize_tObject



289
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 289

alias initialize_t initialize

#ios_architecturesObject



147
148
149
150
151
152
153
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 147

def ios_architectures
  if @select_archs.size != 0
    return @select_archs
  else
    return ios_architectures_t
  end
end

#ios_architectures_tObject



146
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 146

alias ios_architectures_t ios_architectures

#ios_build_optionsObject



84
85
86
87
88
89
90
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 84

def ios_build_options
  if ENV['DISABLE_BITCODE'] && (ENV['DISABLE_BITCODE'].upcase == 'YES' || ENV['DISABLE_BITCODE'].upcase == 'TRUE')
    "ARCHS=\'#{ios_architectures.join(' ')}\' OTHER_CFLAGS=\'-Qunused-arguments\'"
  else
    "ARCHS=\'#{ios_architectures.join(' ')}\' OTHER_CFLAGS=\'-fembed-bitcode -Qunused-arguments\'"
  end
end

#static_linker_definesObject



115
116
117
118
119
120
121
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 115

def static_linker_defines
  static_libs = static_libs_in_sandbox('build') + static_libs_in_sandbox('build-sim')
  static_libs = static_libs.map { |path| Pathname("#{Dir.pwd}/#{path}").parent.to_s }
  vendored_libs = vendored_libraries.map { |path| Pathname(path).parent.to_s }
  link_paths = static_libs + vendored_libs
  link_paths.uniq.join(" ")
end

#static_linker_flags_in_sandboxObject



74
75
76
77
78
79
80
81
82
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 74

def static_linker_flags_in_sandbox
  static_libs = static_libs_in_sandbox('build') + static_libs_in_sandbox('build-sim') + vendored_libraries
  linker_flags = static_libs.map do |lib|
    lib = lib.chomp('.a').split('/').last
    lib.slice!('lib')
    "-l#{lib}"
  end
  linker_flags.reject { |e| e == "-l#{@spec.name}" || e == '-lPods-packager' }
end

#static_linker_flags_in_sandbox_tObject



73
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 73

alias static_linker_flags_in_sandbox_t ios_architectures

#xcodebuild(defines = '', args = '', build_dir = 'build', target = 'Pods-packager', project_root = @static_sandbox_root, config = @config) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 261

def xcodebuild(defines = '', args = '', build_dir = 'build', target = 'Pods-packager', project_root = @static_sandbox_root, config = @config)
  if defined?(Pod::DONT_CODESIGN)
    args = "#{args} CODE_SIGN_IDENTITY=\"\" CODE_SIGNING_REQUIRED=NO"
  end

  command = "xcodebuild #{defines} #{args} CONFIGURATION_BUILD_DIR=#{build_dir} clean build -configuration #{config} -target #{target} -project #{project_root}/Pods.xcodeproj 2>&1"
  UI.puts "exec command: #{command}"
  output = `#{command}`.lines.to_a

  if $?.exitstatus != 0
    if ENV['ENABLE_BACKUP_WORKSPACE'] && (ENV['ENABLE_BACKUP_WORKSPACE'].upcase == 'YES' || ENV['ENABLE_BACKUP_WORKSPACE'].upcase == 'TRUE')
      UI.title 'Backup Workspace' do
        target_dir = "#{@source_dir}/#{@spec.name}-#{@spec.version}"
        work_dir = Dir.pwd
        `mv "#{work_dir}" "#{target_dir}"`
      end
    end
    puts UI::BuildFailedReport.report(command, output)

    # Note: We use `Process.exit` here because it fires a `SystemExit`
    # exception, which gives the caller a chance to clean up before the
    # process terminates.
    #
    # See http://ruby-doc.org/core-1.9.3/Process.html#method-c-exit
    Process.exit
  end
end

#xcodebuild_tObject



259
# File 'lib/cocoapods-packager-ext/ext/builder.rb', line 259

alias xcodebuild_t xcodebuild