17d940bddf90f92cc355c7fa91872b6f0b5e16b2
[flac.git] / src / libFLAC / CMakeLists.txt
1 cmake_minimum_required(VERSION 3.12)
2
3 if(CMAKE_SYSTEM_PROCESSOR MATCHES "[xX]86(_64)?|(AMD|amd)64|i[346]86")
4     option(WITH_AVX "Enable AVX, AVX2 optimizations" ON)
5 endif()
6
7 option(WITH_ASM "Use any assembly optimization routines" ON)
8
9 check_include_file("cpuid.h" HAVE_CPUID_H)
10 check_include_file("sys/param.h" HAVE_SYS_PARAM_H)
11
12 set(CMAKE_REQUIRED_LIBRARIES m)
13 check_function_exists(lround HAVE_LROUND)
14
15 add_compile_definitions(
16     $<$<BOOL:${MSVC}>:_USE_MATH_DEFINES>
17     FLAC__USE_AVX=$<BOOL:${WITH_AVX}>
18     HAVE_LROUND=$<BOOL:${HAVE_LROUND}>
19     $<$<BOOL:${HAVE_CPUID_H}>:HAVE_CPUID_H>
20     $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:FLAC__OVERFLOW_DETECT>)
21
22 if(CMAKE_SYSTEM_PROCESSOR MATCHES "[xX]86_64|(AMD|amd)64")
23     if(CMAKE_SIZEOF_VOID_P EQUAL 4)
24         set(IA32 TRUE)
25     endif()
26     add_compile_definitions(
27         FLAC__CPU_X86_64
28         FLAC__ALIGN_MALLOC_DATA)
29 elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "([xX]|i[346])86")
30     set(IA32 TRUE)
31     add_compile_definitions(
32         FLAC__CPU_IA32
33         FLAC__ALIGN_MALLOC_DATA)
34 endif()
35
36 include(CheckLanguage)
37 check_language(ASM_NASM)
38 if(CMAKE_ASM_NASM_COMPILER)
39     enable_language(ASM_NASM)
40     add_compile_definitions(FLAC__HAS_NASM)
41 endif()
42
43 if(NOT WITH_ASM)
44     add_compile_definitions(FLAC__NO_ASM)
45 endif()
46
47 if(IA32)
48     if(WITH_ASM AND CMAKE_ASM_NASM_COMPILER)
49         add_subdirectory(ia32)
50     endif()
51
52     option(WITH_SSE "Enable SSE2 optimizations" ON)
53     check_c_compiler_flag(-msse2 HAVE_MSSE2_FLAG)
54     if(WITH_SSE)
55         add_compile_options(
56             $<$<BOOL:${HAVE_MSSE2_FLAG}>:-msse2>
57             $<$<BOOL:${MSVC}>:/arch:SSE2>)
58     endif()
59 endif()
60
61
62 set(prefix "${CMAKE_INSTALL_PREFIX}")
63 set(exec_prefix "${CMAKE_INSTALL_PREFIX}")
64 set(libdir "${CMAKE_INSTALL_FULL_LIBDIR}")
65 set(includedir "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
66 configure_file(flac.pc.in flac.pc @ONLY)
67
68 set(SOURCES
69     bitmath.c
70     bitreader.c
71     bitwriter.c
72     cpu.c
73     crc.c
74     fixed.c
75     fixed_intrin_sse2.c
76     fixed_intrin_ssse3.c
77     float.c
78     format.c
79     lpc.c
80     lpc_intrin_sse.c
81     lpc_intrin_sse2.c
82     lpc_intrin_sse41.c
83     lpc_intrin_avx2.c
84     lpc_intrin_vsx.c
85     md5.c
86     memory.c
87     metadata_iterators.c
88     metadata_object.c
89     stream_decoder.c
90     stream_encoder.c
91     stream_encoder_intrin_sse2.c
92     stream_encoder_intrin_ssse3.c
93     stream_encoder_intrin_avx2.c
94     stream_encoder_framing.c
95     window.c
96     $<$<BOOL:${OGG_FOUND}>:ogg_decoder_aspect.c>
97     $<$<BOOL:${OGG_FOUND}>:ogg_encoder_aspect.c>
98     $<$<BOOL:${OGG_FOUND}>:ogg_helper.c>
99     $<$<BOOL:${OGG_FOUND}>:ogg_mapping.c>
100     $<$<BOOL:${WIN32}>:windows_unicode_filenames.c>)
101
102 include_directories("include")
103
104 add_library(FLAC-static STATIC ${SOURCES})
105 target_compile_definitions(FLAC-static PUBLIC FLAC__NO_DLL)
106 target_include_directories(FLAC-static INTERFACE
107     "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
108     "$<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>")
109 target_link_libraries(FLAC-static PUBLIC
110     $<TARGET_NAME_IF_EXISTS:win_utf8_io>
111     $<TARGET_NAME_IF_EXISTS:FLAC-asm>
112     $<$<BOOL:${HAVE_LROUND}>:m>)
113 if(TARGET Ogg::Ogg-static)
114     target_link_libraries(FLAC-static PUBLIC Ogg::Ogg-static)
115 elseif(TARGET Ogg::Ogg)
116     target_link_libraries(FLAC-static PUBLIC Ogg::Ogg)
117 endif()
118
119 add_library(FLAC SHARED ${SOURCES})
120 target_compile_definitions(FLAC PRIVATE FLAC_API_EXPORTS FLAC__USE_VISIBILITY_ATTR)
121 target_include_directories(FLAC INTERFACE
122     "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
123     "$<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>")
124 target_link_libraries(FLAC PRIVATE
125     $<TARGET_NAME_IF_EXISTS:Ogg::Ogg>
126     $<TARGET_NAME_IF_EXISTS:FLAC-asm>
127     $<TARGET_NAME_IF_EXISTS:win_utf8_io>
128     $<$<BOOL:${HAVE_LROUND}>:m>)
129 set_target_properties(FLAC PROPERTIES
130     C_VISIBILITY_PRESET hidden
131     VERSION 8.3.0
132     SOVERSION 8)
133
134 install(TARGETS FLAC FLAC-static EXPORT targets
135     ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}/"
136     LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}/"
137     RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}/")
138 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/flac.pc"
139     DESTINATION "${CMAKE_INSTALL_DATADIR}/pkgconfig")