Skip to content

Commit b26c4aa

Browse files
committed
Abstract to functions.
1 parent 6410941 commit b26c4aa

File tree

1 file changed

+37
-35
lines changed

1 file changed

+37
-35
lines changed

libclc/CMakeLists.txt

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -116,43 +116,50 @@ execute_process( COMMAND ${LLVM_CONFIG} "--bindir"
116116
# These were not properly reported in early LLVM and we don't need them
117117
list( APPEND LLVM_CXX_FLAGS -fno-rtti -fno-exceptions )
118118

119+
# List containing all the toolchain variables.
120+
list( APPEND BINARY_VARIABLES LLVM_CLANG LLVM_AS LLVM_LINK LLVM_OPT LLVM_SPIRV
121+
LIBCLC_REMANGLER )
122+
# List containing all the names of toolchain binaries.
123+
# NOTE: both lists (BINARY_VARIABLES and BINARY_NAMES) must be in sync.
124+
list( APPEND BINARY_NAMES clang llvm-as llvm-link opt llvm-spirv
125+
libclc-remangler )
126+
127+
# find_program needs the variable to be cleared in order to perform a search.
128+
# Make sure that the cached entries are cleared as well.
129+
function( ClearVariables BINARY_VARIABLES_LIST )
130+
foreach( V ${BINARY_VARIABLES_LIST} )
131+
unset( ${V} CACHE )
132+
unset( ${V} PARENT_SCOPE )
133+
endforeach( V )
134+
endfunction()
135+
136+
# Use find_program to locate toolchain binaries.
137+
function( FindToolBinary BINARY_VARIABLES_LIST BINARY_NAMES_LIST PATH_NAME )
138+
list( LENGTH BINARY_NAMES_LIST COUNT )
139+
math( EXPR COUNT "${COUNT}-1" )
140+
foreach( I RANGE ${COUNT} )
141+
list( GET BINARY_VARIABLES_LIST ${I} BV )
142+
list( GET BINARY_NAMES_LIST ${I} BN )
143+
find_program( ${BV} ${BN} PATHS ${PATH_NAME} NO_DEFAULT_PATH )
144+
endforeach( I )
145+
endfunction()
146+
119147
# Use custom toolchain to build libclc, this can be useful when dealing with
120148
# debug builds, that do not need libclc bitcode to be built using debug tools.
121149
if ( EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} )
122150
message( WARNING "Using custom LLVM tools to build libclc: "
123151
"${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR}, "
124152
" make sure that the tools are up to date." )
125153

126-
# find_program needs the variable to be cleared in order to perform a search.
127-
# Make sure that the cached entries are cleared as well.
128-
unset( LLVM_CLANG CACHE )
129-
unset( LLVM_AS CACHE )
130-
unset( LLVM_LINK CACHE )
131-
unset( LLVM_OPT CACHE )
132-
unset( LLVM_SPIRV CACHE )
133-
unset( LIBCLC_REMANGLER CACHE )
134-
unset( LLVM_CLANG )
135-
unset( LLVM_AS )
136-
unset( LLVM_LINK )
137-
unset( LLVM_OPT )
138-
unset( LLVM_SPIRV )
139-
unset( LIBCLC_REMANGLER )
140-
find_program( LLVM_CLANG clang PATHS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR}
141-
NO_DEFAULT_PATH )
142-
find_program( LLVM_AS llvm-as PATHS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR}
143-
NO_DEFAULT_PATH )
144-
find_program( LLVM_LINK llvm-link PATHS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR}
145-
NO_DEFAULT_PATH )
146-
find_program( LLVM_OPT opt PATHS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR}
147-
NO_DEFAULT_PATH )
148-
find_program( LLVM_SPIRV llvm-spirv
149-
PATHS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
150-
find_program( LIBCLC_REMANGLER libclc-remangler
151-
PATHS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
154+
# First clear the variables,
155+
ClearVariables( "${BINARY_VARIABLES}" )
156+
# then set.
157+
FindToolBinary( "${BINARY_VARIABLES}" "${BINARY_NAMES}"
158+
${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} )
159+
152160
if( NOT LLVM_CLANG OR NOT LLVM_OPT OR NOT LLVM_AS OR NOT LLVM_LINK
153161
OR NOT LIBCLC_REMANGLER )
154-
message( WARNING
155-
"Custom toolchain incomplete, will try the default location." )
162+
message( FATAL_ERROR "Custom toolchain incomplete!" )
156163
endif()
157164
endif()
158165

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

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

175177
# Print toolchain
176178
message( "clang: ${LLVM_CLANG}" )

0 commit comments

Comments
 (0)