Skip to content

Commit 17bd517

Browse files
authored
Clean up serialization build code (#8369)
1 parent 37ab461 commit 17bd517

File tree

2 files changed

+26
-48
lines changed

2 files changed

+26
-48
lines changed

packaging/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ if (TARGET Halide_Adams2019)
3737
endif ()
3838

3939
# Halide_LLVM
40-
foreach (dep IN ITEMS Halide_LLVM Halide_wabt Halide_flatbuffers)
40+
foreach (dep IN ITEMS Halide_LLVM Halide_wabt)
4141
if (TARGET ${dep})
4242
install(TARGETS ${dep} EXPORT Halide_Targets)
4343
endif ()

src/CMakeLists.txt

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -442,68 +442,46 @@ add_library(Halide
442442

443443
# Build serialization, enabled by default
444444
option(WITH_SERIALIZATION "Include experimental Serialization/Deserialization code" ON)
445-
446-
# flatbuffers is small and compiles quickly, but if you want/need to use
447-
# a local version (via find_package), configure with FLATBUFFERS_USE_FETCHCONTENT=OFF
448-
option(FLATBUFFERS_USE_FETCHCONTENT "Enable to download the Flatbuffers library via FetchContent" ON)
449-
set(FLATBUFFERS_VER 23.5.26 CACHE STRING "The Flatbuffers version to use (or download)")
450-
451445
if (WITH_SERIALIZATION)
446+
# flatbuffers is small and compiles quickly, but if you want/need to use
447+
# a local version (via find_package), configure with FLATBUFFERS_USE_FETCHCONTENT=OFF
448+
option(FLATBUFFERS_USE_FETCHCONTENT "Enable to download the Flatbuffers library via FetchContent" ON)
449+
set(FLATBUFFERS_VER 23.5.26 CACHE STRING "The Flatbuffers version to use (or download) ")
450+
452451
if (FLATBUFFERS_USE_FETCHCONTENT)
453452
include(FetchContent)
454-
message(STATUS "Fetching flatbuffers ${FLATBUFFERS_VER}...")
455453
FetchContent_Declare(
456454
flatbuffers
457455
GIT_REPOSITORY https://github.com/google/flatbuffers.git
458456
GIT_TAG v${FLATBUFFERS_VER}
459457
GIT_SHALLOW TRUE
458+
SYSTEM
460459
)
461460
# configuration for flatbuffers
462461
set(FLATBUFFERS_BUILD_TESTS OFF)
463462
set(FLATBUFFERS_INSTALL OFF)
464463
FetchContent_MakeAvailable(flatbuffers)
465464
set_target_properties(flatbuffers PROPERTIES POSITION_INDEPENDENT_CODE ON)
466465

467-
add_library(Halide_flatbuffers INTERFACE)
468-
target_sources(Halide_flatbuffers INTERFACE $<BUILD_INTERFACE:$<TARGET_OBJECTS:flatbuffers>>)
469-
target_include_directories(Halide_flatbuffers
470-
SYSTEM # Use -isystem instead of -I; this is a trick so that clang-tidy won't analyze these includes
471-
INTERFACE
472-
$<BUILD_INTERFACE:${flatbuffers_SOURCE_DIR}>/include
473-
$<BUILD_INTERFACE:${flatbuffers_BINARY_DIR}>/include)
474-
set_target_properties(Halide_flatbuffers PROPERTIES EXPORT_NAME flatbuffers)
475-
466+
add_library(flatbuffers::flatbuffers ALIAS flatbuffers)
476467
add_executable(flatbuffers::flatc ALIAS flatc)
468+
477469
message(STATUS "Using fetched-and-built flatbuffers, version ${FLATBUFFERS_VER}")
470+
set(flatbuffers_target "$<BUILD_LOCAL_INTERFACE:flatbuffers::flatbuffers>")
478471
else ()
479-
# Sadly, there seem to be at least three variations of the Flatbuffer package
480-
# in terms of the case of the relevant CMake files; if we guess wrong, we
481-
# fail on case-sensitive file systems. We'll try this as a hack workaround:
482-
# just try all three. (Note that the internal CMake library name appears to be
483-
# `flatbuffers` in all cases.)
484-
set(FB_NAME "")
485-
foreach (N IN ITEMS flatbuffers Flatbuffers FlatBuffers)
486-
# TODO: should we check the version here?
487-
find_package(${N} QUIET)
488-
if (${N}_FOUND)
489-
set(FB_NAME ${N})
490-
message(STATUS "Using installed flatbuffers, version ${${N}_VERSION}")
491-
break()
492-
endif ()
493-
endforeach ()
494-
495-
if (NOT FB_NAME)
496-
message(FATAL_ERROR "WITH_SERIALIZATION is ON and FLATBUFFERS_USE_FETCHCONTENT is OFF, "
497-
"but could not find flatbuffers installed locally. "
498-
"Either install flatbuffers or build with WITH_SERIALIZATION=OFF.")
499-
endif ()
500-
501-
add_library(Halide_flatbuffers ALIAS flatbuffers::flatbuffers)
472+
# Sadly, there seem to be at least three variations of the Flatbuffer
473+
# package in terms of the case of the relevant CMake files. Fortunately,
474+
# the IMPORTED targets appear to be consistently named `flatbuffers`.
475+
find_package(
476+
flatbuffers ${FLATBUFFERS_VER}
477+
NAMES flatbuffers Flatbuffers FlatBuffers
478+
REQUIRED
479+
)
480+
set(flatbuffers_target flatbuffers::flatbuffers)
502481
endif ()
503482

504-
set(fb_dir "${Halide_BINARY_DIR}/flatc/include")
505-
506483
set(fb_def "${CMAKE_CURRENT_SOURCE_DIR}/halide_ir.fbs")
484+
set(fb_dir "${Halide_BINARY_DIR}/include/flatc")
507485
set(fb_header "${fb_dir}/halide_ir.fbs.h")
508486
add_custom_command(
509487
OUTPUT "${fb_header}"
@@ -512,21 +490,21 @@ if (WITH_SERIALIZATION)
512490
VERBATIM
513491
)
514492
add_custom_target(generate_fb_header DEPENDS "${fb_header}")
515-
set_source_files_properties("${fb_header}" PROPERTIES GENERATED TRUE)
516493

517494
add_dependencies(Halide generate_fb_header)
518495
target_include_directories(Halide PRIVATE "$<BUILD_INTERFACE:${fb_dir}>")
519-
target_link_libraries(Halide PRIVATE Halide_flatbuffers)
496+
target_link_libraries(Halide PRIVATE ${flatbuffers_target})
520497
target_compile_definitions(Halide PRIVATE WITH_SERIALIZATION)
521498
endif ()
522499

523500
# Enable serialization testing by intercepting JIT compilation with a serialization roundtrip;
524501
# This is used only for special builds made specifically for testing, and must be disabled by default.
525-
option(WITH_SERIALIZATION_JIT_ROUNDTRIP_TESTING "Intercepting JIT compilation with a serialization roundtrip, for test only" OFF)
502+
cmake_dependent_option(
503+
WITH_SERIALIZATION_JIT_ROUNDTRIP_TESTING "Intercepting JIT compilation with a serialization roundtrip, for test only" OFF
504+
"WITH_SERIALIZATION" OFF
505+
)
526506
if (WITH_SERIALIZATION_JIT_ROUNDTRIP_TESTING)
527-
if (WITH_SERIALIZATION)
528-
target_compile_definitions(Halide PRIVATE WITH_SERIALIZATION_JIT_ROUNDTRIP_TESTING)
529-
endif ()
507+
target_compile_definitions(Halide PRIVATE WITH_SERIALIZATION_JIT_ROUNDTRIP_TESTING)
530508
endif ()
531509

532510
add_library(Halide::Halide ALIAS Halide)

0 commit comments

Comments
 (0)