xref: /aosp_15_r20/external/zstd/build/cmake/programs/CMakeLists.txt (revision 01826a4963a0d8a59bc3812d29bdf0fb76416722)
1# ################################################################
2# Copyright (c) Meta Platforms, Inc. and affiliates.
3# All rights reserved.
4#
5# This source code is licensed under both the BSD-style license (found in the
6# LICENSE file in the root directory of this source tree) and the GPLv2 (found
7# in the COPYING file in the root directory of this source tree).
8# ################################################################
9
10project(programs C)
11
12set(CMAKE_INCLUDE_CURRENT_DIR TRUE)
13
14# Define programs directory, where sources and header files are located
15set(LIBRARY_DIR ${ZSTD_SOURCE_DIR}/lib)
16set(PROGRAMS_DIR ${ZSTD_SOURCE_DIR}/programs)
17include_directories(${PROGRAMS_DIR} ${LIBRARY_DIR} ${LIBRARY_DIR}/common ${LIBRARY_DIR}/compress ${LIBRARY_DIR}/dictBuilder)
18
19if (ZSTD_LEGACY_SUPPORT)
20    set(PROGRAMS_LEGACY_DIR ${PROGRAMS_DIR}/legacy)
21    include_directories(${PROGRAMS_LEGACY_DIR} ${LIBRARY_DIR}/legacy)
22endif ()
23
24if (ZSTD_PROGRAMS_LINK_SHARED)
25    set(PROGRAMS_ZSTD_LINK_TARGET libzstd_shared)
26else ()
27    set(PROGRAMS_ZSTD_LINK_TARGET libzstd_static)
28endif ()
29
30if (MSVC)
31    set(MSVC_RESOURCE_DIR ${ZSTD_SOURCE_DIR}/build/VS2010/zstd)
32    set(PlatformDependResources ${MSVC_RESOURCE_DIR}/zstd.rc)
33endif ()
34
35file(GLOB ZSTD_PROGRAM_SRCS "${PROGRAMS_DIR}/*.c")
36if (MSVC AND ZSTD_PROGRAMS_LINK_SHARED)
37    list(APPEND ZSTD_PROGRAM_SRCS ${LIBRARY_DIR}/common/pool.c ${LIBRARY_DIR}/common/threading.c)
38endif ()
39
40add_executable(zstd ${ZSTD_PROGRAM_SRCS})
41target_link_libraries(zstd ${PROGRAMS_ZSTD_LINK_TARGET})
42if (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
43    target_link_libraries(zstd rt)
44endif ()
45install(TARGETS zstd
46  RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
47  BUNDLE DESTINATION "${CMAKE_INSTALL_BINDIR}")
48
49if (UNIX)
50    add_custom_target(zstdcat ALL ${CMAKE_COMMAND} -E create_symlink zstd zstdcat DEPENDS zstd COMMENT "Creating zstdcat symlink")
51    add_custom_target(unzstd ALL ${CMAKE_COMMAND} -E create_symlink zstd unzstd DEPENDS zstd COMMENT "Creating unzstd symlink")
52    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/zstdcat DESTINATION "${CMAKE_INSTALL_BINDIR}")
53    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/unzstd DESTINATION "${CMAKE_INSTALL_BINDIR}")
54    install(PROGRAMS ${PROGRAMS_DIR}/zstdgrep DESTINATION "${CMAKE_INSTALL_BINDIR}")
55    install(PROGRAMS ${PROGRAMS_DIR}/zstdless DESTINATION "${CMAKE_INSTALL_BINDIR}")
56
57    add_custom_target(zstd.1 ALL
58        ${CMAKE_COMMAND} -E copy ${PROGRAMS_DIR}/zstd.1 .
59        COMMENT "Copying manpage zstd.1")
60    add_custom_target(zstdgrep.1 ALL
61        ${CMAKE_COMMAND} -E copy ${PROGRAMS_DIR}/zstdgrep.1 .
62        COMMENT "Copying manpage zstdgrep.1")
63    add_custom_target(zstdless.1 ALL
64        ${CMAKE_COMMAND} -E copy ${PROGRAMS_DIR}/zstdless.1 .
65        COMMENT "Copying manpage zstdless.1")
66    add_custom_target(zstdcat.1 ALL ${CMAKE_COMMAND} -E create_symlink zstd.1 zstdcat.1 DEPENDS zstd.1 COMMENT "Creating zstdcat.1 symlink")
67    add_custom_target(unzstd.1 ALL ${CMAKE_COMMAND} -E create_symlink zstd.1 unzstd.1 DEPENDS zstd.1 COMMENT "Creating unzstd.1 symlink")
68
69    # Define MAN_INSTALL_DIR if necessary
70    if (MAN_INSTALL_DIR)
71    else ()
72        set(MAN_INSTALL_DIR ${CMAKE_INSTALL_MANDIR}/man1)
73    endif ()
74
75    install(FILES
76        ${CMAKE_CURRENT_BINARY_DIR}/zstd.1
77        ${CMAKE_CURRENT_BINARY_DIR}/zstdcat.1
78        ${CMAKE_CURRENT_BINARY_DIR}/unzstd.1
79        ${CMAKE_CURRENT_BINARY_DIR}/zstdgrep.1
80        ${CMAKE_CURRENT_BINARY_DIR}/zstdless.1
81        DESTINATION "${MAN_INSTALL_DIR}")
82
83    add_executable(zstd-frugal ${PROGRAMS_DIR}/zstdcli.c
84        ${PROGRAMS_DIR}/util.c ${PROGRAMS_DIR}/timefn.c
85        ${PROGRAMS_DIR}/fileio.c ${PROGRAMS_DIR}/fileio_asyncio.c)
86    target_link_libraries(zstd-frugal ${PROGRAMS_ZSTD_LINK_TARGET})
87    set_property(TARGET zstd-frugal APPEND PROPERTY COMPILE_DEFINITIONS "ZSTD_NOBENCH;ZSTD_NODICT;ZSTD_NOTRACE")
88endif ()
89
90# Add multi-threading support definitions
91
92if (ZSTD_MULTITHREAD_SUPPORT)
93    set_property(TARGET zstd APPEND PROPERTY COMPILE_DEFINITIONS "ZSTD_MULTITHREAD")
94
95    if (UNIX)
96        target_link_libraries(zstd ${THREADS_LIBS})
97
98        add_custom_target(zstdmt ALL ${CMAKE_COMMAND} -E create_symlink zstd zstdmt DEPENDS zstd COMMENT "Creating zstdmt symlink")
99        install(FILES ${CMAKE_CURRENT_BINARY_DIR}/zstdmt DESTINATION "${CMAKE_INSTALL_BINDIR}")
100    endif ()
101endif ()
102
103option(ZSTD_ZLIB_SUPPORT "ZLIB SUPPORT" OFF)
104option(ZSTD_LZMA_SUPPORT "LZMA SUPPORT" OFF)
105option(ZSTD_LZ4_SUPPORT "LZ4 SUPPORT" OFF)
106
107# Add gzip support
108if (ZSTD_ZLIB_SUPPORT)
109    find_package(ZLIB REQUIRED)
110
111    if (ZLIB_FOUND)
112        include_directories(${ZLIB_INCLUDE_DIRS})
113        target_link_libraries(zstd ${ZLIB_LIBRARIES})
114        set_property(TARGET zstd APPEND PROPERTY COMPILE_DEFINITIONS "ZSTD_GZCOMPRESS;ZSTD_GZDECOMPRESS")
115    else ()
116        message(SEND_ERROR "zlib library is missing")
117    endif ()
118endif ()
119
120# Add lzma support
121if (ZSTD_LZMA_SUPPORT)
122    find_package(LibLZMA REQUIRED)
123
124    if (LIBLZMA_FOUND)
125        include_directories(${LIBLZMA_INCLUDE_DIRS})
126        target_link_libraries(zstd ${LIBLZMA_LIBRARIES})
127        set_property(TARGET zstd APPEND PROPERTY COMPILE_DEFINITIONS "ZSTD_LZMACOMPRESS;ZSTD_LZMADECOMPRESS")
128    else ()
129        message(SEND_ERROR "lzma library is missing")
130    endif ()
131endif ()
132
133# Add lz4 support
134if (ZSTD_LZ4_SUPPORT)
135    find_package(LibLZ4 REQUIRED)
136
137    if (LIBLZ4_FOUND)
138        include_directories(${LIBLZ4_INCLUDE_DIRS})
139        target_link_libraries(zstd ${LIBLZ4_LIBRARIES})
140        set_property(TARGET zstd APPEND PROPERTY COMPILE_DEFINITIONS "ZSTD_LZ4COMPRESS;ZSTD_LZ4DECOMPRESS")
141    else ()
142        message(SEND_ERROR "lz4 library is missing")
143    endif ()
144endif ()
145