Skip to content

Commit e117920

Browse files
committed
Fix CMake build
1 parent 027bdc5 commit e117920

File tree

12 files changed

+167
-60
lines changed

12 files changed

+167
-60
lines changed

Sources/Basics/FileSystem+Extensions.swift

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2020 Apple Inc. and the Swift project authors
4+
Copyright (c) 2020 - 2021 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See http://swift.org/LICENSE.txt for license information
@@ -110,3 +110,31 @@ extension FileSystem {
110110
return idiomaticConfigDirectory
111111
}
112112
}
113+
114+
// MARK: - script cache
115+
116+
extension FileSystem {
117+
/// SwiftPM cache directory under user's caches directory (if exists)
118+
public var swiftScriptCacheDirectory: AbsolutePath {
119+
return self.dotSwiftScriptCachesDirectory
120+
}
121+
122+
fileprivate var dotSwiftScriptCachesDirectory: AbsolutePath {
123+
return self.dotSwiftPM.appending(component: "scripts")
124+
}
125+
}
126+
127+
extension FileSystem {
128+
public func getOrCreateSwiftScriptCacheDirectory() throws -> AbsolutePath {
129+
let idiomaticCacheDirectory = self.swiftScriptCacheDirectory
130+
// Create idiomatic if necessary
131+
if !self.exists(idiomaticCacheDirectory) {
132+
try self.createDirectory(idiomaticCacheDirectory, recursive: true)
133+
}
134+
// Create ~/.swiftpm if necessary
135+
if !self.exists(self.dotSwiftPM) {
136+
try self.createDirectory(self.dotSwiftPM, recursive: true)
137+
}
138+
return idiomaticCacheDirectory
139+
}
140+
}

Sources/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ add_subdirectory(PackagePlugin)
2222
add_subdirectory(SPMBuildCore)
2323
add_subdirectory(SPMLLBuild)
2424
add_subdirectory(SourceControl)
25+
#add_subdirectory(ScriptingCore)
26+
#add_subdirectory(ScriptParse)
2527
add_subdirectory(swift-build)
2628
add_subdirectory(swift-package)
2729
add_subdirectory(swift-run)
30+
#add_subdirectory(swift-script)
2831
add_subdirectory(swift-test)
2932
add_subdirectory(Workspace)
3033
add_subdirectory(XCBuildSupport)

Sources/Commands/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This source file is part of the Swift.org open source project
22
#
3-
# Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
3+
# Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
44
# Licensed under Apache License v2.0 with Runtime Library Exception
55
#
66
# See http://swift.org/LICENSE.txt for license information
@@ -18,6 +18,7 @@ add_library(Commands
1818
SwiftPackageCollectionsTool.swift
1919
SwiftPackageTool.swift
2020
SwiftRunTool.swift
21+
# SwiftScriptTool.swift
2122
SwiftTestTool.swift
2223
SwiftTool.swift
2324
SymbolGraphExtract.swift
@@ -28,6 +29,8 @@ target_link_libraries(Commands PUBLIC
2829
Build
2930
PackageCollections
3031
PackageGraph
32+
# ScriptingCore
33+
# ScriptParse
3134
SourceControl
3235
TSCBasic
3336
TSCUtility

Sources/Commands/SwiftScriptTool.swift

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,39 @@ See http://swift.org/CONTRIBUTORS.txt for Swift project authors
1111
import ArgumentParser
1212
import Basics
1313
import PackageModel
14-
import TSCBasic
1514
import ScriptParse
1615
import ScriptingCore
16+
import TSCBasic
1717
import Workspace
1818

19+
protocol ScriptCommand: ParsableCommand {
20+
var swiftOptions: SwiftToolOptions { get }
21+
var options: ScriptToolOptions { get }
22+
23+
func run(_ swiftTool: SwiftTool, as productName: String, at cacheDirPath: AbsolutePath) throws
24+
}
25+
26+
extension ScriptCommand {
27+
public func run() throws {
28+
guard let file = options.file else {
29+
throw ScriptError.fileNotFound("")
30+
}
31+
let (productName, cacheDirPath) = try checkAndPerformCache(for: file)
32+
33+
var swiftOptions = swiftOptions
34+
swiftOptions.packagePath = cacheDirPath
35+
swiftOptions.buildPath = nil
36+
let swiftTool = try SwiftTool(options: swiftOptions)
37+
38+
try self.run(swiftTool, as: productName, at: cacheDirPath)
39+
if swiftTool.diagnostics.hasErrors || swiftTool.executionStatus == .failure {
40+
throw ExitCode.failure
41+
}
42+
}
43+
44+
public static var _errorLabel: String { "error" }
45+
}
46+
1947
struct ScriptToolOptions: ParsableArguments {
2048
/// If the executable product should be built before running.
2149
@Flag(name: .customLong("skip-build"), help: "Skip building the executable product")
@@ -55,10 +83,6 @@ public struct SwiftScriptTool: ParsableCommand {
5583
public static var _errorLabel: String { "error" }
5684
}
5785

58-
extension SwiftScriptTool {
59-
static var cacheDir: AbsolutePath { localFileSystem.dotSwiftPM.appending(component: "scripts") }
60-
}
61-
6286
/// swift-run tool namespace
6387
extension SwiftScriptTool {
6488
struct Run: ScriptCommand {
@@ -74,7 +98,7 @@ extension SwiftScriptTool {
7498
/// Whether to print build progress.
7599
@Flag(help: "Print build progress")
76100
var quiet: Bool = false
77-
101+
78102
func run(_ swiftTool: SwiftTool, as productName: String, at cacheDirPath: AbsolutePath) throws {
79103
let output = BufferedOutputByteStream()
80104
if quiet {

Sources/Commands/SwiftTool.swift

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import Build
2626
import XCBuildSupport
2727
import Workspace
2828
import Basics
29-
import ScriptingCore
3029

3130
typealias Diagnostic = TSCBasic.Diagnostic
3231

@@ -284,34 +283,6 @@ extension SwiftCommand {
284283
public static var _errorLabel: String { "error" }
285284
}
286285

287-
protocol ScriptCommand: ParsableCommand {
288-
var swiftOptions: SwiftToolOptions { get }
289-
var options: ScriptToolOptions { get }
290-
291-
func run(_ swiftTool: SwiftTool, as productName: String, at cacheDirPath: AbsolutePath) throws
292-
}
293-
294-
extension ScriptCommand {
295-
public func run() throws {
296-
guard let file = options.file else {
297-
throw ScriptError.fileNotFound("")
298-
}
299-
let (productName, cacheDirPath) = try checkAndPerformCache(for: file, at: SwiftScriptTool.cacheDir)
300-
301-
var swiftOptions = swiftOptions
302-
swiftOptions.packagePath = cacheDirPath
303-
swiftOptions.buildPath = nil
304-
let swiftTool = try SwiftTool(options: swiftOptions)
305-
306-
try self.run(swiftTool, as: productName, at: cacheDirPath)
307-
if swiftTool.diagnostics.hasErrors || swiftTool.executionStatus == .failure {
308-
throw ExitCode.failure
309-
}
310-
}
311-
312-
public static var _errorLabel: String { "error" }
313-
}
314-
315286
public class SwiftTool {
316287
/// The original working directory.
317288
let originalWorkingDirectory: AbsolutePath

Sources/ScriptParse/CMakeLists.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# This source file is part of the Swift.org open source project
2+
#
3+
# Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See http://swift.org/LICENSE.txt for license information
7+
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
8+
9+
add_library(ScriptParse
10+
Models.swift
11+
ScriptParse.swift
12+
StatementKind.swift
13+
misc.swift)
14+
15+
target_link_libraries(ScriptParse PUBLIC
16+
TSCBasic
17+
TSCUtility
18+
Basics
19+
ArgumentParser
20+
SwiftSyntax)
21+
22+
# NOTE(compnerd) workaround for CMake not setting up include flags yet
23+
set_target_properties(ScriptParse PROPERTIES
24+
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
25+
26+
if(USE_CMAKE_INSTALL)
27+
install(TARGETS ScriptParse
28+
ARCHIVE DESTINATION lib
29+
LIBRARY DESTINATION lib
30+
RUNTIME DESTINATION bin)
31+
endif()
32+
set_property(GLOBAL APPEND PROPERTY SwiftPM_EXPORTS ScriptParse)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This source file is part of the Swift.org open source project
2+
#
3+
# Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See http://swift.org/LICENSE.txt for license information
7+
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
8+
9+
add_library(ScriptingCore
10+
Models.swift
11+
utils.swift)
12+
13+
target_link_libraries(ScriptingCore PUBLIC
14+
TSCBasic
15+
TSCUtility
16+
Basics)
17+
18+
# NOTE(compnerd) workaround for CMake not setting up include flags yet
19+
set_target_properties(ScriptingCore PROPERTIES
20+
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
21+
22+
if(USE_CMAKE_INSTALL)
23+
install(TARGETS ScriptingCore
24+
ARCHIVE DESTINATION lib
25+
LIBRARY DESTINATION lib
26+
RUNTIME DESTINATION bin)
27+
endif()
28+
set_property(GLOBAL APPEND PROPERTY SwiftPM_EXPORTS ScriptingCore)

Sources/ScriptingCore/Models.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
//
2-
// File.swift
3-
// File
4-
//
5-
// Created by C YR on 2021/7/19.
6-
//
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2021 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
710

811
import Foundation
912
import TSCBasic

Sources/ScriptingCore/Options.swift

Lines changed: 0 additions & 10 deletions
This file was deleted.

Sources/ScriptingCore/utils.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
2-
This source file is part of the Swift.org open source project
2+
This source file is part of the Swift.org open source project
33

4-
Copyright 2015 - 2016 Apple Inc. and the Swift project authors
5-
Licensed under Apache License v2.0 with Runtime Library Exception
4+
Copyright 2015 - 2021 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
66

7-
See http://swift.org/LICENSE.txt for license information
8-
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9-
*/
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
1010

1111
import Basics
1212
import Foundation
@@ -24,13 +24,17 @@ fileprivate extension AbsolutePath {
2424
public enum ScriptError: Swift.Error {
2525
/// The specified file doesn't exist.
2626
case fileNotFound(String)
27+
/// The target path is directory
28+
case isDirectory(String)
2729
}
2830

2931
extension ScriptError: CustomStringConvertible {
3032
public var description: String {
3133
switch self {
3234
case .fileNotFound(let path):
3335
return "\(path) doesn't exist"
36+
case .isDirectory(let path):
37+
return "\(path) is a directory"
3438
}
3539
}
3640
}
@@ -111,3 +115,7 @@ public func checkAndPerformCache(for file: String, at dirPath: AbsolutePath) thr
111115
return (String(file.dropLast(7)), cacheDirPath)
112116
}
113117
}
118+
119+
public func checkAndPerformCache(for file: String) throws -> (productName: String, cacheDirPath: AbsolutePath) {
120+
try checkAndPerformCache(for: file, at: localFileSystem.getOrCreateSwiftScriptCacheDirectory())
121+
}

0 commit comments

Comments
 (0)