summaryrefslogtreecommitdiff
path: root/meson.build
blob: 14d2245ef53bf7d1d7c677bf5c1e3db0ba31566b (plain)
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
project('libnaunet', 'c', version: '0.01',
  meson_version: '>=0.63.0',
  default_options: ['warning_level=2', 'c_std=c99'],
  license: 'GPL-3.0-only')

compiler = meson.get_compiler('c')
cmake = import('cmake')

is_debug = get_option('debug')
is_minsize = get_option('optimization') == 's'
is_windows = host_machine.system() == 'windows'

naunet_src = [
  'src/common.c',
  'src/util/error.c',
  'src/util/buffer.c',
  'src/util/packet.c',
  'src/util/file/util.c'
]
naunet_inc = [include_directories('include')]
naunet_deps = []
naunet_args = []

alabaster = subproject('libalabaster')
naunet_deps += [alabaster.get_variable('alabaster')]

math = compiler.find_library('m', required: false)
if math.found()
  naunet_deps += [math]
endif

if get_option('event-loop').enabled()
  naunet_src += [
    'src/loop.c',
    'src/poll.c',
    'src/timer.c',
    'src/signal.c',
    'src/packet_pool.c',
    'src/packet_cache.c',
    'src/packet_stream.c',
    'src/multiplex.c',
    'src/rpc.c',
    'src/line_processor.c'
  ]

  # libev gets embedded in ev_embed_compat.c.
  libev = subproject('libev').get_variable('ev')
  naunet_src += ['src/ev_embed_compat.c']
  naunet_deps += [libev]

  naunet_has_curl = false
  if get_option('curl').enabled()
    libcurl_found = false
    if 'curl' not in get_option('force_fallback_for') and get_option('wrap_mode') != 'forcefallback'
      libcurl = dependency('libcurl', required: false, allow_fallback: false)
      libcurl_found = libcurl.found()
    endif
    if not libcurl_found
      curl_opts = cmake.subproject_options()
      reltype = is_minsize ? 'MinSizeRel' : 'Release'
      curl_opts.add_cmake_defines({ 'CMAKE_BUILD_TYPE': is_debug ? 'Debug' : reltype })
      curl_opts.add_cmake_defines({ 'BUILD_CURL_EXE': false })
      curl_opts.add_cmake_defines({ 'BUILD_SHARED_LIBS': false })
      curl_opts.add_cmake_defines({ 'BUILD_STATIC_LIBS': true })
      curl_opts.add_cmake_defines({ 'CURL_USE_LIBSSH2': false })
      curl_opts.add_cmake_defines({ 'CURL_USE_LIBPSL': false })
      curl_opts.add_cmake_defines({ 'CURL_DISABLE_LDAP': true })
      curl_opts.add_cmake_defines({ 'CURL_USE_OPENSSL': true })
      curl_opts.add_cmake_defines({ 'ENABLE_WEBSOCKETS': true })
      libcurl = cmake.subproject('libcurl', options: curl_opts).dependency('libcurl_static')
      zlib = dependency('zlib')
      openssl = dependency('openssl')
      naunet_deps += [zlib, openssl]
      if is_windows
        curl_opts.add_cmake_defines({ 'USE_LIBIDN2': false })
        curl_opts.add_cmake_defines({ 'USE_WIN32_IDN': true })
        naunet_deps += [
          compiler.find_library('bcrypt'),
          compiler.find_library('crypt32')
        ]
      else
        curl_opts.add_cmake_defines({ 'USE_LIBIDN2': true })
        libidn2 = dependency('libidn2')
        naunet_deps += [libidn2]
      endif
    endif
    naunet_has_curl = libcurl.found()
    if naunet_has_curl
      naunet_src += [
        'src/curl/curl.c',
        'src/curl/http.c',
        'src/curl/websocket.c'
      ]
      naunet_deps += [libcurl]
      naunet_args += ['-DNAUNET_HAS_CURL']
    endif
  endif
endif

if get_option('json').enabled()
  #jsmn = subproject('jsmn').get_variable('jsmn')
  #naunet_deps += [jsmn]
  #naunet_args += ['-DNAUNET_HAS_JSON']
  jansson_found = false
  if 'jansson' not in get_option('force_fallback_for') and get_option('wrap_mode') != 'forcefallback'
    jansson = dependency('jansson', required: false, allow_fallback: false)
    jansson_found = jansson.found()
  endif
  if not jansson_found
    jansson_opts = cmake.subproject_options()
    reltype = is_minsize ? 'MinSizeRel' : 'Release'
    jansson_opts.add_cmake_defines({ 'CMAKE_BUILD_TYPE': is_debug ? 'Debug' : reltype })
    jansson_opts.add_cmake_defines({ 'JANSSON_EXAMPLES': false })
    jansson_opts.add_cmake_defines({ 'JANSSON_WITHOUT_TESTS': true })
    jansson_opts.add_cmake_defines({ 'JANSSON_BUILD_DOCS': false })
    jansson_opts.add_cmake_defines({ 'JANSSON_INSTALL': false })
    jansson = cmake.subproject('jansson', options: jansson_opts).dependency('jansson')
  endif
  naunet_deps += [jansson]
  naunet_args += ['-DNAUNET_HAS_JSON']
endif

if is_windows
  naunet_args += ['-DNAUNET_ON_WINDOWS']
  if get_option('win32-compat')
    # https://learn.microsoft.com/en-us/windows/win32/winprog/using-the-windows-headers
    naunet_args += ['-DNAUNET_WIN32_COMPAT_MODE']
  endif
  naunet_src += [
    'src/winwrap.c',
    'src/util/timer/timer_windows.c',
    'src/util/thread/thread_windows.c',
    'src/util/file/file_stdio.c',
    'src/socket/socket_windows.c'
  ]
  naunet_args += ['-DNAUNET_NEEDS_STDIO_ASSIST']
  naunet_has_mmap = false
  naunet_deps += [
    compiler.find_library('kernel32'),
    compiler.find_library('ws2_32')
  ]
else
  naunet_src += [
    'src/util/timer/timer_linux.c',
    'src/util/thread/thread_linux.c',
    'src/util/file/file_linux.c',
    'src/socket/socket_linux.c'
  ]
  if get_option('event-loop').enabled()
    naunet_src += ['src/fs_event/fs_event_inotify.c']
  endif
  naunet_args += ['-D_GNU_SOURCE']
  naunet_has_mmap = true
  pthreads = dependency('pthreads', required: false)
  if pthreads.found()
    naunet_deps += [pthreads]
  endif
  have_pthread_cancel = compiler.has_function('pthread_cancel', prefix: '#include <pthread.h>')
  if have_pthread_cancel
    naunet_args += ['-DNAUNET_HAS_THREAD_CANCEL']
  endif
endif

naunet = declare_dependency(include_directories: naunet_inc,
  dependencies: naunet_deps, sources: naunet_src,
  compile_args: naunet_args)

if get_option('tests').allowed()
  subdir('tests')
endif