mesa/src/glx
Antonio Ospite 222b85328e mesa: replace most occurrences of getenv() with os_get_option()
The standard way to query options in mesa is `os_get_option()` which
abstracts platform-specific mechanisms to get config variables.

However in quite a few places `getenv()` is still used and this may
preclude controlling some options on some systems.

For instance it is not generally possible to use `MESA_DEBUG` on
Android.

So replace most `getenv()` occurrences with  `os_get_option()` to
support configuration options more consistently across different
platforms.

Do the same with `secure_getenv()` replacing it with
`os_get_option_secure()`.

The bulk of the proposed changes are mechanically performed by the
following script:

-----------------------------------------------------------------------
  #!/bin/sh

  set -e

  replace() {

    # Don't replace in some files, for example where `os_get_option` is defined,
    # or in external files
    EXCLUDE_FILES_PATTERN='(src/util/os_misc.c|src/util/u_debug.h|src/gtest/include/gtest/internal/gtest-port.h)'

    # Don't replace some "system" variables
    EXCLUDE_VARS_PATTERN='("XDG|"DISPLAY|"HOME|"TMPDIR|"POSIXLY_CORRECT)'

    git grep "[=!( ]$1(" -- src/ | cut -d ':' -f 1 | sort | uniq | \
      grep -v -E "$EXCLUDE_FILES_PATTERN" | \
      while read -r file;
      do
        # Don't replace usages of XDG_* variables or HOME
        sed -E -e "/$EXCLUDE_VARS_PATTERN/!s/([=!\( ])$1\(/\1$2\(/g" -i "$file";
      done
  }

  # Add const to os_get_option results, to avoid warning about discarded qualifier:
  #   warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
  # but also errors in some cases:
  #   error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
  add_const_results() {
    git grep -l -P '(?<!const )char.*os_get_option' | \
      while read -r file;
      do
        sed -e '/^\s*const/! s/\(char.*os_get_option\)/const \1/g' -i "$file"
      done
  }

  replace 'secure_getenv' 'os_get_option_secure'

  replace 'getenv' 'os_get_option'

  add_const_results
-----------------------------------------------------------------------

After this, the `#include "util/os_misc.h"` is also added in files where
`os_get_option()` was not used before.

And since the replacements from the script above generated some new
`-Wdiscarded-qualifiers` warnings, those have been addressed as well,
generally by declaring `os_get_option()` results as `const char *` and
adjusting some function declarations.

Finally some replacements caused new errors like:

-----------------------------------------------------------------------
../src/gallium/auxiliary/gallivm/lp_bld_misc.cpp:127:31: error: no matching function for call to 'strtok'
  127 |          for (n = 0, option = strtok(env_llc_options, " "); option; n++, option = strtok(NULL, " ")) {
      |                               ^~~~~~
/android-ndk-r27c/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/string.h:124:17: note: candidate function not viable: 1st argument ('const char *') would lose const qualifier
  124 | char* _Nullable strtok(char* _Nullable __s, const char* _Nonnull __delimiter);
      |                 ^      ~~~~~~~~~~~~~~~~~~~
-----------------------------------------------------------------------

Those have been addressed too, copying the const string returned by
`os_get_option()` so that it could be modified.

In particular, the error above has been fixed  by copying the `const
char *env_llc_options` variable in
`src/gallium/auxiliary/gallivm/lp_bld_misc.cpp` to a `char *` which can
be tokenized using `strtok()`.

Reviewed-by: Eric Engestrom <eric@igalia.com>
Reviewed-by: Yonggang Luo <luoyonggang@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/38128>
2025-11-06 04:36:13 +00:00
..
apple mesa: replace most occurrences of getenv() with os_get_option() 2025-11-06 04:36:13 +00:00
windows glapi: rename exported symbols so as not to conflict with old libglapi 2025-01-23 00:49:05 +00:00
applegl_glx.c glx: Remove (almost) all usage of _X_HIDDEN / _X_INTERNAL 2025-02-20 15:47:23 +00:00
clientattrib.c
clientinfo.c glx: Fix segfault when Nvidia PRIME render offload is enabled, but not used 2025-09-20 08:43:00 +00:00
compsize.c
create_context.c glx: Remove (almost) all usage of _X_HIDDEN / _X_INTERNAL 2025-02-20 15:47:23 +00:00
dri3_glx.c dri: Get rid of __DRIbackgroundCallableExtension 2025-07-16 01:32:56 +00:00
dri3_priv.h mesa_interface: replace opaque __DRIscreen with struct dri_screen everywhere 2024-10-22 06:48:02 +00:00
dri_common.c egl,glx: allow OpenGL with old libx11, but disable glthread if it's unsafe 2025-08-21 02:05:26 +00:00
dri_common.h dri: Get rid of __DRIbackgroundCallableExtension 2025-07-16 01:32:56 +00:00
dri_common_query_renderer.c glx: Remove (almost) all usage of _X_HIDDEN / _X_INTERNAL 2025-02-20 15:47:23 +00:00
drisw_glx.c glx/kopper: don't call glFlush from swapbuffers 2025-08-22 00:42:28 +00:00
drisw_priv.h glx: inline __GLXDRIscreen onto base glx_screen 2024-08-19 17:43:59 +00:00
driwindows_glx.c mesa_interface: replace opaque __DRIscreen with struct dri_screen everywhere 2024-10-22 06:48:02 +00:00
eval.c
g_glxglvnddispatchfuncs.c
g_glxglvnddispatchindices.h
glvnd-symbols.txt glx: stop exporting EXT_texture_object functions from libGLX_mesa.so 2025-03-27 05:46:35 +00:00
glx_error.c
glx_error.h glx: Remove (almost) all usage of _X_HIDDEN / _X_INTERNAL 2025-02-20 15:47:23 +00:00
glx_pbuffer.c glx: Fix the GLX_EXT_swap_control_tear drawable attributes 2024-10-23 19:55:53 +00:00
glx_query.c
glxclient.h glx: clean up a litttle remains of DRI2 support. 2025-07-09 14:31:05 -07:00
glxcmds.c glx: Add some NULL pointer checks 2025-11-04 00:21:19 +00:00
glxconfig.c glx: Remove (almost) all usage of _X_HIDDEN / _X_INTERNAL 2025-02-20 15:47:23 +00:00
glxconfig.h
glxcurrent.c glx: Remove (almost) all usage of _X_HIDDEN / _X_INTERNAL 2025-02-20 15:47:23 +00:00
glxext.c mesa: replace most occurrences of getenv() with os_get_option() 2025-11-06 04:36:13 +00:00
glxextensions.c glapi: remove unused functions from dispatch tables 2025-03-03 21:06:06 +00:00
glxextensions.h glapi: remove unused functions from dispatch tables 2025-03-03 21:06:06 +00:00
glxglvnd.c
glxglvnd.h
glxglvnddispatchfuncs.h
glxhash.c glx: Remove (almost) all usage of _X_HIDDEN / _X_INTERNAL 2025-02-20 15:47:23 +00:00
glxhash.h
indirect_glx.c glx: Remove (almost) all usage of _X_HIDDEN / _X_INTERNAL 2025-02-20 15:47:23 +00:00
indirect_init.h
indirect_texture_compression.c
indirect_transpose_matrix.c
indirect_vertex_array.c glapi: remove unused functions from dispatch tables 2025-03-03 21:06:06 +00:00
indirect_vertex_array.h
indirect_vertex_array_priv.h
indirect_vertex_program.c
indirect_window_pos.c
libgl-symbols.txt glx: add a test to verify exported symbols 2025-03-27 05:46:34 +00:00
meson.build glx: provide glx.pc 2025-10-14 20:53:10 +00:00
packrender.h
packsingle.h
pixel.c
pixelstore.c
query_renderer.c glx: Remove (almost) all usage of _X_HIDDEN / _X_INTERNAL 2025-02-20 15:47:23 +00:00
render2.c glx: Fix typos 2025-02-15 17:43:44 +00:00
renderpix.c glx: don't generate indirect GLX dispatch for ARB_imaging functions 2025-03-27 05:46:34 +00:00
single2.c glx: stop exporting EXT_texture_object functions from libGLX_mesa.so 2025-03-27 05:46:35 +00:00
vertarr.c
xfont.c mesa: move src/mapi to src/mesa/glapi 2025-08-06 20:35:26 +00:00