diff --git a/CMakeLists.txt b/CMakeLists.txt index 4be20d1b..488c30bd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,10 @@ project(s2client-api) set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) +# Create our install directories. Necessary so that the install directives in +# the src directory have the correct paths +include("cmake/install_dirs.cmake") + # Windows builds subdirectories Debug/Release. # These variables will overwrite that and put binaries in bin. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${PROJECT_BINARY_DIR}/bin) @@ -79,6 +83,9 @@ set(CIVETWEB_ENABLE_WEBSOCKETS ON CACHE BOOL "" FORCE) set(CIVETWEB_ENABLE_SERVER_EXECUTABLE OFF CACHE BOOL "" FORCE) set(CIVETWEB_ENABLE_IPV6 ON CACHE BOOL "" FORCE) +# Disable installing the executable. +set(CIVETWEB_INSTALL_EXECUTABLE OFF CACHE BOOL "" FORCE) + # Don't build civetweb with sanitizers set(CIVETWEB_ENABLE_ASAN OFF CACHE BOOL "" FORCE) @@ -92,9 +99,13 @@ set(protobuf_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) # Don't build SDL dynamic lib. set(SDL_SHARED OFF CACHE BOOL "" FORCE) -# Run civetwebs cmake. +# Run civetweb's cmake. add_subdirectory("contrib/civetweb") +#set_target_properties(c-executable PROPERTIES FOLDER contrib) +set_target_properties(c-executable PROPERTIES EXCLUDE_FROM_ALL 1 EXCLUDE_FROM_DEFAULT_BUILD 1) +set_target_properties(c-library c-executable PROPERTIES FOLDER contrib) + # Enable IPv6 in civetweb target_compile_options(civetweb-c-library PUBLIC -DUSE_IPV6=1) @@ -136,6 +147,9 @@ if (NOT APPLE) set_target_properties(uninstall PROPERTIES FOLDER CMakePredefinedTargets) endif () +# Install the SC2API cmake package +include("cmake/install.cmake") + if (BUILD_API_EXAMPLES) add_subdirectory("examples") endif () diff --git a/cmake/SC2APIConfig.cmake.in b/cmake/SC2APIConfig.cmake.in new file mode 100644 index 00000000..7f0f15ca --- /dev/null +++ b/cmake/SC2APIConfig.cmake.in @@ -0,0 +1,42 @@ +# Config file for the SC2API package. +# +# It defines the following variables +# SC2API_INCLUDE_DIRS - include directories for sc2api +# SC2API_LIBRARIES - libraries to link against +# +# It does not define the following variables +# SC2API_EXECUTABLE - there are no exectuables at this time + +# Compute paths +get_filename_component(SC2API_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) +set(SC2API_INCLUDE_DIRS "@CONF_INCLUDE_DIRS@") +get_filename_component(SC2API_INCLUDE_DIRS ${SC2API_INCLUDE_DIRS} ABSOLUTE) + +# This is disabled right now, please see s2client-api/cmake/install.cmake for an +# explanation of why. +# Our library dependencies (contains definitions for IMPORTED targets) +# if(NOT TARGET sc2_api AND NOT SC2API_BINARY_DIR) +# include("${SC2API_CMAKE_DIR}/SC2APITargets.cmake") +# endif() + +find_package(Protobuf REQUIRED) + +# This isn't true because of the disabled including of the SC2APITargets.cmake +# ~These are IMPORTED targets created by SC2APITargets.cmake +set( + SC2API_LIBRARIES + sc2api + sc2lib + sc2protocol + sc2utils + ${Protobuf_LIBRARIES} # Required to link with sc2protocol + civetweb # Required to link with sc2protocol + dl # Required to link with civetweb + pthread # Required to link with civetweb +) +if(NOT APPLE) + set(SC2API_LIBRARIES ${SC2API_LIBRARIES} sc2renderer) +endif() + +# If we had executables.. (update file header please) +# set(SC2API_EXECUTABLE [target names]) diff --git a/cmake/install.cmake b/cmake/install.cmake new file mode 100644 index 00000000..3c8162f5 --- /dev/null +++ b/cmake/install.cmake @@ -0,0 +1,42 @@ +# This file contains all of the install logic for the sc2api package. That is, +# this doesn't install the actual libraries and headers, but it provides the +# structures that allow a user to import and use those libraries and headers. + +# Get the relative path from the cmake install dir to the include dir +file( + RELATIVE_PATH + REL_INCLUDE_DIR + "${SC2API_INSTALL_CMAKE_DIR}" + "${SC2API_INSTALL_INCLUDE_DIR}" +) + +# Set up the variables to configure the SC2APIConfig.cmake file with +set(CONF_INCLUDE_DIRS "\${SC2API_CMAKE_DIR}/${REL_INCLUDE_DIR}") + +# Configure the config file that will end up being installed +configure_file( + "cmake/SC2APIConfig.cmake.in" + "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/SC2APIConfig.cmake" + @ONLY +) + +# Install the SC2APIConfig.cmake file +install( + FILES + "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/SC2APIConfig.cmake" + # "${PROJECT_BINARY_DIR}/SC2APIConfigVersion.cmake" + DESTINATION "${SC2API_INSTALL_CMAKE_DIR}" + COMPONENT cmake_package +) + +# This is currently disabled due to some issues with the build process. Due to +# the sc2api target's dependence on CivetWeb's c-library target and the +# sc2renderer target's dependence on SDL2's SDL2-static target we encounter +# errors from cmake when trying to install our own export set. Please see +# issue #90 in the blizzard/s2client-api repo for further discussion. +# Install the export set for use with the install-tree +# install( +# EXPORT SC2API_EXPORTS +# DESTINATION "${SC2API_INSTALL_CMAKE_DIR}" +# COMPONENT cmake_package +# ) diff --git a/cmake/install_dirs.cmake b/cmake/install_dirs.cmake new file mode 100644 index 00000000..040ac81c --- /dev/null +++ b/cmake/install_dirs.cmake @@ -0,0 +1,59 @@ +# This file sets up the paths necessary for installing the sc2api executables, +# libraries, includes, and cmake package. + +# Offer the user the choice of overriding the installation directories. +# Notes: +# - These are APPENDED TO CMAKE_INSTALL_PREFIX if they aren't absolute paths. +# If the user specifies an absolute path from the command line then they're +# used verbatim. +# - The include directory provided is */include/sc2api. Inside this directory +# will be the other include directories and files. E.g. +# */include/sc2api/sc2api/. sc2_api.h is still included via +# # include + +# Set up lib, bin, include install destination dirs +set( + SC2API_INSTALL_LIB_DIR + "lib/sc2api" + CACHE + PATH + "Installation directory for libraries" +) +set( + SC2API_INSTALL_BIN_DIR + "bin/sc2api" + CACHE + PATH + "Installation directory for executables" +) +set( + SC2API_INSTALL_INCLUDE_DIR + "include/sc2api" + CACHE + PATH + "Installation directory for header files" +) + +# Choose our cmake file install dir based on OS +if(WIN32 AND NOT CYGWIN) + set(DEF_INSTALL_CMAKE_DIR cmake) +else() + set(DEF_INSTALL_CMAKE_DIR lib/cmake/SC2API) +endif() + +# Set up actual cmake file install dir +set( + SC2API_INSTALL_CMAKE_DIR + ${DEF_INSTALL_CMAKE_DIR} + CACHE + PATH + "Installation directory for CMake files" +) + +# Make relative paths absolute +foreach(p LIB BIN INCLUDE CMAKE) + set(var SC2API_INSTALL_${p}_DIR) + if(NOT IS_ABSOLUTE "${${var}}") + set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}") + endif() +endforeach() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 81166473..f2fd3088 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -19,6 +19,8 @@ file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/generated/s2clientprotocol) foreach(proto ${proto_files}) get_filename_component(proto_name ${proto} NAME_WE) list(APPEND proto_src ${PROJECT_BINARY_DIR}/generated/s2clientprotocol/${proto_name}.pb.h ${PROJECT_BINARY_DIR}/generated/s2clientprotocol/${proto_name}.pb.cc) + # Get just headers for lib install + list(APPEND sc2protocol_headers "${PROJECT_BINARY_DIR}/generated/s2clientprotocol/${proto_name}.pb.h") endforeach() # Now include that directory @@ -33,6 +35,47 @@ add_library(sc2lib ${sources_sc2_lib}) add_library(sc2protocol ${proto_src} ${proto_files}) add_library(sc2utils ${sources_utils}) +# Glob header files together for install +file(GLOB sc2api_headers "../include/sc2api/*.h") +file(GLOB sc2lib_headers "../include/sc2lib/*.h") +file(GLOB sc2utils_headers "../include/sc2utils/*.h") + +# Set the public headers for install +set_target_properties(sc2api PROPERTIES PUBLIC_HEADER "${sc2api_headers}") +set_target_properties(sc2lib PROPERTIES PUBLIC_HEADER "${sc2lib_headers}") +set_target_properties(sc2protocol PROPERTIES PUBLIC_HEADER "${sc2protocol_headers}") +set_target_properties(sc2utils PROPERTIES PUBLIC_HEADER "${sc2utils_headers}") + +# Install the libraries and their includes +install( + TARGETS sc2api + EXPORT SC2API_EXPORTS + ARCHIVE DESTINATION "lib" + PUBLIC_HEADER DESTINATION "include/sc2api" + COMPONENT api +) +install( + TARGETS sc2lib + EXPORT SC2API_EXPORTS + ARCHIVE DESTINATION "lib" + PUBLIC_HEADER DESTINATION "include/sc2lib" + COMPONENT lib +) +install( + TARGETS sc2protocol + EXPORT SC2API_EXPORTS + ARCHIVE DESTINATION "lib" + PUBLIC_HEADER DESTINATION "include/s2clientprotocol" + COMPONENT protocol +) +install( + TARGETS sc2utils + EXPORT SC2API_EXPORTS + ARCHIVE DESTINATION "lib" + PUBLIC_HEADER DESTINATION "include/sc2utils" + COMPONENT utils +) + set_target_properties(sc2api PROPERTIES DEBUG_POSTFIX "d") set_target_properties(sc2lib PROPERTIES DEBUG_POSTFIX "d") set_target_properties(sc2protocol PROPERTIES DEBUG_POSTFIX "d") @@ -47,6 +90,15 @@ endif () # Exclude SDL and related projects to work around linker issue. if (NOT APPLE) add_library(sc2renderer ${sources_renderer}) + file(GLOB sc2renderer_headers "../include/sc2renderer/*.h") + set_target_properties(sc2renderer PROPERTIES PUBLIC_HEADER "${sc2renderer_headers}") + install( + TARGETS sc2renderer + EXPORT SC2API_EXPORTS + ARCHIVE DESTINATION "lib" + PUBLIC_HEADER DESTINATION "include/sc2renderer" + COMPONENT renderer + ) set_target_properties(sc2renderer PROPERTIES DEBUG_POSTFIX "d") target_link_libraries(sc2renderer SDL2-static) include_directories("../contrib/SDL-mirror/include") @@ -68,7 +120,7 @@ if (MSVC) set_target_properties(civetweb-c-library PROPERTIES COMPILE_FLAGS "/MT") else() - set(CMAKE_CXX_FLAGS "-fpermissive") + set(CMAKE_CXX_FLAGS "-fpermissive") endif() add_dependencies(sc2protocol protoc) @@ -87,7 +139,6 @@ foreach (proto ${proto_files}) endforeach() - if (MSVC) set_target_properties(sc2api PROPERTIES COMPILE_FLAGS "/W4 /WX") set_target_properties(sc2lib PROPERTIES COMPILE_FLAGS "/W4 /WX")