Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix building on Linux #12

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Fix building on Linux #12

wants to merge 9 commits into from

Conversation

Samasaur1
Copy link
Owner

Earlier today, I tried to build on a Linux machine (clang reports the target triple as x86_64-unknown-linux-gnu), and SimpleSwiftServer failed to build. I was able to track down all the bugs and get it building, and they are as follows:

  1. SwiftIP was not building. I have opened a PR (Fix three linux bugs SwiftIP#11) to fix the problems in that project.
  2. You can't import Foundation.NSFileManager on Linux since there is no NSFileManager. Since we were only limiting this import to try to optimize the macOS build, I've removed it (I doubt it was having much of an impact and the simplicity of not having conditional compilation is a priority over any slight reduction in macOS binary size)
  3. The version of Swifter that we are using, 1.4.5, did not build successfully. I was able to patch this to make this project compile, but I'm not sure how to publish this "fix" (since I cherry-picked changes from Swifter's stable branch)

Worth noting this build was with the following version of Swift:

$ swiftc --version
Swift version 5.5.1 (swift-5.5.1-RELEASE)
Target: x86_64-unknown-linux-gnu

This was originally in here so that we didn't import the entirety of Foundation,
  but that's a little overkill of an optimization. Plus, this way we avoid
  conditional compilation, and that is preferable to a slightly smaller macOS executable.
@Samasaur1 Samasaur1 added the bug Something isn't working label Feb 6, 2022
@Samasaur1
Copy link
Owner Author

As of this point in the PR, 1 and 2 are fixed, but I'm still not sure about Swifter

@Samasaur1
Copy link
Owner Author

For anyone wondering why I'm not just upgrading to the latest version of Swifter (and why we're pinned at 1.4.5), when I do use the latest version, both the directory browser and web server modes don't work

@Samasaur1
Copy link
Owner Author

Here's the patch file for the change necessary to version 1.4.5 of Swifter:

diff --git a/Sources/Socket+File.swift b/Sources/Socket+File.swift
index 81f0c48..ff4b57f 100644
--- a/Sources/Socket+File.swift
+++ b/Sources/Socket+File.swift
@@ -19,15 +19,19 @@ import Foundation
             }
             var writeCounter = 0
             while writeCounter < readResult {
-                #if os(Linux)
-                    let writeResult = send(target, &buffer + writeCounter, readResult - writeCounter, Int32(MSG_NOSIGNAL))
-                #else
-                    let writeResult = write(target, &buffer + writeCounter, readResult - writeCounter)
-                #endif
+                let writeResult = buffer.withUnsafeBytes { (ptr) -> Int in
+                    let start = ptr.baseAddress! + writeCounter
+                    let len = readResult - writeCounter
+#if os(Linux)
+                    return send(target, start, len, Int32(MSG_NOSIGNAL))
+#else
+                    return write(target, start, len)
+#endif
+                }
                 guard writeResult > 0 else {
                     return Int32(writeResult)
                 }
-                writeCounter = writeCounter + writeResult
+                writeCounter += writeResult
             }
         }
     }

As I said earlier, this is stolen from the latest version of Swifter.

@Samasaur1
Copy link
Owner Author

To get this building, you can run these commands:

git clone https://github.com/Samasaur1/SimpleSwiftServer
cd SimpleSwiftServer
git checkout linux-compatibility
swift package edit Swifter
cd Packages/Swifter
git apply ../../swifter.patch
cd ../..
swift build

(the swift build line can optionally be swift build -c release)

@Samasaur1
Copy link
Owner Author

ok well that works for some linux builds

@Samasaur1
Copy link
Owner Author

5.3, 5.4, and 5.5 build on both versions of Ubuntu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant