CMakeLists.txt 4.5 KB
Newer Older
Vitaliy Kirsanov's avatar
Vitaliy Kirsanov committed
1 2 3 4 5 6 7 8
cmake_minimum_required(VERSION 3.12)

project(FLAC VERSION 1.3.2 HOMEPAGE_URL "https://www.xiph.org/flac/")

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

option(BUILD_CXXLIBS "Build libFLAC++" ON)
option(BUILD_EXAMPLES "Build and install examples" ON)
Vitaliy Kirsanov's avatar
Vitaliy Kirsanov committed
9 10 11 12 13
option(WITH_OGG "ogg support (default: test for libogg)" ON)

if(WITH_OGG)
    find_package(OGG REQUIRED)
endif()
Vitaliy Kirsanov's avatar
Vitaliy Kirsanov committed
14 15 16 17 18 19 20 21 22 23

if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes -Waggregate-return -Wcast-align -Wnested-externs -Wshadow -Wundef -Wmissing-declarations -Winline")

    if(CMAKE_BULD_TYPE STREQUAL Release)
        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -funroll-loops")
    endif()

    option(ENABLE_SSP "Enable GNU GCC stack smash protection" OFF)
endif()
Vitaliy Kirsanov's avatar
Vitaliy Kirsanov committed
24 25 26 27 28 29
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wcast-align -Wshadow -Wwrite-strings -Wctor-dtor-privacy -Wnon-virtual-dtor -Wreorder -Wsign-promo -Wundef")
endif()
if(CMAKE_C_COMPILER_ID MATCHES "GNU")
    set(CMAKE_EXE_LINKER_FLAGS -no-pie)
endif()
Vitaliy Kirsanov's avatar
Vitaliy Kirsanov committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

include(CMakePackageConfigHelpers)
include(CPack)
include(CTest)
include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
include(CheckSymbolExists)
include(CheckFunctionExists)
include(CheckIncludeFile)
include(CheckCSourceCompiles)
include(CheckCXXSourceCompiles)
include(GNUInstallDirs)
include(UseSystemExtensions)
include(TestBigEndian)

check_include_file("byteswap.h" HAVE_BYTESWAP_H)
check_include_file("inttypes.h" HAVE_INTTYPES_H)
check_include_file("stdint.h" HAVE_STDINT_H)
check_include_file("x86intrin.h" HAVE_X86INTRIN_H)

check_c_source_compiles("int main() { return __builtin_bswap16 (0) ; }" HAVE_BSWAP16)
check_c_source_compiles("int main() { return __builtin_bswap32 (0) ; }" HAVE_BSWAP32)

test_big_endian(CPU_IS_BIG_ENDIAN)

check_c_compiler_flag(-Werror HAVE_WERROR_FLAG)
check_c_compiler_flag(-Wdeclaration-after-statement HAVE_DECL_AFTER_STMT_FLAG)
check_c_compiler_flag("-fstack-protector --param ssp-buffer-size=4" HAVE_SSP_FLAG)
check_c_compiler_flag(-mstackrealign HAVE_STACKREALIGN_FLAG)
check_cxx_compiler_flag(-Weffc++ HAVE_WEFFCXX_FLAG)

if(HAVE_WERROR_FLAG)
    option(ENABLE_WERROR "Enable -Werror in all Makefiles" OFF)
endif()

add_compile_definitions(
    PACKAGE_VERSION="${PROJECT_VERSION}"
    $<$<BOOL:${HAVE_BYTESWAP_H}>:HAVE_BYTESWAP_H>
    $<$<BOOL:${HAVE_INTTYPES_H}>:HAVE_INTTYPES_H>
    $<$<BOOL:${HAVE_STDINT_H}>:HAVE_STDINT_H>
    $<$<BOOL:${MSVC}>:_CRT_SECURE_NO_WARNINGS>
    CPU_IS_BIG_ENDIAN=$<BOOL:${CPU_IS_BIG_ENDIAN}>
    CPU_IS_LITTLE_ENDIAN=$<NOT:$<BOOL:${CPU_IS_BIG_ENDIAN}>>
    FLAC__HAS_X86INTRIN=$<BOOL:${HAVE_X86INTRIN_H}>
    HAVE_BSWAP16=$<BOOL:${HAVE_BSWAP16}>
    HAVE_BSWAP32=$<BOOL:${HAVE_BSWAP32}>)

add_compile_options(
    $<$<BOOL:${MSVC}>:/wd4267>
    $<$<BOOL:${MSVC}>:/wd4996>
    $<$<BOOL:${ENABLE_WERROR}>:-Werror>
    $<$<AND:$<BOOL:${HAVE_SSP_FLAG}>,$<BOOL:${ENABLE_SSP}>>:-fstack-protector>
    $<$<AND:$<BOOL:${HAVE_SSP_FLAG}>,$<BOOL:${ENABLE_SSP}>>:--param>
83
    $<$<AND:$<BOOL:${HAVE_SSP_FLAG}>,$<BOOL:${ENABLE_SSP}>>:ssp-buffer-size=4>
Vitaly Kirsanov's avatar
Vitaly Kirsanov committed
84 85
    $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<BOOL:${HAVE_WEFFCXX_FLAG}>>:-Weffc++>
    $<$<AND:$<COMPILE_LANGUAGE:C>,$<BOOL:${HAVE_DECL_AFTER_STMT_FLAG}>>:-Wdeclaration-after-statement>)
Vitaliy Kirsanov's avatar
Vitaliy Kirsanov committed
86 87 88 89 90 91 92 93 94 95 96 97 98

if(CMAKE_SYSTEM_PROCESSOR STREQUAL "i686" AND HAVE_STACKREALIGN_FLAG)
    add_compile_options(-mstackrealign)
endif()

include_directories("include")

add_subdirectory("doc")
add_subdirectory("src")
add_subdirectory("microbench")
if(BUILD_EXAMPLES)
    add_subdirectory("examples")
endif()
99 100
if(BUILD_TESTING)
    add_subdirectory("test")
Vitaliy Kirsanov's avatar
Vitaliy Kirsanov committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
endif()

install(
    EXPORT targets
    DESTINATION "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake"
    NAMESPACE FLAC::)

configure_package_config_file(
    flac-config.cmake.in flac-config.cmake
    INSTALL_DESTINATION "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake")
write_basic_package_version_file(
    flac-config-version.cmake COMPATIBILITY AnyNewerVersion)

install(
    FILES
        "${CMAKE_CURRENT_BINARY_DIR}/flac-config.cmake"
        "${CMAKE_CURRENT_BINARY_DIR}/flac-config-version.cmake"
Vitaliy Kirsanov's avatar
Vitaliy Kirsanov committed
118
        "cmake/FindOGG.cmake"
Vitaliy Kirsanov's avatar
Vitaliy Kirsanov committed
119 120 121 122 123 124 125
    DESTINATION "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake")

file(GLOB FLAC_HEADERS "include/FLAC/*.h")
file(GLOB FLACXX_HEADERS "include/FLAC++/*.h")
install(FILES ${FLAC_HEADERS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/FLAC")
install(FILES ${FLACXX_HEADERS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/FLAC++")
install(FILES "man/flac.1" "man/metaflac.1" DESTINATION "${CMAKE_INSTALL_MANDIR}")