Skip to content

Instantly share code, notes, and snippets.

@trmwzm
Last active December 8, 2020 21:25
Show Gist options
  • Save trmwzm/f7c9b1aaadf62d4db25630545ce9955c to your computer and use it in GitHub Desktop.
Save trmwzm/f7c9b1aaadf62d4db25630545ce9955c to your computer and use it in GitHub Desktop.
blt fetch
#------------------------------------------------------------------------------
# BLT Tutorial Example: Calc Pi.
#------------------------------------------------------------------------------
cmake_minimum_required(VERSION 3.17)
project( pi_playground )
#------------------------------------------------------------------------------
# Setup BLT
#------------------------------------------------------------------------------
# _blt_tutorial_include_blt_start
if (DEFINED BLT_SOURCE_DIR)
# Support having a shared BLT outside of the repository if given a BLT_SOURCE_DIR
if (NOT EXISTS ${BLT_SOURCE_DIR}/SetupBLT.cmake)
message(FATAL_ERROR "Given BLT_SOURCE_DIR does not contain SetupBLT.cmake")
endif()
else()
# Use internal BLT if no BLT_SOURCE_DIR is given
set(BLT_SOURCE_DIR "${PROJECT_SOURCE_DIR}/cmake/blt" CACHE PATH "")
if (NOT EXISTS ${BLT_SOURCE_DIR}/SetupBLT.cmake)
# Fetch BLT from github
include(FetchContent)
FetchContent_Declare(blt
GIT_REPOSITORY https://github.com/LLNL/blt.git
GIT_TAG origin/develop
SOURCE_DIR ${BLT_SOURCE_DIR})
FetchContent_MakeAvailable(blt)
if(NOT blt_POPULATED)
FetchContent_Populate(blt)
endif()
endif()
if (NOT EXISTS ${BLT_SOURCE_DIR}/SetupBLT.cmake)
message(FATAL_ERROR "Failed to use, or fetch, default BLT_SOURCE_DIR: does not contain SetupBLT.cmake")
endif()
endif()
# Default to C++11 if not set so GTest/GMock can build
if (NOT BLT_CXX_STD)
set(BLT_CXX_STD "c++11" CACHE STRING "")
endif()
include(${BLT_SOURCE_DIR}/SetupBLT.cmake)
#------------------------------------------------------------------------------
# Example 1: Creating a simple executable.
#------------------------------------------------------------------------------
# _blt_tutorial_example_executable_start
blt_add_executable( NAME example_1
SOURCES example_1.cpp )
# _blt_tutorial_example_executable_end
#------------------------------------------------------------------------------
# Example 2: Creating a library and an executable using our library.
#------------------------------------------------------------------------------
# _blt_tutorial_calcpi_library_start
blt_add_library( NAME calc_pi
HEADERS calc_pi.hpp calc_pi_exports.h
SOURCES calc_pi.cpp )
# _blt_tutorial_calcpi_library_end
if(WIN32 AND BUILD_SHARED_LIBS)
target_compile_definitions(calc_pi PUBLIC WIN32_SHARED_LIBS)
endif()
# _blt_tutorial_calcpi_example2_start
blt_add_executable( NAME example_2
SOURCES example_2.cpp
DEPENDS_ON calc_pi)
# _blt_tutorial_calcpi_example2_end
#------------------------------------------------------------------------------
# Test 1: Creating an executable using gtest, using the executable via ctest.
#------------------------------------------------------------------------------
# _blt_tutorial_calcpi_test1_executable_start
blt_add_executable( NAME test_1
SOURCES test_1.cpp
DEPENDS_ON calc_pi gtest)
# _blt_tutorial_calcpi_test1_executable_end
# _blt_tutorial_calcpi_test1_test_start
blt_add_test( NAME test_1
COMMAND test_1)
# _blt_tutorial_calcpi_test1_test_end
#------------------------------------------------------------------------------
# Test 2: Add mpi version of calc_pi, and expand test 1 to also test
# the mpi version.
#------------------------------------------------------------------------------
if(MPI_FOUND)
# _blt_tutorial_calcpi_test2_executable_start
blt_add_library( NAME calc_pi_mpi
HEADERS calc_pi_mpi.hpp calc_pi_mpi_exports.h
SOURCES calc_pi_mpi.cpp
DEPENDS_ON mpi)
if(WIN32 AND BUILD_SHARED_LIBS)
target_compile_definitions(calc_pi_mpi PUBLIC WIN32_SHARED_LIBS)
endif()
blt_add_executable( NAME test_2
SOURCES test_2.cpp
DEPENDS_ON calc_pi calc_pi_mpi gtest)
# _blt_tutorial_calcpi_test2_executable_end
# _blt_tutorial_calcpi_test2_test_start
blt_add_test( NAME test_2
COMMAND test_2
NUM_MPI_TASKS 2) # number of mpi tasks to use
# _blt_tutorial_calcpi_test2_test_end
endif()
#------------------------------------------------------------------------------
# Test 3: Add cuda version of calc_pi, and expand test 1 to also test
# the cuda version.
#------------------------------------------------------------------------------
if(CUDA_FOUND)
# _blt_tutorial_calcpi_cuda_start
blt_add_library( NAME calc_pi_cuda
HEADERS calc_pi_cuda.hpp calc_pi_cuda_exports.h
SOURCES calc_pi_cuda.cpp
DEPENDS_ON cuda)
if(WIN32 AND BUILD_SHARED_LIBS)
target_compile_definitions(calc_pi_cuda PUBLIC WIN32_SHARED_LIBS)
endif()
blt_add_executable( NAME test_3
SOURCES test_3.cpp
DEPENDS_ON calc_pi calc_pi_cuda gtest cuda_runtime)
blt_add_test( NAME test_3
COMMAND test_3)
# _blt_tutorial_calcpi_cuda_end
endif()
#------------------------------------------------------------------------------
# Add Documentation Examples
#------------------------------------------------------------------------------
add_subdirectory(docs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment