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