Permalink
Browse files

Add script to build and package libs in MS vcpkg

This commit adds a new script which can be executed on
a Windows machine to build and package the libraries
for CorsixTH. A new folder called vcpkg will be created
in the same folder as the script. The script is
integrated into CMake and run by default when
targetting MSVC.
  • Loading branch information...
DavidFair authored and TheCycoONE committed Oct 15, 2017
1 parent 7ad371f commit ef0c23866d4aa570629cc144b0a8aff5a1c46f6d
Showing with 282 additions and 23 deletions.
  1. +3 −0 .gitignore
  2. +29 −0 CMake/CopyVcpkgLua.cmake
  3. +61 −0 CMake/VcpkgDeps.cmake
  4. +19 −7 CMakeLists.txt
  5. +24 −14 CorsixTH/CMakeLists.txt
  6. +2 −2 CorsixTH/CorsixTH.rc
  7. +144 −0 scripts/build_vcpkg_deps.ps1
View
@@ -27,6 +27,9 @@ Release/
MinSizeRel/
RelWithDebInfo/
# Ignore the folder where libraries are pre-compiled
vcpkg/
# Build folders
/build*/
View
@@ -0,0 +1,29 @@
# Add an extra step to copy LUA files from vcpkg
add_custom_command(TARGET CorsixTH POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${VCPKG_INSTALLED_PATH}/share/lua"
$<TARGET_FILE_DIR:CorsixTH>)
add_custom_command(TARGET CorsixTH POST_BUILD
COMMAND ${CMAKE_COMMAND} -E remove
$<TARGET_FILE_DIR:CorsixTH>/COPYRIGHT)
add_custom_command(TARGET CorsixTH POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${VCPKG_INSTALLED_PATH}/$<$<CONFIG:Debug>:debug/>bin/lfs.dll"
$<TARGET_FILE_DIR:CorsixTH>/lfs.dll)
add_custom_command(TARGET CorsixTH POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${VCPKG_INSTALLED_PATH}/$<$<CONFIG:Debug>:debug/>bin/lpeg.dll"
$<TARGET_FILE_DIR:CorsixTH>/lpeg.dll)
add_custom_command(TARGET CorsixTH POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${VCPKG_INSTALLED_PATH}/$<$<CONFIG:Debug>:debug/>bin/mime/core.dll"
$<TARGET_FILE_DIR:CorsixTH>/mime/core.dll)
add_custom_command(TARGET CorsixTH POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${VCPKG_INSTALLED_PATH}/$<$<CONFIG:Debug>:debug/>bin/socket/core.dll"
$<TARGET_FILE_DIR:CorsixTH>/socket/core.dll)
View
@@ -0,0 +1,61 @@
# 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.
set(VCPKG_COMMIT_SHA "1993a5eae610b877c7ed865040eaa3fb92e731c4")
# Setup the various paths we are using
set(_VCPKG_SCRIPT_NAME "build_vcpkg_deps.ps1")
set(_SCRIPT_DIR ${CMAKE_SOURCE_DIR}/scripts)
# By default place VCPKG into root folder
set(VCPKG_PARENT_DIR ${CMAKE_SOURCE_DIR} CACHE PATH "Destination for vcpkg dependencies")
# Determine the args to use
if(VCPKG_TARGET_TRIPLET)
set(_VCPKG_TARGET_TRIPLET ${VCPKG_TARGET_TRIPLET})
elseif(CMAKE_GENERATOR_PLATFORM MATCHES "^[Xx]64$" OR CMAKE_GENERATOR MATCHES "Win64$")
set(_VCPKG_TARGET_TRIPLET "x64-windows")
elseif(CMAKE_GENERATOR_PLATFORM MATCHES "^[Aa][Rr][Mm]$" OR CMAKE_GENERATOR MATCHES "ARM$")
set(_VCPKG_TARGET_TRIPLET "arm-windows")
else()
set(_VCPKG_TARGET_TRIPLET "x86-windows")
endif()
set(_VCPKG_ARGS "-VcpkgTriplet " ${_VCPKG_TARGET_TRIPLET})
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()
set(VCPKG_INSTALLED_PATH ${VCPKG_PARENT_DIR}/vcpkg/installed/${_VCPKG_TARGET_TRIPLET})
set(CMAKE_TOOLCHAIN_FILE ${VCPKG_PARENT_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake)
View
@@ -12,8 +12,20 @@
# - WITH_VLD : Build with Visual Leak Detector (requires Visual Studio)
CMAKE_MINIMUM_REQUIRED(VERSION 3.2)
PROJECT(CorsixTH_Top_Level)
SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMake)
IF(CMAKE_GENERATOR MATCHES "^Visual Studio 14 2015" OR CMAKE_GENERATOR MATCHES "^Visual Studio 15 2017")
OPTION(USE_VCPKG_DEPS "Build vcpkg dependencies locally" OFF)
ENDIF()
if(USE_VCPKG_DEPS)
message("Note: Using locally built vcpkg dependencies.")
include(VcpkgDeps)
endif()
PROJECT(CorsixTH_Top_Level)
INCLUDE(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
@@ -28,13 +40,13 @@ ENDIF(MINGW)
INCLUDE(CheckIncludeFiles)
SET(CORSIX_TH_DONE_TOP_LEVEL_CMAKE ON)
# Define our options
if ( MSVC )
OPTION(USE_PRECOMPILED_DEPS "Use Precompiled Dependencies" ON) # On for MSVC
elseif ( UNIX AND CMAKE_COMPILER_IS_GNU)
# Dependency management
if ( 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
OPTION(WITH_MOVIES "Activate in game movies" ON)
@@ -98,8 +110,8 @@ ENDIF(MSVC)
# Get precompiled dependencies before running the various find modules
if ( USE_PRECOMPILED_DEPS )
message("Note: Using Precompiled Dependencies.")
include(PrecompiledDeps)
message("Note: Using precompiled dependencies.")
include( PrecompiledDeps )
endif()
# Environment handling
View
@@ -85,23 +85,33 @@ ELSE()
add_executable(CorsixTH ${corsixth_source_files})
ENDIF()
# Add an extra step to copy built DLLs on MSVC
IF(USE_VCPKG_DEPS)
INCLUDE(CopyVcpkgLua)
ENDIF()
# Finding libraries
# Find SDL
FIND_PACKAGE(SDL2 REQUIRED)
IF(SDL_FOUND)
INCLUDE_DIRECTORIES(${SDL_INCLUDE_DIR})
IF(SDLMAIN_LIBRARY STREQUAL "")
message(FATAL_ERROR "Error: SDL was found but SDLmain was not")
message("Make sure the path is correctly defined or set the environment variable SDLDIR to the correct location")
ENDIF(SDLMAIN_LIBRARY STREQUAL "")
# No need to specify sdlmain separately, the FindSDL.cmake file will take care of that. If not we get an error about it
TARGET_LINK_LIBRARIES(CorsixTH ${SDL_LIBRARY})
message(" SDL found")
ELSE(SDL_FOUND)
message(FATAL_ERROR "Error: SDL library not found, it is required to build. Make sure the path is correctly defined or set the environment variable SDLDIR to the correct location")
ENDIF(SDL_FOUND)
IF (MSVC AND USE_VCPKG_DEPS)
FIND_PACKAGE(SDL2 CONFIG REQUIRED)
TARGET_LINK_LIBRARIES(CorsixTH SDL2::SDL2)
TARGET_LINK_LIBRARIES(CorsixTH SDL2::SDL2main)
ELSE()
FIND_PACKAGE(SDL2 REQUIRED)
IF(SDL_FOUND)
INCLUDE_DIRECTORIES(${SDL_INCLUDE_DIR})
IF(SDLMAIN_LIBRARY STREQUAL "")
message(FATAL_ERROR "Error: SDL was found but SDLmain was not")
message("Make sure the path is correctly defined or set the environment variable SDLDIR to the correct location")
ENDIF(SDLMAIN_LIBRARY STREQUAL "")
# No need to specify sdlmain separately, the FindSDL.cmake file will take care of that. If not we get an error about it
TARGET_LINK_LIBRARIES(CorsixTH ${SDL_LIBRARY})
message(" SDL found")
ELSE(SDL_FOUND)
message(FATAL_ERROR "Error: SDL library not found, it is required to build. Make sure the path is correctly defined or set the environment variable SDLDIR to the correct location")
ENDIF(SDL_FOUND)
ENDIF()
# Find Lua
FIND_PACKAGE(Lua REQUIRED)
IF(Lua_FOUND)
View
@@ -7,7 +7,7 @@
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"
#include "WinResrc.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
@@ -34,7 +34,7 @@ END
2 TEXTINCLUDE
BEGIN
"#include ""afxres.h""\r\n"
"#include ""WinResrc.h""\r\n"
"\0"
END
@@ -0,0 +1,144 @@
# 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.
# Requirements:
# git - Installed and added to path
# Parameters
Param(
[Parameter(Mandatory=$true)][bool]$BuildAnimView,
[Parameter(Mandatory=$true)][string]$VcpkgTriplet,
[Parameter(Mandatory=$true)][string]$VcpkgCommitSha
)
################
# Variables
################
$anim_view_libs = "wxwidgets"
$corsixth_libs = "ffmpeg", "freetype", "lua", "luafilesystem", "lpeg", "sdl2", "sdl2-mixer", "luasocket"
$vcpkg_git_url = "https://github.com/CorsixTH/vcpkg"
$dest_folder_name = "vcpkg"
$dest_folder_path = ".\vcpkg"
###################
# Functions
###################
function run_command($command) {
Invoke-Expression $command
if ($LASTEXITCODE -ne 0) {
throw "Failed to run command :`n $command `nExiting."
}
}
##################
# Main script
##################
# Wrap in function so we can try-catch
function run_script {
# Test the required files are in the path
if ((Get-Command "git.exe") -eq $null) {
throw "Error git was not found. Is it installed, added to your path
and have you restarted since?"
}
# Check we have the latest copy of vcpkg
if (-Not (Test-Path $dest_folder_path)) {
# If vcpkg does not exist clone it
run_command -command "git clone $vcpkg_git_url $dest_folder_name"
Set-Location -Path $dest_folder_path
run_command "git checkout $VcpkgCommitSha"
} else {
# Move into vcpkg folder and update to latest version
Set-Location -Path $dest_folder_path
run_command "git reset --hard; git fetch origin; git checkout $VcpkgCommitSha"
}
$commit_id_filename = "commit_id.txt"
if (-Not (Test-Path $commit_id_filename) -or
(Get-Content $commit_id_filename | Where-Object {$_ -ne $VcpkgCommitSha })) {
# Sha does not match or does not exist.
Write-Output "Dependencies have changed. Bootstrapping and updating vcpkg."
run_command ".\bootstrap-vcpkg.bat"
# Remove any outdated libraries before installing
run_command ".\vcpkg remove --outdated --recurse"
# Update the SHA we last saw
Set-Content -Path $commit_id_filename -Value $VcpkgCommitSha
}
# Build the triplet flag e.g. --triplet "x64-windows"
$triplet = "--triplet `""
$triplet += $VcpkgTriplet
$triplet += '"'
$libs_list = ""
# Build our libs list
foreach ($library in $corsixth_libs) {
$libs_list += $library + ' '
}
# If we are building the animation viewer be sure to include those libs
if ($BuildAnimView) {
foreach ($library in $anim_view_libs) {
$libs_list += $library + ' '
}
}
# Compile them locally
$install_command = ".\vcpkg install " + $triplet + $libs_list
run_command -command $install_command
# Copy various files from bin to tools
$vcpkg_installed_path = ".\installed\"
$vcpkg_installed_path += $VcpkgTriplet
Set-Location $vcpkg_installed_path
Write-Output "Copying files from bin to tools"
$files_to_copy_from_bin = "lfs.dll", "lpeg.dll"
foreach ($file in $files_to_copy_from_bin) {
Copy-Item -Path ".\bin\$file" -Destination ".\tools\lua"
}
Write-Output "Finished building libraries"
}
# Run the script
$starting_dir = Convert-Path .
try {
run_script
# Move back up a dir to return user to original location
Set-Location -Path $starting_dir
} catch [Exception]{
Set-Location -Path $starting_dir
# Echo the exception back out
$_
Exit -1
}

0 comments on commit ef0c238

Please sign in to comment.