Skip to content

Commit

Permalink
Add reason_add_library for MODE=cuda
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcavendish committed Apr 11, 2017
1 parent c2e61f3 commit 4d673a7
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 26 deletions.
49 changes: 28 additions & 21 deletions reason.add_executable.cuda.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,32 @@ function(reason__add_executable__add_executable TARGET_NAME SRCS)
endif()
endfunction()

macro(reason__extract_INCLUDE_DIRECTORIES_to_CUDA_INCLUDE TARGET_NAME)
# Use 'cuda_include_directories' to add include directories
get_target_property(props "${TARGET_NAME}" "INCLUDE_DIRECTORIES")
if((NOT "${props}" STREQUAL "props-NOTFOUND") AND (NOT "${props}" STREQUAL ""))
reason_verbose(" cuda_include_directories:")
foreach(prop IN LISTS props)
# Removes cmake-generator-expression to raw PATH
string(REGEX REPLACE "\\$<.*:" "" prop "${prop}")
string(REGEX REPLACE ">" "" prop "${prop}")
cuda_include_directories("${prop}")
reason_verbose(" ${prop}")
endforeach()
endif()
endmacro()

macro(reason__extract_COMPILE_DEFINITIONS_to_NVCC_FLAGS TARGET_NAME)
# Use CUDA_NVCC_FLAGS to define compile options
get_target_property(props "${TARGET_NAME}" "COMPILE_OPTIONS")
if((NOT "${props}" STREQUAL "props-NOTFOUND") AND (NOT "${props}" STREQUAL ""))
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS};${props}")
string(REGEX REPLACE ";+" ";" CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS}")
string(REGEX REPLACE ";$" "" CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS}")
reason_verbose(" set CUDA_NVCC_FLAGS to: ${CUDA_NVCC_FALGS}")
endif()
endmacro()

function(reason__add_executable__impl)
function(create_cuda_dummy DUMMY_NAME)
set(REASON_VERBOSE FALSE)
Expand All @@ -28,27 +54,8 @@ function(reason__add_executable__impl)
set(DUMMY_NAME "${reason_TARGET}__cuda_dummy")
create_cuda_dummy("${DUMMY_NAME}")

# Use 'cuda_include_directories' to add include directories
get_target_property(props "${DUMMY_NAME}" "INCLUDE_DIRECTORIES")
if((NOT "${props}" STREQUAL "props-NOTFOUND") AND (NOT "${props}" STREQUAL ""))
reason_verbose(" cuda_include_directories:")
foreach(prop IN LISTS props)
# Removes cmake-generator-expression to raw PATH
string(REGEX REPLACE "\\$<.*:" "" prop "${prop}")
string(REGEX REPLACE ">" "" prop "${prop}")
cuda_include_directories("${prop}")
reason_verbose(" ${prop}")
endforeach()
endif()

# Use CUDA_NVCC_FLAGS to define compile options
get_target_property(props "${DUMMY_NAME}" "COMPILE_OPTIONS")
if((NOT "${props}" STREQUAL "props-NOTFOUND") AND (NOT "${props}" STREQUAL ""))
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS};${props}")
string(REGEX REPLACE ";+" ";" CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS}")
string(REGEX REPLACE ";$" "" CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS}")
reason_verbose(" set CUDA_NVCC_FLAGS to: ${CUDA_NVCC_FALGS}")
endif()
reason__extract_INCLUDE_DIRECTORIES_to_CUDA_INCLUDE("${DUMMY_NAME}")
reason__extract_COMPILE_DEFINITIONS_to_NVCC_FLAGS("${DUMMY_NAME}")

reason__add_executable__add_executable("${reason_TARGET}" "${reason_SRCS}")
reason__add_executable__tinclude_dirs("${reason_TARGET}" "${reason_INC_DIRS}")
Expand Down
16 changes: 15 additions & 1 deletion reason.add_library.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ endmacro()

function(reason_add_library)
set(options HELP STATIC SHARED)
set(one_value_args TARGET FN)
set(one_value_args TARGET FN MODE)
set(mlt_value_args INC_DIRS SRCS LINKS DEFINES)
cmake_parse_arguments(reason "${options}" "${one_value_args}" "${mlt_value_args}" "${ARGN}")

Expand All @@ -82,10 +82,24 @@ function(reason_add_library)

reason_set_check(reason_TARGET "You must specify a TARGET when using 'reason_add_library'")
reason_set_check(reason_SRCS "You probably forgot to list the source files when using 'reason_add_library'")
reason_check_incompatible(reason_FN reason_MODE)

if((NOT reason_STATIC) AND (NOT reason_SHARED))
reason_message(FATAL_ERROR "You must specify to build either STATIC or SHARED or both")
endif()

if(reason_MODE)
set(REASON_MODE_FILE "${REASON_MODULE_DIR}/reason.add_library.${reason_MODE}.cmake")
if(NOT EXISTS "${REASON_MODE_FILE}")
reason_message(FATAL_ERROR "reason mode '${reason_MODE}' for 'reason_add_library' does not exist!")
endif()

reason_verbose("Use MODE=${reason_MODE}")
include("${REASON_MODE_FILE}")
reason__add_library__impl()
return()
endif()

if(reason_FN)
set(FN_ADD_LIBRARY "${reason_FN}")
else()
Expand Down
128 changes: 128 additions & 0 deletions reason.add_library.cuda.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
function(reason__add_library__add_library TARGET_NAME TYPE SRCS)
if("${TYPE}" STREQUAL "STATIC")
cuda_add_library("${TARGET_NAME}" STATIC "${SRCS}")
elseif("${TYPE}" STREQUAL "SHARED")
cuda_add_library("${TARGET_NAME}" SHARED "${SRCS}")
else()
reason_message(FATAL_ERROR "reason_add_library has only 2 types: STATIC or SHARED")
endif()

reason_verbose(" cuda_add_library:")
if(REASON_VERBOSE)
foreach(SRC IN LISTS SRCS)
reason_verbose(" [src=${SRC}]")
endforeach()
endif()
endfunction()

macro(reason__extract_INCLUDE_DIRECTORIES_to_CUDA_INCLUDE TARGET_NAME)
# Use 'cuda_include_directories' to add include directories
get_target_property(props "${TARGET_NAME}" "INCLUDE_DIRECTORIES")
if((NOT "${props}" STREQUAL "props-NOTFOUND") AND (NOT "${props}" STREQUAL ""))
reason_verbose(" cuda_include_directories:")
foreach(prop IN LISTS props)
# Removes cmake-generator-expression to raw PATH
string(REGEX REPLACE "\\$<.*:" "" prop "${prop}")
string(REGEX REPLACE ">" "" prop "${prop}")
cuda_include_directories("${prop}")
reason_verbose(" ${prop}")
endforeach()
endif()
endmacro()

macro(reason__extract_COMPILE_DEFINITIONS_to_NVCC_FLAGS TARGET_NAME)
# Use CUDA_NVCC_FLAGS to define compile options
get_target_property(props "${TARGET_NAME}" "COMPILE_OPTIONS")
if((NOT "${props}" STREQUAL "props-NOTFOUND") AND (NOT "${props}" STREQUAL ""))
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS};${props}")
string(REGEX REPLACE ";+" ";" CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS}")
string(REGEX REPLACE ";$" "" CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS}")
reason_verbose(" set CUDA_NVCC_FLAGS to: ${CUDA_NVCC_FALGS}")
endif()
endmacro()

function(reason__add_library_s__impl)
function(create_cuda_dummy_s DUMMY_NAME)
set(REASON_VERBOSE FALSE)
add_library("${DUMMY_NAME}" STATIC EXCLUDE_FROM_ALL "${REASON_MODULE_DIR}/reason.add_library.cuda.dummy_main.cpp")
reason__add_library__tinclude_dirs("${DUMMY_NAME}" "${reason_INC_DIRS}")
reason__add_library__tlink_libs_s("${DUMMY_NAME}" "${reason_LINKS}")
reason__add_library__compile_define("${DUMMY_NAME}" "${reason_DEFINES}")
reason_unique_target_properties("${DUMMY_NAME}")
endfunction()

reason_verbose("cuda-library: [target=${reason_TARGET}_s] [type=STATIC]")

# Backup CUDA_NVCC_FLAGS, and CUDA_NVCC_INCLUDE_DIRS_USER
set(CUDA_NVCC_INCLUDE_DIRS_USER_BACKUP "${CUDA_NVCC_INCLUDE_DIRS_USER}")
set(CUDA_NVCC_FLAGS_BACKUP "${CUDA_NVCC_FLAGS}")

# Create a dummy cuda target to extract include-dir dependencies and compile-options
set(DUMMY_NAME "${reason_TARGET}_s__cuda_dummy")
create_cuda_dummy_s("${DUMMY_NAME}")

reason__extract_INCLUDE_DIRECTORIES_to_CUDA_INCLUDE("${DUMMY_NAME}")
reason__extract_COMPILE_DEFINITIONS_to_NVCC_FLAGS("${DUMMY_NAME}")

reason__add_library__add_library("${reason_TARGET}_s" STATIC "${reason_SRCS}")
reason__add_library__tinclude_dirs("${reason_TARGET}_s" "${reason_INC_DIRS}")
reason__add_library__tlink_libs_s("${reason_TARGET}_s" "${reason_LINKS}")
reason__add_library__compile_define("${reason_TARGET}_s" "${reason_DEFINES}")
reason__add_library__use_cotire("${reason_TARGET}_s")
reason_unique_target_properties("${reason_TARGET}_s")
reason_show_target_properties(VERBOSE "${reason_TARGET}_s")

# Restore CUDA_NVCC_FLAGS, and CUDA_NVCC_INCLUDE_DIRS_USER
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS_BACKUP}")
set(CUDA_NVCC_INCLUDE_DIRS_USER "${CUDA_NVCC_INCLUDE_DIRS_USER_BACKUP}")
endfunction()

function(reason__add_library_d__impl)
function(create_cuda_dummy_d DUMMY_NAME)
set(REASON_VERBOSE FALSE)
add_library("${DUMMY_NAME}" SHARED EXCLUDE_FROM_ALL "${REASON_MODULE_DIR}/reason.add_library.cuda.dummy_main.cpp")
reason__add_library__tinclude_dirs("${DUMMY_NAME}" "${reason_INC_DIRS}")
reason__add_library__tlink_libs_d("${DUMMY_NAME}" "${reason_LINKS}")
reason__add_library__compile_define("${DUMMY_NAME}" "${reason_DEFINES}")
reason_unique_target_properties("${DUMMY_NAME}")
endfunction()

reason_verbose("cuda-library: [target=${reason_TARGET}_d] [type=SHARED]")

# Backup CUDA_NVCC_FLAGS, and CUDA_NVCC_INCLUDE_DIRS_USER
set(CUDA_NVCC_INCLUDE_DIRS_USER_BACKUP "${CUDA_NVCC_INCLUDE_DIRS_USER}")
set(CUDA_NVCC_FLAGS_BACKUP "${CUDA_NVCC_FLAGS}")

# Create a dummy cuda target to extract include-dir dependencies and compile-options
set(DUMMY_NAME "${reason_TARGET}_d__cuda_dummy")
create_cuda_dummy_d("${DUMMY_NAME}")

reason__extract_INCLUDE_DIRECTORIES_to_CUDA_INCLUDE("${DUMMY_NAME}")
reason__extract_COMPILE_DEFINITIONS_to_NVCC_FLAGS("${DUMMY_NAME}")

reason__add_library__add_library("${reason_TARGET}_d" SHARED "${reason_SRCS}")
reason__add_library__tinclude_dirs("${reason_TARGET}_d" "${reason_INC_DIRS}")
reason__add_library__tlink_libs_d("${reason_TARGET}_d" "${reason_LINKS}")
reason__add_library__compile_define("${reason_TARGET}_d" "${reason_DEFINES}")
reason__add_library__use_cotire("${reason_TARGET}_d")
reason__add_library__set_shared_lib_version("${reason_TARGET}_d")
reason__add_library__set_rpath("${reason_TARGET}_d")
reason_unique_target_properties("${reason_TARGET}_d")
reason_show_target_properties(VERBOSE "${reason_TARGET}_d")

# Restore CUDA_NVCC_FLAGS, and CUDA_NVCC_INCLUDE_DIRS_USER
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS_BACKUP}")
set(CUDA_NVCC_INCLUDE_DIRS_USER "${CUDA_NVCC_INCLUDE_DIRS_USER_BACKUP}")
endfunction()

function(reason__add_library__impl)
# Build static library
if(reason_STATIC)
reason__add_library_s__impl()
endif()

# Build shared library
if(reason_SHARED)
reason__add_library_d__impl()
endif()
endfunction()
1 change: 1 addition & 0 deletions reason.add_library.cuda.dummy_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 1 addition & 1 deletion reason.add_library.in.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function(reason__add_library__add_library TARGET_NAME TYPE SRCS)
reason_message(FATAL_ERROR "reason_add_library has only 2 types: STATIC or SHARED")
endif()

reason_verbose(" @FN_ADD_LIBRARY@():")
reason_verbose(" @FN_ADD_LIBRARY@:")
if(REASON_VERBOSE)
foreach(SRC IN LISTS SRCS)
reason_verbose(" [src=${SRC}]")
Expand Down
6 changes: 3 additions & 3 deletions reason.util.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ example:
message("${MessageType}" "${c_green}${ARGV}${c_reset}")
elseif(MessageType STREQUAL VERBOSE)
list(REMOVE_AT ARGV 0)
if(REASON_BRIEF_PATH)
string(REPLACE "${CMAKE_SOURCE_DIR}" "~" ARGV "${ARGV}")
endif()
message("${c_bfuchsia}${ARGV}${c_reset}")
else()
message("${c_silver}${ARGV}${c_reset}")
Expand All @@ -73,9 +76,6 @@ endfunction()

function(reason_verbose)
if(REASON_VERBOSE)
if(REASON_BRIEF_PATH)
string(REPLACE "${CMAKE_SOURCE_DIR}" "~" ARGV "${ARGV}")
endif()
reason_message(VERBOSE "${ARGV}")
endif()
endfunction()
Expand Down

0 comments on commit 4d673a7

Please sign in to comment.