|
| 1 | +cmake_minimum_required(VERSION 3.0) |
| 2 | +project(x-infer) |
| 3 | + |
| 4 | +option(LIB_ONLY "Only build the library" OFF) |
| 5 | +option(BUILD_FOR_DYNAMIC_LINKAGE "Whether to build for dynamic or static bundle linkage" ON) |
| 6 | +option(LINK_LIBS_STATICALLY "Whether to link in all the required libraries statically" ON) |
| 7 | +option(ENABLE_PERF_MONITORING "Whether to enable performance monitoring" OFF) |
| 8 | + |
| 9 | +set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin) |
| 10 | +set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin) |
| 11 | + |
| 12 | +if(LINK_LIBS_STATICALLY) |
| 13 | + message(WARNING "Static linking may cause issues with dlopen on target") |
| 14 | + message("Will statically link executable") |
| 15 | + set(TARGET_COMPILE_FLAGS "-static" |
| 16 | + ) |
| 17 | + set(TARGET_LINKER_FLAGS "-static" |
| 18 | + ) |
| 19 | +else() |
| 20 | + set(TARGET_COMPILE_FLAGS ) |
| 21 | +endif() |
| 22 | + |
| 23 | +if (DEFINED TARGET_OPTIONS) |
| 24 | + set(TARGET_COMPILE_FLAGS "${TARGET_COMPILE_FLAGS} ${TARGET_OPTIONS}") |
| 25 | +endif() |
| 26 | + |
| 27 | +include_directories(${PROJECT_SOURCE_DIR}) |
| 28 | + |
| 29 | +set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} ${TARGET_COMPILE_FLAGS}) |
| 30 | +set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} ${TARGET_COMPILE_FLAGS}) |
| 31 | +set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} ${TARGET_LINKER_FLAGS}) |
| 32 | + |
| 33 | +if(ENABLE_PERF_MONITORING) |
| 34 | + message("Will enable performance monitoring") |
| 35 | + message(WARNING "Performance monitoring is support only on Linux") |
| 36 | + message(WARNING "If you're running in a VM, performance monitoring will be " |
| 37 | + "broken, and will likely break inference.") |
| 38 | + add_definitions(-DENABLE_PERF_MONITORING) |
| 39 | +endif() |
| 40 | + |
| 41 | +add_library(xinfer STATIC x_inference_lib.c x_perf_monitor.c) |
| 42 | + |
| 43 | +if(LIB_ONLY) |
| 44 | + message("Will only build the library") |
| 45 | +else() |
| 46 | + message("Will build the library and the executable") |
| 47 | + if (BUILD_FOR_DYNAMIC_LINKAGE) |
| 48 | + message("Will build the executable for dynamic bundle linkage") |
| 49 | + set(OBJS ) |
| 50 | + add_definitions(-DX_USE_DYNAMIC) |
| 51 | + else() |
| 52 | + if(NOT DEFINED LINKED_BUNDLE) |
| 53 | + message(FATAL_ERROR "When building with static bundle linkage, " |
| 54 | + "bundle must be specified with " |
| 55 | + "-DLINKED_BUNDLE=") |
| 56 | + endif() |
| 57 | + if (NOT DEFINED LINKED_MODEL_NAME) |
| 58 | + message(FATAL_ERROR "When building with static bundle linkage, " |
| 59 | + "model name must be specified with " |
| 60 | + "-DLINKED_MODEL_NAME=") |
| 61 | + endif() |
| 62 | + |
| 63 | + add_definitions(-DX_MODEL_NAME=${LINKED_MODEL_NAME}) |
| 64 | + set(OBJS ${LINKED_BUNDLE}) |
| 65 | + set_source_files_properties(${OBJS} PROPERTIES EXTERNAL_OBJECT TRUE GENERATED TRUE) |
| 66 | + endif() |
| 67 | + link_directories(${LIBRARY_OUTPUT_PATH}) |
| 68 | + add_executable(x-infer main.c ${OBJS}) |
| 69 | + target_link_libraries(x-infer xinfer dl m) |
| 70 | +endif() |
0 commit comments