Xmake Build Utility

repository·dev·Indexed 11 days ago

https://github.com/xmake-io/xmake

A modern, cross-platform build utility based on Lua that combines build backend capabilities, project generation, and a built-in package manager. It supports a wide variety of toolchains including msvc, clang, gcc, rust, zig, and cuda, and can generate project files for Visual Studio, CMake, and Ninja.

Tokens
42.4K
Snippets
155
Records
198
Agent score
94%

What's inside Xmake

  1. What is xmake?

    dev

    xmake is a lightweight, cross-platform build utility based on Lua. It functions as a combined Build backend, Project Generator, Package Manager, and supports Remote/Distributed builds and Caching.

    It is designed to be a modern alternative to Make/Ninja, CMake/Meson, and package managers like Vcpkg/Conan, providing a concise and intuitive configuration syntax via xmake.lua files.

  2. Install xmake

    dev

    You can install xmake using one-line scripts for various environments. For other methods like building from source or using system package managers, refer to the official Installation Guide.

    ### With cURL
    ```bash
    curl -fsSL https://xmake.io/shget.text | bash

    With Wget

    wget https://xmake.io/shget.text -O - | bash

    With PowerShell

    irm https://xmake.io/psget.text | iex
  3. Integrate XMake with Gradle (JNI)

    dev

    Use the xmake-gradle plugin to integrate native C/C++ library compilation into an Android/Gradle build process. Once applied, xmakeBuild tasks are automatically injected into the standard assemble tasks.

    plugins {
      id 'org.tboox.gradle-xmake-plugin' version '1.1.5'
    }
    
    android {
        externalNativeBuild {
            xmake {
                path "jni/xmake.lua"
            }
        }
    }
  4. How Xmake handles configuration changes and rechecks

    dev

    Xmake uses a caching mechanism to avoid unnecessary reconfigurations. A reconfiguration (recheck) is triggered if:

    1. The --clean or --check flag is used.
    2. Project files (tracked via mtimes) have been modified.
    3. The recheck flag is set in the local cache.
    4. The Xmake core files have been updated (detected via os.mtime of core/main.lua).
    5. The host system has changed.

    When a recheck is triggered, Xmake clears several caches, including config, detect, option, package, and toolchain, to ensure a consistent build environment.

  5. Use file patterns and exclusions with the format plugin

    dev

    When using the --files option, you can provide space-separated or environment-variable-expanded patterns. You can also specify files to exclude by using the pipe | character within the pattern string.

    Pattern Syntax:

    • path/to/*.cpp: Matches all .cpp files in a directory.
    • path/to/*.cpp|path/to/exclude/*.cpp: Matches .cpp files but excludes those in the specified directory.

    Note: The plugin translates these patterns into Lua patterns for matching against the project's source and header files.

    xmake format --files="src/*.cpp|src/third_party/*.cpp"
  6. How xmake finds the root project file

    dev

    If a specific project file is not provided via -F or --file, xmake uses a search algorithm to find the root xmake.lua:

    1. It starts from the directory of the provided file (or the current working directory).
    2. It traverses upwards through parent directories.
    3. It returns the first directory in the hierarchy that contains an xmake.lua file.
    4. If no such file is found, it defaults to the provided file path.
  7. AppImage plugin requirements and constraints

    dev

    When using the AppImage packaging plugin, keep the following requirements in mind:

    • Platform: Only supported on Linux.
    • Main Executable: The package must contain at least one binary target. The plugin attempts to find the executable in the usr/bin directory of the installation root or by searching the bindir.
    • Icon Files: Supported formats are strictly .png, .svg, or .xpm. If an iconfile is provided, it must exist at the specified path.
    • Tooling: The plugin requires appimagetool. If not found on the system, it will attempt to install it via the appimage package set.
  8. Use the runself packaging plugin

    dev

    The runself plugin is a packaging mechanism for Xmake that uses makeself to create self-extracting shell script archives. It is designed for Unix-like systems (it returns early on Windows).

    When a package is packed using runself, the plugin:

    1. Locates or installs makeself.sh.
    2. Generates a .lsm specfile with variables replaced from the package's specvars.
    3. Archives the package's source files and components.
    4. Generates a __setup__.sh script containing the installation commands (e.g., cp, rm, mkdir, mv, cd) defined in the package.
    5. Invokes makeself with --gzip, --sha256, and --lsm flags to produce the final output file.

    Note: This plugin is intended to be used within the Xmake packaging framework via the pack plugin system.

  9. How WiX command translation works

    dev

    The WiX plugin translates Xmake installation commands into WiX XML elements. This allows your standard install rules to drive the MSI creation:

    Xmake CommandWiX ElementDescription
    cp<File>Copies files into components. Directories are grouped into single components.
    mkdir<CreateFolder>Creates a directory in the installation folder.
    rm<RemoveFile>Removes a file during uninstall (or install if specified).
    rmdir<RemoveFolder>Removes a directory during uninstall (or install if specified).
    wix(Raw XML)Allows inserting arbitrary WiX XML strings directly.

    Note on cp (Copy): The plugin automatically groups files destined for the same directory into a single WiX <Component> to ensure efficient installation and proper GUID management.

  10. Configure target execution via Lua scripts

    dev

    You can extend or customize how a target runs by defining specific script hooks in your xmake.lua file. Xmake executes these hooks in a specific lifecycle order:

    1. target:script("run_before"): Custom logic before any rules are applied.
    2. Rule Scripts: Any run_before or run scripts defined within target:orderules().
    3. target:script("run"): The primary custom run logic for the target.
    4. Rule Scripts: Any run_after scripts defined within target:orderules().
    5. target:script("run_after"): Custom logic after the target has finished executing.

    This allows you to perform setup (like starting a database) or teardown (like cleaning up temporary files) around your target execution.

    target("test", "test")
        set_kind("binary")
        -- Logic to run before the target
        on_run(function (target) print("Preparing...") end)
        -- Custom run logic
        run("my_binary --arg1")
        -- Logic to run after the target
        after_run(function (target) print("Cleaning up...") end)