CMake: Handier way to find Iconv
[flac.git] / CMakeLists.txt
blob560bb403682d02e6fddf5b49600e7ff50c9c87dd
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_STACK_PROTECTOR "Enable GNU GCC stack smash protection" ON)
19 option(WITH_OGG "ogg support (default: test for libogg)" ON)
21 if(WITH_OGG)
22     find_package(OGG REQUIRED)
23 endif()
25 find_package(Iconv)
26 set(HAVE_ICONV ${Iconv_FOUND})
28 if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
29     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes -Waggregate-return -Wcast-align -Wnested-externs -Wshadow -Wundef -Wmissing-declarations -Winline")
30     set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -funroll-loops")
31 endif()
32 if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
33     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wcast-align -Wshadow -Wwrite-strings -Wctor-dtor-privacy -Wnon-virtual-dtor -Wreorder -Wsign-promo -Wundef")
34 endif()
35 if(CMAKE_C_COMPILER_ID MATCHES "GNU")
36     set(CMAKE_EXE_LINKER_FLAGS -no-pie)
37 endif()
39 include(CMakePackageConfigHelpers)
40 include(CPack)
41 include(CTest)
42 include(CheckCCompilerFlag)
43 include(CheckCXXCompilerFlag)
44 include(CheckSymbolExists)
45 include(CheckFunctionExists)
46 include(CheckIncludeFile)
47 include(CheckCSourceCompiles)
48 include(CheckCXXSourceCompiles)
49 include(GNUInstallDirs)
50 include(UseSystemExtensions)
51 include(TestBigEndian)
53 check_include_file("byteswap.h" HAVE_BYTESWAP_H)
54 check_include_file("inttypes.h" HAVE_INTTYPES_H)
55 check_include_file("stdint.h" HAVE_STDINT_H)
56 check_include_file("x86intrin.h" FLAC__HAS_X86INTRIN)
58 check_function_exists(fseeko HAVE_FSEEKO)
60 check_c_source_compiles("int main() { return __builtin_bswap16 (0) ; }" HAVE_BSWAP16)
61 check_c_source_compiles("int main() { return __builtin_bswap32 (0) ; }" HAVE_BSWAP32)
63 test_big_endian(CPU_IS_BIG_ENDIAN)
65 check_c_compiler_flag(-Werror HAVE_WERROR_FLAG)
66 check_c_compiler_flag(-Wdeclaration-after-statement HAVE_DECL_AFTER_STMT_FLAG)
67 check_c_compiler_flag(-mstackrealign HAVE_STACKREALIGN_FLAG)
68 check_cxx_compiler_flag(-Weffc++ HAVE_WEFFCXX_FLAG)
70 if(WITH_STACK_PROTECTOR)
71   if(NOT MSVC)
72     check_c_compiler_flag("-fstack-protector-strong" HAVE_STACK_PROTECTOR_FLAG)
73   endif()
74 endif()
76 if(HAVE_WERROR_FLAG)
77     option(ENABLE_WERROR "Enable -Werror in all Makefiles" OFF)
78 endif()
80 add_compile_options(
81     $<$<BOOL:${MSVC}>:/wd4267>
82     $<$<BOOL:${MSVC}>:/wd4996>
83     $<$<BOOL:${ENABLE_WERROR}>:-Werror>
84     $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<BOOL:${HAVE_WEFFCXX_FLAG}>>:-Weffc++>
85     $<$<AND:$<COMPILE_LANGUAGE:C>,$<BOOL:${HAVE_DECL_AFTER_STMT_FLAG}>>:-Wdeclaration-after-statement>)
87 if(HAVE_STACK_PROTECTOR_FLAG)
88     add_compile_options(-fstack-protector-strong)
89 endif()
91 if(CMAKE_SYSTEM_PROCESSOR STREQUAL "i686" AND HAVE_STACKREALIGN_FLAG)
92     add_compile_options(-mstackrealign)
93 endif()
95 include_directories("include")
97 include_directories("${CMAKE_CURRENT_BINARY_DIR}")
98 add_definitions(-DHAVE_CONFIG_H)
100 if(MSVC)
101     add_definitions(
102         -D_CRT_SECURE_NO_WARNINGS
103         -D_USE_MATH_DEFINES)
104 endif()
105 if(CMAKE_BUILD_TYPE STREQUAL Debug OR CMAKE_BUILD_TYPE STREQUAL RelWithDebInfo)
106     add_definitions(-DFLAC__OVERFLOW_DETECT)
107 endif()
109 add_subdirectory("src")
110 add_subdirectory("microbench")
111 if(BUILD_DOCS)
112     add_subdirectory("doc")
113 endif()
114 if(BUILD_EXAMPLES)
115     add_subdirectory("examples")
116 endif()
117 if(BUILD_TESTING)
118     add_subdirectory("test")
119 endif()
121 configure_file(config.cmake.h.in config.h)
123 install(
124     EXPORT targets
125     DESTINATION "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake"
126     NAMESPACE FLAC::)
128 configure_package_config_file(
129     flac-config.cmake.in flac-config.cmake
130     INSTALL_DESTINATION "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake")
131 write_basic_package_version_file(
132     flac-config-version.cmake COMPATIBILITY AnyNewerVersion)
134 install(
135     FILES
136         "${CMAKE_CURRENT_BINARY_DIR}/flac-config.cmake"
137         "${CMAKE_CURRENT_BINARY_DIR}/flac-config-version.cmake"
138         "cmake/FindOGG.cmake"
139     DESTINATION "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake")
141 file(GLOB FLAC_HEADERS "include/FLAC/*.h")
142 file(GLOB FLAC++_HEADERS "include/FLAC++/*.h")
143 install(FILES ${FLAC_HEADERS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/FLAC")
144 install(FILES ${FLAC++_HEADERS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/FLAC++")
145 install(FILES "man/flac.1" "man/metaflac.1" DESTINATION "${CMAKE_INSTALL_MANDIR}")