使用xmake編譯swift代碼

xmake不只能夠支持 c/c++文件,同時也支持 objc/c++,甚至swift代碼的編譯。c++

咱們先看一下如何建立Swift工程,首先執行--help,看下幫助文檔:git

xmake create --help

顯示以下:github

Usage: xmake create [options] [target]

Create a new project.

Options: 
    -n NAME, --name=NAME                   The project name.
    -f FILE, --file=FILE                   Create a given xmake.lua file. (default: xmake.lua)
    -P PROJECT, --project=PROJECT          Create from the given project directory.
                                           Search priority:
                                               1. The Given Command Argument
                                               2. The Envirnoment Variable: XMAKE_PROJECT_DIR
                                               3. The Current Directory
    -l LANGUAGE, --language=LANGUAGE       The project language (default: c)
                                               - c
                                               - c++
                                               - objc
                                               - objc++
                                               - swift
    -t TEMPLATE, --template=TEMPLATE       Select the project template id of the given language. (default: 1)
                                               - language: c
                                                 1. The Console Program
                                                 2. The Console Program (tbox)
                                                 3. The Shared Library
                                                 4. The Shared Library (tbox)
                                                 5. The Static Library
                                                 6. The Static Library (tbox)
                                               - language: c++
                                                 1. The Console Program
                                                 2. The Console Program (tbox)
                                                 3. The Shared Library
                                                 4. The Shared Library (tbox)
                                                 5. The Static Library
                                                 6. The Static Library (tbox)
                                               - language: objc
                                                 1. The Console Program
                                               - language: objc++
                                                 1. The Console Program
                                               - language: swift
                                                 1. The Console Program
                                           
    -v, --verbose                          Print lots of verbose information.
        --version                          Print the version number and exit.
    -h, --help                             Print this help message and exit.
                                           
    target                                 Create the given target.
                                           Uses the project name as target if not exists.

能夠看到 只要指定 語言爲swift,工程模板選擇1,就能建立一個基於swift的控制檯項目,具體操做以下:swift

xmake create -l swift -t 1 -P /tmp/test -n swift_test

執行完成後,就會在/tmp/test目錄下自動生成一個名爲swift_test的工程bash

咱們看下生成好的xmake.luathis

-- the debug mode
if modes("debug") then
    
    -- enable the debug symbols
    set_symbols("debug")

    -- disable optimization
    set_optimize("none")
end

-- the release mode
if modes("release") then

    -- set the symbols visibility: hidden
    set_symbols("hidden")

    -- enable fastest optimization
    set_optimize("fastest")

    -- strip all symbols
    set_strip("all")
end

-- add target
add_target("swift_test")

    -- set kind
    set_kind("binary")

    -- add files
    add_files("src/*.swift")

能夠看到,和日常的xmake.lua描述沒什麼區別,惟一的改動就是:add_files("src/*.swift") lua

而生成的main.swift代碼,也很簡單:.net

import Foundation

print("hello world!")

如今咱們進入/tmp/test目錄編譯下:debug

cd /tmp/test
xmake

編譯完後,就能夠運行了:code

xmake r swift_test

顯示效果:

··· hello world! ···

搞定。


相關文章
相關標籤/搜索