Skip to content

Commit

Permalink
Abstract to functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jchlanda committed Dec 14, 2023
1 parent 6410941 commit b26c4aa
Showing 1 changed file with 37 additions and 35 deletions.
72 changes: 37 additions & 35 deletions libclc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,43 +116,50 @@ execute_process( COMMAND ${LLVM_CONFIG} "--bindir"
# These were not properly reported in early LLVM and we don't need them
list( APPEND LLVM_CXX_FLAGS -fno-rtti -fno-exceptions )

# List containing all the toolchain variables.
list( APPEND BINARY_VARIABLES LLVM_CLANG LLVM_AS LLVM_LINK LLVM_OPT LLVM_SPIRV
LIBCLC_REMANGLER )
# List containing all the names of toolchain binaries.
# NOTE: both lists (BINARY_VARIABLES and BINARY_NAMES) must be in sync.
list( APPEND BINARY_NAMES clang llvm-as llvm-link opt llvm-spirv
libclc-remangler )

# find_program needs the variable to be cleared in order to perform a search.
# Make sure that the cached entries are cleared as well.
function( ClearVariables BINARY_VARIABLES_LIST )
foreach( V ${BINARY_VARIABLES_LIST} )
unset( ${V} CACHE )
unset( ${V} PARENT_SCOPE )
endforeach( V )
endfunction()

# Use find_program to locate toolchain binaries.
function( FindToolBinary BINARY_VARIABLES_LIST BINARY_NAMES_LIST PATH_NAME )
list( LENGTH BINARY_NAMES_LIST COUNT )
math( EXPR COUNT "${COUNT}-1" )
foreach( I RANGE ${COUNT} )
list( GET BINARY_VARIABLES_LIST ${I} BV )
list( GET BINARY_NAMES_LIST ${I} BN )
find_program( ${BV} ${BN} PATHS ${PATH_NAME} NO_DEFAULT_PATH )
endforeach( I )
endfunction()

# Use custom toolchain to build libclc, this can be useful when dealing with
# debug builds, that do not need libclc bitcode to be built using debug tools.
if ( EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} )
message( WARNING "Using custom LLVM tools to build libclc: "
"${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR}, "
" make sure that the tools are up to date." )

# find_program needs the variable to be cleared in order to perform a search.
# Make sure that the cached entries are cleared as well.
unset( LLVM_CLANG CACHE )
unset( LLVM_AS CACHE )
unset( LLVM_LINK CACHE )
unset( LLVM_OPT CACHE )
unset( LLVM_SPIRV CACHE )
unset( LIBCLC_REMANGLER CACHE )
unset( LLVM_CLANG )
unset( LLVM_AS )
unset( LLVM_LINK )
unset( LLVM_OPT )
unset( LLVM_SPIRV )
unset( LIBCLC_REMANGLER )
find_program( LLVM_CLANG clang PATHS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR}
NO_DEFAULT_PATH )
find_program( LLVM_AS llvm-as PATHS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR}
NO_DEFAULT_PATH )
find_program( LLVM_LINK llvm-link PATHS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR}
NO_DEFAULT_PATH )
find_program( LLVM_OPT opt PATHS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR}
NO_DEFAULT_PATH )
find_program( LLVM_SPIRV llvm-spirv
PATHS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
find_program( LIBCLC_REMANGLER libclc-remangler
PATHS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
# First clear the variables,
ClearVariables( "${BINARY_VARIABLES}" )
# then set.
FindToolBinary( "${BINARY_VARIABLES}" "${BINARY_NAMES}"
${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} )

if( NOT LLVM_CLANG OR NOT LLVM_OPT OR NOT LLVM_AS OR NOT LLVM_LINK
OR NOT LIBCLC_REMANGLER )
message( WARNING
"Custom toolchain incomplete, will try the default location." )
message( FATAL_ERROR "Custom toolchain incomplete!" )
endif()
endif()

Expand All @@ -164,13 +171,8 @@ message( "" )

# It's OK to call find program again, if the variables have been set in the
# custom location clause, find_program returns immediately.
find_program( LLVM_CLANG clang PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
find_program( LLVM_AS llvm-as PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
find_program( LLVM_LINK llvm-link PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
find_program( LLVM_OPT opt PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
find_program( LLVM_SPIRV llvm-spirv PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
find_program( LIBCLC_REMANGLER libclc-remangler PATHS ${LLVM_TOOLS_BINARY_DIR}
NO_DEFAULT_PATH )
FindToolBinary( "${BINARY_VARIABLES}" "${BINARY_NAMES}"
${LLVM_TOOLS_BINARY_DIR} )

# Print toolchain
message( "clang: ${LLVM_CLANG}" )
Expand Down

0 comments on commit b26c4aa

Please sign in to comment.