|
| 1 | +# Copyright 2021-2021 by Martin Moene |
| 2 | +# |
| 3 | +# https://github.com/martinmoene/string-view-lite |
| 4 | +# |
| 5 | +# Distributed under the Boost Software License, Version 1.0. |
| 6 | +# (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 7 | + |
| 8 | +if( NOT DEFINED CMAKE_MINIMUM_REQUIRED_VERSION ) |
| 9 | + cmake_minimum_required( VERSION 3.8 FATAL_ERROR ) |
| 10 | +endif() |
| 11 | + |
| 12 | +project( example LANGUAGES CXX ) |
| 13 | + |
| 14 | +set( unit_name "string_view" ) |
| 15 | +set( PACKAGE ${unit_name}-lite ) |
| 16 | +set( PROGRAM ${unit_name}-lite ) |
| 17 | + |
| 18 | +message( STATUS "Subproject '${PROJECT_NAME}', examples '${PROGRAM}-*'") |
| 19 | + |
| 20 | +# Target default options and definitions: |
| 21 | + |
| 22 | +set( OPTIONS "" ) |
| 23 | +#set( DEFINITIONS "" ) |
| 24 | + |
| 25 | +# Sources (.cpp), normal and no-exception, and their base names: |
| 26 | + |
| 27 | +set( SOURCES |
| 28 | + 01-basic.cpp |
| 29 | +) |
| 30 | + |
| 31 | +set( SOURCES_NE |
| 32 | + 02-no-exceptions.cpp |
| 33 | +) |
| 34 | + |
| 35 | +string( REPLACE ".cpp" "" BASENAMES "${SOURCES}" ) |
| 36 | +string( REPLACE ".cpp" "" BASENAMES_NE "${SOURCES_NE}" ) |
| 37 | + |
| 38 | +# Determine options: |
| 39 | + |
| 40 | +if( MSVC ) |
| 41 | + message( STATUS "Matched: MSVC") |
| 42 | + |
| 43 | + set( BASE_OPTIONS -W3 ) |
| 44 | + set( EXCEPTIONS_OPTIONS ${BASE_OPTIONS} -EHsc ) |
| 45 | + set( NO_EXCEPTIONS_OPTIONS ${BASE_OPTIONS} ) |
| 46 | + |
| 47 | +elseif( CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|AppleClang" ) |
| 48 | + message( STATUS "CompilerId: '${CMAKE_CXX_COMPILER_ID}'") |
| 49 | + |
| 50 | + set( BASE_OPTIONS -Wall -Wextra -Wconversion -Wsign-conversion -Wno-missing-braces -fno-elide-constructors ) |
| 51 | + set( EXCEPTIONS_OPTIONS ${BASE_OPTIONS} ) |
| 52 | + set( NO_EXCEPTIONS_OPTIONS -fno-exceptions ) |
| 53 | + |
| 54 | +elseif( CMAKE_CXX_COMPILER_ID MATCHES "Intel" ) |
| 55 | + # as is |
| 56 | + message( STATUS "Matched: Intel") |
| 57 | +else() |
| 58 | + # as is |
| 59 | + message( STATUS "Matched: nothing") |
| 60 | +endif() |
| 61 | + |
| 62 | +# Function to emulate ternary operaton `result = b ? x : y`: |
| 63 | + |
| 64 | +macro( ternary var boolean value1 value2 ) |
| 65 | + if( ${boolean} ) |
| 66 | + set( ${var} ${value1} ) |
| 67 | + else() |
| 68 | + set( ${var} ${value2} ) |
| 69 | + endif() |
| 70 | +endmacro() |
| 71 | + |
| 72 | +# Function to create a target: |
| 73 | + |
| 74 | +function( make_target name no_exceptions ) |
| 75 | + ternary( ne no_exceptions "-ne" "" ) |
| 76 | + |
| 77 | + add_executable ( ${PROGRAM}-${name}${ne} ${name}.cpp ) |
| 78 | + target_include_directories ( ${PROGRAM}-${name}${ne} PRIVATE ../include ) |
| 79 | + # target_link_libraries ( ${PROGRAM}-${name}${ne} PRIVATE ${PACKAGE} ) |
| 80 | + if ( no_exceptions ) |
| 81 | + target_compile_options ( ${PROGRAM}-${name}${ne} PRIVATE ${NO_EXCEPTIONS_OPTIONS} ) |
| 82 | + else() |
| 83 | + target_compile_options ( ${PROGRAM}-${name}${ne} PRIVATE ${EXCEPTIONS_OPTIONS} ) |
| 84 | + endif() |
| 85 | + |
| 86 | +endfunction() |
| 87 | + |
| 88 | +# Create targets: |
| 89 | + |
| 90 | +foreach( target ${BASENAMES} ) |
| 91 | + make_target( ${target} FALSE ) |
| 92 | +endforeach() |
| 93 | + |
| 94 | +foreach( target ${BASENAMES_NE} ) |
| 95 | + make_target( ${target} TRUE ) |
| 96 | +endforeach() |
| 97 | + |
| 98 | +# end of file |
0 commit comments