Skip to content

Commit 2663e01

Browse files
authored
Fix openjk cross-compilation (#15806)
Pre-build `compact_glsl` with the host compiler and tell CMake to import it instead of building one for the target, and let `compact_glsl` compile without an OpenGL development package on the build host by providing stubs for the GL types it references.
1 parent 9717aa6 commit 2663e01

2 files changed

Lines changed: 113 additions & 21 deletions

File tree

Lines changed: 93 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,100 @@
1-
diff --git a/codemp/rd-rend2/CMakeLists.txt b/codemp/rd-rend2/CMakeLists.txt
2-
index 786cf25..44ecef9 100644
1+
From: Batocera build system
2+
Subject: [PATCH] Allow cross-compiling: import a pre-built compact_glsl and
3+
keep its sources compilable without an OpenGL development package
4+
5+
When cross-compiling, the rd-rend2 GLSL "compact_glsl" code-generator must run
6+
on the build host (not the target). Two source-level changes let the
7+
build system pre-compile compact_glsl with the host toolchain and hand the
8+
resulting binary to the CMake cross build:
9+
10+
* codemp/rd-rend2/CMakeLists.txt:
11+
- If -Dcompact_glsl_EXECUTABLE=<path> is passed, declare compact_glsl
12+
as an IMPORTED GLOBAL executable target pointing at that path
13+
instead of compiling it for the target architecture.
14+
- The custom-command that invokes compact_glsl uses the bare target
15+
name (resolved by CMake to either the in-tree binary or the imported
16+
location) so the in-build relative path is no longer hard-coded;
17+
this is the same branch used on Win32 / Apple.
18+
19+
* codemp/rd-rend2/qgl.h:
20+
- Wraps the entire body of the header in #if !defined(GLSL_BUILDTOOL).
21+
Under GLSL_BUILDTOOL we provide stubs for the seven GL types that
22+
tr_local.h actually references (GLuint, GLenum, GLint, GLsizei,
23+
GLfloat, GLboolean, GLsync) and the GL_UNSIGNED_INT macro, then
24+
short-circuit before the qgl* function-pointer wrappers and the PFN*
25+
extern declarations. This lets compact_glsl be compiled directly by
26+
the build system's HOSTCXX without an OpenGL development package on
27+
the build machine.
28+
329
--- a/codemp/rd-rend2/CMakeLists.txt
430
+++ b/codemp/rd-rend2/CMakeLists.txt
5-
@@ -193,7 +193,7 @@ if (NOT WIN32 AND NOT APPLE)
6-
target_compile_definitions(compact_glsl PRIVATE "ARCH_STRING=\"${Architecture}\"")
31+
@@ -182,18 +182,25 @@
32+
target_link_libraries(${MPRend2} ${MPRend2Libraries})
33+
target_include_directories(${MPRend2} PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
34+
35+
-# GLSL shader file generator
36+
-add_executable(compact_glsl
37+
- ${MPDir}/rd-rend2/glsl/compact.cpp
38+
- ${MPDir}/rd-rend2/tr_allocator.cpp
39+
- ${MPDir}/rd-rend2/tr_allocator.h
40+
- ${MPDir}/rd-rend2/tr_glsl_parse.cpp)
41+
-target_compile_definitions(compact_glsl PRIVATE "GLSL_BUILDTOOL" "NOMINMAX")
42+
-if (NOT WIN32 AND NOT APPLE)
43+
- target_compile_definitions(compact_glsl PRIVATE "ARCH_STRING=\"${Architecture}\"")
44+
+# GLSL shader file generator. When cross-compiling, the build system can
45+
+# supply a pre-built host binary via -Dcompact_glsl_EXECUTABLE=<path>, in
46+
+# which case we import it instead of building one for the target.
47+
+if(compact_glsl_EXECUTABLE)
48+
+ add_executable(compact_glsl IMPORTED GLOBAL)
49+
+ set_target_properties(compact_glsl PROPERTIES IMPORTED_LOCATION "${compact_glsl_EXECUTABLE}")
50+
+else()
51+
+ add_executable(compact_glsl
52+
+ ${MPDir}/rd-rend2/glsl/compact.cpp
53+
+ ${MPDir}/rd-rend2/tr_allocator.cpp
54+
+ ${MPDir}/rd-rend2/tr_allocator.h
55+
+ ${MPDir}/rd-rend2/tr_glsl_parse.cpp)
56+
+ target_compile_definitions(compact_glsl PRIVATE "GLSL_BUILDTOOL" "NOMINMAX")
57+
+ if (NOT WIN32 AND NOT APPLE)
58+
+ target_compile_definitions(compact_glsl PRIVATE "ARCH_STRING=\"${Architecture}\"")
59+
+ endif()
60+
+ target_include_directories(compact_glsl PRIVATE "${MPRend2IncludeDirectories}")
761
endif()
8-
target_include_directories(compact_glsl PRIVATE "${MPRend2IncludeDirectories}")
62+
-target_include_directories(compact_glsl PRIVATE "${MPRend2IncludeDirectories}")
963
-if (WIN32 OR APPLE)
10-
+if (WIN32 OR APPLE OR CMAKE_CROSSCOMPILING)
64+
+if (WIN32 OR APPLE OR compact_glsl_EXECUTABLE)
1165
add_custom_command(
1266
OUTPUT
1367
${CMAKE_CURRENT_BINARY_DIR}/glsl_shaders.cpp
68+
--- a/codemp/rd-rend2/qgl.h
69+
+++ b/codemp/rd-rend2/qgl.h
70+
@@ -1,5 +1,24 @@
71+
#pragma once
72+
73+
+#if defined(GLSL_BUILDTOOL)
74+
+// The compact_glsl code-generator only includes this header transitively via
75+
+// tr_local.h; it never calls OpenGL. Provide the minimal subset of GL types
76+
+// and macros referenced by tr_local.h so the tool can be compiled on a host
77+
+// that has no OpenGL development files installed, and stop here -- the rest
78+
+// of qgl.h (qgl* function-pointer wrappers and PFN externs) needs the full
79+
+// glext.h definitions and is unused by the tool.
80+
+typedef unsigned int GLenum;
81+
+typedef unsigned char GLboolean;
82+
+typedef unsigned int GLuint;
83+
+typedef int GLint;
84+
+typedef int GLsizei;
85+
+typedef float GLfloat;
86+
+typedef struct __GLsync *GLsync;
87+
+#ifndef GL_UNSIGNED_INT
88+
+#define GL_UNSIGNED_INT 0x1405
89+
+#endif
90+
+#else // GLSL_BUILDTOOL
91+
+
92+
#if defined( __LINT__ )
93+
# include <GL/gl.h>
94+
#elif defined( _WIN32 )
95+
@@ -614,3 +633,5 @@
96+
extern PFNGLQUERYCOUNTERPROC qglQueryCounter;
97+
extern PFNGLGETQUERYOBJECTI64VPROC qglGetQueryObjecti64v;
98+
extern PFNGLGETQUERYOBJECTUI64VPROC qglGetQueryObjectui64v;
99+
+
100+
+#endif // GLSL_BUILDTOOL

package/batocera/ports/openjk/openjk.mk

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ OPENJK_LICENSE = GPL-2.0 license
2525
OPENJK_LICENSE_FILE = LICENSE.txt
2626
OPENJK_EMULATOR_INFO = openjk.emulator.yml
2727

28-
OPENJK_DEPENDENCIES += host-openjk libjpeg-bato libpng sdl2 zlib
28+
OPENJK_DEPENDENCIES += libjpeg-bato libpng sdl2 zlib
2929

3030
OPENJK_CONF_OPTS += -DCMAKE_BUILD_TYPE=Release
3131
OPENJK_CONF_OPTS += -DBUILD_SHARED_LIBS=OFF
@@ -35,30 +35,35 @@ OPENJK_CONF_OPTS += -DBuildJK2SPEngine=ON
3535
OPENJK_CONF_OPTS += -DBuildJK2SPGame=ON
3636
OPENJK_CONF_OPTS += -DBuildJK2SPRdVanilla=ON
3737

38-
HOST_OPENJK_DEPENDENCIES = mesa3d
38+
# compact_glsl is a build-time code generator (it embeds .glsl shader files
39+
# into a C++ source file). When cross-compiling, the binary built with the
40+
# target toolchain cannot be executed on the build host, so pre-build it
41+
# with the host compiler and tell CMake to import it via the cache variable
42+
# added in 001-cross-compile.patch.
43+
OPENJK_COMPACT_GLSL_BIN = $(@D)/host-compact_glsl
44+
OPENJK_COMPACT_GLSL_SRCS = \
45+
$(@D)/codemp/rd-rend2/glsl/compact.cpp \
46+
$(@D)/codemp/rd-rend2/tr_allocator.cpp \
47+
$(@D)/codemp/rd-rend2/tr_glsl_parse.cpp
3948

40-
HOST_OPENJK_CONF_OPTS += -DCMAKE_BUILD_TYPE=Release
41-
HOST_OPENJK_CONF_OPTS += -DCMAKE_FIND_ROOT_PATH="$(HOST_DIR);$(STAGING_DIR)"
42-
HOST_OPENJK_CONF_OPTS += -DBUILD_SHARED_LIBS=OFF
43-
HOST_OPENJK_CONF_OPTS += -DUseInternalSDL2=ON
44-
HOST_OPENJK_CONF_OPTS += -DUseInternalJPEG=ON
45-
HOST_OPENJK_CONF_OPTS += -DUseInternalPNG=ON
49+
define OPENJK_BUILD_HOST_COMPACT_GLSL
50+
$(HOSTCXX) -O2 -std=c++11 -DGLSL_BUILDTOOL -DNOMINMAX \
51+
-DARCH_STRING='"host"' \
52+
-I$(@D)/codemp -I$(@D)/codemp/rd-rend2 -I$(@D)/shared \
53+
-I$(@D)/lib/gsl-lite/include \
54+
-o $(OPENJK_COMPACT_GLSL_BIN) $(OPENJK_COMPACT_GLSL_SRCS)
55+
endef
56+
OPENJK_PRE_CONFIGURE_HOOKS += OPENJK_BUILD_HOST_COMPACT_GLSL
4657

47-
HOST_OPENJK_BUILD_OPTS += --target compact_glsl
58+
OPENJK_CONF_OPTS += -Dcompact_glsl_EXECUTABLE=$(BUILD_DIR)/openjk-$(OPENJK_VERSION)/host-compact_glsl
4859

4960
define OPENJK_EVMAPY
5061
mkdir -p $(TARGET_DIR)/usr/share/evmapy
5162
cp $(BR2_EXTERNAL_BATOCERA_PATH)/package/batocera/ports/openjk/openjk.keys \
5263
$(TARGET_DIR)/usr/share/evmapy
5364
endef
5465

55-
define HOST_OPENJK_INSTALL_CMDS
56-
$(INSTALL) -D -m 0755 $(HOST_OPENJK_BUILDDIR)/compact_glsl \
57-
$(HOST_DIR)/usr/bin/compact_glsl
58-
endef
59-
6066
OPENJK_POST_INSTALL_TARGET_HOOKS += OPENJK_EVMAPY
6167

6268
$(eval $(cmake-package))
63-
$(eval $(host-cmake-package))
6469
$(eval $(emulator-info-package))

0 commit comments

Comments
 (0)