Skip to content

Commit b38a237

Browse files
authored
Merge pull request #192 from rpavlik/buildsys
Portability and Build System Cleanup
2 parents 3e40561 + f438ea2 commit b38a237

28 files changed

Lines changed: 300 additions & 96 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ out/build/
2828
# VS misc
2929
*.sln
3030
*.pyproj
31+
CMakeSettings.json
32+
CppProperties.json
3133

3234
# Don't ignore pipeline stuff
3335
!.azure-pipelines/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
- pr.188.gh.OpenXR-SDK-Source
3+
---
4+
Allow disabling of std::filesystem usage, and detect if it's available and what its requirements are.

external/include/GL/gl_format.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,13 @@ static inline void glGetFormatSize( const GLenum internalFormat, GlFormatSize *
9292
#include <assert.h>
9393

9494
#if defined(_WIN32)
95+
#ifndef NOMINMAX
9596
#define NOMINMAX
96-
#ifndef __cplusplus
97+
#endif
98+
#if defined(_MSC_VER) && !defined(__cplusplus)
9799
#undef inline
98100
#define inline __inline
99-
#endif // __cplusplus
101+
#endif // defined(_MSC_VER) && !defined(__cplusplus)
100102
#endif
101103

102104
typedef unsigned int GLenum;

external/include/utils/threading.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,8 @@ typedef struct
535535
// Note that on Android AttachCurrentThread will reset the thread name.
536536
static void ksThread_SetName( const char * name )
537537
{
538-
#if defined( OS_WINDOWS )
538+
(void)name;
539+
#if defined( OS_WINDOWS ) && !defined(__MINGW32__)
539540
static const unsigned int MS_VC_EXCEPTION = 0x406D1388;
540541

541542
#pragma pack( push, 8 )

src/CMakeLists.txt

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,30 @@
1717
# Author:
1818
#
1919

20+
if(POLICY CMP0075)
21+
cmake_policy(SET CMP0075 NEW)
22+
endif()
23+
2024
# Entire project uses C++14
2125
set(CMAKE_CXX_STANDARD 14)
2226
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
2327
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
2428

2529
include(GNUInstallDirs)
30+
include(StdFilesystemFlags)
2631

2732
### Dependencies
2833
set(OpenGL_GL_PREFERENCE GLVND)
29-
if (NOT CMAKE_SYSTEM_NAME STREQUAL "WindowsStore")
34+
if(NOT CMAKE_SYSTEM_NAME STREQUAL "WindowsStore")
3035
# OpenGL package will be found for UWP apps but gl.h excludes UWP apps so it isn't actually usable.
31-
find_package(OpenGL)
36+
find_package(OpenGL)
3237
endif()
3338

3439
if(OPENGL_FOUND)
3540
add_definitions(-DXR_USE_GRAPHICS_API_OPENGL)
3641
message(STATUS "Enabling OpenGL support")
37-
elseif(BUILD_ALL_EXTENSIONS)
38-
if (NOT CMAKE_SYSTEM_NAME STREQUAL "WindowsStore")
39-
message(FATAL_ERROR "OpenGL not found")
40-
endif()
42+
elseif(BUILD_ALL_EXTENSIONS AND NOT CMAKE_SYSTEM_NAME STREQUAL "WindowsStore")
43+
message(FATAL_ERROR "OpenGL not found")
4144
endif()
4245

4346
if(NOT CMAKE_VERSION VERSION_LESS 3.7.0)
@@ -63,6 +66,7 @@ option(
6366
"Enable exception handling in the loader. Leave this on unless your standard library is built to not throw."
6467
ON
6568
)
69+
6670
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
6771
option(DYNAMIC_LOADER "Build the loader as a .dll library" OFF)
6872
else()
@@ -82,6 +86,8 @@ include(CMakeDependentOption)
8286
cmake_dependent_option(
8387
BUILD_WITH_SYSTEM_JSONCPP "Use system jsoncpp instead of vendored source" ON "JSONCPP_FOUND" OFF
8488
)
89+
cmake_dependent_option(BUILD_WITH_STD_FILESYSTEM "Use std::[experimental::]filesystem." ON
90+
"HAVE_FILESYSTEM_WITHOUT_LIB OR HAVE_FILESYSTEM_NEEDING_LIB" OFF)
8591

8692
# Several files use these compile-time OS switches
8793
if(WIN32)
@@ -91,9 +97,9 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
9197
endif()
9298

9399
# /EHsc (support for C++ exceptions) is default in most configurations but seems missing when building arm/arm64.
94-
IF(MSVC)
95-
SET(CMAKE_CXX_FLAGS "/EHsc")
96-
ENDIF(MSVC)
100+
if(MSVC)
101+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc")
102+
endif()
97103

98104
# This is a little helper library for setting up OpenGL
99105
if(OPENGL_FOUND AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/common/gfxwrapper_opengl.c")
@@ -195,8 +201,26 @@ function(compile_glsl run_target_name)
195201
endfunction()
196202

197203
if(WIN32)
198-
add_definitions(-DXR_USE_GRAPHICS_API_D3D11)
199-
add_definitions(-DXR_USE_GRAPHICS_API_D3D12)
204+
# This is too simple of detection to be worth separating into a find module
205+
find_library(D3D_DXGI_LIBRARY dxgi)
206+
find_library(D3D_COMPILER_LIBRARY d3dcompiler)
207+
find_path(D3D_D3D11_INCLUDE_DIR d3d11.h)
208+
find_library(D3D_D3D11_LIBRARY d3d11)
209+
if(D3D_D3D11_INCLUDE_DIR AND D3D_D3D11_LIBRARY AND D3D_DXGI_LIBRARY AND D3D_COMPILER_LIBRARY)
210+
set(D3D_D3D11_FOUND ON)
211+
set(D3D_D3D11_LIBRARIES ${D3D_D3D11_LIBRARY} ${D3D_DXGI_LIBRARY} ${D3D_COMPILER_LIBRARY})
212+
add_definitions(-DXR_USE_GRAPHICS_API_D3D11)
213+
endif()
214+
215+
find_path(D3D_D3D12_INCLUDE_DIR d3d12.h)
216+
find_library(D3D_D3D12_LIBRARY d3d12)
217+
if(D3D_D3D12_INCLUDE_DIR AND D3D_D3D12_LIBRARY AND D3D_DXGI_LIBRARY)
218+
set(D3D_D3D12_FOUND ON)
219+
set(D3D_D3D12_LIBRARIES ${D3D_D3D12_LIBRARY} ${D3D_DXGI_LIBRARY} ${D3D_COMPILER_LIBRARY})
220+
add_definitions(-DXR_USE_GRAPHICS_API_D3D12)
221+
endif()
222+
223+
find_path(D3D_DIRECTXCOLORS_INCLUDE_DIR DirectXColors.h)
200224
endif()
201225

202226
# Check for the existence of the secure_getenv or __secure_getenv commands
@@ -217,7 +241,7 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/version.cmake)
217241

218242
# Path separators ( : or ; ) are not handled well in CMake.
219243
# This seems like a reasonable approach.
220-
if(WIN32)
244+
if(CMAKE_HOST_SYSTEM_NAME STREQUAL Windows)
221245
set(CODEGEN_PYTHON_PATH
222246
"${PROJECT_SOURCE_DIR}/specification/scripts;${PROJECT_SOURCE_DIR}/src/scripts;$ENV{PYTHONPATH}"
223247
)
@@ -296,12 +320,17 @@ set_target_properties(xr_global_generated_files PROPERTIES FOLDER ${CODEGEN_FOLD
296320
set(COMMON_GENERATED_OUTPUT ${GENERATED_OUTPUT})
297321
if(NOT MSVC)
298322
include(CheckCXXCompilerFlag)
299-
foreach(FLAG -Wall -Werror=unused-parameter -Werror=unused-argument)
323+
include(CheckCCompilerFlag)
324+
foreach(FLAG -Wall -Werror=unused-parameter -Werror=unused-argument -Wpointer-arith)
300325
string(REGEX REPLACE "[^A-Za-z0-9]" "" _flagvar "${FLAG}")
301326
check_cxx_compiler_flag(${FLAG} SUPPORTS_${_flagvar})
302-
if(SUPPORTS_WARNING_${_flagvar})
327+
if(SUPPORTS_${_flagvar})
303328
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLAG}")
304329
endif()
330+
check_c_compiler_flag(${FLAG} SUPPORTS_C_${_flagvar})
331+
if(SUPPORTS_C_${_flagvar})
332+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FLAG}")
333+
endif()
305334
endforeach()
306335
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined")
307336
endif()

src/api_layers/CMakeLists.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ add_library(XrApiLayer_api_dump SHARED
6767
)
6868
set_target_properties(XrApiLayer_api_dump PROPERTIES FOLDER ${API_LAYERS_FOLDER})
6969

70-
target_link_libraries(XrApiLayer_api_dump PRIVATE openxr-all-supported)
70+
target_link_libraries(XrApiLayer_api_dump PRIVATE openxr-all-supported ${CMAKE_THREAD_LIBS_INIT})
7171
add_dependencies(XrApiLayer_api_dump
7272
generate_openxr_header
7373
xr_global_generated_files
@@ -129,7 +129,7 @@ add_library(XrApiLayer_core_validation SHARED
129129
)
130130
set_target_properties(XrApiLayer_core_validation PROPERTIES FOLDER ${API_LAYERS_FOLDER})
131131

132-
target_link_libraries(XrApiLayer_core_validation PRIVATE openxr-all-supported)
132+
target_link_libraries(XrApiLayer_core_validation PRIVATE openxr-all-supported ${CMAKE_THREAD_LIBS_INIT})
133133
add_dependencies(XrApiLayer_core_validation
134134
generate_openxr_header
135135
xr_global_generated_files
@@ -186,20 +186,16 @@ if(WIN32)
186186
set_target_properties(copy-core_validation-def-file PROPERTIES FOLDER ${HELPER_FOLDER})
187187
elseif(APPLE)
188188
# Apple api_dump-specific information
189-
target_compile_options(XrApiLayer_api_dump PRIVATE -Wpointer-arith -Wno-unused-function -Wno-sign-compare)
190189
set_target_properties(XrApiLayer_api_dump PROPERTIES LINK_FLAGS "-Wl")
191190

192191
# Apple core_validation-specific information
193-
target_compile_options(XrApiLayer_core_validation PRIVATE -Wpointer-arith -Wno-unused-function -Wno-sign-compare)
194192
set_target_properties(XrApiLayer_core_validation PROPERTIES LINK_FLAGS "-Wl")
195193

196194
else()
197195
# Linux api_dump-specific information
198-
target_compile_options(XrApiLayer_api_dump PRIVATE -Wpointer-arith -Wno-unused-function -Wno-sign-compare)
199196
set_target_properties(XrApiLayer_api_dump PROPERTIES LINK_FLAGS "-Wl,-Bsymbolic,--exclude-libs,ALL")
200197

201198
# Linux core_validation-specific information
202-
target_compile_options(XrApiLayer_core_validation PRIVATE -Wpointer-arith -Wno-unused-function -Wno-sign-compare)
203199
set_target_properties(XrApiLayer_core_validation PROPERTIES LINK_FLAGS "-Wl,-Bsymbolic,--exclude-libs,ALL")
204200
endif()
205201

src/cmake/StdFilesystemFlags.cmake

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# Copyright 2020, Collabora, Ltd.
2+
#
3+
# SPDX-License-Identifier: BSL-1.0
4+
5+
if(MSVC AND MSVC_VERSION GREATER 1890)
6+
set(HAVE_FILESYSTEM_WITHOUT_LIB
7+
ON
8+
CACHE INTERNAL "" FORCE)
9+
if(MSVC_VERSION GREATER 1910)
10+
# Visual Studio 2017 Update 3 added new filesystem impl,
11+
# which only works in C++17 mode.
12+
set(HAVE_FILESYSTEM_NEEDS_17
13+
ON
14+
CACHE INTERNAL "" FORCE)
15+
endif()
16+
else()
17+
include(CheckCXXSourceCompiles)
18+
19+
# This is just dummy code that is known to not compile if std::filesystem isn't working right
20+
set(_stdfs_test_source
21+
"int main() {
22+
(void)is_regular_file(\"/\");
23+
return 0;
24+
}
25+
"
26+
)
27+
set(_stdfs_conditions
28+
"// If the C++ macro is set to the version containing C++17, it must support
29+
// the final C++17 package
30+
#if __cplusplus >= 201703L
31+
#define USE_FINAL_FS 1
32+
33+
#elif defined(_MSC_VER) && _MSC_VER >= 1900
34+
35+
#if defined(_HAS_CXX17) && _HAS_CXX17
36+
// When MSC supports c++17 use <filesystem> package.
37+
#define USE_FINAL_FS 1
38+
#endif // !_HAS_CXX17
39+
40+
// Right now, GCC still only supports the experimental filesystem items starting in GCC 6
41+
#elif (__GNUC__ >= 6)
42+
#define USE_EXPERIMENTAL_FS 1
43+
44+
// If Clang, check for feature support
45+
#elif defined(__clang__) && (__cpp_lib_filesystem || __cpp_lib_experimental_filesystem)
46+
#if __cpp_lib_filesystem
47+
#define USE_FINAL_FS 1
48+
#else
49+
#define USE_EXPERIMENTAL_FS 1
50+
#endif
51+
52+
#endif
53+
")
54+
set(_stdfs_source
55+
"${_stdfs_conditions}
56+
#ifdef USE_FINAL_FS
57+
#include <filesystem>
58+
using namespace std::filesystem;
59+
#endif
60+
${_stdfs_test_source}
61+
"
62+
)
63+
set(_stdfs_experimental_source
64+
"${_stdfs_conditions}
65+
#ifdef USE_EXPERIMENTAL_FS
66+
#include <experimental/filesystem>
67+
using namespace std::experimental::filesystem;
68+
#endif
69+
${_stdfs_test_source}
70+
"
71+
)
72+
set(_stdfs_needlib_source
73+
"${_stdfs_conditions}
74+
#ifdef USE_FINAL_FS
75+
#include <filesystem>
76+
using namespace std::filesystem;
77+
#endif
78+
#ifdef USE_EXPERIMENTAL_FS
79+
#include <experimental/filesystem>
80+
using namespace std::experimental::filesystem;
81+
#endif
82+
${_stdfs_test_source}
83+
"
84+
)
85+
86+
# First, just look for the include.
87+
# We're checking if it compiles, not if the include exists,
88+
# because the source code uses similar conditionals to decide.
89+
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
90+
unset(CMAKE_REQUIRED_LIBRARIES)
91+
unset(CMAKE_REQUIRED_FLAGS)
92+
check_cxx_source_compiles("${_stdfs_source}" HAVE_FILESYSTEM_IN_STD)
93+
check_cxx_source_compiles("${_stdfs_experimental_source}" HAVE_FILESYSTEM_IN_STDEXPERIMENTAL)
94+
95+
set(CMAKE_REQUIRED_FLAGS "-DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_STANDARD_REQUIRED=TRUE")
96+
check_cxx_source_compiles("${_stdfs_source}" HAVE_FILESYSTEM_IN_STD_17)
97+
unset(CMAKE_REQUIRED_FLAGS)
98+
99+
if(HAVE_FILESYSTEM_IN_STD_17 AND NOT HAVE_FILESYSTEM_IN_STD)
100+
set(HAVE_FILESYSTEM_NEEDS_17
101+
ON
102+
CACHE INTERNAL ""
103+
)
104+
set(CMAKE_REQUIRED_FLAGS "-DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_STANDARD_REQUIRED=TRUE")
105+
else()
106+
set(HAVE_FILESYSTEM_NEEDS_17
107+
OFF
108+
CACHE INTERNAL ""
109+
)
110+
endif()
111+
112+
# Now, see if we need libstdc++fs
113+
set(CMAKE_TRY_COMPILE_TARGET_TYPE EXECUTABLE)
114+
check_cxx_source_compiles("${_stdfs_needlib_source}" HAVE_FILESYSTEM_WITHOUT_LIB)
115+
set(CMAKE_REQUIRED_LIBRARIES stdc++fs)
116+
check_cxx_source_compiles("${_stdfs_needlib_source}" HAVE_FILESYSTEM_NEEDING_LIB)
117+
unset(CMAKE_REQUIRED_LIBRARIES)
118+
unset(CMAKE_TRY_COMPILE_TARGET_TYPE)
119+
120+
endif()
121+
122+
function(openxr_add_filesystem_utils TARGET_NAME)
123+
target_sources(${TARGET_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/src/common/filesystem_utils.cpp)
124+
target_include_directories(${TARGET_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/src/common)
125+
if(NOT BUILD_WITH_STD_FILESYSTEM)
126+
target_compile_definitions(${TARGET_NAME} PRIVATE DISABLE_STD_FILESYSTEM)
127+
else()
128+
if(HAVE_FILESYSTEM_NEEDS_17)
129+
set_property(TARGET ${TARGET_NAME} PROPERTY CXX_STANDARD 17)
130+
set_property(TARGET ${TARGET_NAME} PROPERTY CXX_STANDARD_REQUIRED TRUE)
131+
endif()
132+
if(HAVE_FILESYSTEM_NEEDING_LIB AND NOT HAVE_FILESYSTEM_WITHOUT_LIB)
133+
target_link_libraries(${TARGET_NAME} PRIVATE stdc++fs)
134+
endif()
135+
endif()
136+
endfunction()

src/common/filesystem_utils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "platform_utils.hpp"
2626

2727
#include <cstring>
28+
#include <string>
2829

2930
#if defined DISABLE_STD_FILESYSTEM
3031
#define USE_EXPERIMENTAL_FS 0

src/common/gfxwrapper_opengl.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3190,6 +3190,10 @@ ksGpuWindowEvent ksGpuWindow_ProcessEvents(ksGpuWindow *window) {
31903190

31913191
#elif defined(OS_LINUX_WAYLAND)
31923192

3193+
#ifdef __GNUC__
3194+
#pragma GCC diagnostic push
3195+
#pragma GCC diagnostic ignored "-Wunused-parameter"
3196+
#endif
31933197
static void _keyboard_keymap_cb(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) { close(fd); }
31943198
static void _keyboard_modifiers_cb(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed,
31953199
uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {}
@@ -3337,11 +3341,16 @@ static void _registry_cb(void *data, struct wl_registry *registry, uint32_t id,
33373341

33383342
static void _registry_remove_cb(void *data, struct wl_registry *registry, uint32_t id) {}
33393343

3344+
#ifdef __GNUC__
3345+
#pragma GCC diagnostic pop
3346+
#endif
3347+
33403348
const struct wl_registry_listener registry_listener = {_registry_cb, _registry_remove_cb};
33413349

33423350
bool ksGpuWindow_Create(ksGpuWindow *window, ksDriverInstance *instance, const ksGpuQueueInfo *queueInfo, const int queueIndex,
33433351
const ksGpuSurfaceColorFormat colorFormat, const ksGpuSurfaceDepthFormat depthFormat,
33443352
const ksGpuSampleCount sampleCount, const int width, const int height, const bool fullscreen) {
3353+
(void)queueIndex;
33453354
memset(window, 0, sizeof(ksGpuWindow));
33463355

33473356
window->display = NULL;

0 commit comments

Comments
 (0)