mesa/src/egl/main
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
..
50_mesa.json egl/meson: allow passing a full path to the glvnd json file 2025-06-20 22:18:22 +00:00
egl.def.in
eglapi.c mesa: replace most occurrences of getenv() with os_get_option() 2025-11-06 04:36:13 +00:00
eglarray.c
eglarray.h
eglconfig.c build: avoid redefining unreachable() which is standard in C23 2025-07-31 17:49:42 +00:00
eglconfig.h egl: Implement EGL_EXT_config_select_group 2024-05-11 19:43:47 +00:00
eglconfigdebug.c egl: cast printf argument to the expected type 2025-07-04 15:26:39 +00:00
eglconfigdebug.h
eglcontext.c build: avoid redefining unreachable() which is standard in C23 2025-07-31 17:49:42 +00:00
eglcontext.h
eglcurrent.c
eglcurrent.h egl: refactor to use DETECT_OS_ANDROID instead of ANDROID 2024-02-01 19:29:49 +00:00
egldefines.h
egldevice.c egl: Fix invalid device UUID returned by EGL_EXT_device_persistent_id 2025-09-29 12:56:59 +00:00
egldevice.h egl: Add EGL_EXT_device_query_name and EGL_EXT_device_persistent_id 2025-06-20 15:35:44 +00:00
egldispatchstubs.c
egldispatchstubs.h
egldisplay.c mesa: replace most occurrences of getenv() with os_get_option() 2025-11-06 04:36:13 +00:00
egldisplay.h egl: Remove EGL_MESA_drm_image 2025-07-09 17:49:58 +00:00
egldriver.h egl/wayland: Move bind_wayland_display to legacy build option 2025-07-10 14:51:20 +00:00
eglentrypoint.h egl/wayland: Move bind_wayland_display to legacy build option 2025-07-10 14:51:20 +00:00
eglglobals.c egl: Don't maintain a list of AtExit functions 2025-09-24 21:16:02 +00:00
eglglobals.h egl: Don't maintain a list of AtExit functions 2025-09-24 21:16:02 +00:00
eglglvnd.c
eglimage.c egl/wayland: Move bind_wayland_display to legacy build option 2025-07-10 14:51:20 +00:00
eglimage.h egl: Remove EGL_MESA_drm_image 2025-07-09 17:49:58 +00:00
egllog.c egl: use os_get_option() to allow android to set EGL_LOG_LEVEL 2024-06-21 07:44:36 +00:00
egllog.h
eglsurface.c build: avoid redefining unreachable() which is standard in C23 2025-07-31 17:49:42 +00:00
eglsurface.h egl: Retire NV_post_sub_buffer support. 2025-02-21 02:50:56 +00:00
eglsync.c
eglsync.h
egltypedefs.h