File: cmake-flags

package info (click to toggle)
blender 3.4.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 280,208 kB
  • sloc: ansic: 1,213,366; cpp: 1,148,738; python: 468,812; xml: 13,577; sh: 5,969; javascript: 304; lisp: 247; makefile: 67
file content (428 lines) | stat: -rwxr-xr-x 14,769 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/usr/bin/env python3
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

"""
This tool is for configuring build flags.
"""

WITH_GUI = True

# ----------------------------------------------------------------------------
# Data (flags)
# eg: indervidual flags, compat info.


# ----------------------------------------------------------------------------
# Data (presets)
# eg: profiling, mudflap, debugging.

# setting: ((add, ...), (remove, ...)), ...
PRESETS = {
    "sanitize_address": {
        "CMAKE_CXX_FLAGS": (("-fsanitize=address",), ()),
        "CMAKE_C_FLAGS": (("-fsanitize=address",), ()),
        "CMAKE_EXE_LINKER_FLAGS": (("-lasan",), ()),
    },
    "sanitize_leak": {
        "CMAKE_CXX_FLAGS": (("-fsanitize=leak",), ()),
        "CMAKE_C_FLAGS": (("-fsanitize=leak",), ()),
    },
    "sanitize_undefined": {
        "CMAKE_CXX_FLAGS": (("-fsanitize=undefined",), ()),
        "CMAKE_C_FLAGS": (("-fsanitize=undefined",), ()),
    },
    "sanitize_thread": {
        "CMAKE_CXX_FLAGS": (("-fsanitize=thread",), ()),
        "CMAKE_C_FLAGS": (("-fsanitize=thread",), ()),
    },

    # GCC5
    "sanitize_float_divide_by_zero": {
        "CMAKE_CXX_FLAGS": (("-fsanitize=float-divide-by-zero",), ()),
        "CMAKE_C_FLAGS": (("-fsanitize=float-divide-by-zero",), ()),
    },
    "sanitize_float_cast_overflow": {
        "CMAKE_CXX_FLAGS": (("-fsanitize=float-cast-overflow",), ()),
        "CMAKE_C_FLAGS": (("-fsanitize=float-cast-overflow",), ()),
    },
    "sanitize_int_overflow": {
        "CMAKE_CXX_FLAGS": (("-fsanitize=signed-integer-overflow",), ()),
        "CMAKE_C_FLAGS": (("-fsanitize=signed-integer-overflow",), ()),
    },
    "sanitize_bool": {
        "CMAKE_CXX_FLAGS": (("-fsanitize=bool",), ()),
        "CMAKE_C_FLAGS": (("-fsanitize=bool",), ()),
    },
    "sanitize_enum": {
        "CMAKE_CXX_FLAGS": (("-fsanitize=enum",), ()),
        "CMAKE_C_FLAGS": (("-fsanitize=enum",), ()),
    },
    "sanitize_bounds": {
        "CMAKE_CXX_FLAGS": (("-fsanitize=bounds",), ()),
        "CMAKE_C_FLAGS": (("-fsanitize=bounds",), ()),
    },
    "sanitize_bounds_strict": {
        "CMAKE_CXX_FLAGS": (("-fsanitize=bounds-strict",), ()),
        "CMAKE_C_FLAGS": (("-fsanitize=bounds-strict",), ()),
    },
    "sanitize_vla_bound": {
        "CMAKE_CXX_FLAGS": (("-fsanitize=vla-bound",), ()),
        "CMAKE_C_FLAGS": (("-fsanitize=vla-bound",), ()),
    },
    "sanitize_alignment": {
        "CMAKE_CXX_FLAGS": (("-fsanitize=alignment",), ()),
        "CMAKE_C_FLAGS": (("-fsanitize=alignment",), ()),
    },
    "sanitize_object_size": {
        "CMAKE_CXX_FLAGS": (("-fsanitize=object-size",), ()),
        "CMAKE_C_FLAGS": (("-fsanitize=object-size",), ()),
    },
    "sanitize_nonull_attribute": {
        "CMAKE_CXX_FLAGS": (("-fsanitize=nonnull-attribute",), ()),
        "CMAKE_C_FLAGS": (("-fsanitize=nonnull-attribute",), ()),
    },
    "sanitize_returns_nonull_attribute": {
        "CMAKE_CXX_FLAGS": (("-fsanitize=returns-nonnull-attribute",), ()),
        "CMAKE_C_FLAGS": (("-fsanitize=returns-nonnull-attribute",), ()),
    },

    "warn_all": {
        "CMAKE_CXX_FLAGS": (("-Wall",), ()),
        "CMAKE_C_FLAGS": (("-Wall",), ()),
    },
    "warn_extra": {
        "CMAKE_CXX_FLAGS": (("-Wextra",), ()),
        "CMAKE_C_FLAGS": (("-Wextra",), ()),
    },
    "warn_unused_macros": {
        "CMAKE_CXX_FLAGS": (("-Wunused-macros",), ()),
        "CMAKE_C_FLAGS": (("-Wunused-macros",), ()),
    },
    "warn_undefined_macros": {
        "CMAKE_CXX_FLAGS": (("-Wundef",), ()),
        "CMAKE_C_FLAGS": (("-Wundef",), ()),
    },
    "warn_unused_local_typedefs": {
        "CMAKE_CXX_FLAGS": (("-Wunused-local-typedefs",), ()),
        "CMAKE_C_FLAGS": (("-Wunused-local-typedefs",), ()),
    },
    "warn_pointer_sign": {
        "CMAKE_CXX_FLAGS": (("",), ()),
        "CMAKE_C_FLAGS": (("-Wpointer-sign",), ()),
    },
    "warn_sizeof_pointer_memaccess": {
        "CMAKE_CXX_FLAGS": (("-Wsizeof-pointer-memaccess",), ()),
        "CMAKE_C_FLAGS": (("-Wsizeof-pointer-memaccess",), ()),
    },
    "warn_no_null": {
        "CMAKE_CXX_FLAGS": (("-Wnonnull",), ()),
        "CMAKE_C_FLAGS": (("-Wnonnull",), ()),
    },
    "warn_init_self": {
        "CMAKE_CXX_FLAGS": (("-Winit-self",), ()),
        "CMAKE_C_FLAGS": (("-Winit-self",), ()),
    },
    "warn_format": {
        "CMAKE_CXX_FLAGS": (("-Wformat=2", "-Wno-format-nonliteral", "-Wno-format-y2k"), ()),
        "CMAKE_C_FLAGS": (("-Wformat=2", "-Wno-format-nonliteral", "-Wno-format-y2k"), ()),
    },
    "warn_format": {
        "CMAKE_CXX_FLAGS": (("-Wwrite-strings",), ()),
        "CMAKE_C_FLAGS": (("-Wwrite-strings",), ()),
    },
    "warn_logical_op": {
        "CMAKE_CXX_FLAGS": (("-Wlogical-op",), ()),
        "CMAKE_C_FLAGS": (("-Wlogical-op",), ()),
    },
    "warn_error": {
        "CMAKE_CXX_FLAGS": (("-Werror",), ()),
        "CMAKE_C_FLAGS": (("-Werror",), ()),
    },
    "warn_shadow": {
        "CMAKE_CXX_FLAGS": (("-Wshadow", "-Wno-error=shadow"), ()),
        "CMAKE_C_FLAGS": (("-Wshadow", "-Wno-error=shadow"), ()),
    },
    "warn_missing_include_dirs": {
        "CMAKE_CXX_FLAGS": (("-Wmissing-include-dirs",), ()),
        "CMAKE_C_FLAGS": (("-Wmissing-include-dirs",), ()),
    },
    "warn_double_promotion": {
        "CMAKE_CXX_FLAGS": (("-Wdouble-promotion",), ()),
        "CMAKE_C_FLAGS": (("-Wdouble-promotion",), ()),
    },
    "warn_declaration_after_statement": {
        "CMAKE_C_FLAGS": (("-Wdeclaration-after-statement",), ()),
    },
    "warn_zero_as_null_pointer_constant": {
        "CMAKE_CXX_FLAGS": (("-Wzero-as-null-pointer-constant",), ()),
    },
    "show_color": {
        "CMAKE_C_FLAGS": (("-fdiagnostics-color=always",), ()),
        "CMAKE_CXX_FLAGS": (("-fdiagnostics-color=always",), ()),
    },

    # Optimize
    "optimize_whole_program": {
        "CMAKE_CXX_FLAGS": (("-flto",), ()),
        "CMAKE_C_FLAGS": (("-flto",), ()),
        "CMAKE_EXE_LINKER_FLAGS": (("-flto", "-fwhole-program",), ()),
    },

    # Profile
    "profile_gprof": {
        "CMAKE_CXX_FLAGS": (("-pg",), ()),
        "CMAKE_C_FLAGS": (("-pg",), ()),
        "CMAKE_EXE_LINKER_FLAGS": (("-pg",), ()),
    },
}

# ----------------------------------------------------------------------------
# Utility Functions
# eg: check buildsystem (make or ninja?)


def cmake_flag_buildtype_suffix(flag, build_type):
    """
    Add the build type as a suffix for options that support it.
    this way for Debug builds we edit debug flags.
    eg:
      CMAKE_CXX_FLAGS -> CMAKE_CXX_FLAGS_DEBUG
    """
    if build_type == "":
        return flag

    # perhaps there are more,
    # but these are default that can have _DEBUG... etc added.
    if flag in {'CMAKE_CXX_FLAGS',
                'CMAKE_C_FLAGS',
                'CMAKE_EXE_LINKER_FLAGS',
                'CMAKE_MODULE_LINKER_FLAGS',
                'CMAKE_SHARED_LINKER_FLAGS'}:

        return "%s_%s" % (flag, build_type.upper())
    else:
        return flag


# ----------------------------------------------------------------------------
# CMakeCache.txt Parser (simple)
#
# These functions should run standalone
# format in python is as follows...
#
# CMakeCache.txt is converted into a dict
# the key is the cache ID
# the value is a triple (type, value, description, internal)
# where the description is the comment above conforming to the CMake convention.
#
#
def cmakecache_to_py(filepath, native=True):
    """
    header, cache
    """

    cmake_header = ""
    cmake_cache = {}

    with open(filepath, 'r', encoding='utf-8') as f:
        lines = f.readlines()
        for i in range(len(lines)):
            if lines[i].startswith("#"):
                cmake_header += lines[i]
            else:
                break

        # in case it's not set
        cmake_descr = ""
        cmake_internal = False

        for i in range(len(lines)):
            if lines[i].startswith("//"):
                cmake_descr += lines[i][2:].rstrip() + "\n"
            elif lines[i].startswith("#"):
                if "INTERNAL cache entries" in lines[i]:
                    cmake_internal = True
            elif lines[i][0].isalpha() or lines[i][0] in {"_", "-"}:
                cmake_name, cmake_value = lines[i].split("=", 1)
                if ":" in cmake_name:
                    cmake_name, cmake_type = cmake_name.split(":", 1)
                else:
                    cmake_type = "STRING"
                cmake_value = cmake_value.rstrip()  # remove trailing '\n'
                cmake_descr = cmake_descr.rstrip()

                if native:
                    if cmake_type in {"STRING", "PATH", "FILEPATH", "STATIC", "INTERNAL", "UNINITIALIZED"}:
                        pass
                    elif cmake_type == "BOOL":
                        cmake_value = cmake_value.upper() not in {"NO", "N", "", "OFF", "0", "FALSE"}

                cmake_cache[cmake_name] = (cmake_type, cmake_value, cmake_descr, cmake_internal)

                # in case it's not set
                cmake_descr = ""

    return cmake_header, cmake_cache


def cmakecache_from_py(filepath, cmake_header, cmake_cache):

    with open(filepath, 'w', encoding='utf-8') as f:
        f.write(cmake_header)
        f.write("\n")

        cmake_cache_list = ([], [])

        # sort into external/internal
        for cmake_name, (cmake_type, cmake_value, cmake_descr, cmake_internal) in cmake_cache.items():
            cmake_cache_list[cmake_internal].append((cmake_name, cmake_type, cmake_value, cmake_descr))

        for is_internal, ls in enumerate(cmake_cache_list):
            ls.sort()
            if is_internal:
                f.write("########################\n"
                        "# INTERNAL cache entries\n"
                        "########################\n\n")
            else:
                f.write("########################\n"
                        "# EXTERNAL cache entries\n"
                        "########################\n\n")

            for cmake_name, cmake_type, cmake_value, cmake_descr in ls:
                if cmake_descr:
                    l = None
                    for l in cmake_descr.split("\n"):
                        f.write("//%s\n" % l)
                    del l

                # convert the value
                if cmake_value is True:
                    cmake_value = "TRUE"
                elif cmake_value is False:
                    cmake_value = "FALSE"

                f.write("%s:%s=%s\n" % (cmake_name, cmake_type, cmake_value))
                if not is_internal:
                    f.write("\n")

# cmake_header, cmake_cache = cmakecache_to_py("/src/cmake_debug/CMakeCache.txt~")
# cmakecache_from_py("/src/cmake_debug/CMakeCache.txt", cmake_header, cmake_cache)
# print(cmake_cache)


# ----------------------------------------------------------------------------
# Main Functions (can be run from command line)

def config_set(config_id, state):
    print(config_id, state.get(), dir(state))
    cache = CMAKE_DATA["cmake_cache"]
    build_type = cache["CMAKE_BUILD_TYPE"][1]  # value

    cfg = PRESETS[config_id]
    for key, (add, rem) in cfg.items():
        key = cmake_flag_buildtype_suffix(key, build_type)
        (cmake_type, cmake_value, cmake_descr, cmake_internal) = cache[key]
        data = cmake_value.split()
        if not state.get():
            add, rem = rem, add

        for arg in rem:
            data[:] = [i for i in data if i != arg]
        data.extend(add)

        # print("A", data)
        cmake_value = " ".join(data)
        # print("B", cmake_value)
        cache[key] = (cmake_type, cmake_value, cmake_descr, cmake_internal)
        print("AFTER", cache[key])


def config_check(config_id):
    cache = CMAKE_DATA["cmake_cache"]
    build_type = cache["CMAKE_BUILD_TYPE"][1]  # value

    cfg = PRESETS[config_id]
    for key, (add, rem) in cfg.items():
        key = cmake_flag_buildtype_suffix(key, build_type)
        (cmake_type, cmake_value, cmake_descr, cmake_internal) = cache[key]
        data = cmake_value.split()
        for arg in add:
            if arg not in data:
                return False
    return True


# ----------------------------------------------------------------------------
# Command Line Interface

# ----------------------------------------------------------------------------
# User Interface
if WITH_GUI:
    CMAKE_DATA = {}
    # CMAKE_CACHE = "/src/cmake_debugCMakeCache.txt"
    CMAKE_CACHE = "CMakeCache.txt"

    def cmake_read():
        print("%s: reading..." % CMAKE_CACHE)
        (CMAKE_DATA["cmake_header"],
         CMAKE_DATA["cmake_cache"],
         ) = cmakecache_to_py(CMAKE_CACHE)

    def cmake_write():
        print("%s: writing..." % CMAKE_CACHE)
        CMAKE_CACHE_TMP = CMAKE_CACHE + "~"
        cmakecache_from_py(CMAKE_CACHE_TMP,
                           CMAKE_DATA["cmake_header"],
                           CMAKE_DATA["cmake_cache"])

        import shutil
        shutil.move(CMAKE_CACHE_TMP, CMAKE_CACHE)

    # read before load.
    cmake_read()

    import tkinter
    master = tkinter.Tk()

    row = 0
    tkinter.Label(master, text="Flags:").grid(row=row, sticky=tkinter.W)
    row += 1

    def config_but(my_id):
        global row
        var = tkinter.IntVar()
        var.set(config_check(my_id))
        tkinter.Checkbutton(master,
                            text=my_id.replace("_", " ").capitalize(),
                            variable=var,
                            command=lambda: config_set(my_id, var)).grid(row=row, sticky=tkinter.W)
        row += 1

    for my_id in sorted(PRESETS.keys()):
        config_but(my_id)

    tkinter.Button(master, text='Write', command=cmake_write).grid(row=row, sticky=tkinter.W, pady=4)
    row += 1
    tkinter.Button(master, text='Quit', command=master.quit).grid(row=row, sticky=tkinter.W, pady=4)
    row += 1
    # tkinter.Button(master, text='Show', command=var_states).grid(row=4, sticky=tkinter.W, pady=4)

    tkinter.mainloop()

# EOF