summaryrefslogtreecommitdiffstats
path: root/configure.ac
blob: 61b5df0db79bc86ecc02564daecd5a288eb2ac58 (plain) (blame)
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.68])
AC_INIT([Pica Pica Messenger], [0.9.0dev], [http://picapica.im/bugzilla/], [pica-pica], [http://picapica.im])
AM_INIT_AUTOMAKE([-Wall  foreign subdir-objects])
AC_CONFIG_SRCDIR([PICA_node.c])

AC_CANONICAL_HOST

# Checks for programs.
AC_PROG_CC
AC_PROG_CXX
AC_PROG_INSTALL
AC_SYS_LARGEFILE

# Platform-specific environment settings
AS_CASE([$host],
    [*-apple-darwin*], [LDFLAGS="$LDFLAGS -headerpad_max_install_names"],
    [*-w64-mingw*], [
	AC_CHECK_LIB([ws2_32], [main], [LIBS="$LIBS -lws2_32"], [])
	AC_CHECK_LIB([winmm], [main], [LIBS="$LIBS -lwinmm"], [])
	AC_CHECK_LIB([iphlpapi], [main], [LIBS="$LIBS -liphlpapi"])
	],
    []
)

AC_ARG_WITH([miniupnpc],
  [AS_HELP_STRING([--without-miniupnpc],
  [disable support for miniupnpc])],
  [],
  [with_miniupnpc=yes])

AC_ARG_WITH([qt],
  [AS_HELP_STRING([--with-qt=qt4|qt5|qt6 qt6 is default],
  [Select Qt version, 4, 5 or 6])],
  [qt_version=$withval],
  [qt_version=qt6])

AS_IF([test "x$with_miniupnpc" != xno],
  [AC_CHECK_LIB([miniupnpc], [main],
    [],
    [AC_MSG_FAILURE(
      [miniupnpc test failed (--without-miniupnpc to disable)])],
  [])])

#QT_OPENGL_MODULES is what the VAAPI renderer needs on top of QT_MODULES:
#Qt 6 moved QOpenGLWidget and QOpenGLShaderProgram out of QtWidgets and QtGui
#into modules of their own, Qt 5 has them in the ones above already.
QT_OPENGL_MODULES=
AS_IF([test "x$qt_version" = xqt6],
	[
	QT_MODULES="Qt6Core Qt6Gui Qt6Sql Qt6Network Qt6Widgets"
	QT_OPENGL_MODULES="Qt6OpenGL Qt6OpenGLWidgets"
	QT_UIC_NAMES="uic-qt6 uic6 uic"
	QT_MOC_NAMES="moc-qt6 moc6 moc"
	CFLAGS="-fPIC $CFLAGS"
	CXXFLAGS="-fPIC $CXXFLAGS"
	],
  [test "x$qt_version" = xqt5],
	[
	QT_MODULES="Qt5Core Qt5Gui Qt5Sql Qt5Network Qt5Widgets"
	QT_UIC_NAMES="uic-qt5 uic5 uic"
	QT_MOC_NAMES="moc-qt5 moc5 moc"
	CFLAGS="-fPIC $CFLAGS"
	CXXFLAGS="-fPIC $CXXFLAGS"
	],
  [test "x$qt_version" = xqt4],
	[
	QT_MODULES="QtCore QtGui QtSql QtNetwork"
	QT_UIC_NAMES="uic-qt4 uic4 uic"
	QT_MOC_NAMES="moc-qt4 moc4 moc"
	],
  [AC_MSG_ERROR(["Invalid Qt version"])])

# Checks for common libraries, required by both pica-node and pica-client
AC_SEARCH_LIBS([dlopen], [dl])
AC_CHECK_LIB([crypto], [RAND_add],[],[AC_MSG_ERROR(["libcrypto from OpenSSL package not found"])],[ -lz ])
AC_CHECK_LIB([ssl], [SSL_new],[],[AC_MSG_ERROR(["libssl from OpenSSL package not found"])])
AC_CHECK_LIB([pthread], [pthread_create])

#checking if pica-node is disabled
AC_ARG_ENABLE([node],
    AS_HELP_STRING([--disable-node], [Disable build of pica-node]))

AS_IF([test "x$enable_node" != "xno"], [
  PICA_NODE_ENABLED=yes
])

#checking if pica-client is disabled
AC_ARG_ENABLE([client],
    AS_HELP_STRING([--disable-client], [Disable build of pica-client]))

AS_IF([test "x$enable_client" != "xno"], [
  PICA_CLIENT_ENABLED=yes
])

#checking if VAAPI hardware acceleration is wanted
#unspecified means autodetect: use it when the libraries are there, do without
#them when they are not. --enable-vaapi makes their absence a hard error,
#--disable-vaapi skips looking for them at all.
AC_ARG_ENABLE([vaapi],
    AS_HELP_STRING([--enable-vaapi],
      [Enable VAAPI accelerated video encoding, decoding and rendering (default: autodetect)])
    AS_HELP_STRING([--disable-vaapi],
      [Disable VAAPI accelerated video encoding, decoding and rendering]))

#checking if Direct3D 12 hardware encoding and decoding is wanted. The Windows
#counterpart of the VAAPI path above - there is no libva there, but FFmpeg can
#reach the same GPU encoders and decoders through D3D12 Video. Autodetected the
#same way, and only ever looked for when building for Windows.
AC_ARG_ENABLE([d3d12va],
    AS_HELP_STRING([--enable-d3d12va],
      [Enable Direct3D 12 accelerated video encoding and decoding, Windows only (default: autodetect)])
    AS_HELP_STRING([--disable-d3d12va],
      [Disable Direct3D 12 accelerated video encoding and decoding]))

#checking if PulseAudio device enumeration is wanted. Only affects what the
#settings dialog can list; see the check further down.
AC_ARG_ENABLE([pulse],
    AS_HELP_STRING([--enable-pulse],
      [Enable listing of PulseAudio/PipeWire capture and playback devices (default: autodetect)])
    AS_HELP_STRING([--disable-pulse],
      [Disable listing of PulseAudio/PipeWire devices]))

#checking if the X11 rendering path is wanted
#Qt gives widgets a GLX context by default in an X11 session, and a GLX
#context has no way to import the DMA-BUF handles a VA surface is exported
#as - that path needs EGL. vaPutSurface() leaves OpenGL out of it altogether
#and has the driver draw the surface straight onto an X11 window, which is
#what makes hardware rendering work in such a session. Autodetected like
#VAAPI itself, and pointless without it.
AC_ARG_ENABLE([vaapi-x11],
    AS_HELP_STRING([--enable-vaapi-x11],
      [Enable vaPutSurface() rendering, for X11 sessions where Qt uses GLX rather than EGL (default: autodetect)])
    AS_HELP_STRING([--disable-vaapi-x11],
      [Disable vaPutSurface() rendering]))

#checking if QR code support is wanted: showing one's own Pica Pica ID as a QR
#code, and adding a contact by pointing the camera at theirs. Autodetected
#like VAAPI; see the check further down.
AC_ARG_ENABLE([qrcode],
    AS_HELP_STRING([--enable-qrcode],
      [Enable showing and scanning Pica Pica IDs as QR codes, using ZXing-C++ (default: autodetect)])
    AS_HELP_STRING([--disable-qrcode],
      [Disable QR code support]))

#at least one program must be enabled
AS_IF([test "x$PICA_NODE_ENABLED" != "xyes" && test "x$PICA_CLIENT_ENABLED" != "xyes"  ],
  AC_MSG_ERROR(["Either pica-node or pica-client must be enabled"])
)


AM_CONDITIONAL([PICA_NODE], [test "x$PICA_NODE_ENABLED" = "xyes"])
AM_CONDITIONAL([PICA_CLIENT], [test "x$PICA_CLIENT_ENABLED" = "xyes"])

#pica-node tests
AS_IF([test "x$PICA_NODE_ENABLED" = "xyes"],[
AC_CHECK_LIB([sqlite3], [sqlite3_open], [], [AC_MSG_ERROR(["libsqlite3 library not found"])])
AC_CHECK_LIB([event], [event_get_version], [PICA_NODE_LIBS="-levent"], [AC_MSG_ERROR(["libevent not found"])])
AC_SUBST([PICA_NODE_LIBS])
AC_CHECK_PROG([SQLITE3], [sqlite3], [yes], [no])

AS_IF([test "x$SQLITE3" != xyes],  [ AC_MSG_ERROR(["sqlite3 program not found"])])
])


#pica-client tests
AS_IF([test "x$PICA_CLIENT_ENABLED" = "xyes"],[
#AC_PROG_CXX
PKG_CHECK_MODULES([QT_DEPENDENCIES], [$QT_MODULES])
AS_CASE([$host],
	[*-linux-*],
		[
		PKG_CHECK_MODULES([PLATFORM_DEPS],["alsa"])
		],
	[*-w64-mingw*],
		[
		TARGET_OS=windows
		QT_DEPENDENCIES_LIBS="$QT_DEPENDENCIES_LIBS -mwindows"
		#the WASAPI backend is COM: ole32 for CoCreateInstance and the
		#task allocator, oleaut32 for PROPVARIANT.
		PLATFORM_DEPS_LIBS="$PLATFORM_DEPS_LIBS -lole32 -loleaut32"
		AC_PATH_TOOL(WINDRES, windres, none)
		if test x$WINDRES = xnone; then
			AC_MSG_ERROR("windres not found")
		fi
		],
	[*-apple-darwin*],
		[
		TARGET_OS=darwin
		#the CoreAudio backend: the HAL for device enumeration,
		#AudioUnit/AudioToolbox for the VoiceProcessingIO unit that does
		#the echo cancellation, CoreFoundation for the CFString handling
		#the HAL's names come back as.
		PLATFORM_DEPS_LIBS="$PLATFORM_DEPS_LIBS -framework CoreAudio -framework AudioUnit -framework AudioToolbox -framework CoreFoundation"
		],
	[]
)
AC_SUBST([QT_DEPENDENCIES_CFLAGS])
AC_SUBST([QT_DEPENDENCIES_LIBS])
#Qt 6 installs moc and uic in its libexec directory, which is not in PATH -
#a plain "moc" found there is likely Qt 5's, or qtchooser. Look in the
#directory pkg-config names first.
QT_TOOL_PATH=$PATH
AS_IF([test "x$qt_version" = xqt6],
	[QT_TOOL_PATH=`$PKG_CONFIG --variable=libexecdir Qt6Core`$PATH_SEPARATOR$PATH])
AC_PATH_PROGS([UIC], [$QT_UIC_NAMES], [notfound], [$QT_TOOL_PATH])
AC_PATH_PROGS([MOC], [$QT_MOC_NAMES], [notfound], [$QT_TOOL_PATH])
#fail if not found ...
AS_IF([test "x$UIC" = xnotfound],[AC_MSG_ERROR(["uic program not found. Please install the Qt development package"])])
AS_IF([test "x$MOC" = xnotfound],[AC_MSG_ERROR(["moc program not found. Please install the Qt development package"])])

#check for FFmpeg libraries
PKG_CHECK_MODULES([FFMPEG],["libavcodec libavdevice libavformat libavutil libswresample libswscale"])
AC_SUBST([FFMPEG_CFLAGS])
AC_SUBST([FFMPEG_LIBS])

#check for webrtc-audio-processing, which provides AEC3 - the acoustic echo
#cancellation pica-client runs itself. Not optional: on Linux there is no
#other cancellation in the picture unless the user has set their sound server
#up for it, and on the platforms that have their own this is still what the
#"use own echo cancellation" setting selects.
#
#Either the 1.x or the 2.x ABI will do. They install in parallel under
#separate pkg-config names, and pica-client/echocanceller.cpp carries the few
#conditionals the API break between them needs. 0.x is not a candidate at any
#price: it predates AEC3 entirely, and its whole API is different.
#
#1.x is preferred when both are installed, only because it is the one this has
#had the most hours on. --with-webrtc-audio-processing=2 forces the other,
#which is how the 2.x path gets exercised deliberately rather than by accident
#of what is on the build machine.
AC_ARG_WITH([webrtc-audio-processing],
  [AS_HELP_STRING([--with-webrtc-audio-processing=@<:@auto|1|2@:>@],
  [webrtc-audio-processing major version to build against (default: auto, preferring 1)])],
  [], [with_webrtc_audio_processing=auto])

AS_CASE([$with_webrtc_audio_processing],
  [auto], [webrtc_audio_processing_try="1 2"],
  [1],    [webrtc_audio_processing_try="1"],
  [2],    [webrtc_audio_processing_try="2"],
  [AC_MSG_ERROR([--with-webrtc-audio-processing must be auto, 1 or 2])])

WEBRTC_AUDIO_PROCESSING_MAJOR=
for webrtc_audio_processing_major in $webrtc_audio_processing_try; do
  AS_IF([test "x$WEBRTC_AUDIO_PROCESSING_MAJOR" = x],
    [PKG_CHECK_MODULES([WEBRTC_AUDIO_PROCESSING],
      [webrtc-audio-processing-$webrtc_audio_processing_major],
      [WEBRTC_AUDIO_PROCESSING_MAJOR=$webrtc_audio_processing_major],
      [:])])
done

AS_IF([test "x$WEBRTC_AUDIO_PROCESSING_MAJOR" = x],
  [AC_MSG_ERROR(["webrtc-audio-processing 1.x or 2.x was not found. It provides the acoustic echo cancellation used in calls. On Debian 13 and Ubuntu 25.10 or newer it is libwebrtc-audio-processing-dev; the same name on Ubuntu 24.04 is version 0.3, which predates AEC3 and will not do. On Gentoo it is media-libs/webrtc-audio-processing:1"])])

AC_MSG_NOTICE([using webrtc-audio-processing-$WEBRTC_AUDIO_PROCESSING_MAJOR])

#echocanceller.cpp can work this out from the headers on its own, but only the
#check above knows which one was picked when both are installed.
AC_DEFINE_UNQUOTED([PICA_WEBRTC_AUDIO_PROCESSING_MAJOR],
  [$WEBRTC_AUDIO_PROCESSING_MAJOR],
  [Major version of the webrtc-audio-processing library being built against])

AC_SUBST([WEBRTC_AUDIO_PROCESSING_CFLAGS])
AC_SUBST([WEBRTC_AUDIO_PROCESSING_LIBS])

#the WebRTC headers, and the abseil ones they include, refuse to compile as
#anything older. Recent compilers default to C++17 or later and need nothing
#added; older ones have to be told.
AC_LANG_PUSH([C++])
AC_MSG_CHECKING([whether $CXX compiles C++17 without help])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#if __cplusplus < 201703L
#error C++17 is not the default
#endif]], [])],
  [AC_MSG_RESULT([yes])],
  [
    AC_MSG_RESULT([no])
    pica_saved_cxxflags="$CXXFLAGS"
    CXXFLAGS="$CXXFLAGS -std=c++17"
    AC_MSG_CHECKING([whether $CXX accepts -std=c++17])
    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#if __cplusplus < 201703L
#error C++17 is not enabled
#endif]], [])],
      [AC_MSG_RESULT([yes])],
      [
        AC_MSG_RESULT([no])
        CXXFLAGS="$pica_saved_cxxflags"
        AC_MSG_ERROR(["A C++17 compiler is required to build pica-client"])
      ])
  ])
AC_LANG_POP([C++])

#check for libpulse, used only to list PulseAudio sources and sinks in the
#settings dialog - playing and capturing through PulseAudio goes via FFmpeg's
#own "pulse" device and needs nothing from us. Without it the "PulseAudio /
#PipeWire" driver still works, but offers only the server's default device.
AS_IF([test "x$enable_pulse" != "xno"], [
  PKG_CHECK_MODULES([PULSE], [libpulse],
    [
      PICA_PULSE_ENABLED=yes
      AC_DEFINE([HAVE_LIBPULSE], [1], [Define to 1 if PulseAudio device enumeration is available])
      AC_SUBST([PULSE_CFLAGS])
      AC_SUBST([PULSE_LIBS])
    ],
    [
      AS_IF([test "x$enable_pulse" = "xyes"],
        [AC_MSG_ERROR(["PulseAudio support was requested but libpulse was not found"])],
        [AC_MSG_WARN(["libpulse not found - the PulseAudio driver will only offer the default device"])])
    ])
])

#check for VAAPI libraries, unless explicitly disabled
#libva is the accelerator itself, libva-drm opens it through a DRM render node,
#and EGL is what lets a decoded surface be handed to OpenGL without a copy.
#With Qt 6 the Qt OpenGL modules the renderer is built on are checked here as
#well, so that their absence turns VAAPI off rather than breaking the build.
AS_IF([test "x$enable_vaapi" != "xno"], [
  PKG_CHECK_MODULES([VAAPI], [libva libva-drm egl $QT_OPENGL_MODULES],
    [
      PICA_VAAPI_ENABLED=yes
      AC_DEFINE([HAVE_VAAPI], [1], [Define to 1 if VAAPI accelerated video is available])
      AC_SUBST([VAAPI_CFLAGS])
      AC_SUBST([VAAPI_LIBS])
    ],
    [
      AS_IF([test "x$enable_vaapi" = "xyes"],
        [AC_MSG_ERROR(["VAAPI support was requested but libva, libva-drm, EGL or Qt OpenGL support was not found"])],
        [AC_MSG_WARN(["libva, libva-drm, EGL or Qt OpenGL support not found - building without VAAPI acceleration"])])
    ])
])

#check for libva's X11 rendering support, needed to draw a decoded surface in
#an X11 session, where Qt's OpenGL contexts are GLX rather than EGL ones.
#libGL comes along because working out which of the two a session gives means
#asking GLX whether it owns the context.
AS_IF([test "x$enable_vaapi_x11" = "xyes" && test "x$PICA_VAAPI_ENABLED" != "xyes"],
  [AC_MSG_ERROR(["X11 rendering was requested but VAAPI support is not enabled"])])

AS_IF([test "x$PICA_VAAPI_ENABLED" = "xyes" && test "x$enable_vaapi_x11" != "xno"], [
  PKG_CHECK_MODULES([VAAPI_X11], [libva-x11 x11 gl],
    [
      PICA_VAAPI_X11_ENABLED=yes
      AC_DEFINE([HAVE_VAAPI_X11], [1], [Define to 1 if libva X11 rendering support is available])
      AC_SUBST([VAAPI_X11_CFLAGS])
      AC_SUBST([VAAPI_X11_LIBS])
    ],
    [
      AS_IF([test "x$enable_vaapi_x11" = "xyes"],
        [AC_MSG_ERROR(["X11 rendering was requested but libva-x11, libX11 or libGL was not found"])],
        [AC_MSG_WARN(["libva-x11, libX11 or libGL not found - building without X11 rendering, hardware rendering will need an EGL session"])])
    ])
])

#check whether FFmpeg knows how to encode and decode through Direct3D 12, one
#of the ways a Windows build reaches the GPU: libva is not part of that
#platform, but the hwcontext machinery the VAAPI path uses carries a D3D12
#device type as well, and libavcodec has h264_d3d12va and hevc_d3d12va encoders
#and D3D12 hwaccels for its decoders to go with it.
#
#Nothing is linked for this - libavutil loads d3d12.dll itself when a device is
#created - so all there is to check is that the headers are new enough to name
#the device type and the pixel format. Whether those encoders and hwaccels were
#compiled into the libavcodec being built against, and whether the machine the
#client ends up running on has a GPU that can use them, are both runtime
#questions, answered by falling back to software.
#
#The other Windows paths - Media Foundation encoding, Direct3D 11 and DXVA2
#decoding - need no check of their own: FFmpeg's headers have named them for
#far longer than any FFmpeg this builds against, so they are compiled in on
#every Windows build and left to the runtime to find out whether they work.
AS_IF([test "x$TARGET_OS" = "xwindows" && test "x$enable_d3d12va" != "xno"], [
  AC_MSG_CHECKING([whether FFmpeg supports Direct3D 12 video encoding and decoding])

  pica_save_CPPFLAGS="$CPPFLAGS"
  CPPFLAGS="$CPPFLAGS $FFMPEG_CFLAGS"

  AC_COMPILE_IFELSE(
    [AC_LANG_PROGRAM(
      [[#include <libavutil/hwcontext.h>
        #include <libavutil/pixfmt.h>]],
      [[enum AVHWDeviceType device = AV_HWDEVICE_TYPE_D3D12VA;
        enum AVPixelFormat format = AV_PIX_FMT_D3D12;
        (void)device; (void)format;]])],
    [
      PICA_D3D12VA_ENABLED=yes
      AC_DEFINE([HAVE_D3D12VA], [1], [Define to 1 if FFmpeg can encode and decode video through Direct3D 12])
      AC_MSG_RESULT([yes])
    ],
    [
      AC_MSG_RESULT([no])
      AS_IF([test "x$enable_d3d12va" = "xyes"],
        [AC_MSG_ERROR(["Direct3D 12 acceleration was requested but this FFmpeg has no D3D12 hardware context"])],
        [AC_MSG_WARN(["this FFmpeg has no D3D12 hardware context - building without Direct3D 12 acceleration"])])
    ])

  CPPFLAGS="$pica_save_CPPFLAGS"
])

AS_IF([test "x$TARGET_OS" != "xwindows" && test "x$enable_d3d12va" = "xyes"],
  [AC_MSG_ERROR(["Direct3D 12 acceleration was requested but it only exists on Windows"])])

#check for ZXing-C++, which both draws the QR code the "Show My Pica Pica ID"
#dialog shows and reads the one the camera sees in the "Scan QR Code" dialog.
#
#2.2 is the floor because that is where ReaderOptions, which the scanner is
#written against, first appeared - and what Ubuntu 24.04 ships. Distributions
#with anything older cannot build pica-client anyway, for want of
#webrtc-audio-processing 1.x.
#
#The version alone does not say how a code is drawn, though. ZXing-C++ has two
#writer APIs, and which of them a given libZXing carries is a build option of
#its own: 2.x builds the old MultiFormatWriter by default, 3.x the new
#CreateBarcodeFromText() instead, and either can be built with both or with
#no writer at all. So the two are tried by linking against them, the new one
#first, and a ZXing with neither counts as not found - scanning alone would
#leave nothing to scan from.
AS_IF([test "x$enable_qrcode" != "xno"], [
  PKG_CHECK_MODULES([ZXING], [zxing >= 2.2],
    [pica_zxing_found=yes],
    [pica_zxing_found=no])

  PICA_ZXING_WRITER=
  AS_IF([test "x$pica_zxing_found" = "xyes"], [
    AC_LANG_PUSH([C++])
    pica_save_CPPFLAGS="$CPPFLAGS"
    pica_save_LIBS="$LIBS"
    CPPFLAGS="$CPPFLAGS $ZXING_CFLAGS"
    LIBS="$ZXING_LIBS $LIBS"

    AC_MSG_CHECKING([whether ZXing-C++ can read QR codes])
    AC_LINK_IFELSE(
      [AC_LANG_PROGRAM(
        [[#include <ZXing/ReadBarcode.h>]],
        [[unsigned char pixel = 0;
          ZXing::ImageView image(&pixel, 1, 1, ZXing::ImageFormat::Lum);
          ZXing::ReaderOptions options;
          options.setFormats(ZXing::BarcodeFormat::QRCode);
          return ZXing::ReadBarcode(image, options).text().size() > 0;]])],
      [AC_MSG_RESULT([yes])],
      [
        AC_MSG_RESULT([no])
        pica_zxing_found=no
      ])

    AS_IF([test "x$pica_zxing_found" = "xyes"], [
      AC_MSG_CHECKING([for the ZXing-C++ CreateBarcodeFromText() writer])
      AC_LINK_IFELSE(
        [AC_LANG_PROGRAM(
          [[#include <ZXing/CreateBarcode.h>]],
          [[ZXing::Barcode barcode = ZXing::CreateBarcodeFromText("pica",
                ZXing::CreatorOptions(ZXing::BarcodeFormat::QRCode));
            return barcode.symbol().width();]])],
        [
          AC_MSG_RESULT([yes])
          PICA_ZXING_WRITER=create_barcode
        ],
        [AC_MSG_RESULT([no])])
    ])

    AS_IF([test "x$pica_zxing_found" = "xyes" && test "x$PICA_ZXING_WRITER" = "x"], [
      AC_MSG_CHECKING([for the ZXing-C++ MultiFormatWriter writer])
      AC_LINK_IFELSE(
        [AC_LANG_PROGRAM(
          [[#include <ZXing/MultiFormatWriter.h>
            #include <ZXing/BitMatrix.h>]],
          [[ZXing::BitMatrix matrix =
                ZXing::MultiFormatWriter(ZXing::BarcodeFormat::QRCode).encode(std::string("pica"), 0, 0);
            return matrix.width();]])],
        [
          AC_MSG_RESULT([yes])
          PICA_ZXING_WRITER=multi_format_writer
        ],
        [AC_MSG_RESULT([no])])
    ])

    AS_IF([test "x$pica_zxing_found" = "xyes" && test "x$PICA_ZXING_WRITER" = "x"],
      [pica_zxing_found=no])

    CPPFLAGS="$pica_save_CPPFLAGS"
    LIBS="$pica_save_LIBS"
    AC_LANG_POP([C++])
  ])

  AS_IF([test "x$pica_zxing_found" = "xyes"],
    [
      PICA_QRCODE_ENABLED=yes
      AC_DEFINE([HAVE_QRCODE], [1], [Define to 1 if Pica Pica IDs can be shown and scanned as QR codes])
      AS_IF([test "x$PICA_ZXING_WRITER" = "xcreate_barcode"],
        [AC_DEFINE([HAVE_ZXING_CREATE_BARCODE], [1], [Define to 1 if ZXing-C++ draws barcodes with CreateBarcodeFromText()])])
      AC_SUBST([ZXING_CFLAGS])
      AC_SUBST([ZXING_LIBS])
    ],
    [
      AS_IF([test "x$enable_qrcode" = "xyes"],
        [AC_MSG_ERROR(["QR code support was requested but ZXing-C++ 2.2 or newer, built with both a reader and a writer, was not found"])],
        [AC_MSG_WARN(["ZXing-C++ 2.2 or newer, built with both a reader and a writer, not found - building without QR code support"])])
    ])
])
])

AM_CONDITIONAL([PICA_PULSE], [test "x$PICA_PULSE_ENABLED" = "xyes"])
AM_CONDITIONAL([PICA_VAAPI], [test "x$PICA_VAAPI_ENABLED" = "xyes"])
AM_CONDITIONAL([PICA_VAAPI_X11], [test "x$PICA_VAAPI_X11_ENABLED" = "xyes"])
AM_CONDITIONAL([PICA_QRCODE], [test "x$PICA_QRCODE_ENABLED" = "xyes"])

AC_SUBST([PLATFORM_DEPS_CFLAGS])
AC_SUBST([PLATFORM_DEPS_LIBS])

# Checks for header files.
AC_CHECK_HEADERS([arpa/inet.h limits.h netdb.h netinet/in.h stdlib.h string.h sys/ioctl.h sys/socket.h sys/time.h unistd.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_TYPE_UINT16_T

# Checks for library functions.
AC_CHECK_FUNCS([getcwd inet_ntoa memmove memset select socket strchr strstr strtol])

VERSION=$PACKAGE_VERSION
AC_SUBST([VERSION])

# Both the assemblyIdentity element of a Windows application manifest and the
# FILEVERSION field of a VERSIONINFO resource only accept a version of exactly
# four numbers, which the package version ("0.9.0dev") is not. Cut it at the
# first character that is neither a digit nor a dot and pad the rest with ".0"
# up to four components. The manifest wants them separated by dots and
# VERSIONINFO by commas, so both spellings are substituted.
AC_MSG_CHECKING([for the Windows binary version number])
PICA_WINDOWS_VERSION=`echo "$PACKAGE_VERSION" | sed -e ['s/[^0-9.].*$//'] -e ['s/\.*$//']`
AS_IF([test -z "$PICA_WINDOWS_VERSION"], [PICA_WINDOWS_VERSION=0])
while test `echo "$PICA_WINDOWS_VERSION" | tr -dc . | wc -c` -lt 3
do
	PICA_WINDOWS_VERSION="$PICA_WINDOWS_VERSION.0"
done
PICA_WINDOWS_VERSION_COMMAS=`echo "$PICA_WINDOWS_VERSION" | tr . ,`
AC_MSG_RESULT([$PICA_WINDOWS_VERSION])
AC_SUBST([PICA_WINDOWS_VERSION])
AC_SUBST([PICA_WINDOWS_VERSION_COMMAS])

AM_CONDITIONAL([TARGET_WINDOWS], [test x$TARGET_OS = xwindows])
AM_CONDITIONAL([TARGET_DARWIN], [test x$TARGET_OS = xdarwin])
AC_CONFIG_FILES([Makefile windows/installer32.nsi windows/installer64.nsi windows/pica-client.exe.manifest windows/pica-node.exe.manifest])
AC_OUTPUT