Add support for building tinc with MSVC
[tinc] / meson.build
1 project('tinc', 'c',
2   version: '1.18pre',
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 cc_defs = ['-D_GNU_SOURCE']
39 if os_name == 'sunos'
40   cc_defs += '-D__EXTENSIONS__'
41 endif
42
43 cc_flags = [cc_defs]
44 ld_flags = []
45
46 if opt_static.auto()
47   static = os_name == 'windows'
48 else
49   static = opt_static.enabled()
50 endif
51
52 if static and cc_name != 'msvc'
53   ld_flags += '-static'
54 endif
55
56 if opt_harden
57   if cc_name == 'msvc'
58     # Most of these flags are already ON by default in the latest version of MSVC.
59     # Add anyway in case someone is building using an old toolchain.
60     cc_flags += ['/guard:cf', '/GS']
61     ld_flags += [
62       '/guard:cf',
63       '/NXCOMPAT',
64       '/DYNAMICBASE',
65       cpu_family.endswith('64') ? '/HIGHENTROPYVA' : '/SAFESEH',
66     ]
67   else
68     cc_flags += [
69       '-D_FORTIFY_SOURCE=2',
70       '-fwrapv',
71       '-fno-strict-overflow',
72       '-Wreturn-type',
73       '-Wold-style-definition',
74       '-Wmissing-declarations',
75       '-Wmissing-prototypes',
76       '-Wstrict-prototypes',
77       '-Wredundant-decls',
78       '-Wbad-function-cast',
79       '-Wwrite-strings',
80       '-fdiagnostics-show-option',
81       '-fstrict-aliasing',
82       '-Wmissing-noreturn',
83     ]
84     if cc_name == 'clang'
85       cc_flags += '-Qunused-arguments'
86     endif
87     ld_flags += ['-Wl,-z,relro', '-Wl,-z,now']
88     if os_name == 'windows'
89       ld_flags += ['-Wl,--dynamicbase', '-Wl,--nxcompat']
90     endif
91   endif
92 endif
93
94 cc_flags = cc.get_supported_arguments(cc_flags)
95 ld_flags = cc.get_supported_link_arguments(ld_flags)
96
97 add_project_arguments(cc_flags, language: 'c')
98 add_project_link_arguments(ld_flags, language: 'c')
99
100 build_root = meson.current_build_dir()
101 src_root = meson.current_source_dir()
102
103 prefix = get_option('prefix')
104 dir_bin = prefix / get_option('bindir')
105 dir_data = prefix / get_option('datadir')
106 dir_info = prefix / get_option('infodir')
107 dir_lib = prefix / get_option('libdir')
108 dir_local_state = prefix / get_option('localstatedir')
109 dir_locale = prefix / get_option('localedir')
110 dir_man = prefix / get_option('mandir')
111 dir_sbin = prefix / get_option('sbindir')
112 dir_sysconf = prefix / get_option('sysconfdir')
113
114 if dir_run_state == ''
115   dir_run_state = dir_local_state / 'run'
116 endif
117
118 if not opt_docs.disabled()
119   subdir('doc')
120 endif
121
122 subdir('src')
123
124 if not opt_tests.disabled()
125   subdir('test')
126 endif
127
128 subdir('bash_completion.d')
129
130 if os_name == 'linux' and not opt_systemd.disabled()
131   subdir('systemd')
132 endif
133
134 prog_reformat = find_program('astyle', native: true, required: false)
135 if prog_reformat.found()
136   run_target('reformat', command: [
137     prog_reformat,
138     '--options=@SOURCE_ROOT@/.astylerc', '--recursive',
139     '@SOURCE_ROOT@/*.c', '@SOURCE_ROOT@/*.h',
140   ])
141 endif
142