Skip to content

Commit

Permalink
Simplify target search
Browse files Browse the repository at this point in the history
  • Loading branch information
alexreinking committed Jul 23, 2020
1 parent 1399294 commit 9f19e1b
Showing 1 changed file with 47 additions and 45 deletions.
92 changes: 47 additions & 45 deletions packaging/HalideConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,82 +12,84 @@ endif ()
# Allow people to specify explicitly that they only want Halide
list(REMOVE_ITEM ${CMAKE_FIND_PACKAGE_NAME}_comps Halide)

# Parse components for static/shared preference.
foreach (comp IN ITEMS static shared)
if (comp IN_LIST ${CMAKE_FIND_PACKAGE_NAME}_comps)
set(${CMAKE_FIND_PACKAGE_NAME}_${comp} YES)
list(REMOVE_ITEM ${CMAKE_FIND_PACKAGE_NAME}_comps ${comp})
endif ()
endforeach ()

# Note when both static AND shared are requested
if (${CMAKE_FIND_PACKAGE_NAME}_static AND ${CMAKE_FIND_PACKAGE_NAME}_shared)
set(${CMAKE_FIND_PACKAGE_NAME}_both TRUE)
endif ()

include(CMakeFindDependencyMacro)
find_dependency(Threads)
@__find_LLVM_deps@

# Load common targets that do not depend on shared/static distinction
include("${CMAKE_CURRENT_LIST_DIR}/Halide-Interfaces.cmake")

macro(_Halide_include ARG MSG)
if (NOT EXISTS "${CMAKE_CURRENT_LIST_DIR}/${ARG}")
set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE ${MSG})

# Helper to load targets if they exist, report failure, and create aliases when a single type was requested
macro(_Halide_include TYPE CAUSE)
if (NOT EXISTS "${CMAKE_CURRENT_LIST_DIR}/Halide-Targets-${TYPE}.cmake")
set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE
"Could not find Halide ${TYPE} libraries. ${CAUSE}")
set(${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)
return()
endif ()
include("${CMAKE_CURRENT_LIST_DIR}/${ARG}")
endmacro()

macro(_Halide_aliases TYPE)
add_library(Halide::Halide ALIAS Halide::${TYPE}::Halide)
add_library(Halide::Generator ALIAS Halide::${TYPE}::Generator)
add_library(Halide::RunGenMain ALIAS Halide::${TYPE}::RunGenMain)
endmacro()
include("${CMAKE_CURRENT_LIST_DIR}/Halide-Targets-${TYPE}.cmake")

foreach (comp IN ITEMS static shared)
if (comp IN_LIST ${CMAKE_FIND_PACKAGE_NAME}_comps)
set(${CMAKE_FIND_PACKAGE_NAME}_${comp} YES)
list(REMOVE_ITEM ${CMAKE_FIND_PACKAGE_NAME}_comps ${comp})
_Halide_include("Halide-Targets-${comp}.cmake"
"Could not find Halide ${comp} libraries as requested by component")
if (NOT ${CMAKE_FIND_PACKAGE_NAME}_both)
add_library(Halide::Halide ALIAS Halide::${TYPE}::Halide)
add_library(Halide::Generator ALIAS Halide::${TYPE}::Generator)
add_library(Halide::RunGenMain ALIAS Halide::${TYPE}::RunGenMain)
endif ()
endforeach ()
endmacro()

# Decide which types to load based on
if (${CMAKE_FIND_PACKAGE_NAME}_static OR ${CMAKE_FIND_PACKAGE_NAME}_shared)
if (NOT ${CMAKE_FIND_PACKAGE_NAME}_static)
# only shared requested
_Halide_aliases(shared)
elseif (NOT ${CMAKE_FIND_PACKAGE_NAME}_shared)
# only static requested
_Halide_aliases(static)
if (${CMAKE_FIND_PACKAGE_NAME}_shared)
_Halide_include("shared" "Required by 'shared' component.")
endif ()
# no aliases when both are requested.
elseif (DEFINED ${Halide_SHARED_LIBS})
if (${CMAKE_FIND_PACKAGE_NAME}_static)
_Halide_include("static" "Required by 'static' component.")
endif ()
elseif (DEFINED Halide_SHARED_LIBS)
# Require whatever was requested
if (Halide_SHARED_LIBS)
_Halide_include("Halide-Targets-shared.cmake"
"Could not find Halide shared libraries as requested by Halide_SHARED_LIBS")
_Halide_aliases(shared)
_Halide_include("shared" "Required by Halide_SHARED_LIBS=${Halide_SHARED_LIBS}.")
else ()
_Halide_include("Halide-Targets-static.cmake"
"Could not find Halide static libraries as requested by Halide_SHARED_LIBS")
_Halide_aliases(static)
_Halide_include("static" "Required by Halide_SHARED_LIBS=${Halide_SHARED_LIBS}.")
endif ()
elseif (BUILD_SHARED_LIBS OR NOT DEFINED BUILD_SHARED_LIBS)
# Try shared first, then fall back to static
# Try shared first, then fall back to static.
# Halide prefers shared by default when BUILD_SHARED_LIBS is not defined,
# so this is mimicked here.
if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/Halide-Targets-shared.cmake")
_Halide_include("Halide-Targets-shared.cmake"
"Could not find any Halide binaries. Searched for shared, static.")
_Halide_aliases(shared)
_Halide_include("shared" "Searched for shared, static.")
else ()
_Halide_include("Halide-Targets-static.cmake"
"Could not find any Halide binaries. Searched for shared, static.")
_Halide_aliases(static)
_Halide_include("static" "Searched for shared, static.")
endif ()
else ()
# Try static first, then fall back to shared
if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/Halide-Targets-static.cmake")
_Halide_include("Halide-Targets-static.cmake"
"Could not find any Halide binaries. Searched for static, shared.")
_Halide_aliases(static)
_Halide_include("static" "Searched for static, shared.")
else ()
_Halide_include("Halide-Targets-shared.cmake"
"Could not find any Halide binaries. Searched for static, shared.")
_Halide_aliases(shared)
_Halide_include("shared" "Searched for static, shared.")
endif ()
endif ()

include("${CMAKE_CURRENT_LIST_DIR}/HalideGeneratorHelpers.cmake")
# Aliases are not created, so the helpers aren't available.
if (NOT ${CMAKE_FIND_PACKAGE_NAME}_both)
include("${CMAKE_CURRENT_LIST_DIR}/HalideGeneratorHelpers.cmake")
endif ()

# Load image library dependencies
foreach (comp IN LISTS ${CMAKE_FIND_PACKAGE_NAME}_comps)
if (NOT ${comp} IN_LIST ${CMAKE_FIND_PACKAGE_NAME}_known_components)
set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE
Expand Down

0 comments on commit 9f19e1b

Please sign in to comment.