Skip to content

Commit

Permalink
List headers with target_sources FILE_SETS (#8370)
Browse files Browse the repository at this point in the history
Removes instances of target_include_directories
and installation rules based on those directories.
These are now automatically computed from the
BASE_DIRS (defaults to current source dir) argument
to target_sources.

This models the build more accurately and avoids
accidental installation of unwanted headers. Also
forces us to think about the linking relationships
between components; ideally this will result in a
more accurate build graph.
  • Loading branch information
alexreinking authored Aug 7, 2024
1 parent 17bd517 commit 40ab265
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 88 deletions.
37 changes: 14 additions & 23 deletions packaging/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ install(TARGETS Halide Halide_Generator Halide_GenGen Halide_LanguageOptions
LIBRARY COMPONENT Halide_Runtime
NAMELINK_COMPONENT Halide_Development
ARCHIVE COMPONENT Halide_Development
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
FILE_SET HEADERS COMPONENT Halide_Development)

if (TARGET Halide_Adams2019)
install(TARGETS Halide_Adams2019 Halide_Li2018 Halide_Mullapudi2016 Halide_Anderson2021
Expand All @@ -43,25 +43,27 @@ foreach (dep IN ITEMS Halide_LLVM Halide_wabt)
endif ()
endforeach ()

##
# Runtime headers
##

install(TARGETS Halide_Runtime
EXPORT Halide_Interfaces
FILE_SET HEADERS COMPONENT Halide_Development)

##
# Library-type-agnostic interface targets
##

target_sources(Halide_RunGenMain INTERFACE $<INSTALL_INTERFACE:${Halide_INSTALL_TOOLSDIR}/RunGenMain.cpp>)

install(TARGETS Halide_Tools Halide_ImageIO Halide_RunGenMain Halide_ThreadPool
EXPORT Halide_Interfaces
INCLUDES DESTINATION ${Halide_INSTALL_TOOLSDIR})
install(FILES ${Halide_SOURCE_DIR}/tools/RunGenMain.cpp
DESTINATION ${Halide_INSTALL_TOOLSDIR}
COMPONENT Halide_Development)

install(TARGETS Halide_Runtime
install(TARGETS Halide_Tools Halide_ImageIO Halide_RunGenMain Halide_ThreadPool
EXPORT Halide_Interfaces
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

# Captures both the runtime and Halide.h
install(DIRECTORY ${Halide_BINARY_DIR}/include/
TYPE INCLUDE
COMPONENT Halide_Development
FILES_MATCHING PATTERN "include/*.h")
FILE_SET HEADERS COMPONENT Halide_Development DESTINATION ${Halide_INSTALL_TOOLSDIR})

##
# Patch RPATH for executable targets
Expand Down Expand Up @@ -113,17 +115,6 @@ install(FILES
# Tools
##

install(DIRECTORY ${Halide_SOURCE_DIR}/tools/
DESTINATION ${Halide_INSTALL_TOOLSDIR}
COMPONENT Halide_Development
FILES_MATCHING
PATTERN "*.h"
PATTERN "*.cpp"
PATTERN "*.m"
PATTERN "binary2cpp.cpp" EXCLUDE
PATTERN "build_halide_h.cpp" EXCLUDE
PATTERN "find_inverse.cpp" EXCLUDE)

install(PROGRAMS ${Halide_SOURCE_DIR}/src/autoschedulers/adams2019/adams2019_autotune_loop.sh
${Halide_SOURCE_DIR}/src/autoschedulers/anderson2021/anderson2021_autotune_loop.sh
DESTINATION ${Halide_INSTALL_TOOLSDIR}
Expand Down
72 changes: 44 additions & 28 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
##
# Declare the Halide library target.
##

add_library(Halide)
add_library(Halide::Halide ALIAS Halide)

##
# Lists of source files. Keep ALL lists sorted in alphabetical order.
##

# The externally-visible header files that go into making Halide.h.
# Don't include anything here that includes llvm headers.
# Also *don't* include anything that's only used internally (eg SpirvIR.h).
set(HEADER_FILES
target_sources(
Halide
PRIVATE
FILE_SET private_headers
TYPE HEADERS
FILES
AbstractGenerator.h
AddAtomicMutex.h
AddImageChecks.h
Expand Down Expand Up @@ -175,9 +187,13 @@ set(HEADER_FILES
VectorizeLoops.h
WasmExecutor.h
WrapCalls.h
)
)

set(SOURCE_FILES
# The sources that go into libHalide. For the sake of IDE support, headers that
# exist in src/ but are not public should be included here.
target_sources(
Halide
PRIVATE
AbstractGenerator.cpp
AddAtomicMutex.cpp
AddImageChecks.cpp
Expand Down Expand Up @@ -363,7 +379,7 @@ set(SOURCE_FILES
VectorizeLoops.cpp
WasmExecutor.cpp
WrapCalls.cpp
)
)

set(C_TEMPLATE_FILES
CodeGen_C_prologue
Expand All @@ -381,12 +397,13 @@ set(HTML_TEMPLATE_FILES
##

add_subdirectory(runtime)
target_link_libraries(Halide PRIVATE "$<BUILD_LOCAL_INTERFACE:Halide::initmod>")
target_link_libraries(Halide INTERFACE Halide::Runtime)

##
# Build the template files via binary2cpp.
##

add_library(Halide_c_templates OBJECT)
foreach (f IN LISTS C_TEMPLATE_FILES)
set(SRC "$<SHELL_PATH:${CMAKE_CURRENT_SOURCE_DIR}/${f}.template.cpp>")
set(DST "c_template.${f}.template.cpp")
Expand All @@ -395,7 +412,7 @@ foreach (f IN LISTS C_TEMPLATE_FILES)
COMMAND binary2cpp "halide_c_template_${f}" < "${SRC}" > "${DST}"
DEPENDS "${SRC}" binary2cpp
VERBATIM)
target_sources(Halide_c_templates PRIVATE ${DST})
target_sources(Halide PRIVATE ${DST})
endforeach ()

foreach (f IN LISTS HTML_TEMPLATE_FILES)
Expand All @@ -407,34 +424,33 @@ foreach (f IN LISTS HTML_TEMPLATE_FILES)
COMMAND binary2cpp "${VARNAME}" < "${SRC}" > "${DST}"
DEPENDS "${SRC}" binary2cpp
VERBATIM)
target_sources(Halide_c_templates PRIVATE ${DST})
target_sources(Halide PRIVATE ${DST})
endforeach ()


##
# Build the Halide mono-header.
##

set(HALIDE_H "${Halide_BINARY_DIR}/include/Halide.h")
set(LICENSE_PATH "${Halide_SOURCE_DIR}/LICENSE.txt")
set(headers "$<TARGET_PROPERTY:Halide,HEADER_SET_private_headers>")
add_custom_command(OUTPUT "${HALIDE_H}"
COMMAND ${CMAKE_COMMAND} -E make_directory "$<SHELL_PATH:${Halide_BINARY_DIR}/include>"
COMMAND build_halide_h "$<SHELL_PATH:${LICENSE_PATH}>" ${HEADER_FILES} > "$<SHELL_PATH:${HALIDE_H}>"
DEPENDS build_halide_h "${LICENSE_PATH}" ${HEADER_FILES}
COMMAND build_halide_h "$<SHELL_PATH:${LICENSE_PATH}>" "${headers}" > "$<SHELL_PATH:${HALIDE_H}>"
DEPENDS build_halide_h "${LICENSE_PATH}" "${headers}"
WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}"
COMMAND_EXPAND_LISTS
VERBATIM)
add_custom_target(HalideIncludes DEPENDS "${HALIDE_H}")
add_dependencies(Halide HalideIncludes)

##
# Define the Halide library target.
##

add_library(Halide
${SOURCE_FILES}
${HEADER_FILES}
# Including these as sources works around the need to "install" Halide_initmod
$<TARGET_OBJECTS:Halide_initmod>
$<TARGET_OBJECTS:Halide_c_templates>)
target_sources(
Halide
INTERFACE
FILE_SET HEADERS
BASE_DIRS "${Halide_BINARY_DIR}/include"
FILES "${Halide_BINARY_DIR}/include/Halide.h"
)

##
# Flatbuffers and Serialization dependencies.
Expand Down Expand Up @@ -491,8 +507,14 @@ if (WITH_SERIALIZATION)
)
add_custom_target(generate_fb_header DEPENDS "${fb_header}")

add_dependencies(Halide generate_fb_header)
target_include_directories(Halide PRIVATE "$<BUILD_INTERFACE:${fb_dir}>")
target_sources(
Halide
PRIVATE
FILE_SET fb_headers
TYPE HEADERS
BASE_DIRS "${fb_dir}"
FILES "${fb_header}"
)
target_link_libraries(Halide PRIVATE ${flatbuffers_target})
target_compile_definitions(Halide PRIVATE WITH_SERIALIZATION)
endif ()
Expand All @@ -507,8 +529,6 @@ if (WITH_SERIALIZATION_JIT_ROUNDTRIP_TESTING)
target_compile_definitions(Halide PRIVATE WITH_SERIALIZATION_JIT_ROUNDTRIP_TESTING)
endif ()

add_library(Halide::Halide ALIAS Halide)

target_link_libraries(Halide PRIVATE Halide::LLVM)
target_link_libraries(Halide PUBLIC Halide::LanguageOptions)
target_compile_features(Halide PUBLIC cxx_std_17)
Expand Down Expand Up @@ -542,10 +562,6 @@ target_compile_definitions(Halide PUBLIC
HALIDE_VERSION_MINOR=${Halide_VERSION_MINOR}
HALIDE_VERSION_PATCH=${Halide_VERSION_PATCH})


target_include_directories(Halide INTERFACE "$<BUILD_INTERFACE:${Halide_BINARY_DIR}/include>")
add_dependencies(Halide HalideIncludes)

if (TARGET Halide_wabt)
target_link_libraries(Halide PRIVATE Halide_wabt)
target_compile_definitions(Halide PRIVATE WITH_WABT)
Expand Down
6 changes: 3 additions & 3 deletions src/autoschedulers/adams2019/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ if (WITH_UTILS)
retrain_cost_model.cpp
$<TARGET_OBJECTS:adams2019_weights_obj>)
target_include_directories(adams2019_retrain_cost_model PRIVATE "${Halide_SOURCE_DIR}/src/autoschedulers/adams2019")
target_link_libraries(adams2019_retrain_cost_model PRIVATE ASLog adams2019_cost_model adams2019_train_cost_model Halide::Halide Halide::Plugin)
target_link_libraries(adams2019_retrain_cost_model PRIVATE Halide::ASLog adams2019_cost_model adams2019_train_cost_model Halide::Halide Halide::Plugin)
endif ()

# =================================================================
Expand All @@ -103,7 +103,7 @@ add_autoscheduler(
)

target_include_directories(Halide_Adams2019 PRIVATE "${Halide_SOURCE_DIR}/src/autoschedulers/adams2019")
target_link_libraries(Halide_Adams2019 PRIVATE ASLog ParamParser adams2019_cost_model adams2019_train_cost_model)
target_link_libraries(Halide_Adams2019 PRIVATE Halide::ASLog adams2019_cost_model adams2019_train_cost_model)

# ====================================================
# Auto-tuning support utilities.
Expand All @@ -120,7 +120,7 @@ endif ()

if (WITH_TESTS)
add_executable(adams2019_test_function_dag test_function_dag.cpp FunctionDAG.cpp)
target_link_libraries(adams2019_test_function_dag PRIVATE ASLog Halide::Halide Halide::Tools Halide::Plugin)
target_link_libraries(adams2019_test_function_dag PRIVATE Halide::ASLog Halide::Halide Halide::Tools Halide::Plugin)
add_test(NAME adams2019_test_function_dag COMMAND adams2019_test_function_dag)
set_tests_properties(adams2019_test_function_dag PROPERTIES LABELS "adams2019;autoschedulers_cpu")
endif()
18 changes: 9 additions & 9 deletions src/autoschedulers/anderson2021/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ if (WITH_UTILS)
retrain_cost_model.cpp
$<TARGET_OBJECTS:anderson2021_weights_obj>)
target_include_directories(anderson2021_retrain_cost_model PRIVATE "${Halide_SOURCE_DIR}/src/autoschedulers/anderson2021")
target_link_libraries(anderson2021_retrain_cost_model PRIVATE ASLog anderson2021_cost_model
target_link_libraries(anderson2021_retrain_cost_model PRIVATE Halide::ASLog anderson2021_cost_model
anderson2021_train_cost_model Halide::Halide Halide::Plugin)
endif ()

Expand All @@ -60,7 +60,7 @@ add_autoscheduler(
)

target_include_directories(Halide_Anderson2021 PRIVATE "${Halide_SOURCE_DIR}/src/autoschedulers/anderson2021")
target_link_libraries(Halide_Anderson2021 PRIVATE ASLog ParamParser
target_link_libraries(Halide_Anderson2021 PRIVATE Halide::ASLog
anderson2021_cost_model anderson2021_train_cost_model)

## ====================================================
Expand Down Expand Up @@ -90,44 +90,44 @@ if (WITH_TESTS)

add_executable(anderson2021_test_function_dag test_function_dag.cpp FunctionDAG.cpp)
target_include_directories(anderson2021_test_function_dag PRIVATE "${Halide_SOURCE_DIR}/src/autoschedulers/anderson2021")
target_link_libraries(anderson2021_test_function_dag PRIVATE ASLog Halide::Halide Halide::Tools Halide::Plugin)
target_link_libraries(anderson2021_test_function_dag PRIVATE Halide::ASLog Halide::Halide Halide::Tools Halide::Plugin)

_add_test(anderson2021_test_function_dag)

add_executable(anderson2021_test_bounds test/bounds.cpp FunctionDAG.cpp LoopNest.cpp GPULoopInfo.cpp Tiling.cpp)
target_include_directories(anderson2021_test_bounds PRIVATE "${Halide_SOURCE_DIR}/src/autoschedulers/anderson2021")
target_link_libraries(anderson2021_test_bounds PRIVATE ASLog Halide::Halide Halide::Tools Halide::Plugin)
target_link_libraries(anderson2021_test_bounds PRIVATE Halide::ASLog Halide::Halide Halide::Tools Halide::Plugin)

_add_test(anderson2021_test_bounds)

add_executable(anderson2021_test_parser test/parser.cpp)
target_include_directories(anderson2021_test_parser PRIVATE "${Halide_SOURCE_DIR}/src/autoschedulers/anderson2021")
target_link_libraries(anderson2021_test_parser PRIVATE ASLog Halide::Halide Halide::Tools Halide::Plugin)
target_link_libraries(anderson2021_test_parser PRIVATE Halide::ASLog Halide::Halide Halide::Tools Halide::Plugin)

_add_test(anderson2021_test_parser)

add_executable(anderson2021_test_state test/state.cpp FunctionDAG.cpp LoopNest.cpp GPULoopInfo.cpp State.cpp Tiling.cpp)
target_include_directories(anderson2021_test_state PRIVATE "${Halide_SOURCE_DIR}/src/autoschedulers/anderson2021")
target_link_libraries(anderson2021_test_state PRIVATE ASLog Halide::Halide Halide::Tools Halide::Plugin)
target_link_libraries(anderson2021_test_state PRIVATE Halide::ASLog Halide::Halide Halide::Tools Halide::Plugin)

_add_test(anderson2021_test_state)

add_executable(anderson2021_test_storage_strides test/storage_strides.cpp FunctionDAG.cpp LoopNest.cpp GPULoopInfo.cpp State.cpp Tiling.cpp)
target_include_directories(anderson2021_test_storage_strides PRIVATE "${Halide_SOURCE_DIR}/src/autoschedulers/anderson2021")
target_link_libraries(anderson2021_test_storage_strides PRIVATE ASLog Halide::Halide Halide::Tools Halide::Plugin)
target_link_libraries(anderson2021_test_storage_strides PRIVATE Halide::ASLog Halide::Halide Halide::Tools Halide::Plugin)

_add_test(anderson2021_test_storage_strides)

add_executable(anderson2021_test_thread_info test/thread_info.cpp LoopNest.cpp
FunctionDAG.cpp GPULoopInfo.cpp Tiling.cpp)
target_include_directories(anderson2021_test_thread_info PRIVATE "${Halide_SOURCE_DIR}/src/autoschedulers/anderson2021")
target_link_libraries(anderson2021_test_thread_info PRIVATE ASLog Halide::Halide Halide::Tools Halide::Plugin)
target_link_libraries(anderson2021_test_thread_info PRIVATE Halide::ASLog Halide::Halide Halide::Tools Halide::Plugin)

_add_test(anderson2021_test_thread_info)

add_executable(anderson2021_test_tiling test/tiling.cpp Tiling.cpp)
target_include_directories(anderson2021_test_tiling PRIVATE "${Halide_SOURCE_DIR}/src/autoschedulers/anderson2021")
target_link_libraries(anderson2021_test_tiling PRIVATE ASLog Halide::Halide Halide::Tools Halide::Plugin)
target_link_libraries(anderson2021_test_tiling PRIVATE Halide::ASLog Halide::Halide Halide::Tools Halide::Plugin)

_add_test(anderson2021_test_tiling)
endif()
32 changes: 22 additions & 10 deletions src/autoschedulers/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
add_library(Halide_Plugin INTERFACE)
add_library(Halide::Plugin ALIAS Halide_Plugin)
target_include_directories(Halide_Plugin INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
target_link_libraries(Halide_Plugin INTERFACE Halide::Halide)
target_sources(
Halide_Plugin
INTERFACE
FILE_SET HEADERS
FILES
Errors.h
HalidePlugin.h
ParamParser.h
cmdline.h
PerfectHashMap.h
)
target_link_libraries(Halide_Plugin INTERFACE Halide::Halide Halide::ASLog)

add_library(ASLog STATIC ASLog.cpp)
target_include_directories(ASLog PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
set_property(TARGET ASLog PROPERTY POSITION_INDEPENDENT_CODE YES)

# Sigh, header-only libraries shouldn't be special
add_library(ParamParser INTERFACE)
target_include_directories(ParamParser INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
add_library(Halide_ASLog STATIC)
add_library(Halide::ASLog ALIAS Halide_ASLog)
target_sources(
Halide_ASLog
PRIVATE ASLog.cpp
PUBLIC
FILE_SET HEADERS
FILES ASLog.h
)
set_property(TARGET Halide_ASLog PROPERTY POSITION_INDEPENDENT_CODE YES)

if (WITH_UTILS)
add_executable(featurization_to_sample featurization_to_sample.cpp)
Expand Down
2 changes: 0 additions & 2 deletions src/autoschedulers/li2018/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
add_autoscheduler(NAME Li2018 SOURCES GradientAutoscheduler.cpp)
target_link_libraries(Halide_Li2018 PRIVATE ParamParser)

1 change: 0 additions & 1 deletion src/autoschedulers/mullapudi2016/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
add_autoscheduler(NAME Mullapudi2016 SOURCES AutoSchedule.cpp)
target_link_libraries(Halide_Mullapudi2016 PRIVATE ParamParser)
Loading

0 comments on commit 40ab265

Please sign in to comment.