|
| 1 | +cmake_minimum_required(VERSION 3.11.0) |
| 2 | +project(udp2) |
| 3 | + |
| 4 | +set(CMAKE_CXX_STANDARD 14) |
| 5 | +set(CMAKE_CXX_STANDARD_REQUIRED True) |
| 6 | + |
| 7 | +# Get the top-level project directory |
| 8 | +set(TOP_DIR ${PROJECT_SOURCE_DIR}/../..) |
| 9 | +set(CBDB_INCLUDE_DIR ${TOP_DIR}/src/include) |
| 10 | + |
| 11 | +# CMAKE_INSTALL_PREFIX should be set by the calling Makefile |
| 12 | +# If not set, we'll use a reasonable default but warn about it |
| 13 | +if(NOT DEFINED CMAKE_INSTALL_PREFIX OR CMAKE_INSTALL_PREFIX STREQUAL "/usr/local") |
| 14 | + message(WARNING "CMAKE_INSTALL_PREFIX not set by parent build system, using default") |
| 15 | + set(CMAKE_INSTALL_PREFIX "/usr/local" CACHE PATH "Install prefix" FORCE) |
| 16 | +endif() |
| 17 | + |
| 18 | +# Check for debug/release configuration from main project |
| 19 | +include(CheckSymbolExists) |
| 20 | +set(PG_CONFIG_HEADER_FILE "${CBDB_INCLUDE_DIR}/pg_config.h") |
| 21 | +if(EXISTS "${PG_CONFIG_HEADER_FILE}") |
| 22 | + CHECK_SYMBOL_EXISTS(USE_ASSERT_CHECKING "${PG_CONFIG_HEADER_FILE}" UDP2_USE_DEBUG) |
| 23 | + if(UDP2_USE_DEBUG) |
| 24 | + set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build." FORCE) |
| 25 | + message(STATUS "Setting CMAKE_BUILD_TYPE to 'Debug' based on main project configuration") |
| 26 | + else() |
| 27 | + set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE) |
| 28 | + message(STATUS "Setting CMAKE_BUILD_TYPE to 'Release' based on main project configuration") |
| 29 | + endif() |
| 30 | +else() |
| 31 | + # Fallback to Release if pg_config.h is not found |
| 32 | + if(NOT CMAKE_BUILD_TYPE) |
| 33 | + set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE) |
| 34 | + message(STATUS "Setting default CMAKE_BUILD_TYPE to 'Release'") |
| 35 | + endif() |
| 36 | +endif() |
| 37 | + |
| 38 | +# First, build and install ic_common as a subdirectory |
| 39 | +add_subdirectory(ic_common) |
| 40 | + |
| 41 | +# Set up include directories |
| 42 | +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) |
| 43 | +include_directories(${CMAKE_INSTALL_PREFIX}/include/postgresql/) |
| 44 | +include_directories(${CMAKE_INSTALL_PREFIX}/include/postgresql/udp2/) |
| 45 | +include_directories(${CBDB_INCLUDE_DIR}) |
| 46 | +include_directories(${CBDB_INCLUDE_DIR}/server) |
| 47 | + |
| 48 | +# Set up library directories |
| 49 | +link_directories(${CMAKE_INSTALL_PREFIX}/lib/postgresql/) |
| 50 | + |
| 51 | +# Source files for udp2 module |
| 52 | +set(UDP2_SOURCES |
| 53 | + ic_udp2.c |
| 54 | + ic_modules.c |
| 55 | +) |
| 56 | + |
| 57 | +# Create the udp2 shared library |
| 58 | +add_library(udp2 SHARED ${UDP2_SOURCES}) |
| 59 | + |
| 60 | +# Set compiler flags consistent with main project |
| 61 | +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_GNU_SOURCE -Wall -Wpointer-arith -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wcast-function-type -Wformat-security -fno-strict-aliasing -fwrapv") |
| 62 | + |
| 63 | +if (${CMAKE_BUILD_TYPE} STREQUAL "Debug") |
| 64 | + set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -g -ggdb") |
| 65 | + message(STATUS "Building udp2 in Debug mode") |
| 66 | +elseif (${CMAKE_BUILD_TYPE} STREQUAL "Release") |
| 67 | + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O2 -DNDEBUG") |
| 68 | + message(STATUS "Building udp2 in Release mode") |
| 69 | +endif() |
| 70 | + |
| 71 | +# Link against ic_common library |
| 72 | +target_link_libraries(udp2 ic_common) |
| 73 | + |
| 74 | +# Make sure ic_common is built before udp2 |
| 75 | +add_dependencies(udp2 ic_common) |
| 76 | + |
| 77 | +# Set output name and remove lib prefix |
| 78 | +set_target_properties(udp2 PROPERTIES |
| 79 | + OUTPUT_NAME "udp2" |
| 80 | + PREFIX "" |
| 81 | +) |
| 82 | + |
| 83 | +# Install the udp2 library |
| 84 | +install(TARGETS udp2 |
| 85 | + LIBRARY DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/postgresql/" |
| 86 | + ARCHIVE DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/postgresql/" |
| 87 | +) |
| 88 | + |
| 89 | +# Install udp2 headers |
| 90 | +install(FILES |
| 91 | + "${CMAKE_CURRENT_SOURCE_DIR}/ic_udp2.h" |
| 92 | + "${CMAKE_CURRENT_SOURCE_DIR}/ic_modules.h" |
| 93 | + DESTINATION "${CMAKE_INSTALL_PREFIX}/include/postgresql/" |
| 94 | +) |
0 commit comments