Repository navigation
Expand file tree
/
Copy pathPackage.swift
More file actions
101 lines (98 loc) · 4 KB
/
Copy pathPackage.swift
File metadata and controls
101 lines (98 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// swift-tools-version: 6.0
// The pure layers build and test on macOS here; the pod compiles the same files plus view and nitro.
import Foundation
import PackageDescription
/// Every generated grammar's parser.c under root with the scanner.c beside it, the rule scripts/build-host-treesitter.sh
/// uses. A scanner that includes a header outside its src gets that src as a search path, so the header's own
/// tree_sitter/parser.h include resolves to the grammar's copy.
func grammarSources(_ root: String) -> (sources: [String], searchPaths: [String]) {
let fm = FileManager.default
let languages = URL(fileURLWithPath: Context.packageDirectory).appendingPathComponent(root)
var sources: [String] = []
var searchPaths: [String] = []
func walk(_ dir: URL, _ relative: String) {
guard let names = try? fm.contentsOfDirectory(atPath: dir.path) else { return }
for name in names.sorted() where !name.hasPrefix(".") && name != "node_modules" {
let child = dir.appendingPathComponent(name)
let path = relative.isEmpty ? name : relative + "/" + name
var isDirectory: ObjCBool = false
guard fm.fileExists(atPath: child.path, isDirectory: &isDirectory) else { continue }
if isDirectory.boolValue {
walk(child, path)
} else if name == "parser.c" && dir.lastPathComponent == "src" && path.contains("/grammar/") {
sources.append(path)
let scanner = dir.appendingPathComponent("scanner.c")
guard let text = try? String(contentsOf: scanner, encoding: .utf8) else { continue }
sources.append(relative + "/scanner.c")
if text.contains("#include \"../") { searchPaths.append(relative) }
}
}
}
walk(languages, "")
return (sources, searchPaths)
}
let grammars = grammarSources("languages")
// The example app's custom packages, written by the language command.
let modules = grammarSources("example/modules")
let package = Package(
name: "Codepad",
platforms: [.iOS(.v17), .macOS(.v14)],
targets: [
.target(
name: "CTreeSitter",
path: "cpp/tree-sitter/lib",
sources: ["src/lib.c"],
publicHeadersPath: "include",
cSettings: [.headerSearchPath("src")]
),
.target(
name: "CodepadHostGrammars",
path: "languages",
sources: grammars.sources,
publicHeadersPath: "host/include",
cSettings: grammars.searchPaths.map { .headerSearchPath($0) }
),
.target(
name: "CodepadHostModules",
path: "example/modules",
sources: modules.sources,
// SwiftPM needs a public headers dir; nothing imports this module, so the first grammar's headers serve.
publicHeadersPath: modules.sources.first.map { ($0 as NSString).deletingLastPathComponent + "/tree_sitter" },
cSettings: modules.searchPaths.map { .headerSearchPath($0) }
),
// The core and PCRE2 in one target: only the core includes pcre2.h, so PCRE2 needs no public headers.
.target(
name: "CodepadCore",
dependencies: ["CTreeSitter"],
path: "cpp",
exclude: ["tree-sitter", "pcre2/LICENCE.md", "pcre2/VERSION"],
sources: ["core/src", "pcre2/src"],
publicHeadersPath: "core/include",
cSettings: [
.define("PCRE2_CODE_UNIT_WIDTH", to: "16"),
.define("HAVE_CONFIG_H"),
.headerSearchPath("pcre2/src"),
],
cxxSettings: [
.define("PCRE2_CODE_UNIT_WIDTH", to: "16"),
.define("CODEPAD_TEST_HOOKS"),
.headerSearchPath("pcre2/src"),
]
),
.target(
name: "Codepad",
dependencies: ["CTreeSitter", "CodepadCore"],
path: "ios/Codepad",
exclude: ["nitro", "syntax/CPTreeSitter.h", "syntax/CPCore.h", "view"]
),
.testTarget(
name: "CodepadTests",
dependencies: ["Codepad", "CodepadCore", "CodepadHostGrammars", "CodepadHostModules"],
path: "tests/swift",
// The core's test-only entry points are declared behind this define.
swiftSettings: [.unsafeFlags(["-Xcc", "-DCODEPAD_TEST_HOOKS"])]
),
],
swiftLanguageModes: [.v6],
cxxLanguageStandard: .cxx20
)