Descriptive syntax
target("demo") set_kind("binary") add_files("src/*.c")
target("demo") set_kind("shared") set_objectdir("$(buildir)/.objs") set_targetdir("libs/armeabi") add_files("jni/*.c") on_package(function (target) os.run("ant debug") end) on_install(function (target) os.run("adb install -r ./bin/Demo-debug.apk") end) on_run(function (target) os.run("adb shell am start -n com.demo/com.demo.DemoTest") os.run("adb logcat") end)
Build an executable program
target("demo") set_kind("binary") add_files("src/*.c")
Build a library program that can be configured and switched
target("demo") set_kind("$(kind)") add_files("src/*.c")
$ xmake f --kind=static; xmake $ xmake f --kind=shared; xmake
Added debug and release compilation mode support
if is_mode("debug") then set_symbols("debug") set_optimize("none") end if is_mode("release") then set_symbols("hidden") set_optimize("fastest") set_strip("all") end target("demo") set_kind("binary") add_files("src/*.c")
$ xmake f -m debug; xmake $ xmake f -m release; xmake
Sign ios programs through custom scripts
target("demo") set_kind("binary") add_files("src/*.m") after_build(function (target)) os.run("ldid -S %s", target:targetfile()) end
Built in variable and external variable
add_cxflags("-I$(buildir)")
target("test") add_files("$(projectdir)/src/*.c") add_includedirs("$(buildir)/inc")
target("test") on_run(function (target) os.cp("$(scriptdir)/xxx.h", "$(buildir)/inc") end)
target("test") add_defines("-DTEST=$(var)")
Modify target file name
target("demo") set_kind("binary") set_basename("demo_$(arch)") set_targetdir("$(buildir)/$(plat)")
$ xmake f -p iphoneos -a arm64; xmake
Add Subdirectory Project Module
target("demo") set_kind("binary") add_files("src/demo.c") target("test") set_kind("binary") add_files("src/test.c")
target("demo") set_kind("binary") add_files("src/demo.c") add_subdirs("src/test")
Installation header file
target("tbox") set_kind("static") add_files("src/*.c") add_headers("../(tbox/**.h)|**/impl/**.h") set_headerdir("$(buildir)/inc")
Multi objective dependency building
target("test1") set_kind("static") set_files("*.c") target("test2") set_kind("static") set_files("*.c") target("demo") add_deps("test1", "test2") add_links("test1", "test2")
Merge static libraries
target("demo") set_kind("static") add_files("src/*.c", "libxxx.a", "lib*.a", "xxx.lib")
target("demo") set_kind("binary") add_files("src/*.c", "objs/*.o")
Add custom configuration options
option("test") set_default(false) set_showmenu(true) add_defines("-DTEST")
target("demo") add_options("test")
$ xmake f --test=y $ xmake
Add third-party dependency package
target("test") set_kind("binary") add_packages("zlib", "polarssl", "pcre", "mysql")
Generate configuration header file
target("test") set_config_h("$(buildir)/config.h") set_config_h_prefix("TB_CONFIG")
option("wchar") --Add detection of wchar_t type add_ctypes("wchar_t") --If the detection passes, automatically generate the macro switch of TB_CONFIG_TYPE_HAVE_WCHAR to config. h add_defines_h_if_ok("$(prefix)_TYPE_HAVE_WCHAR") target("test") --Enable automatic generation of header files set_config_h("$(buildir)/config.h") set_config_h_prefix("TB_CONFIG") --Add dependency association on wchar option. Only when this association is added, the detection result of wchar option will be written to the specified config. h add_options("wchar")
Test library header files and interfaces
target("demo") --Setting and enabling config. h set_config_h("$(buildir)/config.h") set_config_h_prefix("TEST") --Set module name prefix only through parameter 1 add_cfunc("libc", nil, nil, {"sys/select.h"}, "select") --Set the link library to be detected simultaneously through parameter 3: libpthread. a add_cfunc("pthread", nil, "pthread", "pthread.h", "pthread_create") --Set interface alias through parameter 2 add_cfunc(nil, "PTHREAD", nil, "pthread.h", "pthread_create")
#ifndef TEST_H #define TEST_H //Macro naming rule: $(prefix) prefix_module name (if not nil) _HAVE_interface name or alias (uppercase) #define TEST_LIBC_HAVE_SELECT 1 #define TEST_PTHREAD_HAVE_PTHREAD_CREATE 1 #define TEST_HAVE_PTHREAD 1 #endif
Custom plug-in task
task("hello") --Set Run Script on_run(function () print("hello xmake!") end)
target("test") after_build(function (target) --Import task module import("core.project.task") --Run the hello task task.run("hello") end)
task("hello") on_run(function (arg1, arg2, arg3) print("hello xmake!", arg1, arg2, arg3) end) target("test") after_build(function (target) import("core.project.task") task.run("hello", {}, "arg1", "arg2", "arg3") end)
Another grammatical style
target { name = "test", defines = "DEBUG", files = {"src/*.c", "test/*.cpp"} }
target("test") set_kind("static") add_defines("DEBUG") add_files("src/*.c", "test/*.cpp")
*For simple projects, the key val method can be used without too complex conditional compilation, which is more concise and readable *For complex projects that require greater controllability and flexibility, it is recommended to use the set add method *Try not to mix the two styles. Although it is supported, it will make the whole project description very messy, so try to unify the style as your own description specification
--Set add style option("demo") set_default(true) set_showmenu(true) set_category("option") set_description("Enable or disable the demo module", " =y|n")
--Key val style option { name = "demo", default = true, showmenu = true, category = "option", desciption = {"Enable or disable the demo module", " =y|n"} }
--Set add style task("hello") on_run(function () print("hello xmake!") end) set_menu { usage = "xmake hello [options]", description = "Hello xmake! ", options = {} }
--Key val style task { name = "hello", run = (function () print("hello xmake!") end), menu = { usage = "xmake hello [options]", description = "Hello xmake! ", options = {} } }
epilogue