Skip to content

Commit 6401bcb

Browse files
committed
chore: add ASMFLAGS option
1 parent fef0f54 commit 6401bcb

2 files changed

Lines changed: 115 additions & 6 deletions

File tree

Sources/BuildScripts/XCFrameworkBuild/base.swift

Lines changed: 107 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ class BaseBuild {
268268
"CPPFLAGS": cFlags,
269269
// 这个要加,不然cmake在编译maccatalyst 会有问题
270270
"CXXFLAGS": cFlags,
271+
"ASMFLAGS": cFlags,
271272
"LDFLAGS": ldFlags,
272273
"PKG_CONFIG_LIBDIR": pkgConfigPath + pkgConfigPathDefault,
273274
"PATH": BaseBuild.defaultPath,
@@ -543,7 +544,7 @@ class BaseBuild {
543544
FileManager.default.createFile(atPath: path, contents: content.data(using: .utf8), attributes: nil)
544545
}
545546

546-
// CFBundleIdentifier must contain only alphanumerics, dots, hyphens
547+
// CFBundleIdentifier must contain only alphanumerics(a-z), dots(.), hyphens(-)
547548
private func normalizeBundleIdentifier(_ identifier: String) -> String {
548549
return identifier.replacingOccurrences(of: "_", with: "-")
549550
}
@@ -744,9 +745,6 @@ class BaseBuild {
744745
} else {
745746
for target in library.targets {
746747
let checksumFile = releaseDirPath + [target.name + ".xcframework.checksum.txt"]
747-
if !FileManager.default.fileExists(atPath: checksumFile.path) {
748-
continue
749-
}
750748
let checksum = try String(contentsOf: checksumFile, encoding: .utf8).trimmingCharacters(in: .whitespacesAndNewlines)
751749
dependencyTargetContent += """
752750
@@ -790,6 +788,111 @@ class BaseBuild {
790788
}
791789
}
792790

791+
class CombineBaseBuild : BaseBuild {
792+
793+
func combineFrameworkName() -> String {
794+
"\(library.rawValue)-combined.a"
795+
}
796+
797+
func combineFrameworks(platform: PlatformType, arch: ArchType) -> [String] {
798+
let thinLibPath = thinDir(platform: platform, arch: arch) + ["lib"]
799+
let staticLibraries = try? FileManager.default.contentsOfDirectory(atPath: thinLibPath.path).filter { $0.hasSuffix(".a") }
800+
guard let staticLibraries = staticLibraries else {
801+
return []
802+
}
803+
// order by create date descending
804+
let sortedFrameworks = staticLibraries.sorted {
805+
let file1Path = thinLibPath + [$0]
806+
let file2Path = thinLibPath + [$1]
807+
let attr1 = try? FileManager.default.attributesOfItem(atPath: file1Path.path)
808+
let attr2 = try? FileManager.default.attributesOfItem(atPath: file2Path.path)
809+
let date1 = attr1?[FileAttributeKey.creationDate] as? Date ?? Date.distantPast
810+
let date2 = attr2?[FileAttributeKey.creationDate] as? Date ?? Date.distantPast
811+
return date1 < date2
812+
}
813+
return sortedFrameworks
814+
}
815+
816+
override func frameworks() throws -> [String] {
817+
["\(library.rawValue)-combined"]
818+
}
819+
820+
override func build(platform: PlatformType, arch: ArchType) throws {
821+
try super.build(platform: platform, arch: arch)
822+
823+
try combineStaticLibraries(platform: platform, arch: arch)
824+
}
825+
826+
func combineStaticLibraries(platform: PlatformType, arch: ArchType) throws {
827+
let frameworks = self.combineFrameworks(platform: platform, arch: arch)
828+
if frameworks.isEmpty {
829+
return
830+
}
831+
832+
print("Create combine static libraries...")
833+
let thinLibPath = thinDir(platform: platform, arch: arch) + ["lib"]
834+
var combinedLibName = combineFrameworkName()
835+
if !combinedLibName.hasSuffix(".a") {
836+
combinedLibName += ".a"
837+
}
838+
var paths: [String] = []
839+
let prefix = thinDir(platform: platform, arch: arch)
840+
if !FileManager.default.fileExists(atPath: prefix.path) {
841+
throw NSError(domain: "no build for \(platform.rawValue) \(arch.rawValue)", code: 1)
842+
}
843+
for framework in frameworks {
844+
let libname = framework.hasPrefix("lib") || framework.hasPrefix("Lib") ? framework : "lib" + framework
845+
let libPath = prefix + ["lib", libname]
846+
if !FileManager.default.fileExists(atPath: libPath.path) {
847+
throw NSError(domain: "no library \(libPath.path) for \(platform.rawValue) \(arch.rawValue)", code: 1)
848+
}
849+
paths.append(libPath.path)
850+
}
851+
852+
let outputPath = prefix + ["lib", combinedLibName]
853+
var arguments = ["-static"]
854+
arguments.append(contentsOf: ["-o", outputPath.path])
855+
for frameworkPath in paths {
856+
arguments.append(frameworkPath)
857+
}
858+
if FileManager.default.fileExists(atPath: outputPath.path) {
859+
try? FileManager.default.removeItem(at: outputPath)
860+
}
861+
try Utility.launch(path: "/usr/bin/libtool", arguments: arguments)
862+
863+
// move old static libraries to origin directory
864+
let backupDirectory = thinLibPath + ["bak"]
865+
try? FileManager.default.createDirectory(at: backupDirectory, withIntermediateDirectories: true, attributes: nil)
866+
for framework in frameworks {
867+
let libname = framework.hasPrefix("lib") || framework.hasPrefix("Lib") ? framework : "lib" + framework
868+
let libPath = prefix + ["lib", libname]
869+
let backupLibPath = backupDirectory + [libname]
870+
try? FileManager.default.moveItem(at: libPath, to: backupLibPath)
871+
}
872+
873+
// create combine pkgconfig
874+
let pkgconfigPath = thinLibPath + ["pkgconfig", "\(library.rawValue).pc"]
875+
if !FileManager.default.fileExists(atPath: pkgconfigPath.path) {
876+
throw NSError(domain: "no pkgconfig \(pkgconfigPath.path) for \(platform.rawValue) \(arch.rawValue)", code: 1)
877+
}
878+
879+
var content = try String(contentsOf: pkgconfigPath)
880+
let combinedLibname = combinedLibName.hasPrefix("lib") ? String(combinedLibName.dropFirst(3).dropLast(2)) : String(combinedLibName.dropLast(2))
881+
content = content.replacingOccurrences(
882+
of: "-L\\$\\{libdir\\}((\\s+-l\\S+)+)",
883+
with: "-L${libdir} -l\(combinedLibname)",
884+
options: .regularExpression
885+
)
886+
887+
// move old pkgconfig to origin directory
888+
let backupPkgconfigPath = backupDirectory + [pkgconfigPath.lastPathComponent]
889+
try? FileManager.default.moveItem(at: pkgconfigPath, to: backupPkgconfigPath)
890+
891+
// replace with combined pkgconfig
892+
FileManager.default.createFile(atPath: pkgconfigPath.path, contents: content.data(using: .utf8), attributes: nil)
893+
}
894+
895+
}
793896

794897
class ZipBaseBuild : BaseBuild {
795898

Sources/BuildScripts/XCFrameworkBuild/main.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,14 @@ private class BuildFFMPEG: BaseBuild {
462462
FileManager.default.createFile(atPath: lldbFile.path, contents: nil, attributes: nil)
463463
let path = directoryURL + "libavcodec/videotoolbox.c"
464464
if let data = FileManager.default.contents(atPath: path.path), var str = String(data: data, encoding: .utf8) {
465-
str = str.replacingOccurrences(of: "kCVPixelBufferOpenGLESCompatibilityKey", with: "kCVPixelBufferMetalCompatibilityKey")
466-
str = str.replacingOccurrences(of: "kCVPixelBufferIOSurfaceOpenGLTextureCompatibilityKey", with: "kCVPixelBufferMetalCompatibilityKey")
465+
var lines = str.components(separatedBy: .newlines)
466+
for (index, line) in lines.enumerated() {
467+
if line.contains("kCVPixelBufferIOSurfaceOpenGLTextureCompatibilityKey") {
468+
lines.insert(" CFDictionarySetValue(buffer_attributes, kCVPixelBufferMetalCompatibilityKey, kCFBooleanTrue);", at: index + 2)
469+
break
470+
}
471+
}
472+
str = lines.joined(separator: "\n")
467473
try? str.write(toFile: path.path, atomically: true, encoding: .utf8)
468474
}
469475
}

0 commit comments

Comments
 (0)