@@ -24,12 +24,18 @@ use_rustdeps = get_option('use_rustdeps')
2424grovedb_cxx_build_dir = get_option (' grovedb_cxx_build_dir' )
2525rustdeps_build_dir = get_option (' rustdeps_build_dir' )
2626
27- # C++ compiler
2827cxx = meson .get_compiler(' cpp' )
28+
29+ # C++ compiler flags
2930cxx_core_flags_list = [
3031 ' -fno-extended-identifiers' ,
3132 ' -fstack-reuse=none' , # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90348
3233]
34+ cxx_harden_flags_list = [
35+ ' -fcf-protection=full' ,
36+ ' -fstack-protector-all' ,
37+ ' -Wstack-protector' ,
38+ ]
3339cxx_warn_flags_list = [
3440 ' -Wconditional-uninitialized' ,
3541 ' -Wdate-time' ,
@@ -52,8 +58,31 @@ cxx_warn_flags_list = [
5258 ' -Wvla' ,
5359]
5460
61+ # C++ linker flags
62+ cxx_harden_lflags_darwin_list = [
63+ ' -fixup_chains' ,
64+ ]
65+ cxx_harden_lflags_linux_list = [
66+ ' -z,now' ,
67+ ' -z,relro' ,
68+ ' -z,separate-code' ,
69+ ]
70+ cxx_harden_lflags_win64_list = [
71+ ' --dynamicbase' ,
72+ ' --enable-reloc-section' ,
73+ ' --high-entropy-va' ,
74+ ' --nxcompat' ,
75+ ]
76+
77+ # C++ preprocessor flags
78+ cxx_harden_release_preproc = [
79+ ' -D_FORTIFY_SOURCE=3' ,
80+ ' -U_FORTIFY_SOURCE' ,
81+ ]
82+
5583# Check compiler support for flags and apply them
5684cxx_flags = []
85+
5786foreach flag : cxx_core_flags_list + cxx_warn_flags_list
5887 if cxx.has_multi_arguments([' -Werror' , flag])
5988 cxx_flags += flag
@@ -65,7 +94,64 @@ if cxx.has_multi_arguments(['-Werror', '-Wformat', '-Wformat-security'])
6594 cxx_flags += [' -Wformat' , ' -Wformat-security' ]
6695endif
6796
68- add_project_arguments (cxx_flags, language : ' cpp' )
97+ harden_build = get_option (' harden_build' )
98+ if harden_build
99+ foreach flag : cxx_harden_flags_list
100+ if cxx.has_multi_arguments([' -Werror' , flag])
101+ cxx_flags += flag
102+ endif
103+ endforeach
104+
105+ if host_machine .cpu_family() == ' aarch64'
106+ if cxx.has_multi_arguments(' -Werror' , ' -mbranch-protection=bti' )
107+ cxx_flags += ' -mbranch-protection=bti'
108+ endif
109+ endif
110+
111+ # -fstack-clash-protection is a no-op on windows, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90458
112+ if host_machine .system() != ' windows'
113+ if cxx.has_multi_arguments(' -Werror' , ' -fstack-clash-protection' )
114+ cxx_flags += ' -fstack-clash-protection'
115+ endif
116+ endif
117+ endif
118+
119+ cxx_lflags = []
120+
121+ if harden_build
122+ cxx_harden_lflags = []
123+ if host_machine .system() == ' darwin'
124+ cxx_harden_lflags = cxx_harden_lflags_darwin_list
125+ elif host_machine .system() == ' windows'
126+ cxx_harden_lflags = cxx_harden_lflags_win64_list
127+ else
128+ cxx_harden_lflags = cxx_harden_lflags_linux_list
129+ endif
130+ foreach flag : cxx_harden_lflags
131+ flag = ' -Wl,' + flag
132+ if cxx.has_multi_link_arguments([' -Werror' , flag])
133+ cxx_lflags += flag
134+ endif
135+ endforeach
136+
137+ if cxx.has_multi_arguments(' -Werror' , ' -fPIE' ) and cxx.has_multi_link_arguments(' -pie' )
138+ cxx_flags += ' -fPIE'
139+ cxx_lflags += ' -pie'
140+ endif
141+ endif
142+
143+ cxx_preproc = []
144+
145+ if harden_build
146+ if get_option (' buildtype' ) != ' debug'
147+ foreach flag : cxx_harden_release_preproc
148+ cxx_preproc += flag
149+ endforeach
150+ endif
151+ endif
152+
153+ add_project_arguments (cxx_flags + cxx_preproc, language : ' cpp' )
154+ add_project_link_arguments (cxx_lflags, language : ' cpp' )
69155
70156# Check for threading support
71157thread_dep = dependency (' threads' , required : true )
0 commit comments