CMakeLists.txt 5.8 KB

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