mesa/src/imagination/vulkan
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
..
pds pvr: add view index support for vertex shaders 2025-09-30 12:15:46 +00:00
winsys pvr: support VK_EXT_map_memory_placed.memoryUnmapReserve 2025-10-14 17:21:59 +00:00
meson.build vulkan: Compute path to write into JSON manifests once, use it everywhere 2025-09-26 10:47:31 +00:00
pvr_blit.c pvr: kill off pvr_private.h 2025-10-02 05:34:09 +00:00
pvr_blit.h pvr: add support for VK_KHR_depth_stencil_resolve 2025-09-30 12:15:51 +00:00
pvr_bo.c pvr: support VK_EXT_map_memory_placed.memoryUnmapReserve 2025-10-14 17:21:59 +00:00
pvr_bo.h pvr: Remove PVR_WINSYS_BO_FLAG_ZERO_ON_ALLOC flag 2023-09-06 12:19:46 +00:00
pvr_border.c pvr: kill off pvr_private.h 2025-10-02 05:34:09 +00:00
pvr_border.h pvr: break out instance/device to separate header 2025-10-02 05:34:05 +00:00
pvr_buffer.h pvr: break out buffer to separate header 2025-10-02 05:34:06 +00:00
pvr_clear.c pvr: kill off pvr_private.h 2025-10-02 05:34:09 +00:00
pvr_clear.h pvr: drop PVRX macro 2024-11-29 15:14:11 +00:00
pvr_cmd_buffer.c treewide: use UTIL_DYNARRAY_INIT 2025-11-04 13:39:48 +00:00
pvr_cmd_buffer.h pvr: break out cmd-buffer to separate header 2025-10-02 05:34:07 +00:00
pvr_common.h pvr: break out descriptor sets to separate header 2025-10-02 05:34:08 +00:00
pvr_csb.c treewide: use UTIL_DYNARRAY_INIT 2025-11-04 13:39:48 +00:00
pvr_csb.h pvr: split out rogue hw-defs to separate folder 2025-10-06 11:02:04 +02:00
pvr_csb_enum_helpers.h pvr: include pvr_common.h instead of pvr_private.h 2025-09-26 08:15:59 +00:00
pvr_descriptor_set.c pvr: kill off pvr_private.h 2025-10-02 05:34:09 +00:00
pvr_descriptor_set.h pvr: break out descriptor sets to separate header 2025-10-02 05:34:08 +00:00
pvr_device.c mesa: replace most occurrences of getenv() with os_get_option() 2025-11-06 04:36:13 +00:00
pvr_device.h pvr: enable KHR_wayland_surface 2025-10-15 08:58:03 +00:00
pvr_dump_bo.c pvr: Return VkResult from winsys buffer_map operation 2023-05-18 15:56:59 +00:00
pvr_dump_bo.h pvr: debug: Add "cs" debug option to dump control stream on job submit 2022-11-25 16:42:55 +00:00
pvr_dump_csb.c pvr: kill off pvr_private.h 2025-10-02 05:34:09 +00:00
pvr_formats.c pvr: kill off pvr_private.h 2025-10-02 05:34:09 +00:00
pvr_formats.h pvr: Add support for custom border colors 2025-09-16 18:26:23 +00:00
pvr_framebuffer.h pvr: break out render-pass to separate headers 2025-10-02 05:34:06 +00:00
pvr_hw_pass.c pvr: kill off pvr_private.h 2025-10-02 05:34:09 +00:00
pvr_hw_pass.h pvr: break out render-pass to separate headers 2025-10-02 05:34:06 +00:00
pvr_image.c pvr: support VK_KHR_device_group 2025-10-14 17:22:00 +00:00
pvr_image.h pvr: break out image to separate header 2025-10-02 05:34:05 +00:00
pvr_job_common.c pvr: kill off pvr_private.h 2025-10-02 05:34:09 +00:00
pvr_job_common.h pvr: avoid including pvr_private.h from headers 2025-10-02 05:34:09 +00:00
pvr_job_compute.c pvr: kill off pvr_private.h 2025-10-02 05:34:09 +00:00
pvr_job_compute.h pvr: Use common queue submit implementation 2023-02-28 21:39:49 +00:00
pvr_job_context.c pvr: kill off pvr_private.h 2025-10-02 05:34:09 +00:00
pvr_job_context.h pvr: avoid including pvr_private.h from headers 2025-10-02 05:34:09 +00:00
pvr_job_render.c pvr: kill off pvr_private.h 2025-10-02 05:34:09 +00:00
pvr_job_render.h pvr: add initial driver support for VK_KHR_multiview 2025-09-30 12:15:45 +00:00
pvr_job_transfer.c pvr: kill off pvr_private.h 2025-10-02 05:34:09 +00:00
pvr_job_transfer.h pvr: Add support to process transfer and blit cmds 2023-04-19 11:01:05 +00:00
pvr_macros.h pvr: break out macros to separate header 2025-10-02 05:34:09 +00:00
pvr_pass.c pvr: kill off pvr_private.h 2025-10-02 05:34:09 +00:00
pvr_pass.h pvr: break out render-pass to separate headers 2025-10-02 05:34:06 +00:00
pvr_pipeline.c treewide: don't check before free 2025-10-15 23:01:33 +00:00
pvr_pipeline.h pvr: avoid including pvr_private.h from headers 2025-10-02 05:34:09 +00:00
pvr_query.c util/dynarray: infer type in append 2025-10-24 18:32:07 +00:00
pvr_query.h pvr: break out queries to separate header 2025-10-02 05:34:07 +00:00
pvr_query_compute.c pvr: kill off pvr_private.h 2025-10-02 05:34:09 +00:00
pvr_queue.c pvr: break out pipelines to separate header 2025-10-02 05:34:08 +00:00
pvr_queue.h pvr: break out queue to separate header 2025-10-02 05:34:04 +00:00
pvr_robustness.c pvr: kill off pvr_private.h 2025-10-02 05:34:09 +00:00
pvr_robustness.h pvr: Add robustness buffer support 2023-03-27 18:36:32 +00:00
pvr_spm.c pvr: kill off pvr_private.h 2025-10-02 05:34:09 +00:00
pvr_spm.h pvr: remove stale comment about pvr_pds_upload 2025-09-26 08:15:59 +00:00
pvr_tex_state.c pvr: kill off pvr_private.h 2025-10-02 05:34:09 +00:00
pvr_tex_state.h pvr: avoid including pvr_private.h from headers 2025-10-02 05:34:09 +00:00
pvr_transfer_frag_store.c pvr: kill off pvr_private.h 2025-10-02 05:34:09 +00:00
pvr_transfer_frag_store.h pvr: merge legacy uscgen code into pvr_usc 2025-09-22 14:52:05 +01:00
pvr_usc.c pvr: amend tile buffer size calculation for eot 2025-10-11 20:28:16 +01:00
pvr_usc.h pvr: avoid including pvr_private.h from headers 2025-10-02 05:34:09 +00:00
pvr_wsi.c pvr: kill off pvr_private.h 2025-10-02 05:34:09 +00:00
pvr_wsi.h pvr: break out wsi to separate header 2025-10-02 05:34:09 +00:00