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
6find_package(Threads REQUIRED)
7
8set(JPEGXL_THREADS_SOURCES
9  threads/resizable_parallel_runner.cc
10  threads/thread_parallel_runner.cc
11  threads/thread_parallel_runner_internal.cc
12  threads/thread_parallel_runner_internal.h
13)
14
15### Define the jxl_threads shared or static target library. The ${target}
16# parameter should already be created with add_library(), but this function
17# sets all the remaining common properties.
18function(_set_jxl_threads _target)
19
20target_compile_options(${_target} PRIVATE ${JPEGXL_INTERNAL_FLAGS})
21target_compile_options(${_target} PUBLIC ${JPEGXL_COVERAGE_FLAGS})
22set_property(TARGET ${_target} PROPERTY POSITION_INDEPENDENT_CODE ON)
23
24target_include_directories(${_target}
25  PRIVATE
26    "${PROJECT_SOURCE_DIR}"
27  PUBLIC
28    "${CMAKE_CURRENT_SOURCE_DIR}/include"
29    "${CMAKE_CURRENT_BINARY_DIR}/include")
30
31target_link_libraries(${_target}
32  PUBLIC ${JPEGXL_COVERAGE_FLAGS} Threads::Threads
33)
34
35set_target_properties(${_target} PROPERTIES
36  CXX_VISIBILITY_PRESET hidden
37  VISIBILITY_INLINES_HIDDEN 1
38  DEFINE_SYMBOL JXL_THREADS_INTERNAL_LIBRARY_BUILD
39)
40
41# Always install the library as jxl_threads.{a,so} file without the "-static"
42# suffix, except in Windows.
43if (NOT WIN32)
44  set_target_properties(${_target} PROPERTIES OUTPUT_NAME "jxl_threads")
45endif()
46install(TARGETS ${_target}
47  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
48  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
49  ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
50
51endfunction()
52
53
54### Static library.
55add_library(jxl_threads-static STATIC ${JPEGXL_THREADS_SOURCES})
56_set_jxl_threads(jxl_threads-static)
57
58# Make jxl_threads symbols neither imported nor exported when using the static
59# library. These will have hidden visibility anyway in the static library case
60# in unix.
61target_compile_definitions(jxl_threads-static
62  PUBLIC -DJXL_THREADS_STATIC_DEFINE)
63
64
65### Public shared library.
66if (((NOT DEFINED "${TARGET_SUPPORTS_SHARED_LIBS}") OR
67     TARGET_SUPPORTS_SHARED_LIBS) AND NOT JPEGXL_STATIC AND BUILD_SHARED_LIBS)
68add_library(jxl_threads SHARED ${JPEGXL_THREADS_SOURCES})
69_set_jxl_threads(jxl_threads)
70
71set_target_properties(jxl_threads PROPERTIES
72  VERSION ${JPEGXL_LIBRARY_VERSION}
73  SOVERSION ${JPEGXL_LIBRARY_SOVERSION}
74  LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
75  RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
76
77  set_target_properties(jxl_threads PROPERTIES
78      LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/jxl/jxl.version)
79  if(APPLE)
80  set_property(TARGET ${target} APPEND_STRING PROPERTY
81      LINK_FLAGS "-Wl,-exported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/jxl/jxl_osx.syms")
82  elseif(WIN32)
83    # Nothing needed here, we use __declspec(dllexport) (jxl_threads_export.h)
84  else()
85  set_property(TARGET jxl_threads APPEND_STRING PROPERTY
86      LINK_FLAGS " -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/jxl/jxl.version")
87  endif()  # APPLE
88
89# Compile the shared library such that the JXL_THREADS_EXPORT symbols are
90# exported. Users of the library will not set this flag and therefore import
91# those symbols.
92target_compile_definitions(jxl_threads
93  PRIVATE -DJXL_THREADS_INTERNAL_LIBRARY_BUILD)
94
95# Generate the jxl/jxl_threads_export.h header, we only need to generate it once
96# but we can use it from both libraries.
97generate_export_header(jxl_threads
98  BASE_NAME JXL_THREADS
99  EXPORT_FILE_NAME include/jxl/jxl_threads_export.h)
100else()
101add_library(jxl_threads ALIAS jxl_threads-static)
102# When not building the shared library generate the jxl_threads_export.h header
103# only based on the static target.
104generate_export_header(jxl_threads-static
105  BASE_NAME JXL_THREADS
106  EXPORT_FILE_NAME include/jxl/jxl_threads_export.h)
107endif()  # TARGET_SUPPORTS_SHARED_LIBS AND NOT JPEGXL_STATIC AND
108         # BUILD_SHARED_LIBS
109
110
111### Add a pkg-config file for libjxl_threads.
112set(JPEGXL_THREADS_LIBRARY_REQUIRES "")
113configure_file("${CMAKE_CURRENT_SOURCE_DIR}/threads/libjxl_threads.pc.in"
114               "libjxl_threads.pc" @ONLY)
115install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libjxl_threads.pc"
116  DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
117