Skip to content

Commit 209e005

Browse files
authored
CI & Build Scripts Refactoring (#10)
* add macos to build workflow * make C17 required * for osx build both archs * Add temp artifacts to builds * fix cmake.yml * Create setup.py * Update setup.py
1 parent 6aec5be commit 209e005

File tree

3 files changed

+142
-3
lines changed

3 files changed

+142
-3
lines changed

.github/workflows/cmake.yml

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
#
2020
# To add more build types (Release, Debug, RelWithDebInfo, etc.) customize the build_type list.
2121
matrix:
22-
os: [ubuntu-latest, windows-latest]
22+
os: [ubuntu-latest, windows-latest, macOS-latest]
2323
build_type: [Release]
2424
c_compiler: [gcc, clang, cl]
2525
include:
@@ -32,13 +32,20 @@ jobs:
3232
- os: ubuntu-latest
3333
c_compiler: clang
3434
cpp_compiler: clang++
35+
- os: macOS-latest
36+
c_compiler: clang
37+
cpp_compiler: clang++
3538
exclude:
3639
- os: windows-latest
3740
c_compiler: gcc
3841
- os: windows-latest
3942
c_compiler: clang
4043
- os: ubuntu-latest
4144
c_compiler: cl
45+
- os: macOS-latest
46+
c_compiler: cl
47+
- os: macOS-latest
48+
c_compiler: gcc
4249

4350
steps:
4451
- uses: actions/checkout@v4
@@ -70,4 +77,29 @@ jobs:
7077
working-directory: ${{ steps.strings.outputs.build-output-dir }}
7178
# Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
7279
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
73-
run: ctest --build-config ${{ matrix.build_type }} --verbose
80+
run: ctest --build-config ${{ matrix.build_type }} --verbose --output-on-failure
81+
82+
- name: Package Build Artifacts
83+
if: ${{ success() }}
84+
run: |
85+
mkdir -p ${{ steps.strings.outputs.build-output-dir }}/package
86+
# Adjust the path to your built library files
87+
cp ${{ steps.strings.outputs.build-output-dir }}/${{ matrix.build_type }}/*GameAnalytics.* ${{ steps.strings.outputs.build-output-dir }}/package/
88+
cp -r ${{ github.workspace }}/include ${{ steps.strings.outputs.build-output-dir }}/package/
89+
90+
- name: Print Package Contents on Windows
91+
if: matrix.os == 'windows-latest'
92+
run: Get-ChildItem -Recurse "${{ steps.strings.outputs.build-output-dir }}\package"
93+
shell: pwsh
94+
95+
- name: Print Package Contents on non-Windows (Linux/macOS)
96+
if: matrix.os != 'windows-latest'
97+
run: ls -la ${{ steps.strings.outputs.build-output-dir }}/package
98+
99+
- name: Upload Build Artifact
100+
if: ${{ success() }}
101+
uses: actions/upload-artifact@v4
102+
with:
103+
name: ga-cpp-sdk-${{ matrix.os }}-${{ matrix.c_compiler }}-${{ matrix.build_type }}
104+
path: ${{ steps.strings.outputs.build-output-dir }}/package/
105+

CMakeLists.txt

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG -D_DEBUG")
2929
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DNDEBUG")
3030

3131
set(CMAKE_CXX_STANDARD 17)
32+
set(CMAKE_CXX_STANDARD_REQUIRED YES)
33+
set(CMAKE_CXX_EXTENSIONS NO)
34+
3235

3336
include_directories(
3437
# gameanalytics includes
@@ -104,7 +107,32 @@ else()
104107
message(STATUS "Using user-specified PLATFORM: ${PLATFORM}")
105108
endif()
106109

107-
# --------------------------- END --------------------------- #
110+
# --------------------------- Detect Architecture Automatically --------------------------- #
111+
112+
# Print the system architecture
113+
message(STATUS "System architecture: ${CMAKE_SYSTEM_PROCESSOR}")
114+
115+
if(${PLATFORM} STREQUAL "osx")
116+
# Set archs to be build for osx to both x86_64 and arm64
117+
set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64")
118+
119+
if(DEFINED CMAKE_OSX_ARCHITECTURES)
120+
message(STATUS "Target architectures (CMAKE_OSX_ARCHITECTURES): ${CMAKE_OSX_ARCHITECTURES}")
121+
else()
122+
message(STATUS "CMAKE_OSX_ARCHITECTURES is not defined.")
123+
endif()
124+
else()
125+
# Detect if it's 32-bit or 64-bit for other systems based on the pointer size
126+
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
127+
message(STATUS "Target is 64-bit")
128+
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
129+
message(STATUS "Target is 32-bit")
130+
else()
131+
message(WARNING "Unknown architecture")
132+
endif()
133+
endif()
134+
135+
# --------------------------- Settings --------------------------- #
108136

109137
if(${GA_USE_PACKAGE})
110138

setup.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import argparse
2+
import os
3+
import subprocess
4+
import shutil
5+
import glob
6+
7+
def run_command(command, shell=True, cwd=None):
8+
if os.name == 'nt': # Check if the OS is Windows
9+
command = f'powershell.exe -Command "{command}"'
10+
result = subprocess.run(command, shell=shell, check=True, text=True, cwd=cwd)
11+
return result
12+
13+
def main():
14+
parser = argparse.ArgumentParser(description="CMake Build and Test Script")
15+
parser.add_argument('--os', required=True, choices=['linux', 'windows', 'macos'], help='Operating System')
16+
parser.add_argument('--build_type', default='Debug', choices=['Release', 'Debug'], help='Build Type')
17+
parser.add_argument('--platform', choices=['linux_x64', 'linux_x86', 'osx', 'win32', 'win64', 'uwp'], help='Platform string for CMake')
18+
parser.add_argument('--build', action='store_true', help='Execute the build step')
19+
parser.add_argument('--test', action='store_true', help='Execute the test step')
20+
args = parser.parse_args()
21+
22+
build_output_dir = os.path.join(os.getcwd(), 'build')
23+
os.makedirs(build_output_dir, exist_ok=True)
24+
25+
if args.os == 'windows':
26+
c_compiler = 'cl'
27+
cpp_compiler = 'cl'
28+
elif args.os == 'linux':
29+
c_compiler = 'gcc'
30+
cpp_compiler = 'g++'
31+
elif args.os == 'macos':
32+
c_compiler = 'clang'
33+
cpp_compiler = 'clang++'
34+
35+
# Configure CMake
36+
cmake_command = f'cmake -B {build_output_dir} -DCMAKE_CXX_COMPILER={cpp_compiler} -DCMAKE_C_COMPILER={c_compiler} -DCMAKE_BUILD_TYPE={args.build_type} -S {os.getcwd()}'
37+
if args.os == 'macos':
38+
cmake_command += ' -G "Xcode"'
39+
if args.platform:
40+
cmake_command += f' -DPLATFORM:STRING={args.platform}'
41+
run_command(cmake_command)
42+
43+
# Build
44+
if args.build:
45+
run_command(f'cmake --build {build_output_dir} --config {args.build_type}')
46+
else:
47+
exit(0)
48+
49+
# Test
50+
if args.test:
51+
run_command(f'ctest --build-config {args.build_type} --verbose --output-on-failure', cwd=build_output_dir)
52+
53+
54+
# Package Build Artifacts
55+
package_dir = os.path.join(build_output_dir, 'package')
56+
os.makedirs(package_dir, exist_ok=True)
57+
files_to_copy = glob.glob(f'{build_output_dir}/{args.build_type}/*GameAnalytics.*')
58+
for file in files_to_copy:
59+
shutil.copy(file, package_dir)
60+
shutil.copytree(os.path.join(os.getcwd(), 'include'), os.path.join(package_dir, 'include'), dirs_exist_ok=True)
61+
62+
# Print Package Contents
63+
if args.os == 'windows':
64+
run_command(f'dir {package_dir}', shell=True)
65+
else:
66+
run_command(f'ls -la {package_dir}', shell=True)
67+
68+
# Print architecture information
69+
#use lipo on macos and linux and dumpbin on windows
70+
if args.os == 'macos':
71+
run_command(f'lipo -info {package_dir}/*GameAnalytics.*')
72+
elif args.os == 'linux':
73+
run_command(f'file {package_dir}/*GameAnalytics.*')
74+
elif args.os == 'windows':
75+
run_command(f'dumpbin /headers {package_dir}/GameAnalytics.lib | findstr machine')
76+
77+
78+
if __name__ == "__main__":
79+
main()

0 commit comments

Comments
 (0)