|
| 1 | +cmake_minimum_required(VERSION 3.28) |
| 2 | + |
| 3 | +project(sparrow-ipc CXX) |
| 4 | + |
| 5 | +set(CMAKE_CXX_STANDARD 20) |
| 6 | +set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 7 | +set(CMAKE_CXX_SCAN_FOR_MODULES OFF) |
| 8 | + |
| 9 | +# Build options |
| 10 | +# ============= |
| 11 | +OPTION(BUILD_TESTS "Build sparrow-ipc test suite" OFF) |
| 12 | +MESSAGE(STATUS "🔧 Build tests: ${BUILD_TESTS}") |
| 13 | + |
| 14 | +set(SCHEMA_DIR ${CMAKE_BINARY_DIR}/format) |
| 15 | +set(FLATBUFFERS_GENERATED_DIR ${CMAKE_BINARY_DIR}/generated) |
| 16 | + |
| 17 | +find_program(FLATC_EXECUTABLE flatc) |
| 18 | + |
| 19 | +if(NOT FLATC_EXECUTABLE) |
| 20 | + message(FATAL_ERROR "flatc not found. Please install Flatbuffers.") |
| 21 | +endif() |
| 22 | + |
| 23 | +# Fetch schemas from apache arrow |
| 24 | +set(SCHEMA_URLS |
| 25 | + "https://raw.githubusercontent.com/apache/arrow/refs/heads/main/format/File.fbs" |
| 26 | + "https://raw.githubusercontent.com/apache/arrow/refs/heads/main/format/Message.fbs" |
| 27 | + "https://raw.githubusercontent.com/apache/arrow/refs/heads/main/format/Schema.fbs" |
| 28 | + "https://raw.githubusercontent.com/apache/arrow/refs/heads/main/format/Tensor.fbs" |
| 29 | + "https://raw.githubusercontent.com/apache/arrow/refs/heads/main/format/SparseTensor.fbs" |
| 30 | + # TODO what about feather.fbs? |
| 31 | +) |
| 32 | + |
| 33 | +file(MAKE_DIRECTORY ${SCHEMA_DIR}) |
| 34 | + |
| 35 | +# Download schemas |
| 36 | +set(FLATBUFFERS_SCHEMAS "") |
| 37 | +foreach(url IN LISTS SCHEMA_URLS) |
| 38 | + get_filename_component(filename ${url} NAME) |
| 39 | + message(STATUS "Downloading schema: ${url}") |
| 40 | + file(DOWNLOAD ${url} ${SCHEMA_DIR}/${filename} |
| 41 | + STATUS status |
| 42 | + SHOW_PROGRESS) |
| 43 | + list(APPEND FLATBUFFERS_SCHEMAS ${SCHEMA_DIR}/${filename}) |
| 44 | +endforeach() |
| 45 | + |
| 46 | +# Generate Flatbuffers C++ headers from the schemas |
| 47 | +file(MAKE_DIRECTORY ${FLATBUFFERS_GENERATED_DIR}) |
| 48 | + |
| 49 | +# Generate output files list |
| 50 | +set(FLATBUFFERS_GENERATED_HEADERS "") |
| 51 | +foreach(fbs_file IN LISTS FLATBUFFERS_SCHEMAS) |
| 52 | + # Generate the corresponding header file name |
| 53 | + get_filename_component(header_name ${fbs_file} NAME_WE) |
| 54 | + list(APPEND FLATBUFFERS_GENERATED_HEADERS "${FLATBUFFERS_GENERATED_DIR}/${header_name}_generated.h") |
| 55 | +endforeach() |
| 56 | + |
| 57 | +add_custom_command( |
| 58 | + OUTPUT ${FLATBUFFERS_GENERATED_HEADERS} |
| 59 | + COMMAND ${FLATC_EXECUTABLE} --cpp -o ${FLATBUFFERS_GENERATED_DIR} --cpp-std c++17 --scoped-enums ${FLATBUFFERS_SCHEMAS} |
| 60 | + DEPENDS ${FLATBUFFERS_SCHEMAS} |
| 61 | + COMMENT "Generating FlatBuffers C++ headers from schemas" |
| 62 | +) |
| 63 | + |
| 64 | +add_custom_target(generate_flatbuffers_headers |
| 65 | + DEPENDS ${FLATBUFFERS_GENERATED_HEADERS} |
| 66 | +) |
| 67 | + |
| 68 | +# Interface target for generated headers |
| 69 | +add_library(flatbuffers_interface INTERFACE) |
| 70 | +target_include_directories(flatbuffers_interface INTERFACE ${FLATBUFFERS_GENERATED_DIR}) |
| 71 | +add_dependencies(flatbuffers_interface generate_flatbuffers_headers) |
| 72 | + |
| 73 | +find_package(FlatBuffers CONFIG REQUIRED) |
| 74 | +find_package(sparrow CONFIG REQUIRED) |
| 75 | + |
| 76 | +# TODO Handle shared/static build later (after more code is available) |
| 77 | +add_library(sparrow-ipc STATIC src/sparrow-ipc.cpp) |
| 78 | +target_link_libraries(sparrow-ipc PRIVATE flatbuffers_interface flatbuffers::flatbuffers sparrow) |
| 79 | + |
| 80 | +add_dependencies(sparrow-ipc generate_flatbuffers_headers) |
| 81 | + |
| 82 | +if(BUILD_TESTS) |
| 83 | + message(STATUS "🧪 Create tests targets") |
| 84 | + enable_testing() |
| 85 | + add_subdirectory(tests) |
| 86 | +endif() |
0 commit comments