Skip to content

Commit

Permalink
Add CMakeLists.txt for examples
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmoene committed Oct 10, 2021
1 parent e0c83bd commit 88ab4a8
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 )

# optional-bare project and version, updated by script/update-version.py:

Expand Down
6 changes: 3 additions & 3 deletions example/01-to_int.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "optional.hpp"
#include "nonstd/optional.hpp"

#include <cstdlib>
#include <iostream>
Expand All @@ -24,5 +24,5 @@ int main( int argc, char * argv[] )
else std::cout << "'" << text << "' isn't a number";
}

// cl -nologo -W3 -EHsc -I../include/nonstd/ 01-to_int.cpp && 01-to_int x1
// g++ -Wall -Wextra -std=c++03 -I../include/nonstd/ -o 01-to_int.exe 01-to_int.cpp && 01-to_int x1
// cl -nologo -W3 -EHsc -I../include/ 01-to_int.cpp && 01-to_int x1
// g++ -Wall -Wextra -std=c++03 -I../include/ -o 01-to_int.exe 01-to_int.cpp && 01-to_int x1
99 changes: 99 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Copyright 2021-2021 by Martin Moene
#
# https://github.com/martinmoene/optional-bare-lite
#
# 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)

if( NOT DEFINED CMAKE_MINIMUM_REQUIRED_VERSION )
cmake_minimum_required( VERSION 3.8 FATAL_ERROR )
endif()

project( example LANGUAGES CXX )

set( unit_name "optional" )
set( PACKAGE ${unit_name}-bare )
set( PROGRAM ${unit_name}-bare )

message( STATUS "Subproject '${PROJECT_NAME}', examples '${PROGRAM}-*'")

# Target default options and definitions:

set( OPTIONS "" )
#set( DEFINITIONS "" )

# Sources (.cpp), normal and no-exception, and their base names:

set( SOURCES
01-to_int.cpp
02-no-exceptions.cpp
)

set( SOURCES_NE
02-no-exceptions.cpp
)

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 name no_exceptions )
ternary( ne no_exceptions "-ne" "" )

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()

endfunction()

# Create targets:

foreach( target ${BASENAMES} )
make_target( ${target} FALSE )
endforeach()

foreach( target ${BASENAMES_NE} )
make_target( ${target} TRUE )
endforeach()

# end of file

0 comments on commit 88ab4a8

Please sign in to comment.