note/src/x/xmake/xmake 解析.md
2024-01-26 17:33:37 +08:00

7.0 KiB

xmake

option

-- core/base/task.lua
function task.common_options()
    if not task._COMMON_OPTIONS then
        task._COMMON_OPTIONS =
        {
            {'q', "quiet",     "k",  nil,   "Quiet operation."                                          }
        ,   {'y', "yes",       "k",  nil,   "Input yes by default if need user confirm."                }
        ,   {nil, "confirm",   "kv", nil,   "Input the given result if need user confirm."
                                        ,   values = function ()
                                                return {"yes", "no", "def"}
                                            end                                                         }
        ,   {'v', "verbose",   "k",  nil,   "Print lots of verbose information for users."              }
        ,   {nil, "root",      "k",  nil,   "Allow to run xmake as root."                               }
        ,   {'D', "diagnosis", "k",  nil,   "Print lots of diagnosis information (backtrace, check info ..) only for developers."
                                        ,   "And we can append -v to get more whole information."
                                        ,   "    e.g. $ xmake -vD"                                      }
        ,   {nil, "version",   "k",  nil,   "Print the version number and exit."                        }
        ,   {'h', "help",      "k",  nil,   "Print this help message and exit."                         }
        ,   {}
        ,   {'F', "file",      "kv", nil,   "Read a given xmake.lua file."                              }
        ,   {'P', "project",   "kv", nil,   "Change to the given project directory."
                                        ,   "Search priority:"
                                        ,   "    1. The Given Command Argument"
                                        ,   "    2. The Envirnoment Variable: XMAKE_PROJECT_DIR"
                                        ,   "    3. The Current Directory"                              }
        ,   {category = "action"}
        }
    end
    return task._COMMON_OPTIONS
end
-- core/base/option.lua
-- parse arguments with the given options
function option.parse(argv, options, opt)
    -- run parser
    local pargs = cli.parsev(argv, flags) 
    --...
end

task

  • build
    • config
-- core/base/task.lua
-- the menu
function task.menu(tasks)
    -- make menu
    local menu = {}
    for taskname, taskinst in pairs(tasks) do

        -- has task menu?
        local taskmenu = taskinst:get("menu")
        -- main?
        if taskinst:get("category") == "main" then

            -- delay to load main menu
            menu.main = function ()

            -- translate main menu
            local mainmenu, errors = task._translate_menu(taskmenu)
            if not mainmenu then
                os.raise(errors)
            end

            -- make tasks for the main menu
            mainmenu.tasks = {}
            for name, inst in pairs(tasks) do
                -- has menu?
                local m = inst:get("menu")
                -- add task
                mainmenu.tasks[name] =
                {
                    category    = inst:get("category"),   
                    shortname   = m.shortname,   
                    description = m.description
                }
            end
            -- ok
            return mainmenu
         end
         -- delay to load task menu
        menu[taskname] = function ()
            local taskmenu, errors = task._translate_menu(taskmenu)
            return taskmenu
        end
    end
    -- ok?
    return menu
end

config

  • global
    • "C:\Users\ouczbs\AppData\Local.xmake\xmake.conf"
  • local
    • D:\ZEngine.xmake\windows\x64
-- core/base/global.lua
-- get the configure file
function global.filepath()
    return path.join(global.directory(), xmake._NAME .. ".conf")
end
-- load the global configuration
function global.load()
    -- load configure from the file first
    local filepath = global.filepath()
    -- load configs
    local results, errors = io.load(filepath)

    -- merge the configure
    for name, value in pairs(results) do
        if global.get(name) == nil then
            global.set(name, value)
        end
    end
end

theme

-- core/base/theme.lua

-- load the given theme
function theme.load(name)
    -- find the theme script path
    local scriptpath = nil
    if name then
        for _, dir in ipairs(theme.directories()) do
            scriptpath = path.join(dir, name, "xmake.lua")
            if os.isfile(scriptpath) then
                break
            end
        end
    end

    -- not exists? uses the default theme
    if not scriptpath or not os.isfile(scriptpath) then
        scriptpath = path.join(os.programdir(), "themes", "default", "xmake.lua")
    end

    -- get interpreter
    local interp = theme._interpreter()

    -- load script
    local ok, errors = interp:load(scriptpath)
    if not ok then
        return nil, errors
    end

    -- load theme
    local results, errors = interp:make("theme", true, false)
    if not results and os.isfile(scriptpath) then
        return nil, errors
    end

    -- get result
    local result = results[name]
    if not result then
        return nil, string.format("the theme %s not found!", name)
    end

    -- new an instance
    local instance, errors = _instance.new(name, result, interp:rootdir())
    if not instance then
        return nil, errors
    end

    -- save the current theme instance
    theme._THEME = instance
    return instance
end

main

--core/_xmake_main.lua
-- load modules
local main = require("main")

-- the main function
function _xmake_main()
    return main.entry()
end
--core/main.lua
function main.entry()
    -- init
    local ok, errors = main._init()
    --[[
        -- core/base/option.lua
        -- parse options
        option.parse(xmake._COMMAND_ARGV, task.common_options(), { allow_unknown = true })
        
    	-- core/base/task.lua
        --load task
    	option._translate(menu)
        task.menu(xmake._COMMAND_ARGV, task.common_options(), { allow_unknown = true })
    ]]
    -- load global configuration
    ok, errors = global.load()
    --[[
    	-- core/base/global.lua
        -- load config
        global.load()
    ]]
     -- load theme
    local theme_inst = theme.load(os.getenv("XMAKE_THEME") or global.get("theme")) or theme.load("default")
    colors.theme_set(theme_inst)
    
    -- init option
    ok, errors = option.init(menu)
    --[[
    	-- core/base/global.lua
        -- load config
        task.menu()
    	option.parse(xmake._COMMAND_ARGV, options, { populate_defaults = false })
    ]]
    
    -- show help?
    if main._show_help() then
        return main._exit(true)
    end
    
    -- get task instance
    local taskname = option.taskname() or "build"
    local taskinst = task.task(taskname) or project.task(taskname)
    
    -- run task
    scheduler:enable(true)
    scheduler:co_start_named("xmake " .. taskname, function ()
        local ok, errors = taskinst:run()
    end)
    ok, errors = scheduler:runloop()
end