Skip to content

Commit

Permalink
Dependency wrangling part 0/N: standard CMake modules (#7658)
Browse files Browse the repository at this point in the history
* Hoist Threads::Threads to the top level

* Remove global OpenGL dependency

This is added by the helpers as-needed. Removing it here lets one build
just libHalide without searching for OpenGL.

* Narrow scope of OpenMP to tutorial

Only the tutorial targets actually use OpenMP. Don't search for OpenMP
if WITH_TUTORIALS is off.

* Move JPEG and PNG deps to tools

Only the Halide::ImageIO library uses these directly, so limiting the
scope protects against unintented use.

* Work around CMake bug

The CMake $<TARGET_NAME_IF_EXISTS:...> genex uses dynamic scoping w.r.t.
the target environment, rather than the usual static scoping. This means
we need to move the PNG and JPEG dependencies higher up.

* Add link to CMake issue in comments.
  • Loading branch information
alexreinking committed Jun 28, 2023
1 parent 470f43c commit 6f2cae6
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 49 deletions.
20 changes: 19 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,27 @@ endif()
# Import dependencies
##

## Threads
option(THREADS_PREFER_PTHREAD_FLAG "When enabled, prefer to use the -pthread flag to explicit linking" ON)
find_package(Threads REQUIRED)

## Complex dependencies
add_subdirectory(dependencies)

## Image formats

# This changes how find_xxx() commands work; the default is to find frameworks before
# standard libraries or headers, but this can be a problem on systems that have Mono
# installed, as it has a framework with the libjpeg and libpng headers present -- so
# CMake finds the headers from Mono but the libraries from Homebrew, and hilarity ensues.
# Setting this to "last" means we always try the standard libraries before the frameworks.
set(CMAKE_FIND_FRAMEWORK LAST)

# TODO: these really belong in tools/, but CMake has a weird bug with $<TARGET_NAME_IF_EXISTS:...>
# https://gitlab.kitware.com/cmake/cmake/-/issues/25033
find_package(JPEG)
find_package(PNG)

##
# Declare options
##
Expand All @@ -114,7 +133,6 @@ cmake_dependent_option(
"Halide_ENABLE_RTTI AND Halide_ENABLE_EXCEPTIONS" OFF
)


##
# Add source directories
##
Expand Down
22 changes: 0 additions & 22 deletions dependencies/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
##
# Standard CMake find-module dependencies
##

set(THREADS_PREFER_PTHREAD_FLAG YES)
find_package(Threads REQUIRED)
set_target_properties(Threads::Threads PROPERTIES IMPORTED_GLOBAL TRUE)

# TODO(https://github.com/halide/Halide/issues/5633): verify this is still correct / necessary for OpenGLCompute
find_package(OpenGL)
if (TARGET OpenGL::GL)
set_target_properties(OpenGL::GL PROPERTIES IMPORTED_GLOBAL TRUE)
endif ()

find_package(OpenMP)
if (TARGET OpenMP::OpenMP_CXX)
set_target_properties(OpenMP::OpenMP_CXX PROPERTIES IMPORTED_GLOBAL TRUE)
endif ()

##
# Third-party dependencies in their own subdirectories
##

add_subdirectory(llvm)

add_subdirectory(jpeg)
add_subdirectory(png)

if (TARGET_SPIRV)
add_subdirectory(spirv)
endif()
Expand Down
13 changes: 0 additions & 13 deletions dependencies/jpeg/CMakeLists.txt

This file was deleted.

13 changes: 0 additions & 13 deletions dependencies/png/CMakeLists.txt

This file was deleted.

3 changes: 3 additions & 0 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ target_compile_options(regexp_replace PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/wd4996>
# Interface target for enabling PNG/JPEG support in Halide
##

# TODO: if/when CMake fixes https://gitlab.kitware.com/cmake/cmake/-/issues/25033
# then move find_package(PNG/JPEG) here.

add_library(Halide_ImageIO INTERFACE)
add_library(Halide::ImageIO ALIAS Halide_ImageIO)
set_target_properties(Halide_ImageIO PROPERTIES EXPORT_NAME ImageIO)
Expand Down
2 changes: 2 additions & 0 deletions tutorial/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
find_package(OpenMP)

configure_file(images/gray.png images/gray.png COPYONLY)
configure_file(images/rgb.png images/rgb.png COPYONLY)

Expand Down

0 comments on commit 6f2cae6

Please sign in to comment.