Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HOLD] Add script to build and package libs in MS vcpkg #1261

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
7ac57db
Use CMake to pull in and use precompiled deps
DavidFair Oct 15, 2017
6b22c90
Added MIT License on the PreCompiled Deps CMake Module
DavidFair Oct 15, 2017
34be1a2
Fixed incorrect arch being selected on MSVC
DavidFair Oct 15, 2017
ba67f86
Fixed Linux being incorrectly use pre-compiled by default
DavidFair Oct 15, 2017
e2d74c2
Add script to build and package libs in MS vcpkg
DavidFair Oct 15, 2017
3303df7
Reworked script to build deps locally
DavidFair Oct 16, 2017
b683b4f
Move vcpkg from CMake folder to scripts
DavidFair Oct 16, 2017
2cf68e2
Merge branch 'use_cmake_to_handle_prebuilt_deps' into Add_script_to_b…
DavidFair Oct 16, 2017
92e5dbe
Integrate vcpkg with CMake
DavidFair Oct 16, 2017
3956b06
Add PR feedback to vcpkg scripts
DavidFair Oct 17, 2017
55a43af
Copy DLLs and tools into build directory
DavidFair Oct 17, 2017
65bc0b1
Revert copying tools into build directory
DavidFair Oct 17, 2017
2511cf2
Use CMake to copy DLLs into build folder
DavidFair Oct 17, 2017
8438a69
Remove unused parameter from vcpkg script
DavidFair Oct 17, 2017
8c8d13b
Use CMake expression to find DLLs to copy in
DavidFair Oct 17, 2017
1da7580
Handle SDL debug and release libraries in the find module
DavidFair Oct 18, 2017
bbaed55
Add handling if dependencies go backwards
DavidFair Oct 18, 2017
7a4c7a5
Fixed CMake failing if SDL debug library was not found
DavidFair Oct 18, 2017
52f10df
Use bool in CMake script to test if SDLd was found
DavidFair Oct 18, 2017
6528902
Fix incorrect CMake var being appended to on Linux
DavidFair Oct 18, 2017
5fd4841
Always clean build vcpkg dependencies when commit changes
DavidFair Oct 18, 2017
db1d94d
Use vcpkg update mechanism instead of clean building
DavidFair Oct 19, 2017
af08bbc
Update vcpkg commit sha for SDL ext using release lib fix
DavidFair Oct 21, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -11,6 +11,7 @@ CorsixTH/config.txt
/Makefile
/SDL
/SDL_mixer-1.2.8
/PrecompiledDeps
/doc

# For CorsixTH directory and Windows
Expand All @@ -26,6 +27,9 @@ Release/
MinSizeRel/
RelWithDebInfo/

# Ignore the folder where libraries are pre-compiled
vcpkg/

# Build folders
/build*/

Expand Down
12 changes: 12 additions & 0 deletions CMake/CopyVcpkgDlls.cmake
@@ -0,0 +1,12 @@
# Add an extra step to copy built DLLs on MSVC
IF (MSVC AND USE_VCPKG_DEPS)

# Copy the DLLs from vcpkg into the resulting build folder
# The DLLs are either in bin/ or debug/bin/ depending on
# build configuration
add_custom_command(TARGET CorsixTH POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${VCPKG_INSTALLED_PATH}/$<$<CONFIG:Debug>:debug/>bin"
$<TARGET_FILE_DIR:CorsixTH>)

ENDIF()
34 changes: 34 additions & 0 deletions CMake/FindSDL2.cmake
Expand Up @@ -60,6 +60,19 @@ find_library(SDL_LIBRARY_TEMP
PATH_SUFFIXES lib
)

# Find debug library
find_library(SDL_LIBRARY_TEMP_D
NAMES SDL2d
HINTS
ENV SDLDIR
PATH_SUFFIXES lib
)

if (SDL_LIBRARY_TEMP_D)
set (_FOUND_SDL_D_LIBRARY TRUE)
endif()


if(NOT SDL_BUILDING_LIBRARY)
if(NOT ${SDL_INCLUDE_DIR} MATCHES ".framework")
# Non-OS X framework versions expect you to also dynamically link to
Expand All @@ -77,6 +90,14 @@ if(NOT SDL_BUILDING_LIBRARY)
/opt/csw
/opt
)

if (_FOUND_SDL_D_LIBRARY)
find_library(SDLMAIN_LIBRARY_D
NAMES SDL2maind
HINTS
ENV SDLDIR
PATH_SUFFIXES lib)
endif()
endif()
endif()

Expand All @@ -101,6 +122,9 @@ if(SDL_LIBRARY_TEMP)
list(FIND SDL_LIBRARY_TEMP "${SDLMAIN_LIBRARY}" _SDL_MAIN_INDEX)
if(_SDL_MAIN_INDEX EQUAL -1)
set(SDL_LIBRARY_TEMP "${SDLMAIN_LIBRARY}" ${SDL_LIBRARY_TEMP})
if (_FOUND_SDL_D_LIBRARY)
set(SDL_LIBRARY_TEMP_D "${SDLMAIN_LIBRARY_D}" ${SDL_LIBRARY_TEMP_D})
endif()
endif()
unset(_SDL_MAIN_INDEX)
endif()
Expand All @@ -120,6 +144,9 @@ if(SDL_LIBRARY_TEMP)
# and try using this line, so I'm just skipping it entirely for OS X.
if(NOT APPLE)
set(SDL_LIBRARY_TEMP ${SDL_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
if (_FOUND_SDL_D_LIBRARY)
set(SDL_LIBRARY_TEMP_D ${SDL_LIBRARY_TEMP_D} ${CMAKE_THREAD_LIBS_INIT})
endif()
endif()

# For MinGW library
Expand All @@ -129,8 +156,10 @@ if(SDL_LIBRARY_TEMP)

# Set the final string here so the GUI reflects the final state.
set(SDL_LIBRARY ${SDL_LIBRARY_TEMP} CACHE STRING "Where the SDL Library can be found")
set(SDL_LIBRARY_D ${SDL_LIBRARY_TEMP_D} CACHE STRING "Where the SDL Debug Library can be found")
# Set the temp variable to INTERNAL so it is not seen in the CMake GUI
set(SDL_LIBRARY_TEMP "${SDL_LIBRARY_TEMP}" CACHE INTERNAL "")
set(SDL_LIBRARY_TEMP_D "${SDL_LIBRARY_TEMP_D}" CACHE INTERNAL "")
endif()

if(SDL_INCLUDE_DIR AND EXISTS "${SDL_INCLUDE_DIR}/SDL_version.h")
Expand All @@ -153,6 +182,11 @@ endif()
include(FindPackageHandleStandardArgs)


# Combine the debug and optimized paths if we have found both
if (_FOUND_SDL_D_LIBRARY AND SDL_LIBRARY_D)
set(SDL_LIBRARY "optimized" ${SDL_LIBRARY} "debug" ${SDL_LIBRARY_D})
endif()

FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL
REQUIRED_VARS SDL_LIBRARY SDL_INCLUDE_DIR
VERSION_VAR SDL_VERSION_STRING)
103 changes: 103 additions & 0 deletions CMake/PrecompiledDeps.cmake
@@ -0,0 +1,103 @@
# Copyright (c) 2017 David Fairbrother
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# Clones and sets any dependencies up
include(ExternalProject)

# Inform CMake about the external project
set ( _DEPS_PROJECT_NAME PrecompiledDependencies)

# Place files into ./precompiled_deps folder
set( PRECOMPILED_DEPS_BASE_DIR ${PROJECT_SOURCE_DIR}/PrecompiledDeps CACHE PATH "Destination for pre-built dependencies")
set( _DEPS_GIT_URL "https://github.com/CorsixTH/deps.git")
# Select the optimal dependencies commit regardless where master is.
set( _DEPS_GIT_SHA "a23eb28bb8998b93215eccf805ee5462d75a57f2" )


ExternalProject_Add( ${_DEPS_PROJECT_NAME}
PREFIX ${PRECOMPILED_DEPS_BASE_DIR}
GIT_REPOSITORY ${_DEPS_GIT_URL}
GIT_TAG ${_DEPS_GIT_SHA}
# As the deps are already build we can skip these
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)

unset( _DEPS_GIT_URL)
unset( _DEPS_GIT_SHA)

# Make sure the final make file / solution does not attempt to build
# the dependencies target
set_target_properties( ${_DEPS_PROJECT_NAME} PROPERTIES
EXCLUDE_FROM_ALL 1
EXCLUDE_FROM_DEFAULT_BUILD 1)

set ( _DEPS_TMP_PATH ${PRECOMPILED_DEPS_BASE_DIR}/tmp)
set ( _DEPS_MODULES_TEMPLATE_NAME ${_DEPS_TMP_PATH}/${_DEPS_PROJECT_NAME})

# Clone if we don't have the deps
if ( NOT EXISTS ${PRECOMPILED_DEPS_BASE_DIR}/src/${_DEPS_PROJECT_NAME}/.git )
message( STATUS "Getting Precompiled Dependencies...")
execute_process( COMMAND ${CMAKE_COMMAND} ARGS -P
${_DEPS_MODULES_TEMPLATE_NAME}-gitclone.cmake
RESULT_VARIABLE return_value)
if (return_value)
message(FATAL_ERROR "Failed to clone precompiled dependencies.")
endif()

# Deps exist, check for updates and checkout the correct tag
else()
message( STATUS "Checking for Precompiled Dependency Updates...")
execute_process( COMMAND ${CMAKE_COMMAND} ARGS -P
${_DEPS_MODULES_TEMPLATE_NAME}-gitupdate.cmake
RESULT_VARIABLE return_value)
if(return_value)
message(FATAL_ERROR "Failed to update precompiled dependencies.")
endif()
endif()

# We can dispose of tmp and modules template name afterwards
unset( _DEPS_TMP_PATH )
unset( _DEPS_MODULES_TEMPLATE_NAME )

# Determine the appropriate libs to use for this compiler
if (UNIX AND CMAKE_COMPILER_IS_GNU)
# We need user to choose which arch they are intending to compile for
set(DEPS_ARCH "x86" CACHE STRING "Architecture of precompiled dependencies to use.")
set_property(CACHE DEPS_ARCH
PROPERTY STRINGS "x86" "x64")
# Generate the folder to use
set (_DEPS_FOLDER_NAME "gnu-linux-" + ${DEPS_ARCH})
else()
message(FATAL_ERROR "Precompiled dependencies do not exist for this platform / compiler combination yet.")
endif()

set (_DEPS_PATH ${PRECOMPILED_DEPS_BASE_DIR}/src/${_DEPS_PROJECT_NAME}/${_DEPS_FOLDER_NAME})

# Update the prefix path - this refers to the base directory that find_xx
# commands use. For example using find_include would automatically append
# the 'include' subdirectory in.
set (CMAKE_PREFIX_PATH ${_DEPS_PATH})


unset(_DEPS_FOLDER_NAME)
unset(_DEPS_PATH)
83 changes: 83 additions & 0 deletions CMake/VcpkgDeps.cmake
@@ -0,0 +1,83 @@
# Copyright (c) 2017 David Fairbrother
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

if ( MSVC AND USE_VCPKG_DEPS)
set ( VCPKG_COMMIT_SHA "01f47f5823b0c0db4a7e3e5b690dbc809429da56" CACHE
STRING "The vcpkg commit to build dependencies from")
mark_as_advanced(FORCE VCPKG_COMMIT_SHA)

# Setup the various paths we are using
set ( _VCPKG_SCRIPT_NAME "build_vcpkg_deps.ps1")
set ( _SCRIPT_DIR ${PROJECT_SOURCE_DIR}/scripts)
# By default place VCPKG into root folder
set ( VCPKG_PARENT_DIR ${PROJECT_SOURCE_DIR} CACHE PATH "Destination for vcpkg dependencies")

# Determine the args to use
if ( CMAKE_CL_64 )
set ( _VCPKG_ARGS "-IsX64Build $True ")
else()
set ( _VCPKG_ARGS "-IsX64Build $False ")
endif()

if ( BUILD_ANIMVIEWER )
string(CONCAT _VCPKG_ARGS ${_VCPKG_ARGS} "-BuildAnimView $True ")
else ()
string(CONCAT _VCPKG_ARGS ${_VCPKG_ARGS} "-BuildAnimView $False ")
endif()

string(CONCAT _VCPKG_ARGS ${_VCPKG_ARGS} "-VcpkgCommitSha " ${VCPKG_COMMIT_SHA} " ")

# Run the build script
set ( _SCRIPT_COMMAND powershell ${_SCRIPT_DIR}/${_VCPKG_SCRIPT_NAME})
execute_process(WORKING_DIRECTORY ${VCPKG_PARENT_DIR}
COMMAND ${_SCRIPT_COMMAND} ${_VCPKG_ARGS}
RESULT_VARIABLE err_val)
if (err_val)
message(FATAL_ERROR "Failed to build vcpkg dependencies. "
"\nIf this error persists try deleting the 'vcpkg' folder.\n")
endif()

# We cannot use a toolchain file at this point despite it being recommended by MS.
# The arch is determined by the generator in use on Windows. For example
# Visual Studio xx Win64 <= implies 64 bit build. If we use a toolchain this
# always defaults to a 32 bit build.

# If the user specified their own generator it is too late at this point.
# We would need to restart CMake at this point, delete the
# cache and invoke CMake again with the toolchain set.

# For both of these reasons we will use CMAKE_PREFIX_PATH

set ( VCPKG_INSTALLED_PATH ${VCPKG_PARENT_DIR}/vcpkg/installed/)
if (CMAKE_CL_64)
string(CONCAT VCPKG_INSTALLED_PATH ${VCPKG_INSTALLED_PATH} "x64-windows")
else()
string(CONCAT VCPKG_INSTALLED_PATH ${VCPKG_INSTALLED_PATH} "x86-windows")
endif()

if(CMAKE_BUILD_TYPE MATCHES "^Debug$" OR NOT DEFINED CMAKE_BUILD_TYPE)
list(APPEND CMAKE_PREFIX_PATH ${VCPKG_INSTALLED_PATH}/debug)
list(APPEND CMAKE_LIBRARY_PATH ${VCPKG_INSTALLED_PATH}/debug/lib/manual-link)
endif()

list(APPEND CMAKE_PREFIX_PATH ${VCPKG_INSTALLED_PATH})
list(APPEND CMAKE_LIBRARY_PATH ${_VCPKG_INSTALLED_DIR}/lib/manual-link)

endif()
18 changes: 17 additions & 1 deletion CMakeLists.txt
Expand Up @@ -28,6 +28,14 @@ ENDIF(MINGW)
INCLUDE(CheckIncludeFiles)
SET(CORSIX_TH_DONE_TOP_LEVEL_CMAKE ON)


# Dependency management
if ( MSVC )
OPTION(USE_VCPKG_DEPS "Build vcpkg dependencies locally" ON)
elseif ( UNIX AND CMAKE_COMPILER_IS_GNU)
OPTION(USE_PRECOMPILED_DEPS "Use Precompiled Dependencies" OFF) # Make *nix systems opt in
endif()

# Define our options
OPTION(WITH_SDL "Activate SDL Renderer" ON) # our default option
OPTION(WITH_AUDIO "Activate Sound" ON) # enabled by default
Expand Down Expand Up @@ -90,8 +98,16 @@ ELSE()
SET(CORSIX_TH_USE_VLD OFF)
ENDIF(MSVC)

# Environment handling
# Get precompiled dependencies before running the various find modules
if ( USE_PRECOMPILED_DEPS )
message("Note: Using precompiled dependencies.")
include( PrecompiledDeps )
elseif ( USE_VCPKG_DEPS)
message("Note: Using locally built vcpkg dependencies.")
include( VcpkgDeps )
endif()

# Environment handling
CHECK_INCLUDE_FILES(inttypes.h CORSIX_TH_HAS_INTTYPES_H)

# Include individual projects
Expand Down
5 changes: 5 additions & 0 deletions CorsixTH/CMakeLists.txt
Expand Up @@ -85,6 +85,11 @@ ELSE()
add_executable(CorsixTH ${corsixth_source_files})
ENDIF()

# Add an extra step to copy built DLLs on MSVC
IF (MSVC AND USE_VCPKG_DEPS)
include(CopyVcpkgDlls)
ENDIF()

# Finding libraries

# Find SDL
Expand Down
4 changes: 2 additions & 2 deletions CorsixTH/CorsixTH.rc
Expand Up @@ -7,7 +7,7 @@
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"
#include "WinResrc.h"

/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
Expand All @@ -34,7 +34,7 @@ END

2 TEXTINCLUDE
BEGIN
"#include ""afxres.h""\r\n"
"#include ""WinResrc.h""\r\n"
"\0"
END

Expand Down