|
| 1 | +# Written in 2016 by Henrik Steffen Gaßmann [email protected] |
| 2 | +# |
| 3 | +# To the extent possible under law, the author(s) have dedicated all |
| 4 | +# copyright and related and neighboring rights to this software to the |
| 5 | +# public domain worldwide. This software is distributed without any warranty. |
| 6 | +# |
| 7 | +# You should have received a copy of the CC0 Public Domain Dedication |
| 8 | +# along with this software. If not, see |
| 9 | +# |
| 10 | +# http://creativecommons.org/publicdomain/zero/1.0/ |
| 11 | +# |
| 12 | +######################################################################## |
| 13 | +cmake_minimum_required(VERSION 2.8.12) |
| 14 | +project(base64) |
| 15 | +set(BASE64_VERSION 0.2.1) |
| 16 | + |
| 17 | +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/Modules") |
| 18 | + |
| 19 | +if (CMAKE_C_COMPILER_ID STREQUAL "GNU" |
| 20 | + OR CMAKE_C_COMPILER_ID STREQUAL "Clang") |
| 21 | + set(BASE64_GNU_C_COMPATIBLE 1) |
| 22 | +endif() |
| 23 | +if (CMAKE_C_COMPILER_ID STREQUAL "GNU" |
| 24 | + OR CMAKE_C_COMPILER_ID STREQUAL "Clang") |
| 25 | + set(BASE64_GNU_CXX_COMPATIBLE 1) |
| 26 | +endif() |
| 27 | + |
| 28 | +# set BASE64_INCLUDED to 1 if you include this project from another |
| 29 | +# cmake project (this is handy in combination with git submodules :) |
| 30 | +if (NOT BASE64_INCLUDED) |
| 31 | + option(BASE64_INSTALL_TARGET "add an install target" ON) |
| 32 | + option(BASE64_BUILD_TESTS "add test projects" OFF) |
| 33 | + |
| 34 | + include(TargetSIMDInstructionSet) |
| 35 | + option(BASE64_WITH_SSSE3 "add SSSE3 codepath" OFF) |
| 36 | + option(BASE64_WITH_AVX2 "add AVX2 codepath" OFF) |
| 37 | + |
| 38 | + #list(APPEND BASE64_NEON_OPTIONS NEON32) |
| 39 | + #list(APPEND BASE64_NEON_OPTIONS NEON32-DEFAULTED) |
| 40 | + #list(APPEND BASE64_NEON_OPTIONS NEON64) |
| 41 | + #if (BASE64_NEON_OPTIONS) |
| 42 | + #set(BASE64_WITH_NEON "none" CACHE STRING "which NEON codepath should be choosen") |
| 43 | + # list(APPEND BASE64_NEON_OPTIONS none) |
| 44 | + # set_property(CACHE BASE64_WITH_NEON PROPERTY STRINGS ${BASE64_NEON_OPTIONS}) |
| 45 | + # if (NOT ";${BASE64_WITH_NEON};" MATCHES ";${BASE64_NEON_OPTIONS};") |
| 46 | + # message(FATAL_ERROR "invalid neon option selected ${BASE64_WITH_NEON}") |
| 47 | + #endif() |
| 48 | + |
| 49 | + #################################################################### |
| 50 | + # platform/compiler specific configuration |
| 51 | + if(MSVC) |
| 52 | + # Force to always compile with W4 |
| 53 | + add_definitions(-D_CRT_SECURE_NO_WARNINGS) |
| 54 | + if(${CMAKE_C_FLAGS} MATCHES "/W[0-4]") |
| 55 | + string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") |
| 56 | + else() |
| 57 | + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4") |
| 58 | + endif() |
| 59 | + endif() |
| 60 | + if (BASE64_GNU_C_COMPATIBLE) |
| 61 | + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wall -Wextra -Wpedantic") |
| 62 | + endif() |
| 63 | +endif() |
| 64 | + |
| 65 | +add_library(base64 STATIC |
| 66 | + lib/lib.c |
| 67 | + lib/codec_avx2.c |
| 68 | + lib/codec_choose.c |
| 69 | + lib/codec_neon32.c |
| 70 | + lib/codec_neon64.c |
| 71 | + lib/codec_plain.c |
| 72 | + lib/codec_ssse3.c |
| 73 | + |
| 74 | + include/libbase64.h |
| 75 | +) |
| 76 | + |
| 77 | +######################################################################## |
| 78 | +# SIMD settings |
| 79 | +define_SIMD_compile_flags() |
| 80 | +if (BASE64_WITH_SSSE3) |
| 81 | + set_source_files_properties(lib/codec_ssse3.c PROPERTIES |
| 82 | + COMPILE_FLAGS ${COMPILE_FLAGS_SSSE3} |
| 83 | + ) |
| 84 | + if (MSVC) |
| 85 | + # if SSSE3 is available it is always enabled by default, |
| 86 | + # but MSVC _never_ defines __SSSE3__ |
| 87 | + set_source_files_properties(lib/codec_ssse3.c PROPERTIES |
| 88 | + COMPILE_DEFINITIONS __SSSE3__ |
| 89 | + ) |
| 90 | + endif() |
| 91 | + set(HAVE_SSSE3 1) |
| 92 | +else() |
| 93 | + set(HAVE_SSSE3 0) |
| 94 | +endif() |
| 95 | +if (BASE64_WITH_AVX2) |
| 96 | + set_source_files_properties(lib/codec_avx2.c PROPERTIES |
| 97 | + COMPILE_FLAGS ${COMPILE_FLAGS_AVX2} |
| 98 | + ) |
| 99 | + set(HAVE_AVX2 1) |
| 100 | +else() |
| 101 | + set(HAVE_AVX2 0) |
| 102 | +endif() |
| 103 | +# this needs much more love... |
| 104 | +set(HAVE_NEON32 0) |
| 105 | +set(HAVE_NEON64 0) |
| 106 | +if (BASE64_WITH_NEON MATCHES NEON32) |
| 107 | + if(BASE64_WITH_NEON MATCHES NEON32-DEFAULTED) |
| 108 | + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMPILE_FLAGS_NEON32}") |
| 109 | + else() |
| 110 | + set_source_files_properties(lib/codec_neon32.c PROPERTIES |
| 111 | + COMPILE_FLAGS ${COMPILE_FLAGS_NEON32} |
| 112 | + ) |
| 113 | + endif() |
| 114 | + set(HAVE_NEON32 1) |
| 115 | +elseif (BASE64_WITH_NEON STREQUAL NEON64) |
| 116 | + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMPILE_FLAGS_NEON64}") |
| 117 | + set(HAVE_NEON64 1) |
| 118 | +endif() |
| 119 | + |
| 120 | + |
| 121 | +target_compile_definitions(base64 |
| 122 | + PRIVATE |
| 123 | + CMD_DEFINED_CONFIG |
| 124 | + HAVE_SSSE3=${HAVE_SSSE3} |
| 125 | + HAVE_AVX2=${HAVE_AVX2} |
| 126 | + HAVE_NEON32=${HAVE_NEON32} |
| 127 | + HAVE_NEON64=${HAVE_NEON64} |
| 128 | +) |
| 129 | + |
| 130 | +######################################################################## |
| 131 | +if (BASE64_BUILD_TESTS) |
| 132 | + enable_testing() |
| 133 | + add_subdirectory(test) |
| 134 | +endif() |
| 135 | + |
| 136 | +######################################################################## |
| 137 | +# install target |
| 138 | +if (BASE64_INSTALL_TARGET) |
| 139 | + install(TARGETS base64 EXPORT base64-targets |
| 140 | + RUNTIME DESTINATION bin/$<CONFIG> |
| 141 | + LIBRARY DESTINATION lib/$<CONFIG> |
| 142 | + ARCHIVE DESTINATION lib/$<CONFIG> |
| 143 | + INCLUDES DESTINATION include |
| 144 | + ) |
| 145 | + install(FILES include/libbase64.h DESTINATION include) |
| 146 | + |
| 147 | + include(CMakePackageConfigHelpers) |
| 148 | + write_basic_package_version_file( |
| 149 | + "${CMAKE_CURRENT_BINARY_DIR}/base64-config-version.cmake" |
| 150 | + VERSION ${BASE64_VERSION} |
| 151 | + COMPATIBILITY ExactVersion |
| 152 | + ) |
| 153 | + install(FILES "${CMAKE_CURRENT_BINARY_DIR}/base64-config-version.cmake" DESTINATION cmake) |
| 154 | + |
| 155 | + configure_file(cmake/base64-config.cmake |
| 156 | + "${CMAKE_CURRENT_BINARY_DIR}/base64-config.cmake" |
| 157 | + COPYONLY |
| 158 | + ) |
| 159 | + install(FILES "${CMAKE_CURRENT_BINARY_DIR}/base64-config.cmake" DESTINATION cmake) |
| 160 | + |
| 161 | + install(EXPORT base64-targets DESTINATION cmake) |
| 162 | +endif() |
0 commit comments