38f6132a0f317108e525aab3bf74f7815e935d6f
[flac.git] / CMakeLists.txt
1 cmake_minimum_required(VERSION 3.12)
2
3 project(FLAC VERSION 1.3.2 HOMEPAGE_URL "https://www.xiph.org/flac/")
4
5 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
6
7 option(BUILD_CXXLIBS "Build libFLAC++" ON)
8 option(BUILD_EXAMPLES "Build and install examples" ON)
9 option(WITH_OGG "ogg support (default: test for libogg)" ON)
10
11 if(WITH_OGG)
12     find_package(OGG REQUIRED)
13 endif()
14
15 if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
16     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes -Waggregate-return -Wcast-align -Wnested-externs -Wshadow -Wundef -Wmissing-declarations -Winline")
17
18     if(CMAKE_BULD_TYPE STREQUAL Release)
19         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -funroll-loops")
20     endif()
21
22     option(ENABLE_SSP "Enable GNU GCC stack smash protection" OFF)
23 endif()
24 if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
25     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wcast-align -Wshadow -Wwrite-strings -Wctor-dtor-privacy -Wnon-virtual-dtor -Wreorder -Wsign-promo -Wundef")
26 endif()
27 if(CMAKE_C_COMPILER_ID MATCHES "GNU")
28     set(CMAKE_EXE_LINKER_FLAGS -no-pie)
29 endif()
30
31 include(CMakePackageConfigHelpers)
32 include(CPack)
33 include(CTest)
34 include(CheckCCompilerFlag)
35 include(CheckCXXCompilerFlag)
36 include(CheckSymbolExists)
37 include(CheckFunctionExists)
38 include(CheckIncludeFile)
39 include(CheckCSourceCompiles)
40 include(CheckCXXSourceCompiles)
41 include(GNUInstallDirs)
42 include(UseSystemExtensions)
43 include(TestBigEndian)
44
45 check_include_file("byteswap.h" HAVE_BYTESWAP_H)
46 check_include_file("inttypes.h" HAVE_INTTYPES_H)
47 check_include_file("stdint.h" HAVE_STDINT_H)
48 check_include_file("x86intrin.h" HAVE_X86INTRIN_H)
49
50 check_function_exists(fseeko HAVE_FSEEKO)
51
52 check_c_source_compiles("int main() { return __builtin_bswap16 (0) ; }" HAVE_BSWAP16)
53 check_c_source_compiles("int main() { return __builtin_bswap32 (0) ; }" HAVE_BSWAP32)
54
55 test_big_endian(CPU_IS_BIG_ENDIAN)
56
57 check_c_compiler_flag(-Werror HAVE_WERROR_FLAG)
58 check_c_compiler_flag(-Wdeclaration-after-statement HAVE_DECL_AFTER_STMT_FLAG)
59 check_c_compiler_flag("-fstack-protector --param ssp-buffer-size=4" HAVE_SSP_FLAG)
60 check_c_compiler_flag(-mstackrealign HAVE_STACKREALIGN_FLAG)
61 check_cxx_compiler_flag(-Weffc++ HAVE_WEFFCXX_FLAG)
62
63 if(HAVE_WERROR_FLAG)
64     option(ENABLE_WERROR "Enable -Werror in all Makefiles" OFF)
65 endif()
66
67 add_compile_definitions(
68     PACKAGE_VERSION="${PROJECT_VERSION}"
69     $<$<BOOL:${HAVE_BYTESWAP_H}>:HAVE_BYTESWAP_H>
70     $<$<BOOL:${HAVE_INTTYPES_H}>:HAVE_INTTYPES_H>
71     $<$<BOOL:${HAVE_STDINT_H}>:HAVE_STDINT_H>
72     $<$<BOOL:${HAVE_FSEEKO}>:HAVE_FSEEKO>
73     $<$<BOOL:${MSVC}>:_CRT_SECURE_NO_WARNINGS>
74     CPU_IS_BIG_ENDIAN=$<BOOL:${CPU_IS_BIG_ENDIAN}>
75     CPU_IS_LITTLE_ENDIAN=$<NOT:$<BOOL:${CPU_IS_BIG_ENDIAN}>>
76     FLAC__HAS_X86INTRIN=$<BOOL:${HAVE_X86INTRIN_H}>
77     HAVE_BSWAP16=$<BOOL:${HAVE_BSWAP16}>
78     HAVE_BSWAP32=$<BOOL:${HAVE_BSWAP32}>)
79
80 add_compile_options(
81     $<$<BOOL:${MSVC}>:/wd4267>
82     $<$<BOOL:${MSVC}>:/wd4996>
83     $<$<BOOL:${ENABLE_WERROR}>:-Werror>
84     $<$<AND:$<BOOL:${HAVE_SSP_FLAG}>,$<BOOL:${ENABLE_SSP}>>:-fstack-protector>
85     $<$<AND:$<BOOL:${HAVE_SSP_FLAG}>,$<BOOL:${ENABLE_SSP}>>:--param>
86     $<$<AND:$<BOOL:${HAVE_SSP_FLAG}>,$<BOOL:${ENABLE_SSP}>>:ssp-buffer-size=4>
87     $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<BOOL:${HAVE_WEFFCXX_FLAG}>>:-Weffc++>
88     $<$<AND:$<COMPILE_LANGUAGE:C>,$<BOOL:${HAVE_DECL_AFTER_STMT_FLAG}>>:-Wdeclaration-after-statement>)
89
90 if(CMAKE_SYSTEM_PROCESSOR STREQUAL "i686" AND HAVE_STACKREALIGN_FLAG)
91     add_compile_options(-mstackrealign)
92 endif()
93
94 include_directories("include")
95
96 add_subdirectory("doc")
97 add_subdirectory("src")
98 add_subdirectory("microbench")
99 if(BUILD_EXAMPLES)
100     add_subdirectory("examples")
101 endif()
102 if(BUILD_TESTING)
103     add_subdirectory("test")
104 endif()
105
106 install(
107     EXPORT targets
108     DESTINATION "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake"
109     NAMESPACE FLAC::)
110
111 configure_package_config_file(
112     flac-config.cmake.in flac-config.cmake
113     INSTALL_DESTINATION "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake")
114 write_basic_package_version_file(
115     flac-config-version.cmake COMPATIBILITY AnyNewerVersion)
116
117 install(
118     FILES
119         "${CMAKE_CURRENT_BINARY_DIR}/flac-config.cmake"
120         "${CMAKE_CURRENT_BINARY_DIR}/flac-config-version.cmake"
121         "cmake/FindOGG.cmake"
122     DESTINATION "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake")
123
124 file(GLOB FLAC_HEADERS "include/FLAC/*.h")
125 file(GLOB FLAC++_HEADERS "include/FLAC++/*.h")
126 install(FILES ${FLAC_HEADERS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/FLAC")
127 install(FILES ${FLAC++_HEADERS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/FLAC++")
128 install(FILES "man/flac.1" "man/metaflac.1" DESTINATION "${CMAKE_INSTALL_MANDIR}")