Skip to content

Commit

Permalink
Merge pull request #42 from EmilDohne/feat-refactor-cmake
Browse files Browse the repository at this point in the history
Update CMake System to simplify and include the changes from @spectras
  • Loading branch information
EmilDohne authored Feb 11, 2024
2 parents fcadb10 + d3e3939 commit e97b675
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 320 deletions.
100 changes: 26 additions & 74 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,111 +1,63 @@
# PhotoshopAPI Requires C++ 20
cmake_minimum_required (VERSION 3.20)
set (CMAKE_CXX_STANDARD 20)

if(MSVC)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".lib")
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif()

# Link the MSVC Runtime libraries into the exe
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

# Enable Hot Reload for MSVC compilers if supported.
if (POLICY CMP0141)
cmake_policy(SET CMP0141 NEW)
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()
cmake_minimum_required (VERSION 3.20)
project ("PhotoshopAPIBuild")

# --------------------------------------------------------------------------
# CMake Build Setup for the PhotoshopAPI
# --------------------------------------------------------------------------
#
# A list of available options can be found below:
#
# PSAPI_BUILD_STATIC: default ON
# Build a static library version of the PhotoshopAPI
# PSAPI_BUILD_TESTS: default ON
# Build the tests associated with the PhotoshopAPI
# PSAPI_BUILD_EXAMPLES: default ON
# Build the examples associated with the PhotoshopAPI
# PSAPI_BUILD_BENCHMARKS: default ON
# Build the benchmarks associated with the PhotoshopAPI
# PSAPI_BUILD_DOCS: default ON
# Builds the documentation, requires some external installs which are documented in the README.md
#
project ("PhotoshopAPIBuild")
# Configurable options

# Initialize our options
option(PSAPI_BUILD_STATIC "Build a static library version of the PhotoshopAPI" ON)
option(PSAPI_BUILD_TESTS "Build the tests associated with the PhotoshopAPI" ON)
option(PSAPI_BUILD_EXAMPLES "Build the examples associated with the PhotoshopAPI" ON)
option(PSAPI_BUILD_BENCHMARKS "Build the benchmarks associated with the PhotoshopAPI" ON)
option(PSAPI_BUILD_DOCS "Builds the documentation, requires some external installs which are documented in the README.md" ON)

# Build setup
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
set (CMAKE_CXX_STANDARD 20)

# Add the cmake/ folder so the FindSphinx module is found
# --------------------------------------------------------------------------
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})

# Specify our own version of zlib-ng rather than the supplied one for blosc2.
# Switch to the debug library if that is the build target
# --------------------------------------------------------------------------
set(PREFER_EXTERNAL_ZLIB ON)
if(CMAKE_BUILD_TYPE STREQUAL "Release")
set (ZLIB_NG_LIBRARY "${CMAKE_BINARY_DIR}/thirdparty/zlib-ng/zlibstatic-ng.lib")
else()
set (ZLIB_NG_LIBRARY "${CMAKE_BINARY_DIR}/thirdparty/zlib-ng/zlibstatic-ngd.lib")
endif()
set (ZLIB_NG_INCLUDE_DIR "${CMAKE_BINARY_DIR}/thirdparty/zlib-ng")


# Add thirdparty libraries
# --------------------------------------------------------------------------

# Modify zlib-ng to only build libs
# Add zlib-ng
set(ZLIB_ENABLE_TESTS OFF)
set(ZLIBNG_ENABLE_TESTS OFF)
set(WITH_GZFILEOP OFF)
add_subdirectory ("${CMAKE_SOURCE_DIR}/thirdparty/zlib-ng" EXCLUDE_FROM_ALL)
add_subdirectory (thirdparty/zlib-ng EXCLUDE_FROM_ALL)

# Modify c-blosc2 to only build libs
# Add c-blosc2
set(DEACTIVATE_ZLIB ON)
set(DEACTIVATE_ZSTD ON)
set(BUILD_TESTS OFF)
set(BUILD_FUZZERS OFF)
set(BUILD_BENCHMARKS OFF)
set(BUILD_EXAMPLES OFF)
add_subdirectory ("${CMAKE_SOURCE_DIR}/thirdparty/c-blosc2" EXCLUDE_FROM_ALL)
add_subdirectory (thirdparty/c-blosc2 EXCLUDE_FROM_ALL)

# Add doctest
add_library(doctest INTERFACE)
target_include_directories(doctest INTERFACE thirdparty/doctest)

# Include PhotoshopAPI sub-projects.
# Projects
# --------------------------------------------------------------------------
if(PSAPI_BUILD_STATIC)
add_subdirectory ("${CMAKE_SOURCE_DIR}/PhotoshopAPI")
add_subdirectory (PhotoshopAPI)
endif()
if(PSAPI_BUILD_TESTS)
add_subdirectory ("${CMAKE_SOURCE_DIR}/PhotoshopTest")
add_subdirectory (PhotoshopTest)
endif()
if(PSAPI_BUILD_BENCHMARKS)
add_subdirectory ("${CMAKE_SOURCE_DIR}/PhotoshopBenchmark")
add_subdirectory (PhotoshopBenchmark)
endif()

# Add our Example directories
# --------------------------------------------------------------------------
if(PSAPI_BUILD_EXAMPLES)
add_subdirectory ("${CMAKE_SOURCE_DIR}/PhotoshopExamples/CreateSimpleDocument")
add_subdirectory ("${CMAKE_SOURCE_DIR}/PhotoshopExamples/CreateGroups")
add_subdirectory ("${CMAKE_SOURCE_DIR}/PhotoshopExamples/AddLayerMasks")
add_subdirectory ("${CMAKE_SOURCE_DIR}/PhotoshopExamples/ModifyLayerStructure")
add_subdirectory (PhotoshopExamples/CreateSimpleDocument)
add_subdirectory (PhotoshopExamples/CreateGroups)
add_subdirectory (PhotoshopExamples/AddLayerMasks)
add_subdirectory (PhotoshopExamples/ModifyLayerStructure)
endif()

# Add our documentation dir
# --------------------------------------------------------------------------
if(PSAPI_BUILD_DOCS)
add_subdirectory ("${CMAKE_SOURCE_DIR}/docs/doxygen")
endif()


# Set the test project (PhotoshopTest) as the default target
# --------------------------------------------------------------------------
add_custom_target(default_target ALL
DEPENDS PhotoshopTest
COMMENT "Building the default target: PhotoshopTest"
)
add_subdirectory (docs/doxygen)
endif()
67 changes: 5 additions & 62 deletions PhotoshopAPI/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,69 +1,12 @@
# Define the project
# --------------------------------------------------------------------------
set(LIB PhotoshopAPI)
project(${LIB})

# Define MY_SOURCES to be a list of all the source files
project(PhotoshopAPI)
file(GLOB_RECURSE MY_SOURCES CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/src/*.cpp")

# Copy the header files from /include and /src to the binary dir
# --------------------------------------------------------------------------

# Set the source directory
set(SOURCE_DIR ${PROJECT_SOURCE_DIR}/src)
# Gather a list of all header files
file(GLOB_RECURSE HEADER_FILES ${SOURCE_DIR}/*.h)
# Set the include dir in our binary
set(BIN_INCLUDE_DIR ${CMAKE_BINARY_DIR}/include)

# Custom target for header file copying
add_custom_target(CopyHeaders
COMMENT "Copying header files to include directory"
VERBATIM
)

# Copy header files to the include directory with folder structure intact
foreach(HEADER_FILE ${HEADER_FILES})
# Get the relative path of the header file
file(RELATIVE_PATH RELATIVE_HEADER ${SOURCE_DIR} ${HEADER_FILE})

# Create the corresponding include directory for the header file
get_filename_component(INCLUDE_SUBDIR ${RELATIVE_HEADER} DIRECTORY)
set(DESTINATION_DIR ${BIN_INCLUDE_DIR}/${INCLUDE_SUBDIR})
file(MAKE_DIRECTORY ${DESTINATION_DIR})

# Copy the header file to the include directory
add_custom_command(TARGET CopyHeaders
COMMAND ${CMAKE_COMMAND} -E copy ${HEADER_FILE} ${DESTINATION_DIR}
COMMENT "Copying ${HEADER_FILE} to ${DESTINATION_DIR}"
)
endforeach()

# Copy the /include/PhotoshopAPI.h separately
add_custom_command(TARGET CopyHeaders
COMMAND ${CMAKE_COMMAND} -E copy "${PROJECT_SOURCE_DIR}/include/PhotoshopAPI.h" ${BIN_INCLUDE_DIR}
COMMENT "Copying ${PROJECT_SOURCE_DIR}/include/PhotoshopAPI.h to ${BIN_INCLUDE_DIR}"
)

# Create our static lib
# --------------------------------------------------------------------------
add_library (${LIB} STATIC ${MY_SOURCES})

# Add dependency to CopyHeaders
add_dependencies(${LIB} CopyHeaders)

# Set some additional includes
target_include_directories(${LIB} PUBLIC
"${PROJECT_SOURCE_DIR}/src/"
"${PROJECT_SOURCE_DIR}/src/Util/"
)

# Include blosc2 headers
target_include_directories(${LIB} PRIVATE "${CMAKE_SOURCE_DIR}/thirdparty/c-blosc2/include")
#include zlib-ng headers
target_include_directories(${LIB} PUBLIC "${CMAKE_BINARY_DIR}/thirdparty/zlib-ng")
target_include_directories(${LIB} PUBLIC "${CMAKE_SOURCE_DIR}/thirdparty/zlib-ng")
add_library(PhotoshopAPI STATIC ${MY_SOURCES})
target_include_directories(PhotoshopAPI PUBLIC include ${CMAKE_SOURCE_DIR}/thirdparty/c-blosc2/include src src/Util)
target_link_libraries(PhotoshopAPI PUBLIC zlibstatic Blosc2::blosc2_static)

if(MSVC)
target_compile_options(${LIB} PRIVATE /MP /DNOMINMAX)
target_compile_options(PhotoshopAPI PRIVATE /MP /DNOMINMAX)
endif()
31 changes: 3 additions & 28 deletions PhotoshopBenchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,3 @@
# This is a minimal CMake setup required to work with the PhotoshopAPI.

# Initialize our Project
set (EXE PhotoshopBenchmark)
project(${EXE})

# Define MY_SOURCES to be a list of all the source files
file(GLOB_RECURSE MY_SOURCES CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/src/*.cpp")
add_executable(${EXE} ${MY_SOURCES})

# Include and link the PhotoshopAPI
target_include_directories(${EXE} PRIVATE "${CMAKE_SOURCE_DIR}/PhotoshopAPI/include")
target_link_libraries(${EXE} PRIVATE PhotoshopAPI)

# Include and link blosc2
target_include_directories(${EXE} PRIVATE "${CMAKE_SOURCE_DIR}/thirdparty/c-blosc2/include")
target_link_directories(${EXE} PRIVATE "${CMAKE_SOURCE_DIR}/thirdparty/c-blosc2")
target_link_libraries(${EXE} PRIVATE blosc2_static)

#include and link zlib-ng
target_include_directories(${EXE} PUBLIC "${CMAKE_BINARY_DIR}/thirdparty/zlib-ng")
target_include_directories(${EXE} PUBLIC "${CMAKE_SOURCE_DIR}/thirdparty/zlib-ng")
target_link_directories(${EXE} PUBLIC "${CMAKE_BINARY_DIR}/thirdparty/zlib-ng")
if(CMAKE_BUILD_TYPE STREQUAL "Release")
target_link_libraries(${EXE} PUBLIC zlibstatic-ng)
else()
target_link_libraries(${EXE} PUBLIC zlibstatic-ngd)
endif()
file(GLOB_RECURSE MY_SOURCES CONFIGURE_DEPENDS "src/*.cpp")
add_executable(PhotoshopBenchmark ${MY_SOURCES})
target_link_libraries(PhotoshopBenchmark PRIVATE PhotoshopAPI)
30 changes: 2 additions & 28 deletions PhotoshopExamples/AddLayerMasks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,2 @@
# This is a minimal CMake setup required to work with the PhotoshopAPI.

# Initialize our Project
set (EXE AddLayerMasks)
project(${EXE})

# Define MY_SOURCES to be a list of all the source files
file(GLOB_RECURSE MY_SOURCES CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/*.cpp")
add_executable(${EXE} ${MY_SOURCES})

# Include and link the PhotoshopAPI
target_include_directories(${EXE} PRIVATE "${CMAKE_SOURCE_DIR}/PhotoshopAPI/include")
target_link_libraries(${EXE} PRIVATE PhotoshopAPI)

# Include and link blosc2
target_include_directories(${EXE} PRIVATE "${CMAKE_SOURCE_DIR}/thirdparty/c-blosc2/include")
target_link_directories(${EXE} PRIVATE "${CMAKE_SOURCE_DIR}/thirdparty/c-blosc2")
target_link_libraries(${EXE} PRIVATE blosc2_static)

#include and link zlib-ng
target_include_directories(${EXE} PUBLIC "${CMAKE_BINARY_DIR}/thirdparty/zlib-ng")
target_include_directories(${EXE} PUBLIC "${CMAKE_SOURCE_DIR}/thirdparty/zlib-ng")
target_link_directories(${EXE} PUBLIC "${CMAKE_BINARY_DIR}/thirdparty/zlib-ng")
if(CMAKE_BUILD_TYPE STREQUAL "Release")
target_link_libraries(${EXE} PUBLIC zlibstatic-ng)
else()
target_link_libraries(${EXE} PUBLIC zlibstatic-ngd)
endif()
add_executable(AddLayerMasks main.cpp)
target_link_libraries(AddLayerMasks PRIVATE PhotoshopAPI)
30 changes: 2 additions & 28 deletions PhotoshopExamples/CreateGroups/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,2 @@
# This is a minimal CMake setup required to work with the PhotoshopAPI.

# Initialize our Project
set (EXE CreateGroups)
project(${EXE})

# Define MY_SOURCES to be a list of all the source files
file(GLOB_RECURSE MY_SOURCES CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/*.cpp")
add_executable(${EXE} ${MY_SOURCES})

# Include and link the PhotoshopAPI
target_include_directories(${EXE} PRIVATE "${CMAKE_SOURCE_DIR}/PhotoshopAPI/include")
target_link_libraries(${EXE} PRIVATE PhotoshopAPI)

# Include and link blosc2
target_include_directories(${EXE} PRIVATE "${CMAKE_SOURCE_DIR}/thirdparty/c-blosc2/include")
target_link_directories(${EXE} PRIVATE "${CMAKE_SOURCE_DIR}/thirdparty/c-blosc2")
target_link_libraries(${EXE} PRIVATE blosc2_static)

#include and link zlib-ng
target_include_directories(${EXE} PUBLIC "${CMAKE_BINARY_DIR}/thirdparty/zlib-ng")
target_include_directories(${EXE} PUBLIC "${CMAKE_SOURCE_DIR}/thirdparty/zlib-ng")
target_link_directories(${EXE} PUBLIC "${CMAKE_BINARY_DIR}/thirdparty/zlib-ng")
if(CMAKE_BUILD_TYPE STREQUAL "Release")
target_link_libraries(${EXE} PUBLIC zlibstatic-ng)
else()
target_link_libraries(${EXE} PUBLIC zlibstatic-ngd)
endif()
add_executable(CreateGroups main.cpp)
target_link_libraries(CreateGroups PRIVATE PhotoshopAPI)
30 changes: 2 additions & 28 deletions PhotoshopExamples/CreateSimpleDocument/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,2 @@
# This is a minimal CMake setup required to work with the PhotoshopAPI.

# Initialize our Project
set (EXE CreateSimpleDocument)
project(${EXE})

# Define MY_SOURCES to be a list of all the source files
file(GLOB_RECURSE MY_SOURCES CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/*.cpp")
add_executable(${EXE} ${MY_SOURCES})

# Include and link the PhotoshopAPI
target_include_directories(${EXE} PRIVATE "${CMAKE_SOURCE_DIR}/PhotoshopAPI/include")
target_link_libraries(${EXE} PRIVATE PhotoshopAPI)

# Include and link blosc2
target_include_directories(${EXE} PRIVATE "${CMAKE_SOURCE_DIR}/thirdparty/c-blosc2/include")
target_link_directories(${EXE} PRIVATE "${CMAKE_SOURCE_DIR}/thirdparty/c-blosc2")
target_link_libraries(${EXE} PRIVATE blosc2_static)

#include and link zlib-ng
target_include_directories(${EXE} PUBLIC "${CMAKE_BINARY_DIR}/thirdparty/zlib-ng")
target_include_directories(${EXE} PUBLIC "${CMAKE_SOURCE_DIR}/thirdparty/zlib-ng")
target_link_directories(${EXE} PUBLIC "${CMAKE_BINARY_DIR}/thirdparty/zlib-ng")
if(CMAKE_BUILD_TYPE STREQUAL "Release")
target_link_libraries(${EXE} PUBLIC zlibstatic-ng)
else()
target_link_libraries(${EXE} PUBLIC zlibstatic-ngd)
endif()
add_executable(CreateSimpleDocument main.cpp)
target_link_libraries(CreateSimpleDocument PRIVATE PhotoshopAPI)
36 changes: 2 additions & 34 deletions PhotoshopExamples/ModifyLayerStructure/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,34 +1,2 @@
# This is a minimal CMake setup required to work with the PhotoshopAPI.

# Initialize our Project
set (EXE ModifyLayerStructure)
project(${EXE})

# Define MY_SOURCES to be a list of all the source files
file(GLOB_RECURSE MY_SOURCES CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/*.cpp")
add_executable(${EXE} ${MY_SOURCES})

# Include and link the PhotoshopAPI
target_include_directories(${EXE} PRIVATE "${CMAKE_SOURCE_DIR}/PhotoshopAPI/include")
target_link_libraries(${EXE} PRIVATE PhotoshopAPI)

# Include and link blosc2
target_include_directories(${EXE} PRIVATE "${CMAKE_SOURCE_DIR}/thirdparty/c-blosc2/include")
target_link_directories(${EXE} PRIVATE "${CMAKE_SOURCE_DIR}/thirdparty/c-blosc2")
target_link_libraries(${EXE} PRIVATE blosc2_static)

#include and link zlib-ng
target_include_directories(${EXE} PUBLIC "${CMAKE_BINARY_DIR}/thirdparty/zlib-ng")
target_include_directories(${EXE} PUBLIC "${CMAKE_SOURCE_DIR}/thirdparty/zlib-ng")
target_link_directories(${EXE} PUBLIC "${CMAKE_BINARY_DIR}/thirdparty/zlib-ng")
if(CMAKE_BUILD_TYPE STREQUAL "Release")
target_link_libraries(${EXE} PUBLIC zlibstatic-ng)
else()
target_link_libraries(${EXE} PUBLIC zlibstatic-ngd)
endif()


# Copy the LayeredFile.psd to our binary directory
add_custom_command(TARGET ${EXE} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${PROJECT_SOURCE_DIR}/LayeredFile.psd $<TARGET_FILE_DIR:${EXE}>/LayeredFile.psd)
add_executable(ModifyLayerStructure main.cpp)
target_link_libraries(ModifyLayerStructure PRIVATE PhotoshopAPI)
Loading

0 comments on commit e97b675

Please sign in to comment.