Skip to content

Commit

Permalink
Add CMake files to compile example
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmoene committed Oct 10, 2021
1 parent e9dff07 commit 7699dc7
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 3 deletions.
129 changes: 129 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Copyright 2018-2021 by Martin Moene
#
# https://github.com/martinmoene/invoke-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)

cmake_minimum_required( VERSION 3.8 FATAL_ERROR )

# invoke-lite project and version, updated by script/update-version.py:

project(
invoke_lite
VERSION 9.9.9
# DESCRIPTION "INVOKE"
# HOMEPAGE_URL "https://github.com/martinmoene/invoke-lite"
LANGUAGES CXX )

# Package information:

set( unit_name "invoke" )
set( package_nspace "nonstd" )
set( package_name "${unit_name}-lite" )
set( package_version "${${PROJECT_NAME}_VERSION}" )

message( STATUS "Project '${PROJECT_NAME}', package '${package_name}' version: '${package_version}'")

# Toplevel or subproject:

if ( CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME )
set( invoke_IS_TOPLEVEL_PROJECT TRUE )
else()
set( invoke_IS_TOPLEVEL_PROJECT FALSE )
endif()

# If toplevel project, enable building and performing of tests, disable building of examples:

option( INVOKE_LITE_OPT_BUILD_TESTS "Build and perform invoke-lite tests" ${invoke_IS_TOPLEVEL_PROJECT} )
option( INVOKE_LITE_OPT_BUILD_EXAMPLES "Build invoke-lite examples" OFF )

option( INVOKE_LITE_OPT_SELECT_STD "Select std::invoke" OFF )
option( INVOKE_LITE_OPT_SELECT_NONSTD "Select nonstd::invoke" OFF )

# If requested, build and perform tests, build examples:

if ( INVOKE_LITE_OPT_BUILD_TESTS )
enable_testing()
add_subdirectory( test )
endif()

if ( INVOKE_LITE_OPT_BUILD_EXAMPLES )
add_subdirectory( example )
endif()

#
# Interface, installation and packaging
#

# CMake helpers:

include( GNUInstallDirs )
include( CMakePackageConfigHelpers )

# Interface library:

add_library(
${package_name} INTERFACE )

add_library(
${package_nspace}::${package_name} ALIAS ${package_name} )

target_include_directories(
${package_name}
INTERFACE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>" )

# Package configuration:
# Note: package_name and package_target are used in package_config_in

set( package_folder "${package_name}" )
set( package_target "${package_name}-targets" )
set( package_config "${package_name}-config.cmake" )
set( package_config_in "${package_name}-config.cmake.in" )
set( package_config_version "${package_name}-config-version.cmake" )

configure_package_config_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/${package_config_in}"
"${CMAKE_CURRENT_BINARY_DIR}/${package_config}"
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${package_folder}"
)

configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/${package_config_version}.in"
"${CMAKE_CURRENT_BINARY_DIR}/${package_config_version}" @ONLY
)

# Installation:

install(
TARGETS ${package_name}
EXPORT ${package_target}
# INCLUDES DESTINATION "${...}" # already set via target_include_directories()
)

install(
EXPORT ${package_target}
NAMESPACE ${package_nspace}::
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${package_folder}"
)

install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/${package_config}"
"${CMAKE_CURRENT_BINARY_DIR}/${package_config_version}"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${package_folder}"
)

install(
DIRECTORY "include/"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)

export(
EXPORT ${package_target}
NAMESPACE ${package_nspace}::
FILE "${CMAKE_CURRENT_BINARY_DIR}/${package_name}-targets.cmake"
)

# end of file
24 changes: 24 additions & 0 deletions cmake/invoke-lite-config-version.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Adapted from write_basic_package_version_file(... COMPATIBILITY SameMajorVersion) output
# ARCH_INDEPENDENT is only present in cmake 3.14 and onwards

set( PACKAGE_VERSION "@package_version@" )

if( PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION )
set( PACKAGE_VERSION_COMPATIBLE FALSE )
else()
if( "@package_version@" MATCHES "^([0-9]+)\\." )
set( CVF_VERSION_MAJOR "${CMAKE_MATCH_1}" )
else()
set( CVF_VERSION_MAJOR "@package_version@" )
endif()

if( PACKAGE_FIND_VERSION_MAJOR STREQUAL CVF_VERSION_MAJOR )
set( PACKAGE_VERSION_COMPATIBLE TRUE )
else()
set( PACKAGE_VERSION_COMPATIBLE FALSE )
endif()

if( PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION )
set( PACKAGE_VERSION_EXACT TRUE )
endif()
endif()
7 changes: 7 additions & 0 deletions cmake/invoke-lite-config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@PACKAGE_INIT@

# Only include targets once:

if( NOT TARGET @package_name@::@package_name@ )
include( "${CMAKE_CURRENT_LIST_DIR}/@package_target@.cmake" )
endif()
95 changes: 95 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Copyright 2021-2021 by Martin Moene
#
# https://github.com/martinmoene/invoke-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 "invoke" )
set( PACKAGE ${unit_name}-lite )
set( PROGRAM ${unit_name}-lite )

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
invoke.cpp
)

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 name no_exceptions )
add_executable ( ${PROGRAM}-${name} ${name}.cpp )
target_include_directories ( ${PROGRAM}-${name} PRIVATE ../../variant-lite/include )
target_link_libraries ( ${PROGRAM}-${name} PRIVATE ${PACKAGE} )
if ( no_exceptions )
target_compile_options ( ${PROGRAM}-${name} PRIVATE ${NO_EXCEPTIONS_OPTIONS} )
else()
target_compile_options ( ${PROGRAM}-${name} 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
6 changes: 3 additions & 3 deletions example/invoke.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

// invoke-lite/example/invoke.cpp

#include <invoke.hpp>
#include "nonstd/invoke.hpp"
#include <iostream>

struct Foo
Expand Down Expand Up @@ -121,7 +121,7 @@ int main()
}

// MSVC 2010 (VC10) and newer
// cl -EHsc -I../include/ invoke.cpp && invoke.exe
// cl -EHsc -I../include invoke.cpp && invoke.exe

// GNUC, std=c++98, c++11, c++17
// g++ -std=c++98 -I../include/ -o invoke.exe invoke.cpp && invoke.exe
// g++ -std=c++98 -I../include -o invoke.exe invoke.cpp && invoke.exe

0 comments on commit 7699dc7

Please sign in to comment.