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

Better Linux support #32

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
75 changes: 75 additions & 0 deletions .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# This starter workflow is for a CMake project running on multiple platforms. There is a different starter workflow if you just want a single platform.
# See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-single-platform.yml
name: CMake on multiple platforms

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build:
runs-on: ${{ matrix.os }}

strategy:
# Set fail-fast to false to ensure that feedback is delivered for all matrix combinations. Consider changing this to true when your workflow is stable.
fail-fast: false

# Set up a matrix to run the following 3 configurations:
# 1. <Windows, Release, latest MSVC compiler toolchain on the default runner image, default generator>
# 2. <Linux, Release, latest GCC compiler toolchain on the default runner image, default generator>
# 3. <Linux, Release, latest Clang compiler toolchain on the default runner image, default generator>
#
# To add more build types (Release, Debug, RelWithDebInfo, etc.) customize the build_type list.
matrix:
os: [ubuntu-latest, windows-latest]
build_type: [Release]
c_compiler: [gcc, clang, cl]
include:
- os: windows-latest
c_compiler: cl
cpp_compiler: cl
- os: ubuntu-latest
c_compiler: gcc
cpp_compiler: g++
- os: ubuntu-latest
c_compiler: clang
cpp_compiler: clang++
exclude:
- os: windows-latest
c_compiler: gcc
- os: windows-latest
c_compiler: clang
- os: ubuntu-latest
c_compiler: cl

steps:
- uses: actions/checkout@v3

- name: Set reusable strings
# Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file.
id: strings
shell: bash
run: |
echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"

- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: >
cmake -B ${{ steps.strings.outputs.build-output-dir }}
-DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}
-DCMAKE_C_COMPILER=${{ matrix.c_compiler }}
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
-S ${{ github.workspace }}

- name: Build
# Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}

- name: Test
working-directory: ${{ steps.strings.outputs.build-output-dir }}
# 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).
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest --build-config ${{ matrix.build_type }}
121 changes: 53 additions & 68 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Project Info
cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
cmake_policy(VERSION 3.18)
project(CrossWindow)
Expand All @@ -7,10 +6,10 @@ enable_language(CXX)

# CMake Settings
set(CMAKE_SUPPRESS_REGENERATION true)
set(DCMAKE_GENERATOR_PLATFORM "x64")
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")

# =============================================================

Expand All @@ -22,20 +21,44 @@ set_property(
STRINGS AUTO WIN32 UWP COCOA UIKIT XCB XLIB MIR WAYLAND ANDROID WASM NOOP
)

set(XWIN_OS AUTO CACHE STRING "Optional: Choose the OS to build for, defaults to AUTO, but can be WINDOWS, MACOS, LINUX, ANDROID, IOS, WASM.")
set(XWIN_OS AUTO CACHE STRING "Optional: Choose the OS to build for, defaults to AUTO, but can be WINDOWS, MACOS, LINUX, ANDROID, IOS, WASM.")
set_property(
CACHE
XWIN_OS PROPERTY
STRINGS AUTO WINDOWS MACOS LINUX ANDROID IOS WASM NOOP
)

function(deduce_linux_display_server)
message( STATUS "XDG session type: " "$ENV{XDG_SESSION_TYPE}" )
if ("$ENV{XDG_SESSION_TYPE}" STREQUAL "wayland")
set(XWIN_API "WAYLAND" PARENT_SCOPE)
elseif ("$ENV{XDG_SESSION_TYPE}" STREQUAL "x11")
set(XWIN_API "XCB" PARENT_SCOPE)
else()
execute_process (
COMMAND bash -c "env | awk -F= '$2 ~ /wayland/ {count++} END {print count}'"
OUTPUT_VARIABLE WaylandCount
)
execute_process (
COMMAND bash -c "env | awk -F= '$2 ~ /x11/ {count++} END {print count}'"
OUTPUT_VARIABLE X11Count
)
message(STATUS "Display server counts: " ${X11Count} " " ${WaylandCount})
if (${X11Count} LESS ${WaylandCount})
set(XWIN_API "WAYLAND" PARENT_SCOPE)
else()
set(XWIN_API "XCB" PARENT_SCOPE)
endif()
endif()
endfunction()

if( NOT (XWIN_OS STREQUAL "AUTO") AND XWIN_API STREQUAL "AUTO")
if(XWIN_OS STREQUAL "WINDOWS")
set(XWIN_API "WIN32")
elseif(XWIN_OS STREQUAL "MACOS")
set(XWIN_API "COCOA")
elseif(XWIN_OS STREQUAL "LINUX")
set(XWIN_API "XLIB")
deduce_linux_display_server()
elseif(XWIN_OS STREQUAL "ANDROID")
set(XWIN_API "ANDROID")
elseif(XWIN_OS STREQUAL "IOS")
Expand All @@ -51,11 +74,11 @@ endif()

if(XWIN_API STREQUAL "AUTO")
if (${CMAKE_HOST_SYSTEM_NAME} MATCHES "Windows")
set(XWIN_API "WIN32" CACHE STRING "A more specific platform selector to choose from, choose the exact OS protocol to use, can be WIN32, UWP, COCOA, UIKIT, XCB, XLIB, MIR, WAYLAND, ANDROID, WASM, NOOP." FORCE)
set(XWIN_API "WIN32" CACHE STRING "A more specific platform selector to choose from, choose the exact OS protocol to use, can be WIN32, UWP, COCOA, UIKIT, XCB, XLIB, MIR, WAYLAND, ANDROID, WASM, NOOP." FORCE)
elseif (${CMAKE_HOST_SYSTEM_NAME} MATCHES "Darwin")
set(XWIN_API "COCOA" CACHE STRING "A more specific platform selector to choose from, choose the exact OS protocol to use, can be WIN32, UWP, COCOA, UIKIT, XCB, XLIB, MIR, WAYLAND, ANDROID, WASM, NOOP." FORCE)
set(XWIN_API "COCOA" CACHE STRING "A more specific platform selector to choose from, choose the exact OS protocol to use, can be WIN32, UWP, COCOA, UIKIT, XCB, XLIB, MIR, WAYLAND, ANDROID, WASM, NOOP." FORCE)
elseif (${CMAKE_HOST_SYSTEM_NAME} MATCHES "Linux")
set(XWIN_API "XCB" CACHE STRING "A more specific platform selector to choose from, choose the exact OS protocol to use, can be WIN32, UWP, COCOA, UIKIT, XCB, XLIB, MIR, WAYLAND, ANDROID, WASM, NOOP." FORCE)
deduce_linux_display_server()
endif()
endif()

Expand All @@ -72,7 +95,9 @@ elseif(XWIN_API STREQUAL "UIKIT")
elseif(XWIN_API STREQUAL "XCB")
set(XWIN_API_PATH "XCB")
elseif(XWIN_API STREQUAL "XLIB")
set(XWIN_API_PATH "XLIB")
set(XWIN_API_PATH "XLib")
elseif(XWIN_API STREQUAL "WAYLAND")
set(XWIN_API_PATH "Wayland")
elseif(XWIN_API STREQUAL "ANDROID")
set(XWIN_API_PATH "Android")
elseif(XWIN_API STREQUAL "UIKIT")
Expand All @@ -82,7 +107,7 @@ elseif(XWIN_API STREQUAL "WASM")
elseif(XWIN_API STREQUAL "NOOP")
set(XWIN_API_PATH "Noop")
else()
message( SEND_ERROR "XWIN_API can only be either AUTO, NOOP, WIN32, UWP, COCOA, UIKIT, XCB, XLIB, MIR, WAYLAND, ANDROID, or WASM.")
message( SEND_ERROR "Detected: ${XWIN_API}; XWIN_API can only be either AUTO, NOOP, WIN32, UWP, COCOA, UIKIT, XCB, XLIB, MIR, WAYLAND, ANDROID, or WASM.")
endif()

message( STATUS "Building CrossWindow for " ${XWIN_API_PATH} )
Expand Down Expand Up @@ -123,58 +148,7 @@ set(XMAIN_SOURCES ${MAIN_SOURCES} CACHE STRING "Global Variable - The source fil

# =============================================================

# CrossWindow Functions
function(xwin_setup versionMajor versionMinor versionPatch versionRevision companyName iconPath)
# @TODO - implement
message("Warning: xwin_setup has not yet been implemented.")
endfunction()

function(xwin_add_executable targetProject targetSources)
message("Creating CrossWindow executable:")

foreach(source IN LISTS XMAIN_SOURCES)
source_group("" FILES "${source}")
endforeach()
set(XWIN_FILES "${XMAIN_SOURCES}" "${targetSources}")

if(XWIN_API STREQUAL "WIN32" OR XWIN_API STREQUAL "UWP")
add_executable(
${targetProject}
WIN32
"${XWIN_FILES}"
)
elseif(XWIN_API STREQUAL "COCOA" OR XWIN_API STREQUAL "UIKIT")
add_executable(
${targetProject}
MACOSX_BUNDLE
${XWIN_FILES}

)
elseif(XWIN_API STREQUAL "XCB" OR XWIN_API STREQUAL "XLIB")
add_executable(
${targetProject}
${XWIN_FILES}
)
elseif(XWIN_API STREQUAL "ANDROID")
add_executable(
${targetProject}
${XWIN_FILES}
)
elseif(XWIN_API STREQUAL "WASM")
add_executable(
${targetProject}
${XWIN_FILES}
)
elseif(XWIN_API STREQUAL "NOOP")
add_executable(
${targetProject}
${XWIN_FILES}
)
endif()

target_compile_definitions(${targetProject} PRIVATE XWIN_${XWIN_API}=1)

endfunction()
set(XWIN_DEFINITIONS XWIN_${XWIN_API}=1 CACHE STRING "Global Variable - The compile definitions for the currently active protocol.")

# =============================================================

Expand Down Expand Up @@ -211,7 +185,7 @@ elseif(XWIN_API STREQUAL "UIKIT")
find_library(MOBILECORESERVICES MobileCoreServices)
find_library(CFNETWORK CFNetwork)
find_library(SYSTEMCONFIGURATION SystemConfiguration)

target_link_libraries(
${PROJECT_NAME}
${UIKIT}
Expand All @@ -234,13 +208,24 @@ elseif(XWIN_API STREQUAL "XLIB")
target_link_libraries(${PROJECT_NAME} ${X11_LIBRARIES})
target_include_directories(${PROJECT_NAME} PUBLIC ${X11_INCLUDE_DIR})
endif()
elseif(XWIN_API STREQUAL "WAYLAND")
find_package(Wayland REQUIRED)
if(Wayland_FOUND)
message("Found Wayland Libraries.")
message("Wayland includes = ${Wayland_INCLUDE_DIRS}")
message("Wayland Libraries = ${Wayland_LIBRARIES}")
target_link_libraries(${PROJECT_NAME} ${Wayland_LIBRARIES})
target_include_directories(${PROJECT_NAME} PUBLIC ${Wayland_INCLUDE_DIRS})
endif()
elseif(XWIN_API STREQUAL "XCB")
find_package(X11 REQUIRED)
message("Found XCB Libraries.")
message("XCB Include Path = ${X11_xcb_INCLUDE_PATH}")
message("XCB Lib = ${X11_xcb_LIB}")
target_link_libraries(${PROJECT_NAME} ${X11_xcb_LIB})
target_include_directories(${PROJECT_NAME} PUBLIC ${X11_xcb_INCLUDE_PATH})
if(X11_FOUND)
message("Found XCB Libraries.")
message("XCB Include Path = ${X11_xcb_INCLUDE_PATH}")
message("XCB Lib = ${X11_xcb_LIB}")
target_link_libraries(${PROJECT_NAME} ${X11_xcb_LIB})
target_include_directories(${PROJECT_NAME} PUBLIC ${X11_xcb_INCLUDE_PATH})
endif()
endif()
# =============================================================

Expand Down
Loading