|
22 | 22 | (append '(("\\.swift$" . swift-mode) ("\\.gyb$" python-mode t)) auto-mode-alist))
|
23 | 23 |
|
24 | 24 | ;; Make sure we know where to find swift-mode
|
25 |
| -(autoload 'swift-mode "swift-mode" |
| 25 | +(autoload 'swift-mode (concat (file-name-directory load-file-name) "swift-mode") |
26 | 26 | "Major mode for editing SWIFT source files.
|
27 | 27 | \\{swift-mode-map}
|
28 | 28 | Runs swift-mode-hook on startup."
|
@@ -255,5 +255,173 @@ takes precedence for files in the Swift project"
|
255 | 255 | 1 2 ,(not :column) ,(not :just-a-warning))
|
256 | 256 | compilation-error-regexp-alist-alist)
|
257 | 257 |
|
| 258 | +(defvar swift-project-directory |
| 259 | + (file-name-directory (directory-file-name (file-name-directory load-file-name))) |
| 260 | + "Directory where the swift project containing this file is located. |
| 261 | +Defaults to the parent directory of `swift-project-settings.el'. |
| 262 | +The setting for file-local values of this variable comes from |
| 263 | +.dir-locals.el in the project's root directory") |
| 264 | +(put 'swift-project-directory 'safe-local-variable 'stringp) |
| 265 | + |
| 266 | +(defun swift-project-default-build-directory (project-directory) |
| 267 | + "Returns the default build directory given a project directory name, `DIR/../build'" |
| 268 | + (concat (file-name-directory (directory-file-name project-directory)) "build/")) |
| 269 | + |
| 270 | +;; This name doesn't end in "function" to avoid being unconditionally marked as risky. |
| 271 | +(defcustom swift-project-build-directory-fn 'swift-project-default-build-directory |
| 272 | + "A function that, given a swift project directory name, |
| 273 | +computes the directory where your build leaves build products. |
| 274 | +Flymake support may search here for a swift compiler to use, for example. |
| 275 | +Defaults to `(concat swift-project-directory \"../build\")'." |
| 276 | + :type 'function |
| 277 | +) |
| 278 | + |
| 279 | +(defun swift-project-executable-find (command) |
| 280 | + "Find the newest executable with the given name in the utils/ directory or in any build directory, falling back to the exec-path as a last resort. |
| 281 | +Given an absolute path, returns it verbatim. This is a pretty |
| 282 | +good heuristic for locating things to use when working on swift |
| 283 | +itself, and is used as the value of swift-find-executable-fn" |
| 284 | + (if (file-name-absolute-p command) command |
| 285 | + (let* ((utility (concat swift-project-directory "utils/" command)) |
| 286 | + (newest (and (file-executable-p utility) utility))) |
| 287 | + (dolist (x (file-expand-wildcards |
| 288 | + (concat (funcall swift-project-build-directory-fn swift-project-directory) |
| 289 | + "*/swift-*/bin/" command))) |
| 290 | + (when (and (file-executable-p x) (or (null newest) (file-newer-than-file-p x newest))) |
| 291 | + (setq newest x))) |
| 292 | + (or newest (executable-find command))))) |
| 293 | + |
| 294 | +(defvar swift-project-sdk-path |
| 295 | + (substring (shell-command-to-string "xcrun --show-sdk-path") 0 -1) |
| 296 | + "The path to the swift SDK to use for syntax checking, etc.") |
| 297 | + |
| 298 | +(defvar swift-project--gyb-temp-file-directory nil) |
| 299 | +(defun swift-project-gyb-temp-file-directory () |
| 300 | + "A directory used for gyb-processed files" |
| 301 | + (or swift-project--gyb-temp-file-directory |
| 302 | + (setq swift-project--gyb-temp-file-directory |
| 303 | + (make-temp-file "swift-project-gyb" :DIRECTORY)))) |
| 304 | + |
| 305 | +(defun swift-project-gyb-output-file-name (input-file-name) |
| 306 | + "Given the name of a .gyb file, return the name of the temporary file we'll use for its expanded result." |
| 307 | + (file-name-sans-extension |
| 308 | + (expand-file-name |
| 309 | + (subst-char-in-string |
| 310 | + ?/ ?! (replace-regexp-in-string |
| 311 | + "!" "!!" |
| 312 | + (if (file-name-absolute-p input-file-name) input-file-name |
| 313 | + (expand-file-name input-file-name)))) |
| 314 | + (swift-project-gyb-temp-file-directory)))) |
| 315 | + |
| 316 | +(defun swift-project-gybbed-file (input-file-name) |
| 317 | + "Given the name of a .gyb file, process it with gyb and return an output file name. |
| 318 | +Given any other file name, just return that name." |
| 319 | + (if (not (string-equal (file-name-extension input-file-name) "gyb")) |
| 320 | + input-file-name |
| 321 | + (let ((result (swift-project-gyb-output-file-name input-file-name))) |
| 322 | + (prog1 result |
| 323 | + (unless (file-newer-than-file-p result input-file-name) |
| 324 | + (with-temp-buffer |
| 325 | + (let* ((gyb (swift-project-executable-find "gyb")) |
| 326 | + (status (call-process gyb nil t nil "-DCMAKE_SIZEOF_VOID_P=8" input-file-name))) |
| 327 | + (unless (eq status 0) |
| 328 | + (error "%s exited with status %s" gyb status))) |
| 329 | + ;; Use write-region instead of write-file to avoid spewing messages. |
| 330 | + (write-region nil nil result nil 566))))))) |
| 331 | + |
| 332 | +(defconst swift-project-stdlib-compile-order |
| 333 | + "Algorithm ArrayBody ArrayBuffer ArrayBufferProtocol ArrayCast Arrays ArrayType Assert AssertCommon BidirectionalCollection Bool BridgeObjectiveC BridgeStorage Builtin BuiltinMath Character CocoaArray Collection CollectionAlgorithms Comparable CompilerProtocols ClosedRange ContiguousArrayBuffer CString CTypes DebuggerSupport DropWhile Dump EmptyCollection Equatable ErrorType Existential Filter FixedPoint FlatMap Flatten FloatingPoint FloatingPointParsing FloatingPointTypes Hashable HashedCollections AnyHashable HashedCollectionsAnyHashableExtensions Hashing HeapBuffer ImplicitlyUnwrappedOptional Index Indices InputStream IntegerArithmetic IntegerParsing Integers Join LazyCollection LazySequence LifetimeManager ManagedBuffer Map MemoryLayout Mirrors Misc MutableCollection NewtypeWrapper ObjCMirrors ObjectIdentifier Optional OptionSet OutputStream Pointer Policy PrefixWhile Print RandomAccessCollection Range RangeReplaceableCollection ReflectionLegacy Repeat REPL Reverse Runtime SipHash Sequence SequenceAlgorithms SequenceWrapper SetAlgebra ShadowProtocols Shims Slice Sort StaticString Stride StringCharacterView String StringBridge StringBuffer StringComparable StringCore StringHashable StringInterpolation StringLegacy StringRangeReplaceableCollection StringIndexConversions StringUnicodeScalarView StringUTF16 StringUTF8 SwiftNativeNSArray UnavailableStringAPIs Unicode UnicodeScalar UnicodeTrie Unmanaged UnsafeBitMap UnsafeBufferPointer UnsafeRawBufferPointer UnsafePointer UnsafeRawPointer WriteBackMutableSlice Availability CollectionOfOne ExistentialCollection Mirror CommandLine SliceBuffer Tuple UnfoldSequence VarArgs Zip" |
| 334 | + |
| 335 | +"Unfortunately, the order in which we send files to the Swift compiler actually matters. We search this list to determine where each source file should go." |
| 336 | +) |
| 337 | + |
| 338 | +(defun swift-project-stdlib-compile-order (filename) |
| 339 | + "Return an integer representing where in the required |
| 340 | +compilation order the given file should appear." |
| 341 | + (save-match-data |
| 342 | + (if (string-match |
| 343 | + (concat "\\<" (regexp-quote (replace-regexp-in-string "^\\(?:.*[/!]\\)?\\([^.]*\\).*" "\\1" filename)) "\\>") |
| 344 | + swift-project-stdlib-compile-order) |
| 345 | + (match-beginning 0) 0))) |
| 346 | + |
| 347 | +(defconst swift-project-common-swiftc-args |
| 348 | + (list "-parse" "-sdk" swift-project-sdk-path |
| 349 | + "-F" (concat (file-name-as-directory swift-project-sdk-path) "../../../Developer/Library/Frameworks") |
| 350 | + "-D" "INTERNAL_CHECKS_ENABLED" |
| 351 | + "-no-link-objc-runtime") |
| 352 | + "The common arguments we'll pass to swiftc for syntax-checking |
| 353 | +everything in the Swift project" ) |
| 354 | + |
| 355 | +(defconst swift-project-single-frontend-swiftc-args |
| 356 | + (append swift-project-common-swiftc-args |
| 357 | + (list "-force-single-frontend-invocation" "-parse-as-library")) |
| 358 | + "The arguments we'll pass to swiftc for syntax-checking |
| 359 | +libraries that require a single frontend invocation" ) |
| 360 | + |
| 361 | +(defconst swift-project-stdlib-aux-swiftc-args |
| 362 | + (append swift-project-single-frontend-swiftc-args |
| 363 | + (list "-Xfrontend" "-sil-serialize-all" "-parse-stdlib")) |
| 364 | + "swiftc arguments for library components that are compiled as |
| 365 | + though they are part of the standard library even though |
| 366 | + they're not strictly in that binary." ) |
| 367 | + |
| 368 | +(defconst swift-project-stdlib-swiftc-args |
| 369 | + (append |
| 370 | + swift-project-stdlib-aux-swiftc-args (list "-nostdimport" "-module-name" "Swift")) |
| 371 | + "The arguments we'll pass to swiftc for syntax-checking the |
| 372 | +standard library" ) |
| 373 | + |
| 374 | +(defun swift-project-files-to-compile-with (relative-file) |
| 375 | + "Given RELATIVE-FILE, a project-relative path, returns a list |
| 376 | +of other files that are compiled along with it." |
| 377 | + (if (and (string-match-p "^test/\|^validation-test/" relative-file) |
| 378 | + (not (string-match-p "^test/multifile" relative-file))) |
| 379 | + nil |
| 380 | + (directory-files (concat swift-project-directory (file-name-directory relative-file)))) |
| 381 | +) |
| 382 | + |
| 383 | +(defun swift-project-swiftc-arguments (relative-file) |
| 384 | + "Given RELATIVE-FILE, a project-relative path, returns a list |
| 385 | +of arguments that are passed to swiftc when compiling it." |
| 386 | + (cond ((string-match-p "^stdlib/public/core/" relative-file) |
| 387 | + swift-project-stdlib-swiftc-args) |
| 388 | + ((string-match-p |
| 389 | + "^stdlib/\(public/SwiftOnoneSupport\|internal\|private/SwiftPrivate\(PthreadExtras\|LibcExtras\)?\)/" |
| 390 | + relative-file) |
| 391 | + swift-project-stdlib-aux-swiftc-args) |
| 392 | + (t swift-project-single-frontend-swiftc-args))) |
| 393 | + |
| 394 | +(defun swift-project-swift-syntax-check (swiftc temp-file) |
| 395 | + "Return a flymake command-line list for syntax-checking the |
| 396 | +current buffer, potentially along with the other .swift and .swift.gyb |
| 397 | +files in the same directory." |
| 398 | + (let ((project-relative-buffer-file (file-relative-name (buffer-file-name) swift-project-directory))) |
| 399 | + (swift-project-gyb-syntax-check1 |
| 400 | + swiftc temp-file |
| 401 | + (swift-project-files-to-compile-with project-relative-buffer-file) |
| 402 | + (swift-project-swiftc-arguments project-relative-buffer-file)))) |
| 403 | + |
| 404 | +(defun swift-project-gyb-syntax-check1 (swiftc temp-file other-files swiftc-arguments) |
| 405 | + "Return a flymake command-line list for syntax-checking the |
| 406 | +current buffer along with the other .swift and .swift.gyb |
| 407 | +files in the same directory." |
| 408 | + (let* (gyb-targets swift-sources) |
| 409 | + (dolist (x (cons temp-file other-files)) |
| 410 | + (unless (file-equal-p x (buffer-file-name)) |
| 411 | + (when (string-match-p "\\.swift$\\|\\.swift\\.gyb$" (if (string-equal x temp-file) (buffer-file-name) x)) |
| 412 | + (let ((swift-file (swift-project-gybbed-file x))) |
| 413 | + (setq swift-sources (cons swift-file swift-sources)) |
| 414 | + (when (string-equal "gyb" (file-name-extension x)) |
| 415 | + (setq gyb-targets (cons swift-file gyb-targets))))))) |
| 416 | + (setq swift-sources |
| 417 | + (sort swift-sources |
| 418 | + (lambda (x y) (< (swift-project-stdlib-compile-order x) |
| 419 | + (swift-project-stdlib-compile-order y))))) |
| 420 | + `(,(swift-project-executable-find "line-directive") |
| 421 | + (,@gyb-targets "--" ,swiftc ,@swiftc-arguments ,@swift-sources)))) |
| 422 | + |
| 423 | +(require 'flymake) |
| 424 | +(add-to-list 'flymake-allowed-file-name-masks '(".+\\.swift.gyb$" flymake-swift-init)) |
| 425 | + |
258 | 426 | (provide 'swift-project-settings)
|
259 | 427 | ;; end of swift-project-settings.el
|
0 commit comments