From 3b26c92c85cef078c3b6d0ec62b642022f7efb6f Mon Sep 17 00:00:00 2001 From: Martin Fouilleul Date: Tue, 21 Feb 2023 17:09:26 +0100 Subject: [PATCH] [osx build/linking] - Remove the need for client apps to explicitly link with all dependent libs and frameworks - Don't require redistributing libEGL/libGLES if not used, by linking them with '-weak-l' option - Change examples build scripts accordingly --- build.sh | 11 +++++++++-- examples/canvas/build.sh | 9 ++------- examples/perf_text/build.sh | 2 +- examples/triangleGLES/build.sh | 9 +++------ examples/triangleGLES/main.c | 4 ++-- examples/triangleMetal/build.sh | 9 ++------- todo.txt | 12 ++++-------- 7 files changed, 23 insertions(+), 33 deletions(-) diff --git a/build.sh b/build.sh index 4bc566c..c8f12c9 100755 --- a/build.sh +++ b/build.sh @@ -63,8 +63,15 @@ if [ $target = 'lib' ] ; then $CC $DEBUG_FLAGS -c -o $BINDIR/milepost_c.o $CFLAGS $FLAGS $INCLUDES $SRCDIR/milepost.c $CC $DEBUG_FLAGS -c -o $BINDIR/milepost_objc.o $FLAGS $INCLUDES $SRCDIR/milepost.m - # build the static library - libtool -static -o $BINDIR/libmilepost.a $BINDIR/milepost_c.o $BINDIR/milepost_objc.o + # build dynamic library + ld -dylib -o $BINDIR/libmilepost.dylib $BINDIR/milepost_c.o $BINDIR/milepost_objc.o -lc -framework Carbon -framework Cocoa -framework Metal -framework QuartzCore -L$BINDIR -weak-lEGL -weak-lGLESv2 + + # change dependent libs path to @rpath. + install_name_tool -change "./libEGL.dylib" '@rpath/libEGL.dylib' $BINDIR/libmilepost.dylib + install_name_tool -change "./libGLESv2.dylib" '@rpath/libGLESv2.dylib' $BINDIR/libmilepost.dylib + + # add executable path to rpath. Client executable can still add its own rpaths if needed, e.g. @executable_path/libs/ etc. + install_name_tool -add_rpath "@executable_path" $BINDIR/libmilepost.dylib else # additional targets diff --git a/examples/canvas/build.sh b/examples/canvas/build.sh index 96c8692..c5b5c50 100755 --- a/examples/canvas/build.sh +++ b/examples/canvas/build.sh @@ -5,12 +5,7 @@ RESDIR=../../resources SRCDIR=../../src INCLUDES="-I$SRCDIR -I$SRCDIR/util -I$SRCDIR/platform -I$SRCDIR/app" -LIBS="-L$BINDIR -lmilepost -framework Carbon -framework Cocoa -framework Metal -framework QuartzCore -L$BINDIR -lEGL -lGLESv2" -FLAGS="-mmacos-version-min=10.15.4 -DDEBUG -DLOG_COMPILE_DEBUG -Wl,-dead_strip" +LIBS="-L$BINDIR -lmilepost" +FLAGS="-mmacos-version-min=10.15.4 -DDEBUG -DLOG_COMPILE_DEBUG" clang -g $FLAGS $LIBS $INCLUDES -o $BINDIR/example_canvas main.c - -# change dynamic libraries install name -#TODO: this shouldn't be needed for apps using only metal backend... -install_name_tool -change "./libEGL.dylib" "@loader_path/libEGL.dylib" $BINDIR/example_canvas -install_name_tool -change "./libGLESv2.dylib" "@loader_path/libGLESv2.dylib" $BINDIR/example_canvas diff --git a/examples/perf_text/build.sh b/examples/perf_text/build.sh index 99833e0..b4d58dc 100755 --- a/examples/perf_text/build.sh +++ b/examples/perf_text/build.sh @@ -5,7 +5,7 @@ RESDIR=../../resources SRCDIR=../../src INCLUDES="-I$SRCDIR -I$SRCDIR/util -I$SRCDIR/platform -I$SRCDIR/app -I$SRCDIR/graphics" -LIBS="-L$BINDIR -lmilepost -framework Cocoa -framework Carbon -framework Metal -framework QuartzCore" +LIBS="-L$BINDIR -lmilepost" FLAGS="-O2 -mmacos-version-min=10.15.4" clang -g $FLAGS $LIBS $INCLUDES -o $BINDIR/perf_text main.c diff --git a/examples/triangleGLES/build.sh b/examples/triangleGLES/build.sh index b4eb7c8..2eb9124 100755 --- a/examples/triangleGLES/build.sh +++ b/examples/triangleGLES/build.sh @@ -3,13 +3,10 @@ BINDIR=../../bin RESDIR=../../resources SRCDIR=../../src +EXTDIR=../../ext -INCLUDES="-I$SRCDIR -I$SRCDIR/util -I$SRCDIR/platform -I$SRCDIR/app -I$SRCDIR/../ext/angle_headers" -LIBS="-L$BINDIR -lmilepost -framework Carbon -framework Cocoa -framework Metal -framework QuartzCore -lGLESv2 -lEGL" +INCLUDES="-I$SRCDIR -I$SRCDIR/util -I$SRCDIR/platform -I$SRCDIR/app -I$EXTDIR" +LIBS="-L$BINDIR -lmilepost" FLAGS="-mmacos-version-min=10.15.4 -DDEBUG -DLOG_COMPILE_DEBUG" clang -g $FLAGS $LIBS $INCLUDES -o $BINDIR/example_gles_triangle main.c - -# change dynamic libraries install name -install_name_tool -change "./libEGL.dylib" "@loader_path/libEGL.dylib" $BINDIR/example_gles_triangle -install_name_tool -change "./libGLESv2.dylib" "@loader_path/libGLESv2.dylib" $BINDIR/example_gles_triangle diff --git a/examples/triangleGLES/main.c b/examples/triangleGLES/main.c index 97c8bac..57833f6 100644 --- a/examples/triangleGLES/main.c +++ b/examples/triangleGLES/main.c @@ -12,8 +12,7 @@ #define _USE_MATH_DEFINES //NOTE: necessary for MSVC #include -#include - +#define MG_INCLUDE_GL_API 1 #include"milepost.h" #define LOG_SUBSYSTEM "Main" @@ -70,6 +69,7 @@ int main() //NOTE: create surface mg_surface surface = mg_surface_create_for_window(window, MG_BACKEND_GLES); + mg_surface_prepare(surface); //NOTE: init shader and gl state GLuint vao; diff --git a/examples/triangleMetal/build.sh b/examples/triangleMetal/build.sh index 69abc2f..4d25f90 100755 --- a/examples/triangleMetal/build.sh +++ b/examples/triangleMetal/build.sh @@ -5,16 +5,11 @@ RESDIR=../../resources SRCDIR=../../src INCLUDES="-I$SRCDIR -I$SRCDIR/util -I$SRCDIR/platform -I$SRCDIR/app" -LIBS="-L$BINDIR -lmilepost -framework Carbon -framework Cocoa -framework Metal -framework QuartzCore -L$BINDIR -lEGL -lGLESv2" -FLAGS="-mmacos-version-min=10.15.4 -DDEBUG -DLOG_COMPILE_DEBUG -Wl,-dead_strip" +LIBS="-L$BINDIR -lmilepost -framework Foundation -framework Metal" +FLAGS="-mmacos-version-min=10.15.4 -DDEBUG -DLOG_COMPILE_DEBUG" xcrun -sdk macosx metal -c -o shader.air shader.metal xcrun -sdk macosx metallib -o shader.metallib shader.air cp shader.metallib $BINDIR/triangle_shader.metallib clang -g $FLAGS $LIBS $INCLUDES -o $BINDIR/example_metal_triangle main.m - -# change dynamic libraries install name -#TODO: this shouldn't be needed for apps using only metal backend... -install_name_tool -change "./libEGL.dylib" "@loader_path/libEGL.dylib" $BINDIR/example_metal_triangle -install_name_tool -change "./libGLESv2.dylib" "@loader_path/libGLESv2.dylib" $BINDIR/example_metal_triangle diff --git a/todo.txt b/todo.txt index 441b7f3..f321d33 100644 --- a/todo.txt +++ b/todo.txt @@ -7,24 +7,20 @@ Overview [/] error on bad option macro combinations [/] write doc about backend option macros -[?] could have an internal, per-platform mp_layer struct that handles resizing/hiding etc... - -> avoid duplication of frame/hiding and duplication of egl code. - [/] EGL surfaces: See how we can isolate platform-specific stuff and just deal with one egl impl... - [ ] Check that we can make GLES and GL surfaces co-exist in the app -[ ] Allow controlling surface overlaying [/] Allow selecting version of GL/GLES context when creating surface - pass/set attributes when creating surface? [/] Automatic surface resizing -[!] Make linking with libEGL optional, even if EGL backend is compiled in milepost? - - use dead_strip_dylib on osx? +[.] Make linking with libEGL optional, even if EGL backend is compiled in milepost? - use /DELAYLOAD:lib on windows? +[.] Remove the need for client apps to link with all dependent libs explicitly [!] Bundle examples with their own resources?? (e.g. avoiding clashes in metal libs files in bin directory) -[!] Sort out gles contents scaling for high dpi on osx +[!] Allow controlling surface overlaying +[!] Sort out contents scaling for high dpi on osx [!] win32 surfaces: only register surface child window once? [!] Fix canvas shader precision issue on OSX [!] Fix canvas perf issue on OSX