Skip to content

Commit

Permalink
Merge 1245900 into acafb18
Browse files Browse the repository at this point in the history
  • Loading branch information
manuyavuz committed Mar 2, 2018
2 parents acafb18 + 1245900 commit e557ec2
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 67 deletions.
155 changes: 92 additions & 63 deletions lib/cocoapods-packager/builder.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Pod
class Builder
def initialize(source_dir, static_sandbox_root, dynamic_sandbox_root, public_headers_root, spec, embedded, mangle, dynamic, config, bundle_identifier, exclude_deps)
def initialize(platform, file_accessors, source_dir, static_sandbox_root, dynamic_sandbox_root, public_headers_root, spec, embedded, mangle, dynamic, config, bundle_identifier, exclude_deps)
@platform == platform
@file_accessors = file_accessors
@source_dir = source_dir
@static_sandbox_root = static_sandbox_root
@dynamic_sandbox_root = dynamic_sandbox_root
Expand All @@ -14,41 +16,52 @@ def initialize(source_dir, static_sandbox_root, dynamic_sandbox_root, public_hea
@exclude_deps = exclude_deps
end

def build(platform, library)
if library
build_static_library(platform)
else
build_framework(platform)
def build(package_type)
if package_type == :static_library
build_static_library
elsif package_type == :static_framework
build_static_framework
elsif package_type == :dynamic_framework
build_dynamic_framework
end
end

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

defines = compile(platform)
build_sim_libraries(platform, defines)
defines = compile
build_sim_libraries(defines)

platform_path = Pathname.new(platform.name.to_s)
platform_path = Pathname.new(@platform.name.to_s)
platform_path.mkdir unless platform_path.exist?
build_library(platform, defines, platform_path + Pathname.new("lib#{@spec.name}.a"))

output = platform_path + Pathname.new("lib#{@spec.name}.a")

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

def build_framework(platform)
UI.puts("Building framework #{@spec} with configuration #{@config}")
def build_static_framework
UI.puts("Building static framework #{@spec} with configuration #{@config}")

defines = compile(platform)
build_sim_libraries(platform, defines)
defines = compile
build_sim_libraries(defines)

create_framework
output = @fwk.versions_path + Pathname.new(@spec.name)

if @dynamic
build_dynamic_framework(platform, defines, "#{@dynamic_sandbox_root}/build/#{@spec.name}.framework/#{@spec.name}")
if @platform.name == :ios
build_static_library_for_ios(output)
else
create_framework(platform.name.to_s)
build_library(platform, defines, @fwk.versions_path + Pathname.new(@spec.name))
copy_headers
copy_license
build_static_library_for_mac(output)
end

copy_resources(platform)
copy_headers
copy_license
copy_resources
end

def link_embedded_resources
Expand All @@ -61,34 +74,29 @@ def link_embedded_resources
end
end

private
def build_dynamic_framework
UI.puts("Building dynamic framework #{@spec} with configuration #{@config}")

def build_dynamic_framework(platform, defines, output)
UI.puts("Building dynamic Framework #{@spec} with configuration #{@config}")
defines = compile
build_sim_libraries(defines)

if @bundle_identifier
defines = "#{defines} PRODUCT_BUNDLE_IDENTIFIER='#{@bundle_identifier}'"
end

output = "#{@dynamic_sandbox_root}/build/#{@spec.name}.framework/#{@spec.name}"

clean_directory_for_dynamic_build
if platform.name == :ios
build_dynamic_framework_for_ios(platform, defines, output)
build_dynamic_framework_for_ios(defines, output)
else
build_dynamic_framework_for_mac(platform, defines, output)
build_dynamic_framework_for_mac(defines, output)
end
end

def build_library(platform, defines, output)
static_libs = static_libs_in_sandbox

if platform.name == :ios
build_static_lib_for_ios(static_libs, defines, output)
else
build_static_lib_for_mac(static_libs, output)
end
copy_resources
end

def build_dynamic_framework_for_ios(platform, defines, output)
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(' ')}'"
Expand All @@ -104,12 +112,12 @@ def build_dynamic_framework_for_ios(platform, defines, output)
# 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}`

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}`
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

def build_dynamic_framework_for_mac(platform, defines, _output)
def build_dynamic_framework_for_mac(defines, _output)
# Specify frameworks to link and search paths
linker_flags = static_linker_flags_in_sandbox
defines = "#{defines} OTHER_LDFLAGS=\"#{linker_flags.join(' ')}\""
Expand All @@ -118,36 +126,37 @@ def build_dynamic_framework_for_mac(platform, defines, _output)
defines = "#{defines} LIBRARY_SEARCH_PATHS=\"#{Dir.pwd}/#{@static_sandbox_root}/build\""
xcodebuild(defines, nil, 'build', @spec.name.to_s, @dynamic_sandbox_root.to_s)

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}`
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

def build_sim_libraries(platform, defines)
if platform.name == :ios
def build_sim_libraries(defines)
if @platform.name == :ios
xcodebuild(defines, '-sdk iphonesimulator', 'build-sim')
end
end

def build_static_lib_for_ios(static_libs, _defines, output)
return if static_libs.count == 0
`libtool -static -o #{@static_sandbox_root}/build/package.a #{static_libs.join(' ')}`

sim_libs = static_libs_in_sandbox('build-sim')
`libtool -static -o #{@static_sandbox_root}/build-sim/package.a #{sim_libs.join(' ')}`

`lipo #{@static_sandbox_root}/build/package.a #{@static_sandbox_root}/build-sim/package.a -create -output #{output}`
def build_static_library_for_ios(output)
static_libs = static_libs_in_sandbox('build') + static_libs_in_sandbox('build-sim') + vendored_libraries
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(' ')}`
end

def build_static_lib_for_mac(static_libs, output)
return if static_libs.count == 0
def build_static_library_for_mac(output)
static_libs = static_libs_in_sandbox + vendored_libraries
`libtool -static -o #{output} #{static_libs.join(' ')}`
end

def build_with_mangling(platform, options)
def build_with_mangling(options)
UI.puts 'Mangling symbols'
defines = Symbols.mangle_for_pod_dependencies(@spec.name, @static_sandbox_root)
defines << ' ' << @spec.consumer(platform).compiler_flags.join(' ')
defines << ' ' << @spec.consumer(@platform).compiler_flags.join(' ')

UI.puts 'Building mangled framework'
xcodebuild(defines, options)
Expand All @@ -163,18 +172,18 @@ def clean_directory_for_dynamic_build
FileUtils.rm_rf('Pods/build')
end

def compile(platform)
def compile
defines = "GCC_PREPROCESSOR_DEFINITIONS='$(inherited) PodsDummy_Pods_#{@spec.name}=PodsDummy_PodPackage_#{@spec.name}'"
defines << ' ' << @spec.consumer(platform).compiler_flags.join(' ')

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

xcodebuild(defines, options)

if @mangle
return build_with_mangling(platform, options)
return build_with_mangling(options)
end

defines
Expand All @@ -190,7 +199,7 @@ def copy_headers
# 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 = "#{@static_sandbox_root}/#{@spec.name}/#{@spec.module_map}"
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
Expand Down Expand Up @@ -262,6 +271,18 @@ def static_libs_in_sandbox(build_dir = 'build')
end
end

def vendored_libraries
if @vendored_libraries
@vendored_libraries
end

libs = []
libs += @file_accessors.flat_map(&:vendored_static_frameworks).map{ |f| f + f.basename}
libs += @file_accessors.flat_map(&:vendored_static_libraries)
@vendored_libraries = libs.compact.map(&:to_s)
@vendored_libraries
end

def static_linker_flags_in_sandbox
linker_flags = static_libs_in_sandbox.map do |lib|
lib.slice!('lib')
Expand All @@ -272,7 +293,15 @@ def static_linker_flags_in_sandbox
end

def ios_build_options
"ARCHS=\'x86_64 i386 arm64 armv7 armv7s\' OTHER_CFLAGS=\'-fembed-bitcode -Qunused-arguments\'"
return "ARCHS=\'#{ios_architectures.join(' ')}\' OTHER_CFLAGS=\'-fembed-bitcode -Qunused-arguments\'"
end

def ios_architectures
archs = ['x86_64', 'i386', 'arm64', 'armv7', 'armv7s']
vendored_libraries.each do |library|
archs = `lipo -info #{library}`.split & archs
end
archs
end

def xcodebuild(defines = '', args = '', build_dir = 'build', target = 'Pods-packager', project_root = @static_sandbox_root, config = @config)
Expand Down
19 changes: 15 additions & 4 deletions lib/pod/command/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,16 @@ def self.options

def initialize(argv)
@embedded = argv.flag?('embedded')
@force = argv.flag?('force')
@library = argv.flag?('library')
@dynamic = argv.flag?('dynamic')
@package_type = if @embedded
:static_framework
elsif @dynamic
:dynamic_framework
elsif @library
:static_library
end
@force = argv.flag?('force')
@mangle = argv.flag?('mangle', true)
@bundle_identifier = argv.option('bundle-identifier', nil)
@exclude_deps = argv.flag?('exclude-deps', false)
Expand Down Expand Up @@ -84,7 +91,7 @@ def build_in_sandbox(platform)
end

begin
perform_build(platform, static_sandbox, dynamic_sandbox)
perform_build(platform, static_sandbox, dynamic_sandbox, static_installer)

ensure # in case the build fails; see Builder#xcodebuild.
Pathname.new(config.sandbox_root).rmtree
Expand Down Expand Up @@ -131,15 +138,19 @@ def create_working_directory
[target_dir, work_dir]
end

def perform_build(platform, static_sandbox, dynamic_sandbox)
def perform_build(platform, static_sandbox, dynamic_sandbox, static_installer)
static_sandbox_root = config.sandbox_root.to_s

if @dynamic
static_sandbox_root = "#{static_sandbox_root}/#{static_sandbox.root.to_s.split('/').last}"
dynamic_sandbox_root = "#{config.sandbox_root}/#{dynamic_sandbox.root.to_s.split('/').last}"
end

file_accessors = static_installer.pod_targets.select {|t| t.pod_name == @spec.name }.flat_map(&:file_accessors)

builder = Pod::Builder.new(
platform,
file_accessors,
@source_dir,
static_sandbox_root,
dynamic_sandbox_root,
Expand All @@ -153,7 +164,7 @@ def perform_build(platform, static_sandbox, dynamic_sandbox)
@exclude_deps
)

builder.build(platform, @library)
builder.build(@package_type)

return unless @embedded
builder.link_embedded_resources
Expand Down

0 comments on commit e557ec2

Please sign in to comment.