Skip to content

Commit

Permalink
Merge pull request #13 from giginet/tests-for-clang
Browse files Browse the repository at this point in the history
Fix clang target generation and add tests
  • Loading branch information
giginet authored Dec 2, 2022
2 parents 32e089b + 4dfa42d commit 901df13
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ DerivedData/
.netrc
.swiftpm
**/Resources/**/Package.resolved
*.xcframework
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"location" : "https://github.com/apple/swift-driver.git",
"state" : {
"branch" : "main",
"revision" : "93c612e842120e5af583c4cc1e7bda60b239b83e"
"revision" : "21c7b056ba03e532703ead166e8f38195a932628"
}
},
{
Expand Down Expand Up @@ -114,7 +114,7 @@
"location" : "https://github.com/apple/swift-tools-support-core.git",
"state" : {
"branch" : "main",
"revision" : "50b34c79fd99da359d83f2f6b90d96634bd8697c"
"revision" : "4cac812e26a1c9ec0379de9ec86c7da2f9e87863"
}
},
{
Expand Down
15 changes: 13 additions & 2 deletions Sources/ScipioKit/ProjectGenerator/ProjectGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class ProjectGenerator {
path: nil
)
)
mainGroup.addChild(productGroup)

let rootObject = addObject(
PBXProject(
Expand All @@ -76,7 +77,6 @@ class ProjectGenerator {
)
)
pbxProj.rootObject = rootObject

}

enum Error: LocalizedError {
Expand Down Expand Up @@ -343,18 +343,29 @@ class ProjectGenerator {
.contains(clangTarget.c99name)

if case .custom(let path) = clangTarget.moduleMapType {
// If modulemap path is specified, use this
return path
} else if hasUmbrellaHeader {
// If package contains umbrella headers, it generates modulemap using Xcode
let files = includeFileRefs.map { fileRef in
PBXBuildFile(file: fileRef, settings: ["ATTRIBUTES": "Public"])
addObject(PBXBuildFile(file: fileRef, settings: ["ATTRIBUTES": ["Public"]]))
}
let headerPhase = addObject(
PBXHeadersBuildPhase(files: files)
)

xcodeTarget.buildPhases.append(headerPhase)

if let allConfigurations = xcodeTarget.buildConfigurationList?.buildConfigurations {
for configuration in allConfigurations {
configuration.buildSettings["CLANG_ENABLE_MODULES"] = true
configuration.buildSettings["DEFINES_MODULE"] = true
}
}

return nil
} else if let generatedModuleMapType = clangTarget.moduleMapType.generatedModuleMapType {
// If package has modulemap type, it generates new modulemap
let generatedModuleMapPath = try generateModuleMap(for: clangTarget, moduleMapType: generatedModuleMapType)
return generatedModuleMapPath
}
Expand Down
1 change: 1 addition & 0 deletions Tests/.swiftlint.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
disabled_rules:
- function_body_length
- force_cast
84 changes: 71 additions & 13 deletions Tests/ScipioKitTests/ProjectGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,24 @@ private let fixturePath = URL(fileURLWithPath: #file)
.appendingPathComponent("Resources")
.appendingPathComponent("Fixtures")
private let testPackagePath = fixturePath.appendingPathComponent("E2ETestPackage")
private let clangPackagePath = fixturePath.appendingPathComponent("ClangPackage")

final class ProjectGeneratorTests: XCTestCase {
private var projectGenerator: ProjectGenerator!
private var package: Package!
private let fileSystem: some FileSystem = localFileSystem

override func setUpWithError() throws {
package = try Package(packageDirectory: testPackagePath)
projectGenerator = ProjectGenerator(package: package,
buildOptions: .init(buildConfiguration: .debug,
isSimulatorSupported: true,
isDebugSymbolsEmbedded: false,
frameworkType: .static,
sdks: [.iOS]),
fileSystem: localFileSystem)

try super.setUpWithError()
private func makeGenerator(for package: Package) throws -> ProjectGenerator {
ProjectGenerator(package: package,
buildOptions: .init(buildConfiguration: .debug,
isSimulatorSupported: true,
isDebugSymbolsEmbedded: false,
frameworkType: .static,
sdks: [.iOS]),
fileSystem: localFileSystem)
}

func testGeneratedProject() throws {
let package = try Package(packageDirectory: testPackagePath)
let projectGenerator = try makeGenerator(for: package)
let projectPath = package.projectPath
try projectGenerator.generate()
XCTAssertTrue(fileSystem.exists(projectPath))
Expand Down Expand Up @@ -88,4 +86,64 @@ final class ProjectGeneratorTests: XCTestCase {
)
}
}

func testGeneratedClangTarget() throws {
let package = try Package(packageDirectory: clangPackagePath)
let projectGenerator = try makeGenerator(for: package)
let projectPath = package.projectPath
try projectGenerator.generate()
XCTAssertTrue(fileSystem.exists(projectPath))

let project = try XcodeProj(pathString: projectPath.path)

// Check targets
let targets = project.pbxproj.nativeTargets
XCTAssertEqual(Set(targets.map(\.name)), ["some_lib"])

let target = try XCTUnwrap(project.pbxproj.targets(named: "some_lib").first)

// Check file tree
XCTAssertEqual(Set(project.pbxproj.groups.compactMap(\.name)), ["some_lib", "include", "Products"])
let rootGroup = try XCTUnwrap(project.pbxproj.rootGroup())
let libraryGroup = try XCTUnwrap(rootGroup.group(named: "some_lib"))
XCTAssertNotNil(libraryGroup.file(named: "some_lib.c"))

let includeGroup = try XCTUnwrap(libraryGroup.group(named: "include"))
XCTAssertNotNil(includeGroup.file(named: "some_lib.h"))

// Check build phase
let buildPhase = try XCTUnwrap(try target.sourcesBuildPhase())
XCTAssertEqual(buildPhase.files?.count, 1)

// Check header
let headerBuildPhase = try XCTUnwrap(target.buildPhases.first(where: { $0 is PBXHeadersBuildPhase }) as? PBXHeadersBuildPhase)
let publicHeader = try XCTUnwrap(headerBuildPhase.files?.first)
XCTAssertEqual(publicHeader.file?.path, "some_lib.h")
XCTAssertEqual(publicHeader.settings?["ATTRIBUTES"] as! [String], ["Public"])

// Check build settings
XCTAssertEqual(target.buildConfigurationList?.buildConfigurations.map(\.name), ["Debug", "Release"])
for configuration in target.buildConfigurationList!.buildConfigurations {
XCTAssertEqual(
configuration.buildSettings["MACH_O_TYPE"] as? String,
"staticlib", "If frameworkType is static, MACH_O_TYPE should be set"
)
XCTAssertEqual(
configuration.buildSettings["BUILD_LIBRARY_FOR_DISTRIBUTION"] as? String,
"YES"
)
XCTAssertEqual(
configuration.buildSettings["FRAMEWORK_SEARCH_PATHS"] as? [String],
["$(inherited)", "$(PLATFORM_DIR)/Developer/Library/Frameworks"]
)
XCTAssertEqual(
configuration.buildSettings["CLANG_ENABLE_MODULES"] as? String,
"YES"
)
XCTAssertEqual(
configuration.buildSettings["DEFINES_MODULE"] as? String,
"YES"
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
25 changes: 25 additions & 0 deletions Tests/ScipioKitTests/Resources/Fixtures/ClangPackage/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// swift-tools-version: 5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "ClangPackage",
platforms: [.iOS(.v11)],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "some_lib",
targets: ["some_lib"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
.target(
name: "some_lib",
dependencies: []
),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ClangPackage

A description of this package.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef some_lib_h
#define some_lib_h

#include <stdio.h>

int add(int lhs, int rhs);

#endif /* some_lib_h */
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "include/some_lib.h"

int add(int lhs, int rhs) {
return lhs + rhs;
}

0 comments on commit 901df13

Please sign in to comment.