Skip to content

Commit 5390b72

Browse files
committed
feat: add CloudSync package and build tool plugin
1 parent efd4f9a commit 5390b72

File tree

5 files changed

+120
-2
lines changed

5 files changed

+120
-2
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ unittest
1313
.vscode
1414
**/node_modules/**
1515
.env
16-
package-lock.json
16+
package-lock.json
17+
.build

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ MAKEFLAGS += -j$(CPUS)
3131
CC = gcc
3232
CFLAGS = -Wall -Wextra -Wno-unused-parameter -I$(SRC_DIR) -I$(SQLITE_DIR) -I$(CURL_DIR)/include
3333
T_CFLAGS = $(CFLAGS) -DSQLITE_CORE -DCLOUDSYNC_UNITTEST -DCLOUDSYNC_OMIT_NETWORK -DCLOUDSYNC_OMIT_PRINT_RESULT
34-
LDFLAGS = -L./$(CURL_DIR)/$(PLATFORM) -lcurl
3534
COVERAGE = false
3635

3736
# Directories
@@ -136,6 +135,8 @@ ifdef NATIVE_NETWORK
136135

137136
$(BUILD_RELEASE)/%_m.o: %.m
138137
$(CC) $(CFLAGS) -fobjc-arc -O3 -fPIC -c $< -o $@
138+
else
139+
LDFLAGS += -L./$(CURL_DIR)/$(PLATFORM) -lcurl
139140
endif
140141

141142
# Windows .def file generation
@@ -154,7 +155,11 @@ extension: $(TARGET)
154155
all: $(TARGET)
155156

156157
# Loadable library
158+
ifdef NATIVE_NETWORK
159+
$(TARGET): $(RELEASE_OBJ) $(DEF_FILE)
160+
else
157161
$(TARGET): $(RELEASE_OBJ) $(DEF_FILE) $(CURL_LIB)
162+
endif
158163
$(CC) $(RELEASE_OBJ) $(DEF_FILE) -o $@ $(LDFLAGS)
159164
ifeq ($(PLATFORM),windows)
160165
# Generate import library for Windows

Package.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// swift-tools-version: 6.1
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "CloudSync",
8+
platforms: [.macOS(.v11), .iOS(.v11)],
9+
products: [
10+
// Products can be used to vend plugins, making them visible to other packages.
11+
.plugin(
12+
name: "CloudSyncPlugin",
13+
targets: ["CloudSyncPlugin"]),
14+
.library(
15+
name: "CloudSync",
16+
targets: ["CloudSync"])
17+
],
18+
targets: [
19+
// Build tool plugin that invokes the Makefile
20+
.plugin(
21+
name: "CloudSyncPlugin",
22+
capability: .buildTool(),
23+
path: "packages/swift/plugin"
24+
),
25+
// CloudSync library target
26+
.target(
27+
name: "CloudSync",
28+
dependencies: [],
29+
path: "packages/swift/extension",
30+
plugins: ["CloudSyncPlugin"]
31+
),
32+
]
33+
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// CloudSync.swift
2+
// This file serves as a placeholder for the CloudSync target.
3+
// The actual SQLite extension is built using the Makefile through the build plugin.
4+
5+
import Foundation
6+
7+
/// Placeholder structure for CloudSync
8+
public struct CloudSync {
9+
/// Returns the path to the built CloudSync dylib inside the XCFramework
10+
public static var path: String {
11+
#if os(macOS)
12+
return "CloudSync.xcframework/macos-arm64_x86_64/CloudSync.framework/CloudSync"
13+
#elseif targetEnvironment(simulator)
14+
return "CloudSync.xcframework/ios-arm64_x86_64-simulator/CloudSync.framework/CloudSync"
15+
#else
16+
return "CloudSync.xcframework/ios-arm64/CloudSync.framework/CloudSync"
17+
#endif
18+
}
19+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import PackagePlugin
2+
import Foundation
3+
4+
@main
5+
struct CloudSync: BuildToolPlugin {
6+
/// Entry point for creating build commands for targets in Swift packages.
7+
func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
8+
let packageDirectory = context.package.directoryURL
9+
let outputDirectory = context.pluginWorkDirectoryURL
10+
return createCloudSyncBuildCommands(packageDirectory: packageDirectory, outputDirectory: outputDirectory)
11+
}
12+
}
13+
14+
#if canImport(XcodeProjectPlugin)
15+
import XcodeProjectPlugin
16+
17+
extension CloudSync: XcodeBuildToolPlugin {
18+
// Entry point for creating build commands for targets in Xcode projects.
19+
func createBuildCommands(context: XcodePluginContext, target: XcodeTarget) throws -> [Command] {
20+
let outputDirectory = context.pluginWorkDirectoryURL
21+
return createCloudSyncBuildCommands(packageDirectory: nil, outputDirectory: outputDirectory)
22+
}
23+
}
24+
25+
#endif
26+
27+
/// Shared function to create CloudSync build commands
28+
func createCloudSyncBuildCommands(packageDirectory: URL?, outputDirectory: URL) -> [Command] {
29+
30+
// For Xcode projects, use current directory; for Swift packages, use provided packageDirectory
31+
let workingDirectory = packageDirectory?.path ?? "$(pwd)"
32+
let packageDirInfo = packageDirectory != nil ? "Package directory: \(packageDirectory!.path)" : "Working directory: $(pwd)"
33+
34+
return [
35+
.prebuildCommand(
36+
displayName: "Building CloudSync XCFramework",
37+
executable: URL(fileURLWithPath: "/bin/bash"),
38+
arguments: [
39+
"-c",
40+
"""
41+
set -e
42+
echo "Starting CloudSync XCFramework prebuild..."
43+
echo "\(packageDirInfo)"
44+
45+
# Clean and create output directory
46+
rm -rf "\(outputDirectory.path)"
47+
mkdir -p "\(outputDirectory.path)"
48+
49+
# Build directly from source directory with custom output paths
50+
cd "\(workingDirectory)" && \
51+
echo "Building XCFramework with native network..." && \
52+
make xcframework NATIVE_NETWORK=ON DIST_DIR="\(outputDirectory.path)" BUILD_RELEASE="\(outputDirectory.path)/build/release" BUILD_TEST="\(outputDirectory.path)/build/test" && \
53+
rm -rf "\(outputDirectory.path)/build" && \
54+
echo "XCFramework build completed successfully!"
55+
"""
56+
],
57+
outputFilesDirectory: outputDirectory
58+
)
59+
]
60+
}

0 commit comments

Comments
 (0)