From 0b8a4f9cb94f2929ba162726abde48fcfe1c5ea3 Mon Sep 17 00:00:00 2001 From: Seunghun Lee Date: Tue, 13 Jun 2023 08:57:20 +0900 Subject: [PATCH] Use -fmacro-prefix-map to strip build path This is taken from wlroots to strip the build-path dependent bytes of each __FILE__ string used by ds_log and related functions. Change-Id: I5660355f210615024bd4018cf63b6da216a128e8 --- include/libds/log.h | 10 ++++++++-- meson.build | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/include/libds/log.h b/include/libds/log.h index e588968..7399a0a 100644 --- a/include/libds/log.h +++ b/include/libds/log.h @@ -31,15 +31,21 @@ enum ds_log_level ds_log_get_level(void); #define _DS_ATTRIB_PRINTF(start, end) #endif +#ifdef _DS_REL_SRC_DIR +#define _DS_FILENAME ((const char *)__FILE__ + sizeof(_DS_REL_SRC_DIR) - 1) +#else +#define _DS_FILENAME __FILE__ +#endif + void _ds_log(enum ds_log_level level, const char *format, ...) _DS_ATTRIB_PRINTF(2, 3); void _ds_vlog(enum ds_log_level level, const char *format, va_list args) _DS_ATTRIB_PRINTF(2, 0); void ds_log_errno(enum ds_log_level level, const char *fmt, ...); #define ds_log(level, fmt, ...) \ - _ds_log(level, "[%s:%d] " fmt, __FILE__, __LINE__, ##__VA_ARGS__) + _ds_log(level, "[%s:%d] " fmt, _DS_FILENAME, __LINE__, ##__VA_ARGS__) #define ds_vlog(level, fmt, args) \ - _ds_vlog(level, "[%s:%d] " fmt, __FILE__, __LINE__, args) + _ds_vlog(level, "[%s:%d] " fmt, _DS_FILENAME, __LINE__, args) #define ds_err(fmt, ...) \ ds_log(DS_ERR, fmt, ##__VA_ARGS__) diff --git a/meson.build b/meson.build index a6f2d55..a9c9fd7 100644 --- a/meson.build +++ b/meson.build @@ -26,6 +26,42 @@ foreach a : global_args_maybe endforeach add_global_arguments(global_args, language: 'c') +# Compute the relative path used by compiler invocations. +source_root = meson.current_source_dir().split('/') +build_root = meson.global_build_root().split('/') +relative_dir_parts = [] +i = 0 +in_prefix = true +foreach p: build_root + if i >= source_root.length() or not in_prefix or p != source_root[i] + in_prefix = false + relative_dir_parts += '..' + endif + i += 1 +endforeach +i = 0 +in_prefix = true +foreach p : source_root + if i >= build_root.length() or not in_prefix or build_root[i] != p + in_prefix = false + relative_dir_parts += p + endif + i += 1 +endforeach +relative_dir = join_paths(relative_dir_parts) + '/' + +if cc.has_argument('-fmacro-prefix-map=/prefix/to/hide=') + add_project_arguments( + '-fmacro-prefix-map=@0@='.format(relative_dir), + language: 'c', + ) +else + add_project_arguments( + '-D_DS_REL_SRC_DIR="@0@"'.format(relative_dir), + language: 'c', + ) +endif + libds_version = meson.project_version() version_arr = libds_version.split('.') libds_version_major = version_arr[0] -- 2.7.4