From 337af6e23f6d3264136c16565546244da23159ba Mon Sep 17 00:00:00 2001 From: Martin Moene Date: Sun, 10 Oct 2021 13:31:34 +0200 Subject: [PATCH] Add CMakeLists.txt for examples https://github.com/martinmoene/nonstd-lite-project/issues/56 --- CMakeLists.txt | 2 +- example/CMakeLists.txt | 79 +++++++++++++++++++++++++++++++++--------- 2 files changed, 64 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 072f8bf..e7ce056 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ # Distributed under the Boost Software License, Version 1.0. # (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -cmake_minimum_required( VERSION 3.5 FATAL_ERROR ) +cmake_minimum_required( VERSION 3.8 FATAL_ERROR ) # span-lite project and version, updated by script/update-version.py: diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 4b47459..3dadea3 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright 2018-2019 by Martin Moene +# Copyright 2018-2021 by Martin Moene # # https://github.com/martinmoene/span-lite # @@ -6,46 +6,93 @@ # (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt) if( NOT DEFINED CMAKE_MINIMUM_REQUIRED_VERSION ) - cmake_minimum_required( VERSION 3.5 FATAL_ERROR ) + cmake_minimum_required( VERSION 3.8 FATAL_ERROR ) endif() project( example LANGUAGES CXX ) set( unit_name "span" ) set( PACKAGE ${unit_name}-lite ) +set( PROGRAM ${unit_name}-lite ) -message( STATUS "Subproject '${PROJECT_NAME}'") +message( STATUS "Subproject '${PROJECT_NAME}', examples '${PROGRAM}-*'") # Target default options and definitions: set( OPTIONS "" ) -set( DEFINITIONS "" ) +#set( DEFINITIONS "" ) -# Sources (.cpp) and their base names: +# Sources (.cpp), normal and no-exception, and their base names: set( SOURCES 01-basic.cpp 02-span.cpp ) -string( REPLACE ".cpp" "" BASENAMES "${SOURCES}" ) +set( SOURCES_NE +) + +string( REPLACE ".cpp" "" BASENAMES "${SOURCES}" ) +string( REPLACE ".cpp" "" BASENAMES_NE "${SOURCES_NE}" ) + +# Determine options: + +if( MSVC ) + message( STATUS "Matched: MSVC") + + set( BASE_OPTIONS -W3 ) + set( EXCEPTIONS_OPTIONS ${BASE_OPTIONS} -EHsc ) + set( NO_EXCEPTIONS_OPTIONS ${BASE_OPTIONS} ) + +elseif( CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|AppleClang" ) + message( STATUS "CompilerId: '${CMAKE_CXX_COMPILER_ID}'") + + set( BASE_OPTIONS -Wall -Wextra -Wconversion -Wsign-conversion -Wno-missing-braces -fno-elide-constructors ) + set( EXCEPTIONS_OPTIONS ${BASE_OPTIONS} ) + set( NO_EXCEPTIONS_OPTIONS -fno-exceptions ) + +elseif( CMAKE_CXX_COMPILER_ID MATCHES "Intel" ) + # as is + message( STATUS "Matched: Intel") +else() + # as is + message( STATUS "Matched: nothing") +endif() + +# Function to emulate ternary operaton `result = b ? x : y`: + +macro( ternary var boolean value1 value2 ) + if( ${boolean} ) + set( ${var} ${value1} ) + else() + set( ${var} ${value2} ) + endif() +endmacro() # Function to create a target: -function( make_target source ) - string( REPLACE ".cpp" "" target "${source}" ) - add_executable ( ${target} ${source} ) - target_link_libraries ( ${target} PRIVATE ${PACKAGE} ) - target_compile_options ( ${target} PRIVATE ${OPTIONS} ) - target_compile_definitions( ${target} PRIVATE ${DEFINITIONS} ) -endfunction() +function( make_target name no_exceptions ) + ternary( ne no_exceptions "-ne" "" ) -# Targets: + add_executable ( ${PROGRAM}-${name}${ne} ${name}.cpp ) + target_include_directories ( ${PROGRAM}-${name}${ne} PRIVATE ../../variant-lite/include ) + target_link_libraries ( ${PROGRAM}-${name}${ne} PRIVATE ${PACKAGE} ) + if ( no_exceptions ) + target_compile_options ( ${PROGRAM}-${name}${ne} PRIVATE ${NO_EXCEPTIONS_OPTIONS} ) + else() + target_compile_options ( ${PROGRAM}-${name}${ne} PRIVATE ${EXCEPTIONS_OPTIONS} ) + endif() -enable_testing() +endfunction() + +# Create targets: foreach( target ${BASENAMES} ) - make_target( ${target} ) + make_target( ${target} FALSE ) +endforeach() + +foreach( target ${BASENAMES_NE} ) + make_target( ${target} TRUE ) endforeach() # end of file