How can I compile a simple Swift cli program for Alpine?

100 views Asked by At

I have a simple cli written in Swift that I want to run on Alpine. I want the binary to be self-contained. How can I do that? I have tried:

  • using swift run and swift build with --static-swift-stdlib (but that has no musl support?)
  • static linking with docker run -v "$PWD:/sources" -w /sources --platform linux/amd64 swift:latest swiftc -emit-executable -static-executable -O main.swift testit, but then the executable testit when running it on Alpine, says it is Unable to obtain Swift runtime path Aborted.

The code of the program:


// The Swift Programming Language
// https://docs.swift.org/swift-book
func getSecret() -> String {
    let CharArr: [Character] = ["T", "h", "i", "s", " ", "a", " ", "s", "e", "c", "r", "e", "t"] 

    let newStr = String(CharArr) 
    return newStr
}
if(CommandLine.arguments.count == 2){
    if (CommandLine.arguments[1] == "spoil"){
        print(getSecret())
    } else {
        if (CommandLine.arguments[1] == getSecret()){
            print("This is correct! Congrats!");
        }else{
            print("This is incorrect. Try again");
        }
    }
} else if (CommandLine.arguments.count==1) {
    print("Welcome to the wrongsecrets Swift binary which hides a secret.");
    print("Use args spoil or a string to guess the password.");
} else {
    print("Too many arguments supplied.");
}

Can you help me please?

0

There are 0 answers