diff options
-rw-r--r-- | common/log.c | 17 | ||||
-rw-r--r-- | include/log.h | 16 | ||||
-rw-r--r-- | meson.build | 40 |
3 files changed, 48 insertions, 25 deletions
diff --git a/common/log.c b/common/log.c index 669fd360..17648e44 100644 --- a/common/log.c +++ b/common/log.c | |||
@@ -93,20 +93,3 @@ void _sway_log(sway_log_importance_t verbosity, const char *fmt, ...) { | |||
93 | sway_log_stderr(verbosity, fmt, args); | 93 | sway_log_stderr(verbosity, fmt, args); |
94 | va_end(args); | 94 | va_end(args); |
95 | } | 95 | } |
96 | |||
97 | // strips the path prefix from filepath | ||
98 | // will try to strip SWAY_SRC_DIR as well as a relative src dir | ||
99 | // e.g. '/src/build/sway/util/log.c' and | ||
100 | // '../util/log.c' will both be stripped to | ||
101 | // 'util/log.c' | ||
102 | const char *_sway_strip_path(const char *filepath) { | ||
103 | static int srclen = sizeof(SWAY_SRC_DIR); | ||
104 | if (strstr(filepath, SWAY_SRC_DIR) == filepath) { | ||
105 | filepath += srclen; | ||
106 | } else if (*filepath == '.') { | ||
107 | while (*filepath == '.' || *filepath == '/') { | ||
108 | ++filepath; | ||
109 | } | ||
110 | } | ||
111 | return filepath; | ||
112 | } | ||
diff --git a/include/log.h b/include/log.h index adf431ab..41399a9f 100644 --- a/include/log.h +++ b/include/log.h | |||
@@ -33,22 +33,26 @@ void _sway_vlog(sway_log_importance_t verbosity, const char *format, va_list arg | |||
33 | void _sway_abort(const char *filename, ...) ATTRIB_PRINTF(1, 2); | 33 | void _sway_abort(const char *filename, ...) ATTRIB_PRINTF(1, 2); |
34 | bool _sway_assert(bool condition, const char* format, ...) ATTRIB_PRINTF(2, 3); | 34 | bool _sway_assert(bool condition, const char* format, ...) ATTRIB_PRINTF(2, 3); |
35 | 35 | ||
36 | // TODO: get meson to precompute this, for better reproducibility/less overhead | 36 | #ifdef SWAY_REL_SRC_DIR |
37 | const char *_sway_strip_path(const char *filepath); | 37 | // strip prefix from __FILE__, leaving the path relative to the project root |
38 | #define _SWAY_FILENAME ((const char *)__FILE__ + sizeof(SWAY_REL_SRC_DIR) - 1) | ||
39 | #else | ||
40 | #define _SWAY_FILENAME __FILE__ | ||
41 | #endif | ||
38 | 42 | ||
39 | #define sway_log(verb, fmt, ...) \ | 43 | #define sway_log(verb, fmt, ...) \ |
40 | _sway_log(verb, "[%s:%d] " fmt, _sway_strip_path(__FILE__), __LINE__, ##__VA_ARGS__) | 44 | _sway_log(verb, "[%s:%d] " fmt, _SWAY_FILENAME, __LINE__, ##__VA_ARGS__) |
41 | 45 | ||
42 | #define sway_vlog(verb, fmt, args) \ | 46 | #define sway_vlog(verb, fmt, args) \ |
43 | _sway_vlog(verb, "[%s:%d] " fmt, _sway_strip_path(__FILE__), __LINE__, args) | 47 | _sway_vlog(verb, "[%s:%d] " fmt, _SWAY_FILENAME, __LINE__, args) |
44 | 48 | ||
45 | #define sway_log_errno(verb, fmt, ...) \ | 49 | #define sway_log_errno(verb, fmt, ...) \ |
46 | sway_log(verb, fmt ": %s", ##__VA_ARGS__, strerror(errno)) | 50 | sway_log(verb, fmt ": %s", ##__VA_ARGS__, strerror(errno)) |
47 | 51 | ||
48 | #define sway_abort(FMT, ...) \ | 52 | #define sway_abort(FMT, ...) \ |
49 | _sway_abort("[%s:%d] " FMT, _sway_strip_path(__FILE__), __LINE__, ##__VA_ARGS__) | 53 | _sway_abort("[%s:%d] " FMT, _SWAY_FILENAME, __LINE__, ##__VA_ARGS__) |
50 | 54 | ||
51 | #define sway_assert(COND, FMT, ...) \ | 55 | #define sway_assert(COND, FMT, ...) \ |
52 | _sway_assert(COND, "[%s:%d] %s:" FMT, _sway_strip_path(__FILE__), __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__) | 56 | _sway_assert(COND, "[%s:%d] %s:" FMT, _SWAY_FILENAME, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__) |
53 | 57 | ||
54 | #endif | 58 | #endif |
diff --git a/meson.build b/meson.build index f657f874..eb5039ee 100644 --- a/meson.build +++ b/meson.build | |||
@@ -13,8 +13,6 @@ project( | |||
13 | 13 | ||
14 | add_project_arguments( | 14 | add_project_arguments( |
15 | [ | 15 | [ |
16 | '-DSWAY_SRC_DIR="@0@"'.format(meson.current_source_dir()), | ||
17 | |||
18 | '-DWL_HIDE_DEPRECATED', | 16 | '-DWL_HIDE_DEPRECATED', |
19 | '-DWLR_USE_UNSTABLE', | 17 | '-DWLR_USE_UNSTABLE', |
20 | 18 | ||
@@ -142,6 +140,44 @@ if git.found() | |||
142 | endif | 140 | endif |
143 | add_project_arguments('-DSWAY_VERSION=@0@'.format(version), language: 'c') | 141 | add_project_arguments('-DSWAY_VERSION=@0@'.format(version), language: 'c') |
144 | 142 | ||
143 | # Compute the relative path used by compiler invocations. | ||
144 | source_root = meson.current_source_dir().split('/') | ||
145 | build_root = meson.build_root().split('/') | ||
146 | relative_dir_parts = [] | ||
147 | i = 0 | ||
148 | in_prefix = true | ||
149 | foreach p : build_root | ||
150 | if i >= source_root.length() or not in_prefix or p != source_root[i] | ||
151 | in_prefix = false | ||
152 | relative_dir_parts += '..' | ||
153 | endif | ||
154 | i += 1 | ||
155 | endforeach | ||
156 | i = 0 | ||
157 | in_prefix = true | ||
158 | foreach p : source_root | ||
159 | if i >= build_root.length() or not in_prefix or build_root[i] != p | ||
160 | in_prefix = false | ||
161 | relative_dir_parts += p | ||
162 | endif | ||
163 | i += 1 | ||
164 | endforeach | ||
165 | relative_dir = join_paths(relative_dir_parts) + '/' | ||
166 | |||
167 | # Strip relative path prefixes from the code if possible, otherwise hide them. | ||
168 | if cc.has_argument('-fmacro-prefix-map=/prefix/to/hide=') | ||
169 | add_project_arguments( | ||
170 | '-fmacro-prefix-map=@0@='.format(relative_dir), | ||
171 | language: 'c', | ||
172 | ) | ||
173 | else | ||
174 | add_project_arguments( | ||
175 | '-DSWAY_REL_SRC_DIR="@0@"'.format(relative_dir), | ||
176 | language: 'c', | ||
177 | ) | ||
178 | endif | ||
179 | |||
180 | |||
145 | sway_inc = include_directories('include') | 181 | sway_inc = include_directories('include') |
146 | 182 | ||
147 | subdir('include') | 183 | subdir('include') |