Alien-Xmake
view release on metacpan or search on metacpan
lib/Alien/Xmake.pod view on Meta::CPAN
=pod
=encoding utf-8
=head1 NAME
Alien::Xmake - Locate, Download, or Build and Install Xmake
=head1 SYNOPSIS
use Alien::Xmake;
my $xmake = Alien::Xmake->new;
print $xmake->version; # v3.0.6
print $xmake->buildid; # HEAD.9fdcf69f6
$xmake->show('platforms'); # @platforms = ('windows', 'linux', ...)
$xmake->show('targets', format => 'json'); # decoded JSON structure
$xmake->create('hello', template => 'console'); # scaffold a project
$xmake->configure(mode => 'debug'); # configure a build
$xmake->build; # build it
$xmake->run; # run the target
system $xmake->exe, '--help';
system $xmake->xrepo, qw[info libpng];
=head1 DESCRIPTION
Xmake is a lightweight, cross-platform build utility based on Lua. It uses a Lua script to maintain project builds, but
is driven by a dependency-free core program written in C. Compared with Makefiles or CMake, the configuration syntax is
(in the opinion of the author) much more concise and intuitive. As such, it's friendly to novices while still
maintaining the flexibly required in a build system. With Xmake, you can focus on your project instead of the build.
Xmake can be used to directly build source code (like with Make or Ninja), or it can generate project source files like
CMake or Meson. It also has a built-in package management system to help users integrate C/C++ dependencies.
If you want to know more, please refer to the L<Documentation|https://xmake.io/guide/quick-start.html>,
L<GitHub|https://github.com/xmake-io/xmake>, or L<Gitee|https://gitee.com/tboox/xmake>. You are also welcome to join
the L<community|https://xmake.io/about/contact.html>.
=for html <p align="center"><img width="916" height="236" src="https://xmake.io/assets/img/index/xmake-basic-render.gif"></p>
=head1 METHODS
A thin wrapper around the C<xmake> command line. Methods named after C<xmake> actions (C<build>, C<clean>, C<create>,
...) stream the command's output to your terminal and return true on success. Query-style methods (C<show>, C<lua -l>,
...) capture the output and return it to you instead. Most methods accept C<%options>; unrecognized extra arguments can
be passed with C<targets =E<gt> [...]> and C<args =E<gt> [...]> and are appended to the command line verbatim.
=head2 C<new( ... )>
my $xmake = Alien::Xmake->new( verbose => 1 );
Creates a new instance.
=over
=item B<verbose>
Constructor option. Boolean. If true, prints the command being run to C<STDOUT> before executing it.
=item B<yes>
Constructor option. Boolean. Auto-confirms any interactive C<xmake>/C<xrepo> prompt by passing C<-y> (equivalent to
C<confirm =E<gt> 'yes'>). Useful when a build is driven programmatically or its output is captured (e.g.
L<Capture::Tiny>) so an install that would normally prompt never hangs waiting on C<STDIN>. C<confirm =E<gt> ...> takes
precedence when both are set.
=item B<confirm>
Constructor option. Supplies an explicit answer (C<yes>, C<no>, or C<def>) to any prompt xmake asks, passed through as
C<--confirm=...>. Takes precedence over C<yes =E<gt> 1>. May be terse: C<Alien::Xmake-E<gt>new( confirm =E<gt> 'yes'
)>.
=item B<file>
Constructor option. A path to the C<xmake.lua> (or any Lua build file) that operations should read. Passed to every
action as C<-F/--file>, so you can drive a project whose build description lives at a non-default path or is generated
at runtime from Perl. Works for C<configure>, C<build>, C<run>, C<install>, C<project>, C<clean>, C<test> and the query
lib/Alien/Xmake.pod view on Meta::CPAN
Only build targets in this group (C<-g>).
=item B<dry_run>
Only print the command line about to be executed (C<--dry-run>).
=item B<jobs>, B<linkjobs>
Number of parallel compile/link jobs.
=item B<linkonly>
Only link (skip compilation) (C<--linkonly>).
=item B<files>
A comma-joined string or arrayref of files to build manually (C<--files=>).
=back
=head2 C<clean( [$target], %options )>
$xmake->clean;
$xmake->clean( all => 1 );
Removes the build artifacts. C<all> cleans dependent targets, C<group> restricts to a target group.
=head2 C<create( $name, %options )>
$xmake->create('hello', template => 'console');
$xmake->create('libz', template => 'shared', language => 'c');
Generates a new project directory named C<$name> (clone of C<xmake create>).
=over
=item B<language>
Project language (C<c>, C<c++>, C<go>, C<rust>, ...).
=item B<template>
Project template (C<console>, C<static>, C<shared>, C<qt.widgetapp>, ...).
=item B<force>
Overwrite an existing directory (C<-f>).
=item B<list>
List all available templates instead of creating (C<--list>).
=item B<project>
Alternate project path (C<-P>).
=back
=head2 C<configure( %options )>
$xmake->configure(mode => 'debug', plat => 'windows', arch => 'x64');
Configures the current project (clone of C<xmake config>). Accepts the shared platform/toolchain options (C<plat>,
C<arch>, C<mode>, C<kind>, C<toolchain>, C<toolchain_host>, C<cross>, C<target_os>, C<sdk>, C<runtimes>, C<vs>,
C<vs_toolset>, C<vs_sdkver>, C<vs_runtime>, C<ndk>, C<ndk_stdcxx>, C<ndk_sdkver>, C<android_sdk>, C<mingw>, C<emsdk>,
C<cuda>, C<qt>, C<qt_host>, C<vcpkg>, C<wdk>, C<build_toolver>, C<ccache>, C<ccachedir>, C<debugger>, C<trybuild>,
C<tryconfigs>, C<require>, C<includedirs>, C<linkdirs>, C<links>, C<syslinks>, C<rc*>, C<fc*>, ...) as well as the
compiler/flag subtables C<cflags>, C<cxflags>, C<cxxflags>, C<mflags>, C<mmflags>, C<mxflags>, C<ldflags>, C<arflags>,
C<asflags>, C<shflags>, and C<cuflags>. Any C<--key=value> pair can be passed via C<set =E<gt> { key =E<gt> value }>.
=over
=item B<clean>
Reset the configuration to defaults (C<-c>).
=item B<check>
Only check the configuration and exit (C<--check>).
=item B<menu>
Open the interactive configuration menu (C<--menu>).
=item B<export>, B<import>
Export/import configuration files.
=item B<builddir>
Alternate build directory (C<-o>).
=back
=head2 C<global( %options )>
$xmake->global(network => 'y', theme => 'default');
$xmake->global(vs => '2022');
Reads/writes global (user-level) configuration. Shares the platform/toolchain options from C<configure( ... )> that
exist on C<global> (C<android_sdk>, C<build_toolver>, C<cuda>, C<emsdk>, C<mingw>, C<ndk>, C<ndk_sdkver>, C<qt>,
C<qt_host>, C<vcpkg>, C<vs>, C<wdk>).
=over
=item B<clean>
Reset global configuration to defaults (C<-c>).
=item B<check>, B<menu>
Same semantics as C<configure( ... )>.
=item B<theme>, B<debugger>, B<ccache>
Global UI/terminal settings.
=item B<cachedir>
Global cache directory.
=item B<policies>
Global policy overrides (C<--policies=>).
=item B<network>, B<proxy>, B<proxy_hosts>, B<proxy_pac>
Network settings, e.g. C<network =E<gt> 'n'> to go offline, C<insecure_ssl =E<gt> 1> to skip HTTPS verification.
=item B<pkg_searchdirs>, B<pkg_cachedir>, B<pkg_installdir>
Global package search/cache/install directories.
=back
=head2 C<install( [$target], %options )>
$xmake->install;
$xmake->install('hello', installdir => './dist');
Installs the built binaries to a staging directory (C<DESTDIR>).
=over
=item B<installdir>
The install directory (C<-o>); C<bindir>, C<libdir>, C<includedir> adjust the per-kind subdirectories.
=item B<all>
Install all targets (C<-a>).
=item B<group>
Install all targets in the group (C<-g>).
=item B<binaries>, B<headers>, B<libraries>, B<packages>
Set to C<y> or C<n> to enable or disable installing that kind of file.
=back
=head2 C<uninstall( [$target], %options )>
$xmake->uninstall;
$xmake->uninstall('hello');
Uninstalls installed binaries from the install directory. Supports C<installdir>, C<bindir>, C<libdir>, C<includedir>,
C<group> and C<admin>.
=head2 C<package( [$target], %options )>
$xmake->package; # package release artifacts
$xmake->package('hello', outputdir => './dist');
Packages the built targets into distribution archives/installers. C<outputdir> selects the destination, C<format> the
package format (C<-f>, e.g. C<deb>, C<rpm>, C<nsis>), C<all> includes dependent targets, and C<homepage>,
C<description>, C<url>, C<version> and C<shasum> fill the package metadata.
=head2 C<pack( [$pkg], %options )>
$xmake->pack('libpng', formats => ['zip', 'targz'], jobs => 4);
Bundles an installed package from the local package cache into distribution archives. C<outputdir> selects the
destination, C<formats> the archive format(s) (C<zip>, C<targz>, ...), C<basename>, C<autobuild> and C<jobs> tune the
archive name, rebuild behavior and parallelism.
=head2 C<require( [$pkg], %options )>
$xmake->require('libpng');
my $list = $xmake->require( list => 1 );
my $json = $xmake->require( 'libpng', depgraph => 1, format => 'json' );
Installs and manages the packages declared (or requested) for the current project.
=over
=item B<list>
List the required packages (C<-l>, captured output).
=item B<scan>
Scan for missing/unused package configs (C<--scan>, captured).
=item B<info>
Show package information (C<--info>, captured).
=item B<depgraph>
Show the package dependency graph (C<--depgraph>, captured; combine with C<format =E<gt> 'json'> or C<'dot'>).
=item B<force>, B<shallow>, B<jobs>, B<linkjobs>, B<clean>
Standard package-install controls. B<clean_modes> resets the configs of the matched packages, B<clean> only clears
unused packages, B<build> always build from source, and B<addon> manages addon packages.
=back
=head2 C<run( [$target], %options )>
$xmake->run;
$xmake->run('hello', args => [qw[--flag value]]);
Runs the build target (or the C<run> script). C<debug> attaches a debugger, C<all>/C<group> select targets, C<workdir>
sets the working directory (C<-w>), C<jobs> sets the parallelism and C<detach> runs the target in the background. Extra
program arguments go in C<args>.
=head2 C<test( [$target], %options )>
$xmake->test;
$xmake->test( 'hello', rebuild => 1 );
Runs the project's tests. C<group>, C<workdir>, C<jobs> and C<rebuild> behave as elsewhere.
=head2 C<update( [$version], %options )>
$xmake->update; # update xmake itself
$xmake->update('v3.0.6', scriptonly => 1);
Updates the C<xmake> installation. C<version> optionally pins a version. C<scriptonly> only updates the scripts
(C<-s>), C<integrate> re-integrates the shell environment, C<force> downloads even when up to date and C<uninstall>
flags the previous version for removal.
=head2 C<service( %options )>
$xmake->service( start => 1, distcc => 1 ); # run the distcc service
$xmake->service( status => 1 );
Controls the built-in xmake services. One of C<start>, C<stop>, C<restart>, C<status>, C<connect>, C<disconnect>,
C<reconnect>, C<sync>, C<clean>; C<remote>, C<distcc>, C<ccache>, C<add-user>, C<rm-user>, C<gen-token> select or
modify the service, C<host> and C<session> target a specific server/session, and C<logs>/C<pull> fetch server logs.
=head2 C<addon( [$name], %options )>
$xmake->addon('gcc_flags', install => 1);
my $found = $xmake->addon('flags', search => 1); # captured output
Installs, removes, lists or searches xmake addon packages (C<install>, C<remove>, C<list>, C<search>, C<upgrade>).
C<all> applies an operation to every addon and C<force> bypasses checks.
=head2 C<check( [$checker], %options )>
my $list = $xmake->check( list => 1 );
$xmake->check('gcc_flags.logic');
Lists available checkers (captured) or runs a named check script. C<info> explains a checker (captured).
=head2 C<doxygen( [$srcdir], %options )>
$xmake->doxygen('./src', outputdir => './docs');
Generates Doxygen documentation for C<$srcdir>. C<outputdir> selects the documentation directory.
=head2 C<format( [$target], %options )>
$xmake->format('hello', style => 'google');
$xmake->format( dry_run => 1, files => ['a.c', 'b.c'] );
Formats the project sources with clang-format. C<style> chooses the style (C<google>, C<llvm>, ...), C<create> writes a
default C<.clang-format>, C<dry_run> only reports files that would change (C<-n>), C<error> treats style mismatches as
errors (C<-e>), C<files> limits formatting to the given files, and C<all>/C<group>/C<jobs> behave as elsewhere.
=head2 C<lua( [$script], %options )>
lib/Alien/Xmake.pod view on Meta::CPAN
Returns the directory containing the Xmake executable; push it onto your C<PATH>. For a 'system' install this step will
not be required.
=head2 C<cflags( )>
A stub returning an empty value, provided for compatibility with consumers that expect the standard Alien API surface.
Use C<pkg_config( )> or L<Alien::Xrepo> for real flags.
=head2 C<libs( )>
A stub returning an empty value, provided for compatibility with consumers that expect the standard Alien API surface.
Use C<pkg_config( )> or L<Alien::Xrepo> for real flags.
=head2 C<dynamic_libs( )>
A stub returning an empty list, provided for compatibility with consumers that expect the standard Alien API surface.
Use C<pkg_config( )> or L<Alien::Xrepo> for real flags.
=head2 C<alien_helper( )>
use alienfile;
# ...
[ '%{xmake}', 'install' ],
Returns a hashref of helpers: C<%{xmake}> and C<%{xrepo}>, suitable for use in C<alienfile> recipes.
=head1 Alien::Base Helper
To use Xmake in your C<alienfile>s, require this module and use C<%{xmake}> and C<%{xrepo}>.
use alienfile;
# ...
[ '%{xmake}', 'install' ],
[ '%{xrepo}', 'install ...' ]
# ...
=head1 Xmake Cookbook
Xmake is severely underrated so I'll add more nifty things here but for now just a quick example.
You're free to create your own projects, of course, but Xmake comes with the ability to generate an entire project for
you:
$ xmake create -P hi # generates a basic console project in C++ and xmake.lua build script
$ cd hi
$ xmake -y # builds the project if required, installing defined prerequisite libs, etc.
$ xmake run # runs the target binary which prints 'hello, world!'
C<xmake create> is a lot like C<minil new> in that it generates a new project for you that's ready to build even before
you change anything. It even tosses a C<.gitignore> file in. You can generate projects in C++, Go, Objective C, Rust,
Swift, D, Zig, Vale, Pascal, Nim, Fortran, and more. You can also generate boilerplate projects for simple console
apps, static and shared libraries, macOS bundles, GUI apps based on Qt or wxWidgets, IOS apps, and more.
See C<xmake create --help> for a full list.
The following examples drive xmake from Perl. These all call the wrapper methods (see L</METHODS>), so output is
streamed to your terminal and each returns truthy on success.
=head2 Scaffold, configure, build, test and run a target
The whole edit-run-debug lifecycle. C<create> scaffolds a fresh project (C<template> picks the base), C<configure> sets
the build mode, then C<build>/C<test>/C<run> compile, run the tests (if any) and launch the binary:
use v5.40;
use Alien::Xmake;
my $x = Alien::Xmake->new;
$x->create('demo', template => 'console'); # writes xmake.lua + src/main.cpp
chdir 'demo';
$x->configure(mode => 'release'); # configure the build
$x->build; # compile it
$x->test; # run target tests (none for a console template)
$x->run; # execute the binary
=head2 Generate files for another build system or IDE
C<project> reads your C<xmake.lua> and emits other build systems' project files, so you never maintain a second build
config by hand. C<kind> selects the generator and C<lsp> asks for IDE-friendly compile data:
use v5.40;
use Alien::Xmake;
my $x = Alien::Xmake->new;
$x->project(kind => 'make'); # a Makefile
$x->project(kind => 'compile_commands', lsp => 1); # compile_commands.json for clangd/IDEs
=head2 Ask xmake what it can target
C<show> introspects the C<xmake> install and the current project. Hand it a list name and (optionally) C<format =E<gt>
'json'> to get the parsed result; the L</METHODS> list names are C<platforms>, C<architectures>, C<toolchains>,
C<buildmodes>, C<targets>, C<packages>, C<rules>, C<themes>, C<envs>, C<apis> and C<policies>. C<config> reflects the
metadata of this L<Alien::Xmake> install itself (set up at construction; see C<new> under L</METHODS>):
use v5.40;
use Alien::Xmake;
my $x = Alien::Xmake->new;
say for $x->show('platforms'); # windows, linux, macosx, android, ...
say for $x->show('architectures');
my @targets = $x->show('targets', format => 'json'); # inside a project dir
say $targets[0]->{name};
say 'installed: ' . $x->config('install_type'); # share | system
say 'version : ' . $x->config('version');
The list queries also work from a bare C<show> (no project needed) for the global catalogs. Several return rich JSON:
the C<apis> value is a big HASH of the built-in module APIs, while C<buildmodes>, C<platforms>, C<rules>, C<themes> and
C<targets> come back as ARRAY refs. C<architectures>, C<envs>, C<policies> and C<toolchains> still print aligned table
text (xmake does not JSON-serialize them), so those come back as a plain listing:
use v5.40;
use Alien::Xmake;
my $x = Alien::Xmake->new;
my $apis = $x->show( 'apis', format => 'json' ); # HASH of built-in module APIs
say for @{ $apis->{description_builtin_module_apis} }; # hash.md5, hash.uuid, ...
( run in 0.795 second using v1.01-cache-2.11-cpan-c32d3c3b8f1 )