Embedded Swift MIPS

Hello,

I was trying out using LLVM to target the PS2, which uses a mostly normal MIPS CPU just with a weird FPU. I got a hello world working with Zig, and I don't believe Swift object files would be any different — except for the standard library which isn't bundled for the target, preventing compilation in the first place.

I believe I tried it and asked about it in the past but didn't get anywhere. How do I compile the standard library for my target? The toolchain includes a bunch of targets, so it clearly defines them somewhere, but I couldn't find where. Some scripts seemed to have comments related to supporting building the library with CMake but I don't believe it was implemented.

The swift toolchain does not currently support that target: you'd have to add support for it. To cross-compile just the standard library, use the command to build the compiler in the doc and add the flags --skip-build-cmark --build-llvm=0 --build-swift-tools=0 --native-clang-tools-path=/path/to/prebuilt-swift-toolchain/usr/bin --native-swift-tools-path=/path/to/prebuilt-swift-toolchain/usr/bin, as seen in the build preset for building a standalone standard library or the docs for cross-compiling the stdlib to Android. Note that the build-script assumes that your C sysroot for your platform is in /, so you'll have to hack that in to the build-script if it isn't.

If you decide to add this port, I suggest you grep the stdlib/ directory for platforms like linux and Android and architectures like arm64 or x86_64 to see what platform-specific code there is already in the current source. That will give you an idea of the work required to add a new platform.

1 Like

Are you sure the function you linked to is relevant? This list includes only operating systems, and is not the full list of Embedded Swift targets.

I do not care about porting full Swift and its standard library, the massive binaries are very much not desirable with just 32 megabytes of memory.


What I was looking for is where the toolchain defines for which triples to bundle the embedded standard library.

OK, I figured you'd need to port to the PS2 OS too, didn't know you wanted the purely embedded subset.

It appears the supported embedded triples in trunk are listed in this CMake config. You can take a look at recent pulls adding support for AVR or i686 to see what kinds of changes would be needed for MIPS.

Great :slight_smile:
I don't think there is anything to do besides adding the target there. It's a completely normal 32/64 bit target.

Though for the PS2 specifically there are some subtle issues that would require a dedicated target. Not sure if that also requires adding it to LLVM.

  • The FPU is not entirely standard, only supports single precision floats and I think it doesn't handle things like infinities or NaN.
  • I think the abi is not standard, which would be important to link existing libraries like the libc from ps2sdk. This definitely requires work in LLVM :frowning:

I do not know what the reasons are for ps2sdk using the custom gcc target, it's not very well documented, but it would be nice to support it.

I don't think I've ever been able to get the build script to work.

CMake Warning (dev) at CMakeLists.txt:46 (enable_language):
  project() should be called prior to this enable_language() call.
This warning is for project developers.  Use -Wno-dev to suppress it.

-- The C compiler identification is AppleClang 16.0.0.16000026
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang - skipped
-- Detecting C compile features
-- Detecting C compile features - done
CMake Warning (dev) at CMakeLists.txt:47 (enable_language):
  project() should be called prior to this enable_language() call.
This warning is for project developers.  Use -Wno-dev to suppress it.

-- The CXX compiler identification is AppleClang 16.0.0.16000026
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning (dev) at CMakeLists.txt:68 (enable_language):
  project() should be called prior to this enable_language() call.
This warning is for project developers.  Use -Wno-dev to suppress it.

-- The ASM compiler identification is AppleClang
-- Found assembler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
CMake Error at CMakeLists.txt:91 (include):
  include could not find requested file:

    SwiftUtils


CMake Error at CMakeLists.txt:96 (include):
  include could not find requested file:

    SwiftCompilerCapability


CMake Warning (dev) at CMakeLists.txt:108 (enable_language):
  project() should be called prior to this enable_language() call.
This warning is for project developers.  Use -Wno-dev to suppress it.

-- The Swift compiler identification is Apple 6.0.3
-- Check for working Swift compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc
-- Check for working Swift compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc - works
CMake Error at CMakeLists.txt:329 (include):
  include could not find requested file:

    /Users/teampuzel/OpenSource/swift-project/swift/cmake/SwiftVersion.cmake


CMake Error at CMakeLists.txt:446 (is_build_type_optimized):
  Unknown CMake command "is_build_type_optimized".

Not sure what it's doing and why it's even using the Xcode toolchain.


This also happens when just trying to build the entire toolchain normally. This build script is extremely fragile, I am on latest macOS and it still doesn't work with the most basic commands.