1# 3.1 is OK for most parts. However: 2# 3.3 is needed in src/libFLAC 3# 3.5 is needed in src/libFLAC/ia32 4# 3.9 is needed in 'doc' because of doxygen_add_docs() 5cmake_minimum_required(VERSION 3.5) 6 7if(NOT (CMAKE_BUILD_TYPE OR CMAKE_CONFIGURATION_TYPES OR DEFINED ENV{CFLAGS} OR DEFINED ENV{CXXFLAGS})) 8 set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo") 9endif() 10 11project(FLAC VERSION 1.4.2) # HOMEPAGE_URL "https://www.xiph.org/flac/") 12 13list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") 14 15option(BUILD_CXXLIBS "Build libFLAC++" ON) 16option(BUILD_PROGRAMS "Build and install programs" ON) 17option(BUILD_EXAMPLES "Build and install examples" ON) 18option(BUILD_TESTING "Build tests" ON) 19option(BUILD_DOCS "Build and install doxygen documents" ON) 20option(WITH_FORTIFY_SOURCE "Enable protection against buffer overflows" ON) 21option(WITH_STACK_PROTECTOR "Enable GNU GCC stack smash protection" ON) 22option(INSTALL_MANPAGES "Install MAN pages" ON) 23option(INSTALL_PKGCONFIG_MODULES "Install PkgConfig modules" ON) 24option(INSTALL_CMAKE_CONFIG_MODULE "Install CMake package-config module" ON) 25option(WITH_OGG "ogg support (default: test for libogg)" ON) 26option(BUILD_SHARED_LIBS "Build shared instead of static libraries" OFF) 27 28set(VERSION ${PROJECT_VERSION}) 29 30if(NOT UNIX) 31 # This is to make sure testing works when building with a DLL 32 set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/objs) 33 set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/objs) 34endif() 35 36if(WITH_OGG) 37 if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/ogg") 38 add_subdirectory("ogg") 39 set(OGG_FOUND 1 CACHE INTERNAL "ogg has been added as subdirectory") 40 set_target_properties(ogg PROPERTIES FOLDER Libraries) 41 if(BUILD_TESTING) 42 set_target_properties(test_bitwise test_framing PROPERTIES FOLDER Tests) 43 endif() 44 else() 45 if(NOT TARGET Ogg::ogg) 46 find_package(Ogg REQUIRED) 47 else() 48 set(OGG_FOUND 1 CACHE INTERNAL "ogg has already been built") 49 endif() 50 set(OGG_PACKAGE "ogg") 51 endif() 52endif() 53 54find_program (HAVE_GIT git) 55 56if(HAVE_GIT) 57 execute_process( 58 COMMAND git --git-dir=.git describe --tags --exact-match 59 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 60 OUTPUT_VARIABLE GIT_COMMIT_TAG 61 OUTPUT_STRIP_TRAILING_WHITESPACE 62 ERROR_QUIET 63 ) 64 execute_process( 65 COMMAND git --git-dir=.git log -1 --pretty=format:%h 66 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 67 OUTPUT_VARIABLE GIT_COMMIT_HASH 68 OUTPUT_STRIP_TRAILING_WHITESPACE 69 ERROR_QUIET 70 ) 71 execute_process( 72 COMMAND git --git-dir=.git log -1 --pretty=format:%cd --date=format:%Y%m%d 73 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 74 OUTPUT_VARIABLE GIT_COMMIT_DATE 75 OUTPUT_STRIP_TRAILING_WHITESPACE 76 ERROR_QUIET 77 ) 78endif() 79 80if(NOT WIN32) 81 find_package(Iconv) 82 set(HAVE_ICONV ${Iconv_FOUND}) 83endif() 84 85if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang") 86 set(CMAKE_C_FLAGS "-Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes -Waggregate-return -Wcast-align -Wnested-externs -Wshadow -Wundef -Wmissing-declarations -Winline ${CMAKE_C_FLAGS}") 87 set(CMAKE_C_FLAGS_RELEASE "-DNDEBUG ${CMAKE_C_FLAGS_RELEASE}") 88endif() 89if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") 90 set(CMAKE_CXX_FLAGS "-Wall -Wextra -Wcast-align -Wshadow -Wwrite-strings -Wctor-dtor-privacy -Wnon-virtual-dtor -Wreorder -Wsign-promo -Wundef ${CMAKE_CXX_FLAGS}") 91 set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG ${CMAKE_CXX_FLAGS_RELEASE}") 92endif() 93if(MSVC) 94 set(CMAKE_C_FLAGS_RELEASE "/O2 /Ob2 /Oi /Ot /Oy /DNDEBUG ${CMAKE_C_FLAGS_RELEASE}") 95endif() 96 97include(CMakePackageConfigHelpers) 98include(CheckCCompilerFlag) 99include(CheckCXXCompilerFlag) 100include(CheckSymbolExists) 101include(CheckFunctionExists) 102include(CheckIncludeFile) 103include(CheckCSourceCompiles) 104include(CheckCXXSourceCompiles) 105include(CheckLibraryExists) 106include(GNUInstallDirs) 107include(UseSystemExtensions) 108include(TestBigEndian) 109enable_testing() 110 111check_include_file("byteswap.h" HAVE_BYTESWAP_H) 112check_include_file("inttypes.h" HAVE_INTTYPES_H) 113check_include_file("stdint.h" HAVE_STDINT_H) 114check_include_file("stdbool.h" HAVE_STDBOOL_H) 115check_include_file("arm_neon.h" FLAC__HAS_NEONINTRIN) 116 117if(NOT HAVE_STDINT_H OR NOT HAVE_STDBOOL_H) 118 message(SEND_ERROR "Header stdint.h and/or stdbool.h not found") 119endif() 120 121if(MSVC) 122 check_include_file("intrin.h" FLAC__HAS_X86INTRIN) 123else() 124 check_include_file("x86intrin.h" FLAC__HAS_X86INTRIN) 125endif() 126 127check_function_exists(fseeko HAVE_FSEEKO) 128 129check_c_source_compiles("int main() { return __builtin_bswap16 (0) ; }" HAVE_BSWAP16) 130check_c_source_compiles("int main() { return __builtin_bswap32 (0) ; }" HAVE_BSWAP32) 131check_c_source_compiles(" 132 #include <langinfo.h> 133 int main() 134 { 135 char* cs = nl_langinfo(CODESET); 136 return !cs; 137 }" 138 HAVE_LANGINFO_CODESET) 139 140test_big_endian(CPU_IS_BIG_ENDIAN) 141 142check_c_compiler_flag(-Werror HAVE_WERROR_FLAG) 143check_c_compiler_flag(-Wdeclaration-after-statement HAVE_DECL_AFTER_STMT_FLAG) 144check_c_compiler_flag(-mstackrealign HAVE_STACKREALIGN_FLAG) 145check_cxx_compiler_flag(-Weffc++ HAVE_WEFFCXX_FLAG) 146 147if(MINGW AND (WITH_FORTIFY_SOURCE OR WITH_STACK_PROTECTOR)) 148 check_library_exists("ssp.a" __stack_chk_fail "" HAVE_LIBSSP) 149 if(NOT HAVE_LIBSSP) 150 message(WARNING "Could not find libssp in MinGW, stack protection and/or FORTIFY_SOURCE are unavailable") 151 else() 152 link_libraries("ssp.a") 153 endif() 154elseif(NOT MSVC) 155 set(HAVE_LIBSSP 1) 156endif() 157 158if(WITH_STACK_PROTECTOR) 159 if(NOT MSVC) 160 check_c_compiler_flag("-fstack-protector-strong" HAVE_STACK_PROTECTOR_FLAG) 161 endif() 162endif() 163 164if(HAVE_WERROR_FLAG) 165 option(ENABLE_WERROR "Enable -Werror in all Makefiles" OFF) 166endif() 167 168add_compile_options( 169 $<$<BOOL:${MSVC}>:/wd4267> 170 $<$<BOOL:${MSVC}>:/wd4996> 171 $<$<BOOL:${ENABLE_WERROR}>:-Werror> 172 $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<BOOL:${HAVE_WEFFCXX_FLAG}>>:-Weffc++> 173 $<$<AND:$<COMPILE_LANGUAGE:C>,$<BOOL:${HAVE_DECL_AFTER_STMT_FLAG}>>:-Wdeclaration-after-statement>) 174 175if(WITH_FORTIFY_SOURCE AND HAVE_LIBSSP) 176 add_definitions(-D_FORTIFY_SOURCE=2) 177endif() 178 179if(HAVE_STACK_PROTECTOR_FLAG AND HAVE_LIBSSP) 180 add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-fstack-protector-strong>) 181endif() 182 183if(CMAKE_SYSTEM_PROCESSOR STREQUAL "i686" AND HAVE_STACKREALIGN_FLAG) 184 add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-mstackrealign>) 185endif() 186 187include_directories("include") 188 189include_directories("${CMAKE_CURRENT_BINARY_DIR}") 190add_definitions(-DHAVE_CONFIG_H) 191 192if(MSVC) 193 add_definitions( 194 -D_CRT_SECURE_NO_WARNINGS 195 -D_USE_MATH_DEFINES) 196endif() 197if(CMAKE_BUILD_TYPE STREQUAL Debug OR CMAKE_BUILD_TYPE STREQUAL RelWithDebInfo) 198 add_definitions(-DFLAC__OVERFLOW_DETECT) 199endif() 200 201add_subdirectory("src") 202add_subdirectory("microbench") 203if(BUILD_DOCS) 204 add_subdirectory("doc") 205endif() 206if(BUILD_EXAMPLES) 207 add_subdirectory("examples") 208endif() 209if(BUILD_TESTING) 210 add_subdirectory("test") 211endif() 212 213# The following folder layout is mostly for MSVC 214set_property(GLOBAL PROPERTY USE_FOLDERS ON) 215 216set_target_properties(FLAC grabbag getopt replaygain_analysis replaygain_synthesis utf8 PROPERTIES FOLDER Libraries) 217if(BUILD_CXXLIBS) 218 set_target_properties(FLAC++ PROPERTIES FOLDER Libraries) 219endif() 220if(BUILD_PROGRAMS) 221 set_target_properties(flacapp metaflac PROPERTIES FOLDER Programs) 222endif() 223if(BUILD_TESTING) 224 set_target_properties(test_libFLAC test_libs_common test_picture test_seeking test_streams test_cuesheet PROPERTIES FOLDER Tests) 225 if(BUILD_CXXLIBS) 226 set_target_properties(test_libFLAC++ PROPERTIES FOLDER Tests) 227 endif() 228endif() 229if(BUILD_EXAMPLES) 230 set_target_properties(decode_file encode_file PROPERTIES FOLDER Examples) 231 if(BUILD_CXXLIBS) 232 set_target_properties(decode_file_cxx encode_file_cxx PROPERTIES FOLDER Examples) 233 endif() 234endif() 235if(BUILD_UTILS) 236 set_target_properties(flacdiff flactimer PROPERTIES FOLDER Utils) 237endif() 238 239configure_file(config.cmake.h.in config.h) 240 241if(INSTALL_CMAKE_CONFIG_MODULE) 242 install( 243 EXPORT targets 244 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" 245 NAMESPACE FLAC::) 246 export(EXPORT targets NAMESPACE FLAC:: FILE FLACTargets.cmake) 247 248 configure_package_config_file( 249 ${PROJECT_SOURCE_DIR}/flac-config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/flac-config.cmake 250 INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") 251 write_basic_package_version_file( 252 ${CMAKE_CURRENT_BINARY_DIR}/flac-config-version.cmake COMPATIBILITY AnyNewerVersion) 253 254 install( 255 FILES ${CMAKE_CURRENT_BINARY_DIR}/flac-config.cmake ${CMAKE_CURRENT_BINARY_DIR}/flac-config-version.cmake 256 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" 257 ) 258 259 install( 260 FILES 261 "${CMAKE_CURRENT_BINARY_DIR}/flac-config.cmake" 262 "${CMAKE_CURRENT_BINARY_DIR}/flac-config-version.cmake" 263 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") 264endif() 265 266file(GLOB FLAC_HEADERS "include/FLAC/*.h") 267file(GLOB FLAC++_HEADERS "include/FLAC++/*.h") 268install(FILES ${FLAC_HEADERS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/FLAC") 269install(FILES ${FLAC++_HEADERS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/FLAC++") 270if(INSTALL_MANPAGES) 271 find_program (HAVE_PANDOC pandoc) 272 if(HAVE_PANDOC) 273 file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/man") 274 add_custom_command( 275 OUTPUT man/flac.1 276 COMMAND pandoc --standalone --to man "${CMAKE_SOURCE_DIR}/man/flac.md" > man/flac.1 277 ) 278 add_custom_command( 279 OUTPUT man/metaflac.1 280 COMMAND pandoc --standalone --to man "${CMAKE_SOURCE_DIR}/man/metaflac.md" > man/metaflac.1 281 ) 282 add_custom_target(man ALL DEPENDS man/flac.1 man/metaflac.1) 283 install(FILES "${CMAKE_BINARY_DIR}/man/flac.1" "${CMAKE_BINARY_DIR}/man/metaflac.1" DESTINATION "${CMAKE_INSTALL_MANDIR}/man1") 284 else() 285 if(EXISTS "${CMAKE_SOURCE_DIR}/man/flac.1" AND EXISTS "${CMAKE_SOURCE_DIR}/man/metaflac.1") 286 install(FILES "man/flac.1" "man/metaflac.1" DESTINATION "${CMAKE_INSTALL_MANDIR}/man1") 287 else() 288 message(SEND_ERROR "Pandoc nor prebuild manpages are found. Cannot install manpages. Set INSTALL_MANPAGES to OFF to build without man pages") 289 endif() 290 endif() 291endif() 292