# copy from https://github.com/CocoaPods/cocoapods-packager require 'English' require 'shellwords' require 'swordfish/helpers/framework.rb' require 'swordfish/config/config_builder' require 'swordfish/helpers/build_utils' module Ocean class Framework class Builder include Pod #Debug下还待完成 def initialize(spec, file_accessor, platform, source_dir, isRootSpec = true, build_model="Debug") @spec = spec @source_dir = source_dir @file_accessor = file_accessor @platform = platform @build_model = build_model @isRootSpec = isRootSpec #vendored_static_frameworks 只有 xx.framework 需要拼接为 xx.framework/xx by slj vendored_static_frameworks = file_accessor.vendored_static_frameworks.map do |framework| path = framework extn = File.extname path if extn.downcase == '.framework' path = File.join(path,File.basename(path, extn)) end path end @vendored_libraries = (vendored_static_frameworks + file_accessor.vendored_static_libraries).map(&:to_s) end def build # # 编译真机 defines = compile # # 编译模拟器 build_sim_libraries(defines) defines end def lipo_build(defines) UI.section("Building static Library #{@spec}") do # defines = compile # build_sim_libraries(defines) output = framework.versions_path + Pathname.new(@spec.name) build_static_library_for_ios(output) copy_headers copy_license copy_resources cp_to_source_dir end framework end private def cp_to_source_dir framework_name = "#{@spec.name}.framework" target_dir = File.join(Ocean::Config::Builder.instance.zip_dir,framework_name) FileUtils.rm_rf(target_dir) if File.exist?(target_dir) zip_dir = Ocean::Config::Builder.instance.zip_dir FileUtils.mkdir_p(zip_dir) unless File.exist?(zip_dir) `cp -fa #{@platform}/#{framework_name} #{target_dir}` end #模拟器,目前只支持 debug x86-64 def build_sim_libraries(defines) UI.message 'Building simulator libraries' # archs = %w[i386 x86_64] archs = ios_architectures_sim archs.map do |arch| xcodebuild(defines, "-sdk iphonesimulator ARCHS=\'#{arch}\' ", "build-#{arch}",@build_model) end end def static_libs_in_sandbox(build_dir = 'build') file = Dir.glob("#{build_dir}/lib#{target_name}.a") unless file UI.warn "file no find = #{build_dir}/lib#{target_name}.a" end file end def build_static_library_for_ios(output) UI.message "Building ios libraries with archs #{ios_architectures}" static_libs = static_libs_in_sandbox('build') + static_libs_in_sandbox('build-simulator') + @vendored_libraries # if is_debug_model ios_architectures.map do |arch| static_libs += static_libs_in_sandbox("build-#{arch}") + @vendored_libraries end ios_architectures_sim do |arch| static_libs += static_libs_in_sandbox("build-#{arch}") + @vendored_libraries end # end build_path = Pathname("build") build_path.mkpath unless build_path.exist? # if is_debug_model libs = (ios_architectures + ios_architectures_sim) .map do |arch| library = "build-#{arch}/lib#{@spec.name}.a" library end # else # libs = ios_architectures.map do |arch| # library = "build/package-#{@spec.name}-#{arch}.a" # # libtool -arch_only arm64 -static -o build/package-armv64.a build/libIMYFoundation.a build-simulator/libIMYFoundation.a # # 从liBFoundation.a 文件中,提取出 arm64 架构的文件,命名为build/package-armv64.a # UI.message "libtool -arch_only #{arch} -static -o #{library} #{static_libs.join(' ')}" # `libtool -arch_only #{arch} -static -o #{library} #{static_libs.join(' ')}` # library # end # end # build_static_library_with_vendored_libraries(libs) UI.message "lipo -create -output #{output} #{libs.join(' ')}" `lipo -create -output #{output} #{libs.join(' ')}` end def build_static_library_with_vendored_libraries(libs) lipo_libraries = Array.new spec_dir = File.join(Dir.pwd,"Pods/#{@spec.name}") Dir.chdir(spec_dir) do libraries = Dir.glob('**/*.a') libraries.each do |l| libs << Pathname.new(File.join(Dir.pwd,l)) end end # if lipo_libraries.empty? == false # UI.message "lipo -create -output #{output} #{lipo_libraries.join(' ')}" # `lipo -create -output #{output} #{lipo_libraries.join(' ')}` # end end def ios_build_options "ARCHS=\'#{ios_architectures.join(' ')}\' " end def ios_architectures # >armv7 # iPhone4 # iPhone4S # >armv7s 去掉 # iPhone5 # iPhone5C # >arm64 # iPhone5S(以上) # >i386 # iphone5,iphone5s以下的模拟器 # >x86_64 # iphone6以上的模拟器 # archs = %w[x86_64 arm64 armv7s i386] archs = %w[arm64] # @vendored_libraries.each do |library| # archs = `lipo -info #{library}`.split & archs # end archs end def ios_architectures_sim archs = %w[x86_64] # TODO 处理是否需要 i386 archs end def compile defines = "GCC_PREPROCESSOR_DEFINITIONS='$(inherited)'" defines += ' ' defines += @spec.consumer(@platform).compiler_flags.join(' ') options = ios_build_options # if is_debug_model archs = ios_architectures # archs = %w[arm64 armv7 armv7s] archs.map do |arch| xcodebuild(defines, "ARCHS=\'#{arch}\' ","build-#{arch}",@build_model) end # else # xcodebuild(defines,options) # end defines end def is_debug_model @build_model == "Debug" end def target_name #区分多平台,如配置了多平台,会带上平台的名字 # 如libwebp-iOS if @spec.available_platforms.count > 1 "#{@spec.name}-#{Platform.string_name(@spec.consumer(@platform).platform_name)}" else @spec.name end end def xcodebuild(defines = '', args = '', build_dir = 'build', build_model = 'Debug') unless File.exist?("Pods.xcodeproj") #cocoapods-generate v2.0.0 command = "xcodebuild #{defines} #{args} CONFIGURATION_BUILD_DIR=#{File.join(File.expand_path("..", build_dir), File.basename(build_dir))} clean build -configuration #{build_model} SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES -target #{target_name} -project ./Pods/Pods.xcodeproj 2>&1" else command = "xcodebuild #{defines} #{args} CONFIGURATION_BUILD_DIR=#{build_dir} clean build -configuration #{build_model} -target #{target_name} -project ./Pods.xcodeproj 2>&1" end UI.message "command = #{command}" output_str = `#{command}` output = output_str.lines.to_a if $CHILD_STATUS.exitstatus != 0 raise <<~EOF Build command failed: #{command} Output: #{output.map { |line| " #{line}" }.join} EOF Process.exit end end def copy_headers #by slj 如果没有头文件,去 "Headers/Public"拿 # if public_headers.empty? public_headers = Array.new spec_header_dir = Ocean::Build::Utils.spec_header_dir(@spec) raise "copy_headers #{spec_header_dir} no exist " unless File.exist?(spec_header_dir) Dir.chdir(spec_header_dir) do headers = Dir.glob('**/*.{hpp,h}') headers.each do |h| public_headers << Pathname.new(File.join(Dir.pwd,h)) end end # xxx-swift.h copy swiftmodule_files = Array.new is_swift_module = true (ios_architectures + ios_architectures_sim) .map do |arch| temp_path = "build-#{arch}/Swift Compatibility Header/#{@spec.name}-swift.h" if File.exist?Pathname.new(File.join(Dir.pwd,temp_path)) is_swift_module &= true else is_swift_module = false end # swiftmodule temp_path = "build-#{arch}/#{@spec.name}.swiftmodule" if File.exist?Pathname.new(File.join(Dir.pwd,temp_path)) Dir.chdir(temp_path) do headers = Dir.glob('**/*.{swiftdoc,swiftmodule,swiftinterface}') headers.each do |h| swiftmodule_files << Pathname.new(File.join(Dir.pwd,h)) end end end end # 取arm64 xxx-swift.h if is_swift_module temp_path = "build-arm64/Swift Compatibility Header/#{@spec.name}-swift.h" public_headers << Pathname.new(File.join(Dir.pwd,temp_path)) end module_name = @spec.module_name || @spec.name # FileUtils.cp_r("#{spec_header_dir}/.",framework.headers_path, force: true) has_umbrella_header = false public_headers.each do |h| `ditto "#{h}" "#{framework.headers_path}/#{h.basename}"` if h.basename == "#{module_name}-" has_umbrella_header = true end end # swift 拷贝xxx.swiftmodule文件 swiftmodule_files.each do |h| `ditto "#{h}" "#{framework.swift_module_path}/#{h.basename}"` end if !@spec.module_map.nil? module_map_file = @file_accessor.module_map if Pathname(module_map_file).exist? module_map = File.read(module_map_file) end # elsif public_headers.map(&:basename).map(&:to_s).include?("#{@spec.name}.h") # append_module_content = "" # if is_swift_module # append_module_content = <<-MAP # module #{@spec.name}.Swift { # header "#{@spec.name}-Swift.h" # requires objc # } # MAP # end # # module_map = <<-MAP # framework module #{module_name} { # umbrella header "#{@spec.name}.h" # # export * # module * { export * } # } # #{append_module_content} # MAP else # by ChildhoodAndy # try to read modulemap file from module dir # module_map_file = File.join(Ocean::Build::Utils.spec_module_dir(@spec), "#{@spec.name}.modulemap") # if Pathname(module_map_file).exist? # module_map = File.read(module_map_file) # end # append_module_content = "" if is_swift_module append_module_content = <<-MAP module #{module_name}.Swift { header "#{@spec.name}-Swift.h" requires objc } MAP end header_file = "#{module_name}-umbrella.h" if File.exist?Pathname.new(File.join(Dir.pwd,"#{framework.headers_path}/#{header_file}")) # 有modulemap的情况 module_map = <<-MAP framework module #{module_name} { umbrella header "#{header_file}" export * module * { export * } } #{append_module_content} MAP end end unless module_map.nil? UI.message "Writing module map #{module_map}" unless framework.module_map_path.exist? framework.module_map_path.mkpath end File.write("#{framework.module_map_path}/module.modulemap", module_map) end end def copy_license UI.message 'Copying license' license_file = @spec.license[:file] || 'LICENSE' `cp "#{license_file}" .` if Pathname(license_file).exist? end def copy_resources resource_dir = './build/*.bundle' # resource_dir = './build-armv7/*.bundle' if File.exist?('./build-armv7') resource_dir = './build-arm64/*.bundle' if File.exist?('./build-arm64') bundles = Dir.glob(resource_dir) bundle_names = [@spec, *@spec.recursive_subspecs].flat_map do |spec| begin consumer = spec.consumer(@platform) consumer.resource_bundles.keys + consumer.resources.map do |r| File.basename(r, '.bundle') if File.extname(r) == 'bundle' end rescue Exception => e UI.message "copy_resources error #{e}" next end end.compact.uniq bundles.select! do |bundle| bundle_name = File.basename(bundle, '.bundle') bundle_names.include?(bundle_name) end if bundles.count > 0 UI.message "Copying bundle files #{bundles}" bundle_files = bundles.join(' ') `cp -rp #{bundle_files} #{framework.resources_path} 2>&1` end real_source_dir = @source_dir unless @isRootSpec spec_source_dir = File.join(Dir.pwd,"#{@spec.name}") unless File.exist?(spec_source_dir) spec_source_dir = File.join(Dir.pwd,"Pods/#{@spec.name}") end raise "copy_resources #{spec_source_dir} no exist " unless File.exist?(spec_source_dir) real_source_dir = spec_source_dir end resources = [@spec, *@spec.recursive_subspecs].flat_map do |spec| begin expand_paths(real_source_dir, spec.consumer(@platform).resources) rescue Exception => e UI.message "copy_resources error #{e}" next end end.compact.uniq if resources.count == 0 && bundles.count == 0 framework.delete_resources return end if resources.count > 0 #把 路径转义。 避免空格情况下拷贝失败 escape_resource = [] resources.each do |source| escape_resource << Shellwords.join(source) end UI.message "Copying resources #{escape_resource}" `cp -rp #{escape_resource.join(' ')} #{framework.resources_path}` end end def expand_paths(source_dir, path_specs) path_specs.map do |path_spec| Dir.glob(File.join(source_dir, path_spec)) end end def framework @framework ||= begin framework = Framework.new(@spec.name, @platform.name.to_s) framework.make framework end end end end end