1# Copyright (c) the JPEG XL Project Authors. All rights reserved.
2#
3# Use of this source code is governed by a BSD-style
4# license that can be found in the LICENSE file.
5
6# Lists all source files for the JPEG XL decoder library. These are also used
7# by the encoder: the encoder uses both dec and enc ourse files, while the
8# decoder uses only dec source files.
9# TODO(lode): further prune these files and move to JPEGXL_INTERNAL_SOURCES_ENC:
10#             only those files that the decoder absolutely needs, and or not
11#             only for encoding, should be listed here.
12set(JPEGXL_INTERNAL_SOURCES_DEC
13  jxl/ac_context.h
14  jxl/ac_strategy.cc
15  jxl/ac_strategy.h
16  jxl/alpha.cc
17  jxl/alpha.h
18  jxl/ans_common.cc
19  jxl/ans_common.h
20  jxl/ans_params.h
21  jxl/aux_out.cc
22  jxl/aux_out.h
23  jxl/aux_out_fwd.h
24  jxl/base/arch_macros.h
25  jxl/base/bits.h
26  jxl/base/byte_order.h
27  jxl/base/cache_aligned.cc
28  jxl/base/cache_aligned.h
29  jxl/base/compiler_specific.h
30  jxl/base/data_parallel.cc
31  jxl/base/data_parallel.h
32  jxl/base/file_io.h
33  jxl/base/iaca.h
34  jxl/base/os_macros.h
35  jxl/base/override.h
36  jxl/base/padded_bytes.cc
37  jxl/base/padded_bytes.h
38  jxl/base/printf_macros.h
39  jxl/base/profiler.h
40  jxl/base/random.cc
41  jxl/base/random.h
42  jxl/base/scope_guard.h
43  jxl/base/span.h
44  jxl/base/status.cc
45  jxl/base/status.h
46  jxl/base/thread_pool_internal.h
47  jxl/blending.cc
48  jxl/blending.h
49  jxl/box_content_decoder.cc
50  jxl/box_content_decoder.h
51  jxl/chroma_from_luma.cc
52  jxl/chroma_from_luma.h
53  jxl/codec_in_out.h
54  jxl/coeff_order.cc
55  jxl/coeff_order.h
56  jxl/coeff_order_fwd.h
57  jxl/color_encoding_internal.cc
58  jxl/color_encoding_internal.h
59  jxl/color_management.cc
60  jxl/color_management.h
61  jxl/common.h
62  jxl/compressed_dc.cc
63  jxl/compressed_dc.h
64  jxl/convolve-inl.h
65  jxl/convolve.cc
66  jxl/convolve.h
67  jxl/dct-inl.h
68  jxl/dct_block-inl.h
69  jxl/dct_scales.cc
70  jxl/dct_scales.h
71  jxl/dct_util.h
72  jxl/dec_ans.cc
73  jxl/dec_ans.h
74  jxl/dec_bit_reader.h
75  jxl/dec_cache.cc
76  jxl/dec_cache.h
77  jxl/dec_context_map.cc
78  jxl/dec_context_map.h
79  jxl/dec_external_image.cc
80  jxl/dec_external_image.h
81  jxl/dec_frame.cc
82  jxl/dec_frame.h
83  jxl/dec_group.cc
84  jxl/dec_group.h
85  jxl/dec_group_border.cc
86  jxl/dec_group_border.h
87  jxl/dec_huffman.cc
88  jxl/dec_huffman.h
89  jxl/dec_modular.cc
90  jxl/dec_modular.h
91  jxl/dec_noise.cc
92  jxl/dec_noise.h
93  jxl/dec_params.h
94  jxl/dec_patch_dictionary.cc
95  jxl/dec_patch_dictionary.h
96  jxl/dec_reconstruct.cc
97  jxl/dec_reconstruct.h
98  jxl/dec_transforms-inl.h
99  jxl/dec_upsample.cc
100  jxl/dec_upsample.h
101  jxl/dec_xyb-inl.h
102  jxl/dec_xyb.cc
103  jxl/dec_xyb.h
104  jxl/decode.cc
105  jxl/decode_to_jpeg.cc
106  jxl/decode_to_jpeg.h
107  jxl/enc_bit_writer.cc
108  jxl/enc_bit_writer.h
109  jxl/entropy_coder.cc
110  jxl/entropy_coder.h
111  jxl/epf.cc
112  jxl/epf.h
113  jxl/fast_dct-inl.h
114  jxl/fast_dct.cc
115  jxl/fast_dct.h
116  jxl/fast_dct128-inl.h
117  jxl/fast_dct16-inl.h
118  jxl/fast_dct256-inl.h
119  jxl/fast_dct32-inl.h
120  jxl/fast_dct64-inl.h
121  jxl/fast_dct8-inl.h
122  jxl/fast_math-inl.h
123  jxl/field_encodings.h
124  jxl/fields.cc
125  jxl/fields.h
126  jxl/filters.cc
127  jxl/filters.h
128  jxl/filters_internal.h
129  jxl/frame_header.cc
130  jxl/frame_header.h
131  jxl/gauss_blur.cc
132  jxl/gauss_blur.h
133  jxl/headers.cc
134  jxl/headers.h
135  jxl/huffman_table.cc
136  jxl/huffman_table.h
137  jxl/icc_codec.cc
138  jxl/icc_codec.h
139  jxl/icc_codec_common.cc
140  jxl/icc_codec_common.h
141  jxl/image.cc
142  jxl/image.h
143  jxl/image_bundle.cc
144  jxl/image_bundle.h
145  jxl/image_metadata.cc
146  jxl/image_metadata.h
147  jxl/image_ops.h
148  jxl/jpeg/dec_jpeg_data.cc
149  jxl/jpeg/dec_jpeg_data.h
150  jxl/jpeg/dec_jpeg_data_writer.cc
151  jxl/jpeg/dec_jpeg_data_writer.h
152  jxl/jpeg/dec_jpeg_output_chunk.h
153  jxl/jpeg/dec_jpeg_serialization_state.h
154  jxl/jpeg/jpeg_data.cc
155  jxl/jpeg/jpeg_data.h
156  jxl/jxl_inspection.h
157  jxl/lehmer_code.h
158  jxl/linalg.h
159  jxl/loop_filter.cc
160  jxl/loop_filter.h
161  jxl/luminance.cc
162  jxl/luminance.h
163  jxl/memory_manager_internal.cc
164  jxl/memory_manager_internal.h
165  jxl/modular/encoding/context_predict.h
166  jxl/modular/encoding/dec_ma.cc
167  jxl/modular/encoding/dec_ma.h
168  jxl/modular/encoding/encoding.cc
169  jxl/modular/encoding/encoding.h
170  jxl/modular/encoding/ma_common.h
171  jxl/modular/modular_image.cc
172  jxl/modular/modular_image.h
173  jxl/modular/options.h
174  jxl/modular/transform/palette.h
175  jxl/modular/transform/rct.cc
176  jxl/modular/transform/rct.h
177  jxl/modular/transform/squeeze.cc
178  jxl/modular/transform/squeeze.h
179  jxl/modular/transform/transform.cc
180  jxl/modular/transform/transform.h
181  jxl/noise.h
182  jxl/opsin_params.cc
183  jxl/opsin_params.h
184  jxl/passes_state.cc
185  jxl/passes_state.h
186  jxl/patch_dictionary_internal.h
187  jxl/quant_weights.cc
188  jxl/quant_weights.h
189  jxl/quantizer-inl.h
190  jxl/quantizer.cc
191  jxl/quantizer.h
192  jxl/rational_polynomial-inl.h
193  jxl/render_pipeline/low_memory_render_pipeline.cc
194  jxl/render_pipeline/low_memory_render_pipeline.h
195  jxl/render_pipeline/render_pipeline.cc
196  jxl/render_pipeline/render_pipeline.h
197  jxl/render_pipeline/render_pipeline_stage.h
198  jxl/render_pipeline/simple_render_pipeline.cc
199  jxl/render_pipeline/simple_render_pipeline.h
200  jxl/render_pipeline/stage_blending.cc
201  jxl/render_pipeline/stage_blending.h
202  jxl/render_pipeline/stage_chroma_upsampling.cc
203  jxl/render_pipeline/stage_chroma_upsampling.h
204  jxl/render_pipeline/stage_epf.cc
205  jxl/render_pipeline/stage_epf.h
206  jxl/render_pipeline/stage_gaborish.cc
207  jxl/render_pipeline/stage_gaborish.h
208  jxl/render_pipeline/stage_noise.cc
209  jxl/render_pipeline/stage_noise.h
210  jxl/render_pipeline/stage_patches.cc
211  jxl/render_pipeline/stage_patches.h
212  jxl/render_pipeline/stage_splines.cc
213  jxl/render_pipeline/stage_splines.h
214  jxl/render_pipeline/stage_spot.cc
215  jxl/render_pipeline/stage_spot.h
216  jxl/render_pipeline/stage_upsampling.cc
217  jxl/render_pipeline/stage_upsampling.h
218  jxl/render_pipeline/stage_write.cc
219  jxl/render_pipeline/stage_write.h
220  jxl/render_pipeline/stage_xyb.cc
221  jxl/render_pipeline/stage_xyb.h
222  jxl/render_pipeline/stage_ycbcr.cc
223  jxl/render_pipeline/stage_ycbcr.h
224  jxl/render_pipeline/test_render_pipeline_stages.h
225  jxl/sanitizers.h
226  jxl/simd_util-inl.h
227  jxl/size_constraints.h
228  jxl/splines.cc
229  jxl/splines.h
230  jxl/toc.cc
231  jxl/toc.h
232  jxl/transfer_functions-inl.h
233  jxl/transpose-inl.h
234  jxl/xorshift128plus-inl.h
235)
236
237# List of source files only needed by the encoder or by tools (including
238# decoding tools), but not by the decoder library.
239set(JPEGXL_INTERNAL_SOURCES_ENC
240  jxl/butteraugli/butteraugli.cc
241  jxl/butteraugli/butteraugli.h
242  jxl/butteraugli_wrapper.cc
243  jxl/dec_file.cc
244  jxl/dec_file.h
245  jxl/enc_ac_strategy.cc
246  jxl/enc_ac_strategy.h
247  jxl/enc_adaptive_quantization.cc
248  jxl/enc_adaptive_quantization.h
249  jxl/enc_ans.cc
250  jxl/enc_ans.h
251  jxl/enc_ans_params.h
252  jxl/enc_ar_control_field.cc
253  jxl/enc_ar_control_field.h
254  jxl/enc_butteraugli_comparator.cc
255  jxl/enc_butteraugli_comparator.h
256  jxl/enc_butteraugli_pnorm.cc
257  jxl/enc_butteraugli_pnorm.h
258  jxl/enc_cache.cc
259  jxl/enc_cache.h
260  jxl/enc_chroma_from_luma.cc
261  jxl/enc_chroma_from_luma.h
262  jxl/enc_cluster.cc
263  jxl/enc_cluster.h
264  jxl/enc_coeff_order.cc
265  jxl/enc_coeff_order.h
266  jxl/enc_color_management.cc
267  jxl/enc_color_management.h
268  jxl/enc_comparator.cc
269  jxl/enc_comparator.h
270  jxl/enc_context_map.cc
271  jxl/enc_context_map.h
272  jxl/enc_detect_dots.cc
273  jxl/enc_detect_dots.h
274  jxl/enc_dot_dictionary.cc
275  jxl/enc_dot_dictionary.h
276  jxl/enc_entropy_coder.cc
277  jxl/enc_entropy_coder.h
278  jxl/enc_external_image.cc
279  jxl/enc_external_image.h
280  jxl/enc_fast_heuristics.cc
281  jxl/enc_file.cc
282  jxl/enc_file.h
283  jxl/enc_frame.cc
284  jxl/enc_frame.h
285  jxl/enc_gamma_correct.h
286  jxl/enc_group.cc
287  jxl/enc_group.h
288  jxl/enc_heuristics.cc
289  jxl/enc_heuristics.h
290  jxl/enc_huffman.cc
291  jxl/enc_huffman.h
292  jxl/enc_icc_codec.cc
293  jxl/enc_icc_codec.h
294  jxl/enc_image_bundle.cc
295  jxl/enc_image_bundle.h
296  jxl/enc_jxl_skcms.h
297  jxl/enc_modular.cc
298  jxl/enc_modular.h
299  jxl/enc_noise.cc
300  jxl/enc_noise.h
301  jxl/enc_params.h
302  jxl/enc_patch_dictionary.cc
303  jxl/enc_patch_dictionary.h
304  jxl/enc_photon_noise.cc
305  jxl/enc_photon_noise.h
306  jxl/enc_quant_weights.cc
307  jxl/enc_quant_weights.h
308  jxl/enc_splines.cc
309  jxl/enc_splines.h
310  jxl/enc_toc.cc
311  jxl/enc_toc.h
312  jxl/enc_transforms-inl.h
313  jxl/enc_transforms.cc
314  jxl/enc_transforms.h
315  jxl/enc_xyb.cc
316  jxl/enc_xyb.h
317  jxl/encode.cc
318  jxl/encode_internal.h
319  jxl/gaborish.cc
320  jxl/gaborish.h
321  jxl/huffman_tree.cc
322  jxl/huffman_tree.h
323  jxl/jpeg/enc_jpeg_data.cc
324  jxl/jpeg/enc_jpeg_data.h
325  jxl/jpeg/enc_jpeg_data_reader.cc
326  jxl/jpeg/enc_jpeg_data_reader.h
327  jxl/jpeg/enc_jpeg_huffman_decode.cc
328  jxl/jpeg/enc_jpeg_huffman_decode.h
329  jxl/linalg.cc
330  jxl/modular/encoding/enc_debug_tree.cc
331  jxl/modular/encoding/enc_debug_tree.h
332  jxl/modular/encoding/enc_encoding.cc
333  jxl/modular/encoding/enc_encoding.h
334  jxl/modular/encoding/enc_ma.cc
335  jxl/modular/encoding/enc_ma.h
336  jxl/modular/transform/enc_palette.cc
337  jxl/modular/transform/enc_palette.h
338  jxl/modular/transform/enc_rct.cc
339  jxl/modular/transform/enc_rct.h
340  jxl/modular/transform/enc_squeeze.cc
341  jxl/modular/transform/enc_squeeze.h
342  jxl/modular/transform/enc_transform.cc
343  jxl/modular/transform/enc_transform.h
344  jxl/optimize.cc
345  jxl/optimize.h
346  jxl/progressive_split.cc
347  jxl/progressive_split.h
348)
349
350set(JPEGXL_DEC_INTERNAL_LIBS
351  brotlidec-static
352  brotlicommon-static
353  hwy
354)
355
356if(JPEGXL_ENABLE_PROFILER)
357list(APPEND JPEGXL_DEC_INTERNAL_LIBS jxl_profiler)
358endif()
359
360set(JPEGXL_INTERNAL_LIBS
361  ${JPEGXL_DEC_INTERNAL_LIBS}
362  brotlienc-static
363  Threads::Threads
364)
365
366# strips the -static suffix from all the elements in LIST
367function(strip_static OUTPUT_VAR LIB_LIST)
368  foreach(lib IN LISTS ${LIB_LIST})
369    string(REGEX REPLACE "-static$" "" lib "${lib}")
370    list(APPEND out_list "${lib}")
371  endforeach()
372  set(${OUTPUT_VAR} ${out_list} PARENT_SCOPE)
373endfunction()
374
375if (JPEGXL_ENABLE_SKCMS)
376  list(APPEND JPEGXL_INTERNAL_FLAGS -DJPEGXL_ENABLE_SKCMS=1)
377  if (JPEGXL_BUNDLE_SKCMS)
378    list(APPEND JPEGXL_INTERNAL_FLAGS -DJPEGXL_BUNDLE_SKCMS=1)
379    # skcms objects are later added to JPEGXL_INTERNAL_OBJECTS
380  else ()
381    list(APPEND JPEGXL_INTERNAL_LIBS skcms)
382  endif ()
383else ()
384  list(APPEND JPEGXL_INTERNAL_LIBS lcms2)
385endif ()
386
387if (NOT JPEGXL_ENABLE_TRANSCODE_JPEG)
388  list(APPEND JPEGXL_INTERNAL_FLAGS -DJPEGXL_ENABLE_TRANSCODE_JPEG=0)
389endif ()
390
391set(OBJ_COMPILE_DEFINITIONS
392  JPEGXL_MAJOR_VERSION=${JPEGXL_MAJOR_VERSION}
393  JPEGXL_MINOR_VERSION=${JPEGXL_MINOR_VERSION}
394  JPEGXL_PATCH_VERSION=${JPEGXL_PATCH_VERSION}
395  # Used to determine if we are building the library when defined or just
396  # including the library when not defined. This is public so libjxl shared
397  # library gets this define too.
398  JXL_INTERNAL_LIBRARY_BUILD
399)
400
401# Decoder-only object library
402add_library(jxl_dec-obj OBJECT ${JPEGXL_INTERNAL_SOURCES_DEC})
403target_compile_options(jxl_dec-obj PRIVATE ${JPEGXL_INTERNAL_FLAGS})
404target_compile_options(jxl_dec-obj PUBLIC ${JPEGXL_COVERAGE_FLAGS})
405set_property(TARGET jxl_dec-obj PROPERTY POSITION_INDEPENDENT_CODE ON)
406target_include_directories(jxl_dec-obj PUBLIC
407  ${PROJECT_SOURCE_DIR}
408  ${CMAKE_CURRENT_SOURCE_DIR}/include
409  $<TARGET_PROPERTY:hwy,INTERFACE_INCLUDE_DIRECTORIES>
410  $<TARGET_PROPERTY:brotlicommon-static,INTERFACE_INCLUDE_DIRECTORIES>
411)
412target_compile_definitions(jxl_dec-obj PUBLIC
413  ${OBJ_COMPILE_DEFINITIONS}
414)
415if (JPEGXL_ENABLE_PROFILER)
416target_link_libraries(jxl_dec-obj PUBLIC jxl_profiler)
417endif()
418
419# Object library. This is used to hold the set of objects and properties.
420add_library(jxl_enc-obj OBJECT ${JPEGXL_INTERNAL_SOURCES_ENC})
421target_compile_options(jxl_enc-obj PRIVATE ${JPEGXL_INTERNAL_FLAGS})
422target_compile_options(jxl_enc-obj PUBLIC ${JPEGXL_COVERAGE_FLAGS})
423set_property(TARGET jxl_enc-obj PROPERTY POSITION_INDEPENDENT_CODE ON)
424target_include_directories(jxl_enc-obj PUBLIC
425  ${PROJECT_SOURCE_DIR}
426  ${CMAKE_CURRENT_SOURCE_DIR}/include
427  $<TARGET_PROPERTY:hwy,INTERFACE_INCLUDE_DIRECTORIES>
428  $<TARGET_PROPERTY:brotlicommon-static,INTERFACE_INCLUDE_DIRECTORIES>
429)
430target_compile_definitions(jxl_enc-obj PUBLIC
431  ${OBJ_COMPILE_DEFINITIONS}
432)
433if (JPEGXL_ENABLE_PROFILER)
434target_link_libraries(jxl_enc-obj PUBLIC jxl_profiler)
435endif()
436
437#TODO(lode): don't depend on CMS for the core library
438if (JPEGXL_ENABLE_SKCMS)
439  target_include_directories(jxl_enc-obj PRIVATE
440    $<TARGET_PROPERTY:skcms,INCLUDE_DIRECTORIES>
441  )
442else ()
443  target_include_directories(jxl_enc-obj PRIVATE
444    $<TARGET_PROPERTY:lcms2,INCLUDE_DIRECTORIES>
445  )
446endif ()
447
448# Headers for exporting/importing public headers
449include(GenerateExportHeader)
450set_target_properties(jxl_dec-obj PROPERTIES
451  CXX_VISIBILITY_PRESET hidden
452  VISIBILITY_INLINES_HIDDEN 1
453  DEFINE_SYMBOL JXL_INTERNAL_LIBRARY_BUILD
454)
455target_include_directories(jxl_dec-obj PUBLIC
456    ${CMAKE_CURRENT_BINARY_DIR}/include)
457
458set_target_properties(jxl_enc-obj PROPERTIES
459  CXX_VISIBILITY_PRESET hidden
460  VISIBILITY_INLINES_HIDDEN 1
461  DEFINE_SYMBOL JXL_INTERNAL_LIBRARY_BUILD
462)
463generate_export_header(jxl_enc-obj
464  BASE_NAME JXL
465  EXPORT_FILE_NAME include/jxl/jxl_export.h)
466target_include_directories(jxl_enc-obj PUBLIC
467    ${CMAKE_CURRENT_BINARY_DIR}/include)
468
469# Private static library. This exposes all the internal functions and is used
470# for tests.
471add_library(jxl_dec-static STATIC
472  $<TARGET_OBJECTS:jxl_dec-obj>
473)
474target_link_libraries(jxl_dec-static
475  PUBLIC ${JPEGXL_COVERAGE_FLAGS} ${JPEGXL_DEC_INTERNAL_LIBS} hwy)
476target_include_directories(jxl_dec-static PUBLIC
477  "${PROJECT_SOURCE_DIR}"
478  "${CMAKE_CURRENT_SOURCE_DIR}/include"
479  "${CMAKE_CURRENT_BINARY_DIR}/include")
480
481# The list of objects in the static and shared libraries.
482set(JPEGXL_INTERNAL_OBJECTS
483  $<TARGET_OBJECTS:jxl_enc-obj>
484  $<TARGET_OBJECTS:jxl_dec-obj>
485)
486if (JPEGXL_ENABLE_SKCMS AND JPEGXL_BUNDLE_SKCMS)
487  list(APPEND JPEGXL_INTERNAL_OBJECTS $<TARGET_OBJECTS:skcms-obj>)
488endif()
489
490# Private static library. This exposes all the internal functions and is used
491# for tests.
492# TODO(lode): once the source files are correctly split so that it is possible
493# to do, remove $<TARGET_OBJECTS:jxl_dec-obj> here and depend on jxl_dec-static
494add_library(jxl-static STATIC ${JPEGXL_INTERNAL_OBJECTS})
495target_link_libraries(jxl-static
496  PUBLIC ${JPEGXL_COVERAGE_FLAGS} ${JPEGXL_INTERNAL_LIBS} hwy)
497target_include_directories(jxl-static PUBLIC
498  "${PROJECT_SOURCE_DIR}"
499  "${CMAKE_CURRENT_SOURCE_DIR}/include"
500  "${CMAKE_CURRENT_BINARY_DIR}/include")
501
502# JXL_EXPORT is defined to "__declspec(dllimport)" automatically by CMake
503# in Windows builds when including headers from the C API and compiling from
504# outside the jxl library. This is required when using the shared library,
505# however in windows this causes the function to not be found when linking
506# against the static library. This define JXL_EXPORT= here forces it to not
507# use dllimport in tests and other tools that require the static library.
508target_compile_definitions(jxl-static INTERFACE -DJXL_EXPORT=)
509target_compile_definitions(jxl_dec-static INTERFACE -DJXL_EXPORT=)
510
511# TODO(deymo): Move TCMalloc linkage to the tools/ directory since the library
512# shouldn't do any allocs anyway.
513if(${JPEGXL_ENABLE_TCMALLOC})
514  pkg_check_modules(TCMallocMinimal REQUIRED IMPORTED_TARGET
515      libtcmalloc_minimal)
516  # tcmalloc 2.8 has concurrency issues that makes it sometimes return nullptr
517  # for large allocs. See https://github.com/gperftools/gperftools/issues/1204
518  # for details.
519  if(TCMallocMinimal_VERSION VERSION_EQUAL 2.8)
520    message(FATAL_ERROR
521        "tcmalloc version 2.8 has a concurrency bug. You have installed "
522        "version ${TCMallocMinimal_VERSION}, please either downgrade tcmalloc "
523        "to version 2.7, upgrade to 2.8.1 or newer or pass "
524        "-DJPEGXL_ENABLE_TCMALLOC=OFF to jpeg-xl cmake line. See the following "
525        "bug for details:\n"
526        "   https://github.com/gperftools/gperftools/issues/1204\n")
527  endif()
528  target_link_libraries(jxl-static PUBLIC PkgConfig::TCMallocMinimal)
529endif()  # JPEGXL_ENABLE_TCMALLOC
530
531# Install the static library too, but as jxl.a file without the -static except
532# in Windows.
533if (NOT WIN32)
534  set_target_properties(jxl-static PROPERTIES OUTPUT_NAME "jxl")
535  set_target_properties(jxl_dec-static PROPERTIES OUTPUT_NAME "jxl_dec")
536endif()
537install(TARGETS jxl-static DESTINATION ${CMAKE_INSTALL_LIBDIR})
538install(TARGETS jxl_dec-static DESTINATION ${CMAKE_INSTALL_LIBDIR})
539
540if (((NOT DEFINED "${TARGET_SUPPORTS_SHARED_LIBS}") OR
541     TARGET_SUPPORTS_SHARED_LIBS) AND NOT JPEGXL_STATIC AND BUILD_SHARED_LIBS)
542
543# Public shared library.
544add_library(jxl SHARED ${JPEGXL_INTERNAL_OBJECTS})
545strip_static(JPEGXL_INTERNAL_SHARED_LIBS JPEGXL_INTERNAL_LIBS)
546target_link_libraries(jxl PUBLIC ${JPEGXL_COVERAGE_FLAGS})
547target_link_libraries(jxl PRIVATE ${JPEGXL_INTERNAL_SHARED_LIBS})
548# Shared library include path contains only the "include/" paths.
549target_include_directories(jxl PUBLIC
550  "${CMAKE_CURRENT_SOURCE_DIR}/include"
551  "${CMAKE_CURRENT_BINARY_DIR}/include")
552set_target_properties(jxl PROPERTIES
553  VERSION ${JPEGXL_LIBRARY_VERSION}
554  SOVERSION ${JPEGXL_LIBRARY_SOVERSION}
555  LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
556  RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
557
558# Public shared decoder library.
559add_library(jxl_dec SHARED $<TARGET_OBJECTS:jxl_dec-obj>)
560strip_static(JPEGXL_DEC_INTERNAL_SHARED_LIBS JPEGXL_DEC_INTERNAL_LIBS)
561target_link_libraries(jxl_dec PUBLIC ${JPEGXL_COVERAGE_FLAGS})
562target_link_libraries(jxl_dec PRIVATE ${JPEGXL_DEC_INTERNAL_SHARED_LIBS})
563# Shared library include path contains only the "include/" paths.
564target_include_directories(jxl_dec PUBLIC
565  "${CMAKE_CURRENT_SOURCE_DIR}/include"
566  "${CMAKE_CURRENT_BINARY_DIR}/include")
567set_target_properties(jxl_dec PROPERTIES
568  VERSION ${JPEGXL_LIBRARY_VERSION}
569  SOVERSION ${JPEGXL_LIBRARY_SOVERSION}
570  LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
571  RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
572
573# Check whether the linker support excluding libs
574set(LINKER_EXCLUDE_LIBS_FLAG "-Wl,--exclude-libs=ALL")
575include(CheckCSourceCompiles)
576list(APPEND CMAKE_EXE_LINKER_FLAGS ${LINKER_EXCLUDE_LIBS_FLAG})
577check_c_source_compiles("int main(){return 0;}" LINKER_SUPPORT_EXCLUDE_LIBS)
578list(REMOVE_ITEM CMAKE_EXE_LINKER_FLAGS ${LINKER_EXCLUDE_LIBS_FLAG})
579
580# Add a jxl.version file as a version script to tag symbols with the
581# appropriate version number. This script is also used to limit what's exposed
582# in the shared library from the static dependencies bundled here.
583foreach(target IN ITEMS jxl jxl_dec)
584  set_target_properties(${target} PROPERTIES
585      LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/jxl/jxl.version)
586  if(APPLE)
587  set_property(TARGET ${target} APPEND_STRING PROPERTY
588      LINK_FLAGS "-Wl,-exported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/jxl/jxl_osx.syms")
589  elseif(WIN32)
590    # Nothing needed here, we use __declspec(dllexport) (jxl_export.h)
591  else()
592  set_property(TARGET ${target} APPEND_STRING PROPERTY
593      LINK_FLAGS " -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/jxl/jxl.version")
594  endif()  # APPLE
595  # This hides the default visibility symbols from static libraries bundled into
596  # the shared library. In particular this prevents exposing symbols from hwy
597  # and skcms in the shared library.
598  if(${LINKER_SUPPORT_EXCLUDE_LIBS})
599    set_property(TARGET ${target} APPEND_STRING PROPERTY
600        LINK_FLAGS " ${LINKER_EXCLUDE_LIBS_FLAG}")
601  endif()
602endforeach()
603
604# Only install libjxl shared library. The libjxl_dec is not installed since it
605# contains symbols also in libjxl which would conflict if programs try to use
606# both.
607install(TARGETS jxl
608  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
609  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
610  ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
611else()
612add_library(jxl ALIAS jxl-static)
613add_library(jxl_dec ALIAS jxl_dec-static)
614endif()  # TARGET_SUPPORTS_SHARED_LIBS AND NOT JPEGXL_STATIC AND
615         # BUILD_SHARED_LIBS
616
617# Add a pkg-config file for libjxl.
618set(JPEGXL_LIBRARY_REQUIRES
619    "libhwy libbrotlicommon libbrotlienc libbrotlidec")
620if(NOT JPEGXL_ENABLE_SKCMS)
621  set(JPEGXL_LIBRARY_REQUIRES "${JPEGXL_LIBRARY_REQUIRES} lcms2")
622endif()
623configure_file("${CMAKE_CURRENT_SOURCE_DIR}/jxl/libjxl.pc.in"
624               "libjxl.pc" @ONLY)
625install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libjxl.pc"
626  DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
627