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

Linux (and maybe Windows?) support #69

Closed
wants to merge 5 commits into from
Closed

Linux (and maybe Windows?) support #69

wants to merge 5 commits into from

Conversation

finnvoor
Copy link
Owner

@finnvoor finnvoor commented Aug 3, 2024

  • Add more fallbacks for tool locations
  • Fix clang linker flags on linux
  • avoid xcrun (swift toolchain env var?)
  • env var for setting arm toolchain path
  • linux CI
  • verify .pdx works on linux simulator + device
  • add docs

closes #68

@finnvoor finnvoor changed the title linux Linux support Aug 3, 2024
@finnvoor finnvoor changed the title Linux support Linux (and maybe Windows?) support Aug 4, 2024
@FliiFe

This comment was marked as outdated.

@FliiFe
Copy link
Contributor

FliiFe commented Feb 14, 2025

May I suggest the following patch ? The compiler actually struggles with unresolved relative paths and discards those include paths. Using this patch fixes compilation without setting environment variables.

--- a/Plugins/PDCPlugin/PDCPlugin.swift	2025-02-14 15:54:26.782547459 +0100
+++ b/Plugins/PDCPlugin/PDCPlugin.swift	2025-02-14 15:59:19.550330283 +0100
@@ -100,7 +100,7 @@
             throw Tools.Error.armNoneEabiGCCNotFound
         }
 
-        let cFlags = gccIncludePaths.flatMap { ["-I", $0] }
+        let cFlags = gccIncludePaths.flatMap { ["-I", URL(fileURLWithPath: $0).standardized.path] }
 
         let swiftFlags = cFlags.flatMap { ["-Xcc", $0] } + [
             "-O",

@FliiFe
Copy link
Contributor

FliiFe commented Feb 14, 2025

The following patch fixes simulator support. On linux, a shared object file (.so) is expected instead of a .dylib like on macOS.

--- a/Plugins/PDCPlugin/PDCPlugin.swift	2025-02-14 15:54:26.782547459 +0100
+++ b/Plugins/PDCPlugin/PDCPlugin.swift	2025-02-14 16:53:53.723881200 +0100
@@ -213,19 +213,18 @@
                     print("building pdex.dylib")
 
                     #if os(Linux)
-                    let linkerFlags = ["-Wl,--undefined=_eventHandlerShim", "-Wl,--undefined=_eventHandler"]
+                    let linkerFlags = ["-Wl,--undefined=_eventHandlerShim", "-Wl,--undefined=_eventHandler", "-shared", "-o", sourcePath.appending(["pdex.so"]).string]
                     #else
-                    let linkerFlags = ["-Wl,-exported_symbol,_eventHandlerShim", "-Wl,-exported_symbol,_eventHandler"]
+                    let linkerFlags = ["-Wl,-exported_symbol,_eventHandlerShim", "-Wl,-exported_symbol,_eventHandler", "-dynamiclib", "-rdynamic", "-o", sourcePath.appending(["pdex.dylib"]).string]
                     #endif
 
                     try tools.clang([
                         "-nostdlib", "-dead_strip"
                     ] + linkerFlags + [
-                        module.modulePath(for: .simulator), "-dynamiclib", "-rdynamic", "-lc", "-lm",
+                        module.modulePath(for: .simulator), "-lc", "-lm",
                         "-DTARGET_SIMULATOR=1", "-DTARGET_EXTENSION=1",
                         "-I", ".",
                         "-I", "\(playdateSDK)/C_API",
-                        "-o", sourcePath.appending(["pdex.dylib"]).string,
                         "\(playdateSDK)/C_API/buildsupport/setup.c"
                     ])
                 case .productDependency:

@finnvoor
Copy link
Owner Author

@FliiFe thanks for taking a look at this! Unfortunately this PR is a bit out of date with the current PDCPlugin, so will need some changes. I'm not set up at the moment to test in a linux env, so if you're interested and have time it would be great if you could open a new PR with any changes necessary to build on linux, and I'm happy to merge. Otherwise I'll try to get an env set up locally when I have some free time.

@finnvoor
Copy link
Owner Author

just putting this here for reference, these are the commands I ran on a fresh linux install to get the build working last time I tried:

sudo apt-get install wget -y
wget https://download.panic.com/playdate_sdk/Linux/PlaydateSDK-latest.tar.gz
wget https://download.swift.org/development/ubuntu2204/swift-DEVELOPMENT-SNAPSHOT-2024-08-01-a/swift-DEVELOPMENT-SNAPSHOT-2024-08-01-a-ubuntu22.04.tar.gz
sudo apt-get install -y gcc-arm-none-eabi
tar xzf PlaydateSDK-latest.tar.gz
tar xzf swift-DEVELOPMENT-SNAPSHOT-2024-08-01-a-ubuntu22.04.tar.gz
rm *.gz
sudo apt-get install git -y
git clone https://github.com/finnvoor/PlaydateKitTemplate.git
git clone https://github.com/finnvoor/PlaydateKit.git
sudo apt-get install binutils git gnupg2 libc6-dev libcurl4-openssl-dev libedit2 libgcc-11-dev libpython3-dev libsqlite3-0 libstdc++-11-dev libxml2-dev libz3-dev pkg-config python3-lldb-13 tzdata unzip zlib1g-dev -y

@FliiFe
Copy link
Contributor

FliiFe commented Feb 14, 2025

I'll look into it. For reference, with the two patches above and the following Dockerfile, everything builds correctly for simulator and device

FROM swiftlang/swift:nightly-main-jammy

# Update apt cache and install dependencies
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        wget \
        gcc-arm-none-eabi \
        libnewlib-arm-none-eabi \
        libstdc++-arm-none-eabi-newlib \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Fetch and extract the Playdate SDK
RUN mkdir -p /opt
WORKDIR /opt
RUN wget https://download.panic.com/playdate_sdk/Linux/PlaydateSDK-latest.tar.gz \
    && tar -zxvf PlaydateSDK-latest.tar.gz \
    && rm PlaydateSDK-latest.tar.gz \
    && mv PlaydateSDK-* PlaydateSDK \
    && chmod -R 755 /opt

# Set environment variables
ENV PLAYDATE_SDK_PATH=/opt/PlaydateSDK \
    ARM_TOOLCHAIN_PATH=/usr/lib/gcc/arm-none-eabi/10.3.1/

RUN mkdir /.swiftpm /.cache && chmod 777 /.swiftpm /.cache

# Set default work directory
RUN mkdir -p /mnt
VOLUME /mnt
WORKDIR /mnt

@finnvoor
Copy link
Owner Author

moved to #113

@finnvoor finnvoor closed this Feb 18, 2025
@finnvoor finnvoor deleted the linux branch February 18, 2025 20:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error swift package pdc on Windows
2 participants