Skip to content
This repository has been archived by the owner on Apr 2, 2020. It is now read-only.

Commit

Permalink
Add support for building Linux binaries in the SDK (#2)
Browse files Browse the repository at this point in the history
* Add scripts to build 32-bit and 64-bit Linux

* Add OpenSSL search to toolchain files

* Fix up OpenSSL search

* Update toolchain files again

* Set linker flags as well

* Try setting cache instead

* Finalise build config

* Copy across Steam libraries as necessary

* Fix up file copy for Windows/macOS

* Add tests for platforms / update test code

* Update CMakeLists.txt to copy out curl files for testing

* Fix up TEST PASS count

* Fix distribution logic

* v2

* Prevent multiple poll / changelog on SCM

* Replace nil with "none" because require return values are weird

* Don't support client connect on 32-bit macOS for Steam API
  • Loading branch information
hach-que authored Sep 5, 2017
1 parent 15494ca commit ace1d53
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 47 deletions.
16 changes: 15 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.8)
cmake_minimum_required(VERSION 3.5)
project(HiveMP.ClientConnect)

set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
Expand All @@ -15,6 +15,15 @@ option(ENABLE_MANUAL "" OFF)
SET(BUILD_TESTING OFF CACHE BOOL "Use some expat option")
SET(BUILD_CURL_EXE OFF CACHE BOOL "Use some expat option")

# Configure GCC for C++11
SET(CMAKE_CXX_STANDARD 11)

# Detect Linux
if(UNIX AND NOT APPLE)
set(LINUX TRUE)
endif()

# Platform-specific settings
if(WIN32)
option(CMAKE_USE_WINSSL "" ON)
endif()
Expand All @@ -23,6 +32,11 @@ if(APPLE)
option(CMAKE_USE_OPENSSL "Use OpenSSL code. Experimental" OFF)
SET(CURL_CA_PATH "none" CACHE STRING "Use some expat option")
endif()
if(LINUX)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE})
endif()

add_subdirectory(curl)
add_subdirectory(HiveMP.ClientConnect)
Expand Down
2 changes: 1 addition & 1 deletion HiveMP.ClientConnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../curl/include)

find_program(
LUA_EXE
NAMES lua lua53 lua52 lua51
NAMES lua lua53 lua52 lua51 lua5.3 lua5.2 lua5.1
PATHS ${CMAKE_CURRENT_SOURCE_DIR}/../lua-win32)

add_custom_command(
Expand Down
36 changes: 30 additions & 6 deletions HiveMP.SteamTest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,33 @@ add_executable(HiveMP.SteamTest main.cpp)

target_link_libraries(HiveMP.SteamTest LINK_PUBLIC HiveMP.ClientConnect)

configure_file(json.lua ${CMAKE_BINARY_DIR}/json.lua COPYONLY)
configure_file(test.lua ${CMAKE_BINARY_DIR}/test.lua COPYONLY)
configure_file(steam.lua ${CMAKE_BINARY_DIR}/steam.lua COPYONLY)
configure_file(steam_api.dll ${CMAKE_BINARY_DIR}/steam_api.dll COPYONLY)
configure_file(steam_api64.dll ${CMAKE_BINARY_DIR}/steam_api64.dll COPYONLY)
configure_file(steam_appid.txt ${CMAKE_BINARY_DIR}/steam_appid.txt COPYONLY)
if(LINUX)
set(SUBDIR "")
else()
set(SUBDIR "Release/")
endif()

configure_file(json.lua ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${SUBDIR}json.lua COPYONLY)
configure_file(test.lua ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${SUBDIR}test.lua COPYONLY)
configure_file(steam.lua ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${SUBDIR}steam.lua COPYONLY)
configure_file(steam_appid.txt ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${SUBDIR}steam_appid.txt COPYONLY)
configure_file(../HiveMP.ClientConnect/lua-curl/lua/cURL.lua ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${SUBDIR}cURL.lua COPYONLY)
configure_file(../HiveMP.ClientConnect/lua-curl/lua/cURL/utils.lua ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${SUBDIR}cURL/utils.lua COPYONLY)
configure_file(../HiveMP.ClientConnect/lua-curl/lua/cURL/safe.lua ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${SUBDIR}cURL/safe.lua COPYONLY)
configure_file(../HiveMP.ClientConnect/lua-curl/lua/cURL/impl/cURL.lua ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${SUBDIR}cURL/impl/cURL.lua COPYONLY)

if(WIN32)
configure_file(steam_api.dll ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${SUBDIR}steam_api.dll COPYONLY)
configure_file(steam_api64.dll ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${SUBDIR}steam_api64.dll COPYONLY)
endif()
if(APPLE)
configure_file(osx32/libsteam_api.dylib ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${SUBDIR}libsteam_api.dylib COPYONLY)
endif()
if(LINUX)
configure_file(linux32/libsteam_api.so ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${SUBDIR}linux32/libsteam_api.so COPYONLY)
configure_file(linux64/libsteam_api.so ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${SUBDIR}linux64/libsteam_api.so COPYONLY)
endif()

if(LINUX)
set_target_properties(HiveMP.SteamTest PROPERTIES OUTPUT_NAME "HiveMP.SteamTest-exe")
endif()
34 changes: 28 additions & 6 deletions HiveMP.SteamTest/steam.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,32 @@ int SteamAPI_ISteamFriends_GetFriendRelationship(intptr_t instancePtr, uint64_t
int SteamAPI_ISteamFriends_GetFriendPersonaState(intptr_t instancePtr, uint64_t steamIDFriend);
const char* SteamAPI_ISteamFriends_GetFriendPersonaName(intptr_t instancePtr, uint64_t steamIDFriend);
]]
if ffi.arch == "x64" then
return ffi.load("steam_api64")
elseif ffi.arch == "x86" then
return ffi.load("steam_api")
else
return nil
if ffi.os == "Linux" then
if ffi.arch == "x64" then
return ffi.load("linux64/libsteam_api.so")
elseif ffi.arch == "x86" then
return ffi.load("linux32/libsteam_api.so")
else
return "none"
end
elseif ffi.os == "OSX" then
if ffi.arch == "x64" then
return ffi.load("libsteam_api.dylib")
elseif ffi.arch == "x86" then
-- libsteam_api.dylib causes a segmentation fault on 32-bit macOS, so
-- we don't support this configuration. In the future, macOS is moving
-- to 64-bit only systems, and most games will be running in 64-bit
-- mode on any modern macOS anyway.
return "none"
else
return "none"
end
elseif ffi.os == "Windows" then
if ffi.arch == "x64" then
return ffi.load("steam_api64")
elseif ffi.arch == "x86" then
return ffi.load("steam_api")
else
return "none"
end
end
50 changes: 27 additions & 23 deletions HiveMP.SteamTest/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ print("TEST: test.lua started")

function init()
if not didSteamInit then
if not steam.SteamAPI_Init() then return end
local user = steam.SteamAPI_GetHSteamUser()
if user == nil then return end
local pipe = steam.SteamAPI_GetHSteamPipe()
if pipe == nil then return end
local client = steam.SteamInternal_CreateInterface("SteamClient017")
if client == nil then return end
local friends = steam.SteamAPI_ISteamClient_GetISteamFriends(client, user, pipe, "SteamFriends015")
if friends == nil then return end
steamInst = { user = user, pipe = pipe, client = client, friends = friends }
if steam ~= "none" then
if not steam.SteamAPI_Init() then return end
local user = steam.SteamAPI_GetHSteamUser()
if user == nil then return end
local pipe = steam.SteamAPI_GetHSteamPipe()
if pipe == nil then return end
local client = steam.SteamInternal_CreateInterface("SteamClient017")
if client == nil then return end
local friends = steam.SteamAPI_ISteamClient_GetISteamFriends(client, user, pipe, "SteamFriends015")
if friends == nil then return end
steamInst = { user = user, pipe = pipe, client = client, friends = friends }
end
didSteamInit = true
end
end
Expand Down Expand Up @@ -121,19 +123,21 @@ function session_put_hotpatch(id, endpoint, api_key, parameters_json)

local friends = {}
if didSteamInit then
local friendCount = steam.SteamAPI_ISteamFriends_GetFriendCount(steamInst.friends, 0xFFFF)
for i = 0, friendCount do
local friendId = steam.SteamAPI_ISteamFriends_GetFriendByIndex(steamInst.friends, i, 0xFFFF)
local friendRel = steam.SteamAPI_ISteamFriends_GetFriendRelationship(steamInst.friends, friendId)
local friendState = steam.SteamAPI_ISteamFriends_GetFriendPersonaState(steamInst.friends, friendId)
local friendName = ffi.string(steam.SteamAPI_ISteamFriends_GetFriendPersonaName(steamInst.friends, friendId))

table.insert(friends, {
id = tostring(friendId),
relationship = tonumber(friendRel),
state = tonumber(friendState),
name = tostring(friendName)
})
if steam ~= "none" then
local friendCount = steam.SteamAPI_ISteamFriends_GetFriendCount(steamInst.friends, 0xFFFF)
for i = 0, friendCount do
local friendId = steam.SteamAPI_ISteamFriends_GetFriendByIndex(steamInst.friends, i, 0xFFFF)
local friendRel = steam.SteamAPI_ISteamFriends_GetFriendRelationship(steamInst.friends, friendId)
local friendState = steam.SteamAPI_ISteamFriends_GetFriendPersonaState(steamInst.friends, friendId)
local friendName = ffi.string(steam.SteamAPI_ISteamFriends_GetFriendPersonaName(steamInst.friends, friendId))

table.insert(friends, {
id = tostring(friendId),
relationship = tonumber(friendRel),
state = tonumber(friendState),
name = tostring(friendName)
})
end
end
end

Expand Down
13 changes: 11 additions & 2 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ stage("Build") {
parallel (
"Windows" : {
node('windows') {
checkout scm
checkout poll: false, changelog: false, scm: scm
bat 'git submodule update --init'
bat 'powershell.exe .\\Build.ps1'
stash includes: 'dist/**', name: 'windows'
Expand All @@ -11,12 +11,21 @@ stage("Build") {
},
"macOS" : {
node('mac') {
checkout scm
checkout poll: false, changelog: false, scm: scm
sh 'git submodule update --init'
sh './build.mac.sh'
stash includes: 'dist/**', name: 'mac'
archiveArtifacts 'dist/**'
}
},
"Linux" : {
node('linux') {
checkout scm
sh 'git submodule update --init'
sh './build.linux.sh'
stash includes: 'dist/**', name: 'linux'
archiveArtifacts 'dist/**'
}
}
)
}
57 changes: 57 additions & 0 deletions build.linux.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash

set -e
set -x

cd "$(dirname "$0")"
ROOT=$(pwd)
cd $ROOT

sed -i -e 's/add_subdirectory\(docs\)/#add_subdirectory\(docs\)/g' curl/CMakeLists.txt

# Builds are faster if we don't clear the CMake cache.
SHASUM=$(shasum CMakeLists.txt | awk '{print $1}')
VERSION=v2

if [ ! -d buildlinux32_${SHASUM}_${VERSION} ]; then
mkdir buildlinux32_${SHASUM}_${VERSION}
fi
cd buildlinux32_${SHASUM}_${VERSION}
cmake -G "Unix Makefiles" -D CMAKE_BUILD_TYPE=Release -D CMAKE_TOOLCHAIN_FILE=../toolchain/Linux-i386.cmake ..
make

cd $ROOT

if [ ! -d buildlinux64_${SHASUM}_${VERSION} ]; then
mkdir buildlinux64_${SHASUM}_${VERSION}
fi
cd buildlinux64_${SHASUM}_${VERSION}
cmake -G "Unix Makefiles" -D CMAKE_BUILD_TYPE=Release -D CMAKE_TOOLCHAIN_FILE=../toolchain/Linux-x86_64.cmake ..
make

echo "Testing 32-bit binaries..."
cd $ROOT/buildlinux32_${SHASUM}_${VERSION}/Release
./HiveMP.SteamTest-exe | tee result.txt
if [ "$(cat result.txt | grep -c "TEST PASS")" != "2" ]; then
echo "Test failed!"
exit 1
fi

echo "Testing 64-bit binaries..."
cd $ROOT/buildlinux64_${SHASUM}_${VERSION}/Release
./HiveMP.SteamTest-exe | tee result.txt
if [ "$(cat result.txt | grep -c "TEST PASS")" != "2" ]; then
echo "Test failed!"
exit 1
fi

cd $ROOT

echo "Creating distribution structure..."
if [ -d dist ]; then
rm -Rf dist
fi
mkdir -pv dist/sdk/Linux32
mkdir -pv dist/sdk/Linux64
cp buildlinux32_${SHASUM}_${VERSION}/Release/*.so dist/sdk/Linux32/
cp buildlinux64_${SHASUM}_${VERSION}/Release/*.so dist/sdk/Linux64/
33 changes: 25 additions & 8 deletions build.mac.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,40 @@ sed -i -e 's/add_subdirectory\(docs\)/#add_subdirectory\(docs\)/g' curl/CMakeLis

# Builds are faster if we don't clear the CMake cache.
SHASUM=$(shasum CMakeLists.txt | awk '{print $1}')
VERSION=v2

if [ ! -d buildmac32_${SHASUM}_v1 ]; then
mkdir buildmac32_${SHASUM}_v1
if [ ! -d buildmac32_${SHASUM}_${VERSION} ]; then
mkdir buildmac32_${SHASUM}_${VERSION}
fi
cd buildmac32_${SHASUM}_v1
cd buildmac32_${SHASUM}_${VERSION}
cmake -G "Xcode" -D CMAKE_OSX_ARCHITECTURES=i386 -D OPENSSL_INCLUDE_DIR=/usr/local/opt/openssl/include ..
xcodebuild -project HiveMP.ClientConnect.xcodeproj -configuration Release build

cd $ROOT

if [ ! -d buildmac64_${SHASUM}_v1 ]; then
mkdir buildmac64_${SHASUM}_v1
if [ ! -d buildmac64_${SHASUM}_${VERSION} ]; then
mkdir buildmac64_${SHASUM}_${VERSION}
fi
cd buildmac64_${SHASUM}_v1
cd buildmac64_${SHASUM}_${VERSION}
cmake -G "Xcode" -D CMAKE_OSX_ARCHITECTURES=x86_64 -D OPENSSL_INCLUDE_DIR=/usr/local/opt/openssl/include ..
xcodebuild -project HiveMP.ClientConnect.xcodeproj -configuration Release build

echo "Testing 32-bit binaries..."
cd $ROOT/buildmac32_${SHASUM}_${VERSION}/Release
./HiveMP.SteamTest | tee result.txt
if [ "$(cat result.txt | grep -c "TEST PASS")" != "2" ]; then
echo "Test failed!"
exit 1
fi

echo "Testing 64-bit binaries..."
cd $ROOT/buildmac64_${SHASUM}_${VERSION}/Release
./HiveMP.SteamTest | tee result.txt
if [ "$(cat result.txt | grep -c "TEST PASS")" != "2" ]; then
echo "Test failed!"
exit 1
fi

cd $ROOT

echo "Creating distribution structure..."
Expand All @@ -36,5 +53,5 @@ if [ -d dist ]; then
fi
mkdir -pv dist/sdk/Mac32
mkdir -pv dist/sdk/Mac64
cp buildmac32_${SHASUM}_v1/Release/*.dylib dist/sdk/Mac32/
cp buildmac64_${SHASUM}_v1/Release/*.dylib dist/sdk/Mac64/
cp buildmac32_${SHASUM}_${VERSION}/Release/*.dylib dist/sdk/Mac32/
cp buildmac64_${SHASUM}_${VERSION}/Release/*.dylib dist/sdk/Mac64/
8 changes: 8 additions & 0 deletions toolchain/Linux-i386.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
set(CMAKE_C_FLAGS "-m32" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS "-m32" CACHE STRING "" FORCE)
set(CMAKE_SHARED_LINKER_FLAGS "-m32" CACHE STRING "" FORCE)
set(CMAKE_STATIC_LINKER_FLAGS "-m32" CACHE STRING "" FORCE)
set(CMAKE_EXE_LINKER_FLAGS "-m32" CACHE STRING "" FORCE)

set(OPENSSL_INCLUDE_DIR /usr/include/i386-linux-gnu)
set(OPENSSL_ROOT_DIR "/usr/lib/i386-linux-gnu" CACHE STRING "" FORCE)
8 changes: 8 additions & 0 deletions toolchain/Linux-x86_64.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
set(CMAKE_C_FLAGS "-m64" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS "-m64" CACHE STRING "" FORCE)
set(CMAKE_SHARED_LINKER_FLAGS "-m64" CACHE STRING "" FORCE)
set(CMAKE_STATIC_LINKER_FLAGS "-m64" CACHE STRING "" FORCE)
set(CMAKE_EXE_LINKER_FLAGS "-m64" CACHE STRING "" FORCE)

set(OPENSSL_INCLUDE_DIR /usr/include/x86_64-linux-gnu)
set(OPENSSL_ROOT_DIR "/usr/lib/x86_64-linux-gnu" CACHE STRING "" FORCE)

0 comments on commit ace1d53

Please sign in to comment.