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