Support this project by becoming a sponsor. Your logo will show up here with a link to your website. 🙏
You can also consider sponsoring us to get extra technical support services via the Github sponsor program, This gives you access to the xmake-io/technical-support repository, where you can get more information on consulting.
- Handling Issues with higher priority
- One-to-one technical consulting service
- Review your xmake.lua and provide suggestions for improvement
Xmake.sh is a script-only build utility like autotools.
- No any dependencies
- No installation, only one shell script file
- Easy, similar configuration syntax to xmake
- Compatible with autotools, same usage
- Generate makefile
- Generate build.ninja
- Custom toolchains
- Detect options, code snippets, dependencies, compiler features
- Support builtin variables
Copy ./configure script file to your project root directory.
We just write xmake.sh project file, like this:
#!/bin/sh
set_project "hello"
set_version "1.0.1" "%Y%m%d%H%M"
option "debug" "Enable debug compilation mode." false
option "tests" "Enable tests." true
option "pthread"
add_links "pthread"
add_cincludes "pthread.h"
add_cfuncs "pthread_create"
option_end
option "cxx_constexpr"
set_languages "c++11"
add_cxxsnippets "constexpr int k = 0;"
option_end
option "lua"
add_cfuncs "lua_pushstring"
add_cincludes "lua.h" "lualib.h" "lauxlib.h"
before_check "option_find_lua"
option_end
option_find_lua() {
option "lua"
add_cxflags `pkg-config --cflags lua5.4 2>/dev/null`
add_ldflags `pkg-config --libs lua5.4 2>/dev/null`
option_end
}
set_warnings "all" "error"
set_languages "c99" "c++11"
if is_mode "debug"; then
set_symbols "debug"
set_optimizes "none"
else
set_strip "all"
set_symbols "hidden"
set_optimizes "smallest"
fi
target "demo"
set_kind "binary"
add_deps "foo" "bar"
add_files "*.cpp"
add_includedirs "foo" "bar"
add_configfiles "config.h.in"
set_configdir "${builddir}/include"
add_headerfiles "${builddir}/include/config.h" "hello"
add_headerfiles "(bar/*.h)" "hello"
add_headerfiles "foo/(*.h)" "hello"
add_installfiles "res/(png/**.png)" "share"
add_options "lua"
if has_config "debug"; then
add_defines "DEBUG" "TEST"
fi
if is_plat "linux" "macosx"; then
add_defines "POSIX"
fi
if has_config "pthread"; then
set_configvar "HAS_PTHREAD" 1
fi
if has_config "cxx_constexpr"; then
set_configvar "HAS_CONSTEXPR" 1
fi
includes "foo" "bar"
if has_config "tests"; then
includes "tests"
fi$ ./configure$ make$ make install$ make run$ ./configure --generator=ninja$ ninja$ ninja install$ ninja runSet the project name.
set_project "hello"Set the project version, optional build date format and soname version.
set_version "1.0.1" "%Y%m%d%H%M" "1"Include sub-project files from subdirectories.
includes "foo" "bar"# check target platform: linux, macosx, windows, mingw, cross, ...
if is_plat "linux" "macosx"; then ... fi
# check target architecture: x86_64, arm64, ...
if is_arch "x86_64"; then ... fi
# check host OS: linux, macosx, msys, cygwin, ...
if is_host "linux"; then ... fi
# check build mode: debug, release, ...
if is_mode "debug"; then ... fi
# check if an option config value equals a specific value
if is_config "option_name" "value"; then ... fi
# check if an option is enabled
if has_config "pthread"; then ... fiSet a config option value.
set_config "option_name" "value"Define a build target. All target settings between target and target_end (or the next target) belong to this target.
target "demo"
set_kind "binary"
add_files "*.cpp"
target_endSet the target type: binary, static, or shared.
set_kind "binary"
set_kind "static"
set_kind "shared"Set whether this target is built by default.
set_default falseAdd target dependencies.
add_deps "foo" "bar"Add source files with glob patterns. Supports {replace = {search, replace}} to replace text content before compilation.
The original source files will not be modified, temporary files will be generated in the build/.replace/ directory.
add_files "*.cpp"
add_files "src/**/*.c"
add_files "*.cpp" "{replace = {HELLO, hello}}" "{replace = {VERSION, 1.0.0}}"Set the target output file naming.
set_basename "demo" # output base name
set_extension ".exe" # output file extension
set_filename "demo.exe" # output full filename
set_prefixname "lib" # output prefix (e.g. "lib" for libfoo.a)Set custom output directories for the target binary and object files.
set_targetdir "${builddir}/bin"
set_objectdir "${builddir}/objs"Add preprocessor definitions or undefinitions. Use {public} to propagate to dependent targets.
add_defines "DEBUG" "TEST"
add_defines "FOO=bar" "{public}"
add_undefines "NDEBUG"Add header search directories. Use {public} to propagate to dependent targets.
add_includedirs "src" "include"
add_includedirs "include" "{public}"Add library search directories. Use {public} to propagate to dependent targets.
add_linkdirs "/usr/local/lib"
add_linkdirs "lib" "{public}"Add runtime library search directories (RPATH).
add_rpathdirs "/usr/local/lib"Add link libraries or system libraries. Use {public} to propagate to dependent targets.
add_links "foo" "bar"
add_links "foo" "{public}"
add_syslinks "pthread" "dl" "m"Set the strip mode: all, debug.
set_strip "all"Set the symbol info mode: debug, hidden.
set_symbols "debug"
set_symbols "hidden"Set the C/C++ language standards.
set_languages "c99" "c++11"Set the warning levels: all, error, allextra, everything, none.
set_warnings "all" "error"Set the optimization levels: none, fast, faster, fastest, smallest, aggressive.
set_optimizes "fastest"Add Apple frameworks and framework search directories (macOS/iOS). Use {public} to propagate to dependent targets.
add_frameworks "CoreFoundation" "Security"
add_frameworkdirs "/Library/Frameworks"Add raw compiler and linker flags directly.
add_cflags "-fno-strict-aliasing" # C only
add_cxflags "-DFOO" # C and C++
add_cxxflags "-fno-rtti" # C++ only
add_mflags "-fmodules" # Objective-C only
add_mxflags "-fmodules" # Objective-C and Objective-C++
add_mxxflags "-fno-objc-arc" # Objective-C++ only
add_asflags "-DASM_FLAG" # Assembler
add_ldflags "-L/usr/local/lib" # Binary linker
add_shflags "-shared" # Shared library linker
add_arflags "-cr" # Static library archiverAdd config header template files with variable substitution. Template files use ${VAR} placeholders.
add_configfiles "config.h.in"
set_configdir "${builddir}/include"
set_configvar "HAS_PTHREAD" 1
set_configvar "VERSION" "1.0.0"Set install directory and add files for installation. Parentheses () define the relative path portion to install.
set_installdir "/usr/local"
add_installfiles "res/(png/**.png)" "share"
add_headerfiles "(include/*.h)" "mylib"
add_headerfiles "src/(*.h)" "mylib"Bind option dependencies to this target. Use {public} to propagate to dependent targets.
add_options "pthread" "lua"Set callback functions to run before or after installation.
before_install "my_before_install"
after_install "my_after_install"
my_before_install() {
echo "preparing install ..."
}Set version for the target with optional build date format and soname version.
set_version "1.0.1" "%Y%m%d%H%M" "1"Define a config option. Short form with inline description and default value, or block form for complex detection.
# short form
option "debug" "Enable debug mode." false
# block form
option "pthread"
add_links "pthread"
add_cincludes "pthread.h"
add_cfuncs "pthread_create"
option_endSet option properties.
option "myopt"
set_default true
set_description "Enable my feature."
set_showmenu true
option_endSet a callback function to run before option detection.
option "lua"
add_cfuncs "lua_pushstring"
add_cincludes "lua.h"
before_check "option_find_lua"
option_end
option_find_lua() {
option "lua"
add_cxflags `pkg-config --cflags lua5.4 2>/dev/null`
add_ldflags `pkg-config --libs lua5.4 2>/dev/null`
option_end
}Check for C/C++ functions, includes, types, and code snippets during option detection.
option "pthread"
add_cfuncs "pthread_create" # check C functions exist
add_cxxfuncs "std::thread::joinable" # check C++ functions exist
add_cincludes "pthread.h" # check C headers exist
add_cxxincludes "thread" # check C++ headers exist
add_ctypes "pthread_t" # check C types exist
add_cxxtypes "std::string" # check C++ types exist
add_csnippets "int x = 0;" # check C code snippet compiles
add_cxxsnippets "constexpr int k = 0;" # check C++ code snippet compiles
option_endAdd flags that are applied when the option is enabled.
option "myopt"
add_defines "USE_MYOPT"
add_undefines "OLD_OPT"
add_includedirs "/usr/local/include"
add_linkdirs "/usr/local/lib"
add_links "mylib"
add_syslinks "dl"
add_cflags "-DFOO"
add_cxflags "-DBAR"
add_cxxflags "-std=c++17"
add_ldflags "-L/opt/lib"
option_endDefine a custom toolchain.
toolchain "myclang"
set_toolset "cc" "clang"
set_toolset "cxx" "clang++"
set_toolset "ld" "clang++"
set_toolset "sh" "clang++"
set_toolset "ar" "ar"
toolchain_endSet the compiler/linker program for a specific tool kind: cc, cxx, mm, mxx, as, ld, sh, ar.
set_toolset "cc" "gcc"
set_toolset "cxx" "g++"
set_toolset "ld" "g++"
set_toolset "ar" "ar"- Email:waruqi@gmail.com
- Homepage:xmake.io
- Community
- Chat on reddit
- Chat on telegram
- Chat on gitter
- Chat on discord
- Chat on QQ Group: 343118190, 662147501
- Source Code:Github, Gitee
- Wechat Public: tboox-os