xref: /aosp_15_r20/external/fmtlib/doc/get-started.md (revision 5c90c05cd622c0a81b57953a4d343e0e489f2e08)
1*5c90c05cSAndroid Build Coastguard Worker# Get Started
2*5c90c05cSAndroid Build Coastguard Worker
3*5c90c05cSAndroid Build Coastguard WorkerCompile and run {fmt} examples online with [Compiler Explorer](
4*5c90c05cSAndroid Build Coastguard Workerhttps://godbolt.org/z/P7h6cd6o3).
5*5c90c05cSAndroid Build Coastguard Worker
6*5c90c05cSAndroid Build Coastguard Worker{fmt} is compatible with any build system. The next section describes its usage
7*5c90c05cSAndroid Build Coastguard Workerwith CMake, while the [Build Systems](#build-systems) section covers the rest.
8*5c90c05cSAndroid Build Coastguard Worker
9*5c90c05cSAndroid Build Coastguard Worker## CMake
10*5c90c05cSAndroid Build Coastguard Worker
11*5c90c05cSAndroid Build Coastguard Worker{fmt} provides two CMake targets: `fmt::fmt` for the compiled library and
12*5c90c05cSAndroid Build Coastguard Worker`fmt::fmt-header-only` for the header-only library. It is recommended to use
13*5c90c05cSAndroid Build Coastguard Workerthe compiled library for improved build times.
14*5c90c05cSAndroid Build Coastguard Worker
15*5c90c05cSAndroid Build Coastguard WorkerThere are three primary ways to use {fmt} with CMake:
16*5c90c05cSAndroid Build Coastguard Worker
17*5c90c05cSAndroid Build Coastguard Worker* **FetchContent**: Starting from CMake 3.11, you can use [`FetchContent`](
18*5c90c05cSAndroid Build Coastguard Worker  https://cmake.org/cmake/help/v3.30/module/FetchContent.html) to automatically
19*5c90c05cSAndroid Build Coastguard Worker  download {fmt} as a dependency at configure time:
20*5c90c05cSAndroid Build Coastguard Worker
21*5c90c05cSAndroid Build Coastguard Worker        include(FetchContent)
22*5c90c05cSAndroid Build Coastguard Worker
23*5c90c05cSAndroid Build Coastguard Worker        FetchContent_Declare(
24*5c90c05cSAndroid Build Coastguard Worker          fmt
25*5c90c05cSAndroid Build Coastguard Worker          GIT_REPOSITORY https://github.com/fmtlib/fmt
26*5c90c05cSAndroid Build Coastguard Worker          GIT_TAG        e69e5f977d458f2650bb346dadf2ad30c5320281) # 10.2.1
27*5c90c05cSAndroid Build Coastguard Worker        FetchContent_MakeAvailable(fmt)
28*5c90c05cSAndroid Build Coastguard Worker
29*5c90c05cSAndroid Build Coastguard Worker        target_link_libraries(<your-target> fmt::fmt)
30*5c90c05cSAndroid Build Coastguard Worker
31*5c90c05cSAndroid Build Coastguard Worker* **Installed**: You can find and use an [installed](#installation) version of
32*5c90c05cSAndroid Build Coastguard Worker  {fmt} in your `CMakeLists.txt` file as follows:
33*5c90c05cSAndroid Build Coastguard Worker
34*5c90c05cSAndroid Build Coastguard Worker        find_package(fmt)
35*5c90c05cSAndroid Build Coastguard Worker        target_link_libraries(<your-target> fmt::fmt)
36*5c90c05cSAndroid Build Coastguard Worker
37*5c90c05cSAndroid Build Coastguard Worker* **Embedded**: You can add the {fmt} source tree to your project and include it
38*5c90c05cSAndroid Build Coastguard Worker  in your `CMakeLists.txt` file:
39*5c90c05cSAndroid Build Coastguard Worker
40*5c90c05cSAndroid Build Coastguard Worker        add_subdirectory(fmt)
41*5c90c05cSAndroid Build Coastguard Worker        target_link_libraries(<your-target> fmt::fmt)
42*5c90c05cSAndroid Build Coastguard Worker
43*5c90c05cSAndroid Build Coastguard Worker## Installation
44*5c90c05cSAndroid Build Coastguard Worker
45*5c90c05cSAndroid Build Coastguard Worker### Debian/Ubuntu
46*5c90c05cSAndroid Build Coastguard Worker
47*5c90c05cSAndroid Build Coastguard WorkerTo install {fmt} on Debian, Ubuntu, or any other Debian-based Linux
48*5c90c05cSAndroid Build Coastguard Workerdistribution, use the following command:
49*5c90c05cSAndroid Build Coastguard Worker
50*5c90c05cSAndroid Build Coastguard Worker    apt install libfmt-dev
51*5c90c05cSAndroid Build Coastguard Worker
52*5c90c05cSAndroid Build Coastguard Worker### Homebrew
53*5c90c05cSAndroid Build Coastguard Worker
54*5c90c05cSAndroid Build Coastguard WorkerInstall {fmt} on macOS using [Homebrew](https://brew.sh/):
55*5c90c05cSAndroid Build Coastguard Worker
56*5c90c05cSAndroid Build Coastguard Worker    brew install fmt
57*5c90c05cSAndroid Build Coastguard Worker
58*5c90c05cSAndroid Build Coastguard Worker### Conda
59*5c90c05cSAndroid Build Coastguard Worker
60*5c90c05cSAndroid Build Coastguard WorkerInstall {fmt} on Linux, macOS, and Windows with [Conda](
61*5c90c05cSAndroid Build Coastguard Workerhttps://docs.conda.io/en/latest/), using its [conda-forge package](
62*5c90c05cSAndroid Build Coastguard Workerhttps://github.com/conda-forge/fmt-feedstock):
63*5c90c05cSAndroid Build Coastguard Worker
64*5c90c05cSAndroid Build Coastguard Worker    conda install -c conda-forge fmt
65*5c90c05cSAndroid Build Coastguard Worker
66*5c90c05cSAndroid Build Coastguard Worker### vcpkg
67*5c90c05cSAndroid Build Coastguard Worker
68*5c90c05cSAndroid Build Coastguard WorkerDownload and install {fmt} using the vcpkg package manager:
69*5c90c05cSAndroid Build Coastguard Worker
70*5c90c05cSAndroid Build Coastguard Worker    git clone https://github.com/Microsoft/vcpkg.git
71*5c90c05cSAndroid Build Coastguard Worker    cd vcpkg
72*5c90c05cSAndroid Build Coastguard Worker    ./bootstrap-vcpkg.sh
73*5c90c05cSAndroid Build Coastguard Worker    ./vcpkg integrate install
74*5c90c05cSAndroid Build Coastguard Worker    ./vcpkg install fmt
75*5c90c05cSAndroid Build Coastguard Worker
76*5c90c05cSAndroid Build Coastguard Worker<!-- The fmt package in vcpkg is kept up to date by Microsoft team members and
77*5c90c05cSAndroid Build Coastguard Workercommunity contributors. If the version is out of date, please [create an
78*5c90c05cSAndroid Build Coastguard Workerissue or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg
79*5c90c05cSAndroid Build Coastguard Workerrepository. -->
80*5c90c05cSAndroid Build Coastguard Worker
81*5c90c05cSAndroid Build Coastguard Worker## Building from Source
82*5c90c05cSAndroid Build Coastguard Worker
83*5c90c05cSAndroid Build Coastguard WorkerCMake works by generating native makefiles or project files that can be
84*5c90c05cSAndroid Build Coastguard Workerused in the compiler environment of your choice. The typical workflow
85*5c90c05cSAndroid Build Coastguard Workerstarts with:
86*5c90c05cSAndroid Build Coastguard Worker
87*5c90c05cSAndroid Build Coastguard Worker    mkdir build  # Create a directory to hold the build output.
88*5c90c05cSAndroid Build Coastguard Worker    cd build
89*5c90c05cSAndroid Build Coastguard Worker    cmake ..     # Generate native build scripts.
90*5c90c05cSAndroid Build Coastguard Worker
91*5c90c05cSAndroid Build Coastguard Workerrun in the `fmt` repository.
92*5c90c05cSAndroid Build Coastguard Worker
93*5c90c05cSAndroid Build Coastguard WorkerIf you are on a Unix-like system, you should now see a Makefile in the
94*5c90c05cSAndroid Build Coastguard Workercurrent directory. Now you can build the library by running `make`.
95*5c90c05cSAndroid Build Coastguard Worker
96*5c90c05cSAndroid Build Coastguard WorkerOnce the library has been built you can invoke `make test` to run the tests.
97*5c90c05cSAndroid Build Coastguard Worker
98*5c90c05cSAndroid Build Coastguard WorkerYou can control generation of the make `test` target with the `FMT_TEST`
99*5c90c05cSAndroid Build Coastguard WorkerCMake option. This can be useful if you include fmt as a subdirectory in
100*5c90c05cSAndroid Build Coastguard Workeryour project but don't want to add fmt's tests to your `test` target.
101*5c90c05cSAndroid Build Coastguard Worker
102*5c90c05cSAndroid Build Coastguard WorkerTo build a shared library set the `BUILD_SHARED_LIBS` CMake variable to `TRUE`:
103*5c90c05cSAndroid Build Coastguard Worker
104*5c90c05cSAndroid Build Coastguard Worker    cmake -DBUILD_SHARED_LIBS=TRUE ..
105*5c90c05cSAndroid Build Coastguard Worker
106*5c90c05cSAndroid Build Coastguard WorkerTo build a static library with position-independent code (e.g. for
107*5c90c05cSAndroid Build Coastguard Workerlinking it into another shared library such as a Python extension), set the
108*5c90c05cSAndroid Build Coastguard Worker`CMAKE_POSITION_INDEPENDENT_CODE` CMake variable to `TRUE`:
109*5c90c05cSAndroid Build Coastguard Worker
110*5c90c05cSAndroid Build Coastguard Worker    cmake -DCMAKE_POSITION_INDEPENDENT_CODE=TRUE ..
111*5c90c05cSAndroid Build Coastguard Worker
112*5c90c05cSAndroid Build Coastguard WorkerAfter building the library you can install it on a Unix-like system by
113*5c90c05cSAndroid Build Coastguard Workerrunning `sudo make install`.
114*5c90c05cSAndroid Build Coastguard Worker
115*5c90c05cSAndroid Build Coastguard Worker### Building the Docs
116*5c90c05cSAndroid Build Coastguard Worker
117*5c90c05cSAndroid Build Coastguard WorkerTo build the documentation you need the following software installed on
118*5c90c05cSAndroid Build Coastguard Workeryour system:
119*5c90c05cSAndroid Build Coastguard Worker
120*5c90c05cSAndroid Build Coastguard Worker- [Python](https://www.python.org/)
121*5c90c05cSAndroid Build Coastguard Worker- [Doxygen](http://www.stack.nl/~dimitri/doxygen/)
122*5c90c05cSAndroid Build Coastguard Worker- [MkDocs](https://www.mkdocs.org/) with `mkdocs-material`, `mkdocstrings`,
123*5c90c05cSAndroid Build Coastguard Worker  `pymdown-extensions` and `mike`
124*5c90c05cSAndroid Build Coastguard Worker
125*5c90c05cSAndroid Build Coastguard WorkerFirst generate makefiles or project files using CMake as described in
126*5c90c05cSAndroid Build Coastguard Workerthe previous section. Then compile the `doc` target/project, for example:
127*5c90c05cSAndroid Build Coastguard Worker
128*5c90c05cSAndroid Build Coastguard Worker    make doc
129*5c90c05cSAndroid Build Coastguard Worker
130*5c90c05cSAndroid Build Coastguard WorkerThis will generate the HTML documentation in `doc/html`.
131*5c90c05cSAndroid Build Coastguard Worker
132*5c90c05cSAndroid Build Coastguard Worker## Build Systems
133*5c90c05cSAndroid Build Coastguard Worker
134*5c90c05cSAndroid Build Coastguard Worker### build2
135*5c90c05cSAndroid Build Coastguard Worker
136*5c90c05cSAndroid Build Coastguard WorkerYou can use [build2](https://build2.org), a dependency manager and a build
137*5c90c05cSAndroid Build Coastguard Workersystem, to use {fmt}.
138*5c90c05cSAndroid Build Coastguard Worker
139*5c90c05cSAndroid Build Coastguard WorkerCurrently this package is available in these package repositories:
140*5c90c05cSAndroid Build Coastguard Worker
141*5c90c05cSAndroid Build Coastguard Worker- <https://cppget.org/fmt/> for released and published versions.
142*5c90c05cSAndroid Build Coastguard Worker- <https://github.com/build2-packaging/fmt> for unreleased or custom versions.
143*5c90c05cSAndroid Build Coastguard Worker
144*5c90c05cSAndroid Build Coastguard Worker**Usage:**
145*5c90c05cSAndroid Build Coastguard Worker
146*5c90c05cSAndroid Build Coastguard Worker- `build2` package name: `fmt`
147*5c90c05cSAndroid Build Coastguard Worker- Library target name: `lib{fmt}`
148*5c90c05cSAndroid Build Coastguard Worker
149*5c90c05cSAndroid Build Coastguard WorkerTo make your `build2` project depend on `fmt`:
150*5c90c05cSAndroid Build Coastguard Worker
151*5c90c05cSAndroid Build Coastguard Worker- Add one of the repositories to your configurations, or in your
152*5c90c05cSAndroid Build Coastguard Worker  `repositories.manifest`, if not already there:
153*5c90c05cSAndroid Build Coastguard Worker
154*5c90c05cSAndroid Build Coastguard Worker        :
155*5c90c05cSAndroid Build Coastguard Worker        role: prerequisite
156*5c90c05cSAndroid Build Coastguard Worker        location: https://pkg.cppget.org/1/stable
157*5c90c05cSAndroid Build Coastguard Worker
158*5c90c05cSAndroid Build Coastguard Worker- Add this package as a dependency to your `manifest` file (example
159*5c90c05cSAndroid Build Coastguard Worker  for version 10):
160*5c90c05cSAndroid Build Coastguard Worker
161*5c90c05cSAndroid Build Coastguard Worker        depends: fmt ~10.0.0
162*5c90c05cSAndroid Build Coastguard Worker
163*5c90c05cSAndroid Build Coastguard Worker- Import the target and use it as a prerequisite to your own target
164*5c90c05cSAndroid Build Coastguard Worker  using `fmt` in the appropriate `buildfile`:
165*5c90c05cSAndroid Build Coastguard Worker
166*5c90c05cSAndroid Build Coastguard Worker        import fmt = fmt%lib{fmt}
167*5c90c05cSAndroid Build Coastguard Worker        lib{mylib} : cxx{**} ... $fmt
168*5c90c05cSAndroid Build Coastguard Worker
169*5c90c05cSAndroid Build Coastguard WorkerThen build your project as usual with `b` or `bdep update`.
170*5c90c05cSAndroid Build Coastguard Worker
171*5c90c05cSAndroid Build Coastguard Worker### Meson
172*5c90c05cSAndroid Build Coastguard Worker
173*5c90c05cSAndroid Build Coastguard Worker[Meson WrapDB](https://mesonbuild.com/Wrapdb-projects.html) includes an `fmt`
174*5c90c05cSAndroid Build Coastguard Workerpackage.
175*5c90c05cSAndroid Build Coastguard Worker
176*5c90c05cSAndroid Build Coastguard Worker**Usage:**
177*5c90c05cSAndroid Build Coastguard Worker
178*5c90c05cSAndroid Build Coastguard Worker- Install the `fmt` subproject from the WrapDB by running:
179*5c90c05cSAndroid Build Coastguard Worker
180*5c90c05cSAndroid Build Coastguard Worker        meson wrap install fmt
181*5c90c05cSAndroid Build Coastguard Worker
182*5c90c05cSAndroid Build Coastguard Worker  from the root of your project.
183*5c90c05cSAndroid Build Coastguard Worker
184*5c90c05cSAndroid Build Coastguard Worker- In your project's `meson.build` file, add an entry for the new subproject:
185*5c90c05cSAndroid Build Coastguard Worker
186*5c90c05cSAndroid Build Coastguard Worker        fmt = subproject('fmt')
187*5c90c05cSAndroid Build Coastguard Worker        fmt_dep = fmt.get_variable('fmt_dep')
188*5c90c05cSAndroid Build Coastguard Worker
189*5c90c05cSAndroid Build Coastguard Worker- Include the new dependency object to link with fmt:
190*5c90c05cSAndroid Build Coastguard Worker
191*5c90c05cSAndroid Build Coastguard Worker        my_build_target = executable(
192*5c90c05cSAndroid Build Coastguard Worker          'name', 'src/main.cc', dependencies: [fmt_dep])
193*5c90c05cSAndroid Build Coastguard Worker
194*5c90c05cSAndroid Build Coastguard Worker**Options:**
195*5c90c05cSAndroid Build Coastguard Worker
196*5c90c05cSAndroid Build Coastguard WorkerIf desired, {fmt} can be built as a static library, or as a header-only library.
197*5c90c05cSAndroid Build Coastguard Worker
198*5c90c05cSAndroid Build Coastguard WorkerFor a static build, use the following subproject definition:
199*5c90c05cSAndroid Build Coastguard Worker
200*5c90c05cSAndroid Build Coastguard Worker    fmt = subproject('fmt', default_options: 'default_library=static')
201*5c90c05cSAndroid Build Coastguard Worker    fmt_dep = fmt.get_variable('fmt_dep')
202*5c90c05cSAndroid Build Coastguard Worker
203*5c90c05cSAndroid Build Coastguard WorkerFor the header-only version, use:
204*5c90c05cSAndroid Build Coastguard Worker
205*5c90c05cSAndroid Build Coastguard Worker    fmt = subproject('fmt')
206*5c90c05cSAndroid Build Coastguard Worker    fmt_dep = fmt.get_variable('fmt_header_only_dep')
207*5c90c05cSAndroid Build Coastguard Worker
208*5c90c05cSAndroid Build Coastguard Worker### Android NDK
209*5c90c05cSAndroid Build Coastguard Worker
210*5c90c05cSAndroid Build Coastguard Worker{fmt} provides [Android.mk file](
211*5c90c05cSAndroid Build Coastguard Workerhttps://github.com/fmtlib/fmt/blob/master/support/Android.mk) that can be used
212*5c90c05cSAndroid Build Coastguard Workerto build the library with [Android NDK](
213*5c90c05cSAndroid Build Coastguard Workerhttps://developer.android.com/tools/sdk/ndk/index.html).
214*5c90c05cSAndroid Build Coastguard Worker
215*5c90c05cSAndroid Build Coastguard Worker### Other
216*5c90c05cSAndroid Build Coastguard Worker
217*5c90c05cSAndroid Build Coastguard WorkerTo use the {fmt} library with any other build system, add
218*5c90c05cSAndroid Build Coastguard Worker`include/fmt/base.h`, `include/fmt/format.h`, `include/fmt/format-inl.h`,
219*5c90c05cSAndroid Build Coastguard Worker`src/format.cc` and optionally other headers from a [release archive](
220*5c90c05cSAndroid Build Coastguard Workerhttps://github.com/fmtlib/fmt/releases) or the [git repository](
221*5c90c05cSAndroid Build Coastguard Workerhttps://github.com/fmtlib/fmt) to your project, add `include` to include
222*5c90c05cSAndroid Build Coastguard Workerdirectories and make sure `src/format.cc` is compiled and linked with your code.
223