Run shfmt as part of the reformat target.
[tinc] / meson.build
1 project('tinc', 'c',
2   version: run_command([find_program('python3'), 'version.py', 'short'], check: true).stdout(),
3   license: 'GPL-2.0-or-later',
4   meson_version: '>=0.51',
5   default_options: [
6     'c_std=c11',
7     'warning_level=3',
8     'buildtype=debugoptimized',
9   ],
10 )
11
12 dir_run_state = get_option('runstatedir')
13 opt_crypto = get_option('crypto')
14 opt_curses = get_option('curses')
15 opt_debug = get_option('debug')
16 opt_docs = get_option('docs')
17 opt_harden = get_option('hardening')
18 opt_jumbograms = get_option('jumbograms')
19 opt_lz4 = get_option('lz4')
20 opt_lzo = get_option('lzo')
21 opt_miniupnpc = get_option('miniupnpc')
22 opt_readline = get_option('readline')
23 opt_static = get_option('static')
24 opt_systemd = get_option('systemd')
25 opt_tests = get_option('tests')
26 opt_tunemu = get_option('tunemu')
27 opt_uml = get_option('uml')
28 opt_vde = get_option('vde')
29 opt_zlib = get_option('zlib')
30
31 meson_version = meson.version()
32
33 cc = meson.get_compiler('c')
34 os_name = host_machine.system()
35 cpu_family = host_machine.cpu_family()
36 cc_name = cc.get_id()
37
38 python = find_program('python3')
39 if meson_version.version_compare('>=0.55')
40   python_path = python.full_path()
41 else
42   python_path = python.path()
43 endif
44
45 cc_defs = ['-D_GNU_SOURCE']
46 if os_name == 'sunos'
47   cc_defs += '-D__EXTENSIONS__'
48 endif
49
50 cc_flags = [cc_defs]
51 ld_flags = []
52
53 if opt_static.auto()
54   static = os_name == 'windows'
55 else
56   static = opt_static.enabled()
57 endif
58
59 if static and cc_name != 'msvc'
60   ld_flags += '-static'
61 endif
62
63 if opt_harden
64   if cc_name == 'msvc'
65     # Most of these flags are already ON by default in the latest version of MSVC.
66     # Add anyway in case someone is building using an old toolchain.
67     cc_flags += ['/guard:cf', '/GS']
68     ld_flags += [
69       '/guard:cf',
70       '/NXCOMPAT',
71       '/DYNAMICBASE',
72       cpu_family.endswith('64') ? '/HIGHENTROPYVA' : '/SAFESEH',
73     ]
74   else
75     cc_flags += [
76       '-D_FORTIFY_SOURCE=2',
77       '-fwrapv',
78       '-fno-strict-overflow',
79       '-Wreturn-type',
80       '-Wold-style-definition',
81       '-Wmissing-declarations',
82       '-Wmissing-prototypes',
83       '-Wstrict-prototypes',
84       '-Wredundant-decls',
85       '-Wbad-function-cast',
86       '-Wwrite-strings',
87       '-fdiagnostics-show-option',
88       '-fstrict-aliasing',
89       '-Wmissing-noreturn',
90     ]
91     if cc_name == 'clang'
92       cc_flags += '-Qunused-arguments'
93     endif
94     ld_flags += ['-Wl,-z,relro', '-Wl,-z,now']
95     if os_name == 'windows'
96       ld_flags += ['-Wl,--dynamicbase', '-Wl,--nxcompat']
97     endif
98   endif
99 endif
100
101 cc_flags = cc.get_supported_arguments(cc_flags)
102 ld_flags = cc.get_supported_link_arguments(ld_flags)
103
104 add_project_arguments(cc_flags, language: 'c')
105 add_project_link_arguments(ld_flags, language: 'c')
106
107 build_root = meson.current_build_dir()
108 src_root = meson.current_source_dir()
109
110 prefix = get_option('prefix')
111 dir_bin = prefix / get_option('bindir')
112 dir_data = prefix / get_option('datadir')
113 dir_info = prefix / get_option('infodir')
114 dir_lib = prefix / get_option('libdir')
115 dir_local_state = prefix / get_option('localstatedir')
116 dir_locale = prefix / get_option('localedir')
117 dir_man = prefix / get_option('mandir')
118 dir_sbin = prefix / get_option('sbindir')
119 dir_sysconf = prefix / get_option('sysconfdir')
120
121 if dir_run_state == ''
122   dir_run_state = dir_local_state / 'run'
123 endif
124
125 if not opt_docs.disabled()
126   subdir('doc')
127 endif
128
129 subdir('src')
130
131 if not opt_tests.disabled()
132   subdir('test')
133 endif
134
135 subdir('bash_completion.d')
136
137 if os_name == 'linux' and not opt_systemd.disabled()
138   subdir('systemd')
139 endif
140
141 run_target('reformat', command: [
142   find_program('python3'),
143   '@SOURCE_ROOT@/reformat.py',
144 ])