CMakeLists.txt 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. cmake_minimum_required(VERSION 3.16)
  2. project(stable-diffusion.cpp-rest
  3. VERSION 1.0.0
  4. DESCRIPTION "REST API server for stable-diffusion.cpp"
  5. LANGUAGES CXX
  6. )
  7. # Set C++ standard
  8. set(CMAKE_CXX_STANDARD 17)
  9. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  10. set(CMAKE_CXX_EXTENSIONS OFF)
  11. set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
  12. # Set build type if not specified
  13. if(NOT CMAKE_BUILD_TYPE)
  14. set(CMAKE_BUILD_TYPE Release)
  15. endif()
  16. # Add cmake modules path
  17. list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
  18. # Include dependencies
  19. include(FindDependencies)
  20. # Option for CUDA support
  21. option(SD_CUDA_SUPPORT "Enable CUDA support in stable-diffusion.cpp" ON)
  22. # Option for building Web UI
  23. option(BUILD_WEBUI "Build the web UI and bundle it with the server" ON)
  24. # Find CUDA if support is enabled
  25. if(SD_CUDA_SUPPORT)
  26. find_package(CUDAToolkit REQUIRED)
  27. message(STATUS "CUDA Toolkit found: ${CUDAToolkit_VERSION}")
  28. endif()
  29. # ExternalProject for stable-diffusion.cpp
  30. include(ExternalProject)
  31. # Set up external project for stable-diffusion.cpp
  32. ExternalProject_Add(stable-diffusion.cpp
  33. GIT_REPOSITORY https://github.com/leejet/stable-diffusion.cpp.git
  34. GIT_TAG master-334-d05e46c
  35. SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/stable-diffusion.cpp-src"
  36. BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/stable-diffusion.cpp-build"
  37. CMAKE_ARGS
  38. -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
  39. -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/stable-diffusion.cpp-install
  40. -DSD_CUDA=${SD_CUDA_SUPPORT}
  41. BUILD_COMMAND ${CMAKE_COMMAND} --build . --config ${CMAKE_BUILD_TYPE}
  42. INSTALL_COMMAND ${CMAKE_COMMAND} --install . --config ${CMAKE_BUILD_TYPE}
  43. LOG_DOWNLOAD ON
  44. LOG_CONFIGURE ON
  45. LOG_BUILD ON
  46. LOG_INSTALL ON
  47. )
  48. # Get the include directories and libraries from the external project
  49. ExternalProject_Get_Property(stable-diffusion.cpp SOURCE_DIR BINARY_DIR)
  50. # Set include directories for the external project
  51. set(SD_CPP_INCLUDE_DIRS
  52. ${CMAKE_CURRENT_BINARY_DIR}/stable-diffusion.cpp-install/include
  53. ${SOURCE_DIR}
  54. ${SOURCE_DIR}/ggml/include
  55. )
  56. # Set library directories
  57. set(SD_CPP_LIB_DIR ${CMAKE_CURRENT_BINARY_DIR}/stable-diffusion.cpp-install/lib)
  58. # Build Web UI if enabled
  59. if(BUILD_WEBUI)
  60. # Check if Node.js and npm are available
  61. find_program(NPM_EXECUTABLE npm)
  62. if(NPM_EXECUTABLE)
  63. message(STATUS "npm found: ${NPM_EXECUTABLE}")
  64. message(STATUS "Web UI will be built and bundled")
  65. # Set the output directory for the web UI
  66. set(WEBUI_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/webui)
  67. set(WEBUI_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/webui)
  68. # Get git commit hash for versioning
  69. find_package(Git QUIET)
  70. if(GIT_FOUND)
  71. execute_process(
  72. COMMAND ${GIT_EXECUTABLE} rev-parse --short=8 HEAD
  73. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  74. OUTPUT_VARIABLE GIT_COMMIT_HASH
  75. OUTPUT_STRIP_TRAILING_WHITESPACE
  76. ERROR_QUIET
  77. )
  78. if(NOT GIT_COMMIT_HASH)
  79. set(GIT_COMMIT_HASH "unknown")
  80. endif()
  81. else()
  82. set(GIT_COMMIT_HASH "unknown")
  83. endif()
  84. message(STATUS "Git commit hash for UI versioning: ${GIT_COMMIT_HASH}")
  85. # Get build timestamp
  86. string(TIMESTAMP BUILD_TIMESTAMP "%Y-%m-%dT%H:%M:%SZ" UTC)
  87. # Create custom target for building the web UI
  88. add_custom_target(webui-build
  89. COMMAND ${CMAKE_COMMAND} -E echo "Building Web UI..."
  90. # Generate version file
  91. COMMAND ${CMAKE_COMMAND} -E echo "{\"version\":\"${GIT_COMMIT_HASH}\",\"buildTime\":\"${BUILD_TIMESTAMP}\"}" > ${WEBUI_SOURCE_DIR}/public/version.json
  92. COMMAND ${NPM_EXECUTABLE} install
  93. COMMAND ${NPM_EXECUTABLE} run build
  94. COMMAND ${CMAKE_COMMAND} -E copy_directory ${WEBUI_SOURCE_DIR}/out ${WEBUI_OUTPUT_DIR}
  95. WORKING_DIRECTORY ${WEBUI_SOURCE_DIR}
  96. COMMENT "Building Web UI with npm (version: ${GIT_COMMIT_HASH})"
  97. VERBATIM
  98. )
  99. # Set a variable that can be used by the server
  100. set(WEBUI_BUILT ON)
  101. set(WEBUI_PATH ${WEBUI_OUTPUT_DIR} CACHE PATH "Path to built web UI files")
  102. message(STATUS "Web UI output directory: ${WEBUI_OUTPUT_DIR}")
  103. else()
  104. message(WARNING "npm not found. Web UI will not be built. Install Node.js to enable Web UI building.")
  105. set(BUILD_WEBUI OFF)
  106. endif()
  107. else()
  108. message(STATUS "Web UI building is disabled")
  109. endif()
  110. # Add subdirectories
  111. add_subdirectory(src)
  112. # Create an interface target for stable-diffusion.cpp
  113. add_library(sd-cpp INTERFACE)
  114. add_dependencies(sd-cpp stable-diffusion.cpp)
  115. target_include_directories(sd-cpp INTERFACE ${SD_CPP_INCLUDE_DIRS})
  116. target_link_directories(sd-cpp INTERFACE ${SD_CPP_LIB_DIR})
  117. # Set up libraries list based on CUDA support
  118. set(SD_LIBS
  119. stable-diffusion
  120. ggml
  121. ggml-base
  122. ggml-cpu
  123. )
  124. if(SD_CUDA_SUPPORT)
  125. list(APPEND SD_LIBS ggml-cuda)
  126. list(APPEND SD_LIBS CUDA::cudart CUDA::cublas CUDA::cuda_driver)
  127. endif()
  128. target_link_libraries(sd-cpp INTERFACE ${SD_LIBS})
  129. # Print configuration summary
  130. message(STATUS "Configuration Summary:")
  131. message(STATUS " Build Type: ${CMAKE_BUILD_TYPE}")
  132. message(STATUS " C++ Standard: ${CMAKE_CXX_STANDARD}")
  133. message(STATUS " CUDA Support: ${SD_CUDA_SUPPORT}")
  134. message(STATUS " Build Web UI: ${BUILD_WEBUI}")
  135. if(BUILD_WEBUI AND WEBUI_BUILT)
  136. message(STATUS " Web UI Path: ${WEBUI_PATH}")
  137. endif()