xref: /aosp_15_r20/external/libultrahdr/docs/building.md (revision 89a0ef05262152531a00a15832a2d3b1e3990773)
1## libultrahdr CMake Build Instructions
2
3[![Build Status](https://github.com/google/libultrahdr/actions/workflows/cmake_linux.yml/badge.svg?event=push)](https://github.com/google/libultrahdr/actions/workflows/cmake_linux.yml?query=event%3Apush)
4[![Build Status](https://github.com/google/libultrahdr/actions/workflows/cmake_mac.yml/badge.svg?event=push)](https://github.com/google/libultrahdr/actions/workflows/cmake_mac.yml?query=event%3Apush)
5[![Build Status](https://github.com/google/libultrahdr/actions/workflows/cmake_win.yml/badge.svg?event=push)](https://github.com/google/libultrahdr/actions/workflows/cmake_win.yml?query=event%3Apush)
6[![Build Status](https://github.com/google/libultrahdr/actions/workflows/cmake_android.yml/badge.svg?event=push)](https://github.com/google/libultrahdr/actions/workflows/cmake_android.yml?query=event%3Apush)
7[![Fuzz Status](https://oss-fuzz-build-logs.storage.googleapis.com/badges/libultrahdr.svg)](https://introspector.oss-fuzz.com/project-profile?project=libultrahdr)
8
9### Requirements
10
11- [CMake](http://www.cmake.org) v3.15 or later
12- C++ compiler, supporting at least C++17.
13- libultrahdr uses jpeg compression format to store sdr image and gainmap quotient.
14  So, libjpeg or any other jpeg codec that is ABI and API compatible with libjpeg.
15
16The library offers a way to skip installing libjpeg by passing `UHDR_BUILD_DEPS=1`
17at the time of configure. That is, `cmake -DUHDR_BUILD_DEPS=1` will clone jpeg codec
18from [link](https://github.com/libjpeg-turbo/libjpeg-turbo.git) and include it in
19the build process. This is however not recommended.
20
21If jpeg is included in the build process then,
22- C compiler
23- For building x86/x86_64 SIMD optimizations, [NASM](http://www.nasm.us) or
24 [Yasm](http://yasm.tortall.net).
25  * If using NASM, 2.13 or later is required.
26  * If using Yasm, 1.2.0 or later is required.
27
28### CMake Options
29
30There are a few options that can be passed to CMake to modify how the code
31is built.<br>
32To set these options and parameters, use `-D<Parameter_name>=<value>`.
33
34All CMake options are passed at configure time, i.e., by running
35`cmake -DOPTION_ONE=1 -DOPTION_TWO=0 ...` <br>
36before running `cmake --build ...`<br>
37
38For example, to build unit tests in a new subdirectory called 'build', run:
39
40```sh
41cmake -G "Unix Makefiles" -S. -Bbuild -DUHDR_BUILD_TESTS=1 ../
42```
43and then build with:
44
45```sh
46cmake --build build
47```
48
49Following is a list of available options:
50
51| CMake Option | Default Value | Notes |
52|:-------------|:--------------|:-----|
53| `CMAKE_BUILD_TYPE` | Release | See CMake documentation [here](https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html). |
54| `BUILD_SHARED_LIBS` | ON | See CMake documentation [here](https://cmake.org/cmake/help/latest/variable/BUILD_SHARED_LIBS.html). <ul><li> If `BUILD_SHARED_LIBS` is **OFF**, in the linking phase, static versions of dependencies are chosen. However, the executable targets are not purely static because the system libraries used are still dynamic. </li></ul> |
55| `UHDR_BUILD_EXAMPLES` | ON | Build sample application. This application demonstrates how to use [ultrahdr_api.h](../ultrahdr_api.h). |
56| `UHDR_BUILD_TESTS` | OFF | Build Unit Tests. Mostly for Devs. During development, different modules of libuhdr library are validated using GoogleTest framework. Developers after making changes to library are expected to run these tests to ensure every thing is functional. |
57| `UHDR_BUILD_BENCHMARK` | OFF | Build Benchmark Tests. These are for profiling libuhdr encode/decode API. Resources used by benchmark tests are shared [here](https://storage.googleapis.com/android_media/external/libultrahdr/benchmark/UltrahdrBenchmarkTestRes-1.1.zip). These are downloaded and extracted automatically during the build process for later benchmarking. <ul><li> Benchmark tests are not supported on Windows and this parameter is forced to **OFF** internally while building on **WIN32** platforms. </li></ul>|
58| `UHDR_BUILD_FUZZERS` | OFF | Build Fuzz Test Applications. Mostly for Devs. <ul><li> Fuzz applications are built by instrumenting the entire software suite. This includes dependency libraries. This is done by forcing `UHDR_BUILD_DEPS` to **ON** internally. </li></ul> |
59| `UHDR_BUILD_DEPS` | OFF | Clone and Build project dependencies and not use pre-installed packages. |
60| `UHDR_BUILD_JAVA` | OFF | Build JNI wrapper, Java front-end classes and Java sample application. |
61| `UHDR_ENABLE_LOGS` | OFF | Build with verbose logging. |
62| `UHDR_ENABLE_INSTALL` | ON | Enable install and uninstall targets for libuhdr package. <ul><li> For system wide installation it is best if dependencies are acquired from OS package manager instead of building from source. This is to avoid conflicts with software that is using a different version of the said dependency and also links to libuhdr. So if `UHDR_BUILD_DEPS` is **ON** then `UHDR_ENABLE_INSTALL` is forced to **OFF** internally. |
63| `UHDR_ENABLE_INTRINSICS` | ON | Build with SIMD acceleration. Sections of libuhdr are accelerated for Arm Neon architectures and these are enabled. <ul><li> For x86/x86_64 architectures currently no SIMD acceleration is present. Consequently this option has no effect. </li><li> This parameter has no effect no SIMD configuration settings of dependencies. </li></ul> |
64| `UHDR_ENABLE_GLES` | OFF | Build with GPU acceleration. |
65| `UHDR_ENABLE_WERROR` | OFF | Enable -Werror when building. |
66| `UHDR_MAX_DIMENSION` | 8192 | Maximum dimension supported by the library. The library defaults to handling images upto resolution 8192x8192. For different resolution needs use this option. For example, `-DUHDR_MAX_DIMENSION=4096`. |
67| `UHDR_SANITIZE_OPTIONS` | OFF | Build library with sanitize options. Values set to this parameter are passed to directly to compilation option `-fsanitize`. For example, `-DUHDR_SANITIZE_OPTIONS=address,undefined` adds `-fsanitize=address,undefined` to the list of compilation options. CMake configuration errors are raised if the compiler does not support these flags. This is useful during fuzz testing. <ul><li> As `-fsanitize` is an instrumentation option, dependencies are also built from source instead of using pre-builts. This is done by forcing `UHDR_BUILD_DEPS` to **ON** internally. </li></ul> |
68| `UHDR_BUILD_PACKAGING` | OFF | Build distribution packages using CPack. |
69| | | |
70
71### Generator
72
73The CMake generator preferred is ninja. Consequently, ninja is added to the list of prerequisite packages. This need not be the case. If the platform is equipped with a different generator, it can be tried and ninja installation can be skipped.
74
75### Build Steps
76
77Check out the source code:
78
79```sh
80git clone https://github.com/google/libultrahdr.git
81cd libultrahdr
82mkdir build_directory
83cd build_directory
84```
85
86### Linux Platform
87
88Install the prerequisite packages before building:
89
90```sh
91sudo apt install cmake pkg-config libjpeg-dev ninja-build
92```
93
94Compile and Test:
95
96```sh
97cmake -G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DUHDR_BUILD_TESTS=1 ../
98ninja
99ctest
100```
101
102This will generate the following files under `build_directory`:
103
104**libuhdr.so.{version}** - Shared library for the libuhdr API <br>
105**libuhdr.so** - Symlink to shared library <br>
106**libuhdr.a** - Static link library for the libuhdr API <br>
107**libuhdr.pc** - libuhdr pkg-config file <br>
108**ultrahdr_app** - sample application <br>
109**ultrahdr_unit_test** - unit tests <br>
110
111Installation:
112
113```sh
114sudo ninja install
115```
116
117This installs the headers, pkg-config, and shared libraries. By default the headers are put in `/usr/local/include/`, libraries in `/usr/local/lib/` and pkg-config file in `/usr/local/lib/pkgconfig/`. You may need to add path `/usr/local/lib/` to `LD_LIBRARY_PATH` if binaries linking with ultrahdr library are unable to load it at run time. e.g. `export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/`.
118
119Uninstallation:
120
121```sh
122sudo ninja uninstall
123```
124
125### macOS Platform
126
127Install the prerequisite packages before building:
128
129```sh
130brew install cmake pkg-config jpeg ninja
131```
132
133Compile and Test:
134
135```sh
136cmake -G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DUHDR_BUILD_TESTS=1 ../
137ninja
138ctest
139```
140
141This will generate the following files under `build_directory`:
142
143**libuhdr.{version}.dylib** - Shared library for the libuhdr API <br>
144**libuhdr.dylib** - Symlink to shared library <br>
145**libuhdr.a** - Static link library for the libuhdr API <br>
146**libuhdr.pc** - libuhdr pkg-config file <br>
147**ultrahdr_app** - sample application <br>
148**ultrahdr_unit_test** - unit tests <br>
149
150Installation:
151
152```sh
153sudo ninja install
154```
155
156This installs the headers, pkg-config, and shared libraries. By default the headers are put in `/usr/local/include/`, libraries in `/usr/local/lib/` and pkg-config file in `/usr/local/lib/pkgconfig/`. You may need to add path `/usr/local/lib/` to `DYLD_FALLBACK_LIBRARY_PATH` if binaries are unable to load uhdr library e.g. `export DYLD_FALLBACK_LIBRARY_PATH=$DYLD_FALLBACK_LIBRARY_PATH:/usr/local/lib/`.
157
158Uninstallation:
159
160```sh
161sudo ninja uninstall
162```
163
164### Windows Platform - MSYS2 Env
165
166Install the prerequisite packages before building:
167
168```sh
169pacman -S mingw-w64-ucrt-x86_64-libjpeg-turbo mingw-w64-ucrt-x86_64-ninja
170```
171
172Compile and Test:
173
174```sh
175cmake -G Ninja -DUHDR_BUILD_TESTS=1 ../
176ninja
177ctest
178```
179
180This will generate the following files under `build_directory`:
181
182**libuhdr.dll** - Shared library for the libuhdr API <br>
183**libuhdr.dll.a** - Import library for the libuhdr API <br>
184**libuhdr.a** - Static link library for the libuhdr API <br>
185**libuhdr.pc** - libuhdr pkg-config file <br>
186**ultrahdr_app** - sample application <br>
187**ultrahdr_unit_test** - unit tests <br>
188
189### Windows Platform - MSVC Env
190
191#### IDE
192
193Compile and Test:
194
195```sh
196cmake -G "Visual Studio 16 2019" -DUHDR_BUILD_DEPS=1 -DUHDR_BUILD_TESTS=1 ../
197cmake --build ./ --config=Release
198ctest -C Release
199```
200
201#### Command Line
202
203Compile and Test:
204
205```sh
206cmake -G "NMake Makefiles" -DUHDR_BUILD_DEPS=1 -DUHDR_BUILD_TESTS=1 ../
207cmake --build ./
208ctest
209```
210
211This will generate the following files under `build_directory`:
212
213**uhdr.dll** - Shared library for the libuhdr API <br>
214**uhdr.lib** - Import library for the libuhdr API <br>
215**uhdr-static.lib** - Static link library for the libuhdr API <br>
216**ultrahdr_app** - sample application <br>
217**ultrahdr_unit_test** - unit tests <br>
218
219### Cross-Compilation - Build System Linux
220
221#### Target - Linux Platform - Armv7 Arch
222
223Install the prerequisite packages before building:
224
225```sh
226sudo apt install gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf
227```
228
229Compile:
230
231```sh
232cmake -G Ninja -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/arm-linux-gnueabihf.cmake -DUHDR_BUILD_DEPS=1 ../
233ninja
234```
235
236#### Target - Linux Platform - Armv8 Arch
237
238Install the prerequisite packages before building:
239
240```sh
241sudo apt install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
242```
243
244Compile:
245
246```sh
247cmake -G Ninja -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/aarch64-linux-gnu.cmake -DUHDR_BUILD_DEPS=1 ../
248ninja
249```
250
251#### Target - Linux Platform - RISC-V Arch (64 bit)
252
253Install the prerequisite packages before building:
254
255```sh
256sudo apt install gcc-riscv64-linux-gnu g++-riscv64-linux-gnu
257```
258
259Compile:
260
261```sh
262cmake -G Ninja -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/riscv64-linux-gnu.cmake -DUHDR_BUILD_DEPS=1 ../
263ninja
264```
265
266This will generate the following files under `build_directory`:
267
268**libuhdr.so.{version}** - Shared library for the libuhdr API <br>
269**libuhdr.so** - Symlink to shared library <br>
270**libuhdr.a** - Static link library for the libuhdr API <br>
271**ultrahdr_app** - sample application <br>
272**ultrahdr_unit_test** - unit tests <br>
273
274#### Target - Linux Platform - RISC-V Arch (32 bit)
275
276Install the prerequisite packages before building:
277
278```sh
279# Download from https://github.com/riscv-collab/riscv-gnu-toolchain/releases
280sudo ln -s {your_dir}/riscv/bin/riscv32-unknown-linux-gnu-g++ /usr/bin/riscv32-linux-gnu-g++
281sudo ln -s {your_dir}/riscv/bin/riscv32-unknown-linux-gnu-gcc /usr/bin/riscv32-linux-gnu-gcc
282```
283
284Compile:
285
286```sh
287cmake -G Ninja -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/riscv32-linux-gnu.cmake -DUHDR_BUILD_DEPS=1 ../
288ninja
289```
290
291This will generate the following files under `build_directory`:
292
293**libuhdr.so.{version}** - Shared library for the libuhdr API <br>
294**libuhdr.so** - Symlink to shared library <br>
295**libuhdr.a** - Static link library for the libuhdr API <br>
296**ultrahdr_app** - sample application <br>
297**ultrahdr_unit_test** - unit tests <br>
298
299#### Target - Linux Platform - LOONG Arch (64 bit)
300
301Install the prerequisite packages before building:
302
303```sh
304sudo apt install gcc-loongarch64-linux-gnu g++-loongarch64-linux-gnu
305```
306
307Compile:
308
309```sh
310cmake -G Ninja -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/loong64-linux-gnu.cmake -DUHDR_BUILD_DEPS=1 ../
311ninja
312```
313
314#### Target - Android Platform
315
316Install the prerequisite packages before building:
317
318```sh
319wget https://dl.google.com/android/repository/android-ndk-r26d-linux.zip
320unzip android-ndk-r26d-linux.zip
321```
322
323Choose target architecture with -DANDROID_ABI={armeabi-v7a, arm64-v8a, x86, x86_64}
324
325Compile:
326```sh
327cmake -G Ninja -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/android.cmake -DUHDR_ANDROID_NDK_PATH=/opt/android-ndk-r26d/ -DUHDR_BUILD_DEPS=1 -DANDROID_ABI="Selected Architecture" -DANDROID_PLATFORM=android-23 ../
328ninja
329```
330
331This will generate the following files under `build_directory`:
332
333**libuhdr.so** - Shared library for the libuhdr API <br>
334**libuhdr.a** - Static link library for the libuhdr API <br>
335**ultrahdr_app** - sample application <br>
336**ultrahdr_unit_test** - unit tests <br>
337
338#### Target - Wasm
339
340Install the prerequisite packages before building: Follow the instructions given [here](https://emscripten.org/docs/getting_started/downloads.html#installation-instructions-using-the-emsdk-recommended).
341
342Compile:
343```sh
344emcmake cmake -G Ninja ../
345ninja
346```
347
348This will generate the following files under `build_directory`:
349
350**ultrahdr_app.wasm** - wasm module <br>
351**ultrahdr_app.js** - sample application <br>
352
353## Building Fuzzers
354
355Refer to [fuzzers.md](fuzzers.md) for complete instructions.
356