1##
2 # @file configure.ac
3 # @brief an input file for autoconf tool
4 # (c) 2013 by Mega Limited, Auckland, New Zealand
5 #
6 # This file is part of the MEGAcmd.
7 #
8 # MEGAcmd is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 #
12 # @copyright Simplified (2-clause) BSD License.
13 #
14 # You should have received a copy of the license along with this
15 # program.
16##
17
18#                                               -*- Autoconf -*-
19# Process this file with autoconf to produce a configure script.
20
21m4_include([sdk/m4/ax_check_compile_flag.m4])
22m4_include([sdk/m4/ax_check_link_flag.m4])
23m4_include([sdk/m4/ax_cxx_compile_stdcxx.m4])
24
25m4_define([megacmd_major_version], [$(cat src/megacmdversion.h | grep "define MEGACMD_MAJOR" | cut -d" " -f 3)])
26m4_define([megacmd_minor_version], [$(cat src/megacmdversion.h | grep "define MEGACMD_MINOR" | cut -d" " -f 3)])
27m4_define([megacmd_micro_version], [$(cat src/megacmdversion.h | grep "define MEGACMD_MICRO" | cut -d" " -f 3)])
28
29m4_define([megacmd_version],
30          [megacmd_major_version.megacmd_minor_version.megacmd_micro_version])
31
32
33# libtool interface versioning
34m4_define([megacmd_lt_revision], [0])
35m4_define([megacmd_lt_current], [$(grep define <src/megacmdversion.h | awk '{print @S|@3}' | awk 'BEGIN { RS = "" ; FS = "\n" } {printf @S|@1*10000+@S|@2*100+@S|@3}')])
36m4_define([megacmd_lt_age], [0])
37
38AC_INIT([MEGAcmd], m4_esyscmd([ grep define < src/megacmdversion.h | awk '{print $3}' | awk 'BEGIN { RS = "" ; FS = "\n" } {printf $1"."$2"."$3}']), [https://github.com/meganz/megacmd])
39
40AM_INIT_AUTOMAKE([1.11 foreign silent-rules])
41
42AC_PROG_CC
43AC_CONFIG_FILES(Makefile)
44AC_OUTPUT
45
46AC_CONFIG_SUBDIRS(sdk)
47
48
49# Define _GNU_SOURCE
50# AC_GNU_SOURCE
51AC_USE_SYSTEM_EXTENSIONS
52
53AC_CONFIG_HEADERS([src/megacmdconfig.h]) #this avoids inline definitions of stuff already defined in sdk's config.h (e.g: -DPACKAGE_NAME)
54
55LT_INIT([shared disable-static win32-dll])
56#AC_CONFIG_MACRO_DIR([m4])
57
58# enable silent build
59m4_ifndef([AM_SILENT_RULES], [m4_define([AM_SILENT_RULES],[])])
60AM_SILENT_RULES([yes])
61
62LT_CURRENT=megacmd_lt_current
63LT_REVISION=megacmd_lt_revision
64LT_AGE=megacmd_lt_age
65
66AC_SUBST(LT_CURRENT)
67AC_SUBST(LT_REVISION)
68AC_SUBST(LT_AGE)
69
70# Checks for typedefs, structures, and compiler characteristics.
71AC_TYPE_SIZE_T
72AC_TYPE_UINT8_T
73AC_TYPE_UINT16_T
74AC_TYPE_UINT32_T
75AC_TYPE_UINT64_T
76AC_TYPE_PID_T
77AC_TYPE_OFF_T
78
79AH_VERBATIM([__STDC_FORMAT_MACROS],
80            [/* C99 says: define this to get the PRI... macros from stdint.h */
81#ifndef __STDC_FORMAT_MACROS
82# define __STDC_FORMAT_MACROS 1
83#endif])
84
85AC_CHECK_TYPES([ssize_t])
86
87AC_CANONICAL_HOST
88
89# Check programs
90AC_PROG_OBJCXX
91AC_PROG_CXX
92if test "$CXX" = no || test "$CXX:$GXX" = "g++:"; then
93  AC_MSG_ERROR([C++ compiler not found !])
94fi
95
96AC_PROG_LIBTOOL
97AM_SANITY_CHECK
98
99# Check for cppcheck
100AC_CHECK_PROG(HAVE_CPPCHECK, cppcheck, yes)
101AM_CONDITIONAL(CPPCHECK, test -n "$HAVE_CPPCHECK")
102
103# set C++
104AC_LANG_CPLUSPLUS
105
106# Check for C++11 standard
107AX_CXX_COMPILE_STDCXX([11], [noext], [mandatory], [default])
108
109# Check headers
110AC_STDC_HEADERS
111AC_HEADER_STDC
112AC_HEADER_STDBOOL
113AC_HEADER_DIRENT
114
115
116# Add 64-bits file support on some hosts
117AC_SYS_LARGEFILE
118# use new interfaces
119AC_FUNC_FSEEKO
120AC_TYPE_OFF_T
121
122# Checks for typedefs, structures, and compiler characteristics.
123AC_CHECK_SIZEOF([uint64_t])
124AC_STRUCT_TM
125AC_TYPE_OFF_T
126AC_TYPE_SIZE_T
127
128AC_CHECK_HEADERS([arpa/inet.h netdb.h netinet/in.h stddef.h stdint.h stdlib.h sys/socket.h sys/timeb.h htonl glob.h malloc.h sys/malloc.h malloc/malloc.h])
129
130# Debug
131AC_ARG_ENABLE(debug,
132    AS_HELP_STRING([--enable-debug], [enable support for running in debug mode]),
133    [], [enable_debug=no])
134
135# Enable debug flags / build
136if test "x$enable_debug" = "xyes" ; then
137    AM_CXXFLAGS="-Wall -g -ggdb3 -O0"
138    CXXFLAGS="-Wall -g -ggdb3 -O0"
139    AC_SUBST([AM_CXXFLAGS])
140    AC_DEFINE(DEBUG, 1, [Define to enable debug logging])
141else
142    CXXFLAGS="$CXXFLAGS -DNDEBUG=1"
143fi
144
145# Check if we can use -fPIC flag
146AX_CHECK_COMPILE_FLAG([-fPIC], [
147  AX_CHECK_LINK_FLAG([-fPIC],
148    [CXXFLAGS="$CXXFLAGS -fPIC"]
149  )
150])
151
152# Enable backups
153CXXFLAGS="$CXXFLAGS -DENABLE_BACKUPS=1"
154
155AM_CONDITIONAL(ENABLE_STATIC, test x$enable_static = xyes)
156AC_SUBST(ENABLE_STATIC)
157
158
159# TARGET OS:
160WIN32=no
161DARWIN=no
162ANDROID=no
163LINUX=no
164AC_MSG_CHECKING([if building for Win32 platform])
165case $host in
166  *-*-cygwin*)
167    LIBS_EXTRA="-luser32 -lkernel32"
168    WIN32=yes
169    ;;
170  *-*-mingw*)
171    LIBS_EXTRA="-lws2_32 -lcrypt32 -lole32 -lwinmm -lshlwapi -lshell32"
172    CXXFLAGS="$CXXFLAGS -DUNICODE -DWINVER=0x0501 -DHAVE_STRUCT_TIMESPEC"
173    WIN32=yes
174    ;;
175  *-apple-darwin*)
176    AC_DEFINE([_XOPEN_SOURCE], [500], [Define _XOPEN_SOURCE])
177    AC_DEFINE([_DARWIN_C_SOURCE], [1], [Define _DARWIN_C_SOURCE])
178    DARWIN=yes
179    ;;
180  *-android*)
181    ANDROID=yes
182    ;;
183  *-linux*)
184    LINUX=yes
185    ;;
186  *)
187    LIBS_EXTRA=""
188    ;;
189esac
190AC_MSG_RESULT([${WIN32}])
191AM_CONDITIONAL([WIN32], [test "${WIN32}" = "yes"])
192AM_CONDITIONAL([DARWIN], [test "${DARWIN}" = "yes"])
193AM_CONDITIONAL([ANDROID], [test "${ANDROID}" = "yes"])
194AM_CONDITIONAL([LINUX], [test "${LINUX}" = "yes"])
195
196SAVE_LDFLAGS=$LDFLAGS
197SAVE_CXXFLAGS=$CXXFLAGS
198SAVE_CPPFLAGS=$CPPFLAGS
199
200#
201# PCRE
202#
203#with megacmd 0.9.4 & sdk 3.2.0, libpcrecpp is no linked in megasdk even when compiled with --with-pcre. Hence we need to check here
204#Plus, contrary to what the sdk will do, if non-specified and available we default it to true
205
206#libpcre
207AC_MSG_CHECKING(for libpcrecpp)
208pcre=false
209AC_ARG_WITH(pcre,
210  AS_HELP_STRING(--with-pcre=PATH, base of libpcrecpp installation),
211  [
212   case $with_pcre in
213   no)
214    pcre=false
215     ;;
216   yes)
217    AC_CHECK_HEADERS([pcrecpp.h],, [
218        AC_MSG_ERROR([pcrecpp.h header not found or not usable])
219    ])
220    AC_CHECK_LIB(pcrecpp, [main], [PCRE_LIBS="-lpcrecpp"],[
221            AC_MSG_ERROR([Could not find libpcrecpp])
222    ])
223    AC_CHECK_LIB(pcre, [pcre_compile], [PCRE_LIBS="$PCRE_LIBS -lpcre"],[
224            AC_MSG_ERROR([Could not find libpcre])
225    ])
226    pcre=true
227     ;;
228   *)
229
230    # determine if library is installed
231    if test -d "$with_pcre/lib64"; then
232        LDFLAGS="-L$with_pcre/lib64 $LDFLAGS"
233        CXXFLAGS="-I$with_pcre/include $CXXFLAGS"
234        CPPFLAGS="-I$with_pcre/include $CPPFLAGS"
235
236        AC_CHECK_HEADERS(pcrecpp.h,
237         PCRE_CXXFLAGS="-I$with_pcre/include"
238         PCRE_CPPFLAGS="-I$with_pcre/include"
239         PCRE_LDFLAGS="-L$with_pcre/lib64",
240         AC_MSG_ERROR([pcrecpp.h header not found or not usable])
241         )
242    else
243        LDFLAGS="-L$with_pcre/lib32 $LDFLAGS"
244        CXXFLAGS="-I$with_pcre/include $CXXFLAGS"
245        CPPFLAGS="-I$with_pcre/include $CPPFLAGS"
246
247        AC_CHECK_HEADERS(pcrecpp.h,
248         PCRE_CXXFLAGS="-I$with_pcre/include"
249         PCRE_CPPFLAGS="-I$with_pcre/include"
250         PCRE_LDFLAGS="-L$with_pcre/lib32",
251         AC_MSG_ERROR([pcrecpp.h header not found or not usable])
252         )
253    fi
254
255    AC_CHECK_LIB(pcrecpp, [main], [PCRE_LIBS="-lpcrecpp"],[
256            AC_MSG_ERROR([Could not find libpcrecpp])
257    ])
258    AC_CHECK_LIB(pcre, [pcre_compile], [PCRE_LIBS="$PCRE_LIBS -lpcre"],[
259            AC_MSG_ERROR([Could not find libpcre])
260    ])
261
262    pcre=true
263
264    #restore
265    LDFLAGS=$SAVE_LDFLAGS
266    CXXFLAGS=$SAVE_CXXFLAGS
267    CPPFLAGS=$SAVE_CPPFLAGS
268    ;;
269   esac
270  ],
271  [AC_MSG_RESULT([--with-pcre not specified])
272      AC_CHECK_HEADERS([pcrecpp.h], [pcre=true], [])
273    AC_CHECK_LIB(pcrecpp, [main], [PCRE_LIBS="-lpcrecpp"],[])
274    AC_CHECK_LIB(pcre, [pcre_compile], [PCRE_LIBS="$PCRE_LIBS -lpcre"],[])
275  ])
276
277if test "x$pcre" = "xtrue" ; then
278    AC_DEFINE(USE_PCRE, [1], [Define to use libpcre])
279fi
280AM_CONDITIONAL([USE_PCRE], [test "x$pcre" = "xtrue"])
281
282AC_SUBST(PCRE_CXXFLAGS)
283AC_SUBST(PCRE_CPPFLAGS)
284AC_SUBST(PCRE_LDFLAGS)
285AC_SUBST(PCRE_LIBS)
286
287
288#################################################
289###      CLIENTS SPECIFIC DEPENDENCIES        ###
290#################################################
291#termcap
292AC_MSG_CHECKING(for termcap)
293AC_ARG_WITH(termcap,
294  AS_HELP_STRING(--with-termcap=PATH, base of termcap installation),
295  [AC_MSG_RESULT($with_termcap)
296   case $with_termcap in
297   no)
298     ;;
299   yes)
300    AC_CHECK_LIB([termcap], [tputs], [TERMCAP_LIBS="-ltermcap"],
301        [AC_MSG_NOTICE([termcap library not found or not usable.])]
302    )
303    ;;
304   *)
305
306    # determine if library is installed
307    if test -d "$with_termcap/lib"; then
308        LDFLAGS="-L$with_termcap/lib $LDFLAGS"
309        CXXFLAGS="-I$with_termcap/include $CXXFLAGS"
310        CPPFLAGS="-I$with_termcap/include $CPPFLAGS"
311
312        AC_CHECK_HEADERS([termcap.h],[
313            TERMCAP_LDFLAGS="-L$with_termcap/lib"
314            TERMCAP_CXXFLAGS="-I$with_termcap/include"
315            TERMCAP_CPPFLAGS="-I$with_termcap/include"],
316            AC_MSG_NOTICE([termcap.h header not found or not usable])
317        )
318    else
319        LDFLAGS="-L$with_termcap $LDFLAGS"
320        CXXFLAGS="-I$with_termcap $CXXFLAGS"
321        CPPFLAGS="-I$with_termcap $CPPFLAGS"
322
323        AC_CHECK_HEADERS([termcap.h],[
324            TERMCAP_LDFLAGS="-L$with_termcap"
325            TERMCAP_CXXFLAGS="-I$with_termcap"
326            TERMCAP_CPPFLAGS="-I$with_termcap"],
327            AC_MSG_NOTICE([termcap.h header not found or not usable])
328        )
329    fi
330
331    AC_CHECK_LIB([termcap], [tputs], [TERMCAP_LIBS="-ltermcap"], [
332        AC_MSG_NOTICE([termcap library not found or not usable.])])
333
334    #restore
335    LDFLAGS=$SAVE_LDFLAGS
336    CXXFLAGS=$SAVE_CXXFLAGS
337    CPPFLAGS=$SAVE_CPPFLAGS
338
339    ;;
340   esac
341  ],
342  [AC_MSG_RESULT([--with-termcap not specified])
343    AC_CHECK_LIB([termcap], [tputs], [TERMCAP_LIBS="-ltermcap"],
344        [AC_MSG_NOTICE([termcap library not found or not usable.])]
345    )
346  ]
347)
348AC_SUBST(TERMCAP_LDFLAGS)
349AC_SUBST(TERMCAP_LIBS)
350AC_SUBST(TERMCAP_CXXFLAGS)
351AC_SUBST(TERMCAP_CPPFLAGS)
352
353# ReadLine
354AC_MSG_CHECKING(for Readline)
355AC_ARG_WITH(readline,
356  AS_HELP_STRING(--with-readline=PATH, base of Readline installation),
357  [AC_MSG_RESULT($with_readline)
358   case $with_readline in
359   no)
360    AC_MSG_ERROR([readline library is required for the sample client.])
361     ;;
362   yes)
363    AC_CHECK_HEADERS([readline/readline.h],, [
364        AC_MSG_ERROR([readline/readline.h header not found or not usable])
365    ])
366    # readline requires termcap (or ncurses)
367    SAVE_LIBS=$LIBS
368    LIBS="$TERMCAP_LIBS $LIBS"
369    AC_CHECK_LIB([readline], [rl_save_prompt], [RL_LIBS="-lreadline"], [
370        AC_MSG_ERROR([readline library is required for the sample client.])],
371        [$LIBS])
372    LIBS=$SAVE_LIBS
373     ;;
374   *)
375
376    # determine if library is installed
377    if test -d "$with_readline/lib"; then
378        LDFLAGS="-L$with_readline/lib $LDFLAGS"
379        CXXFLAGS="-I$with_readline/include $CXXFLAGS"
380        CPPFLAGS="-I$with_readline/include $CPPFLAGS"
381        AC_CHECK_HEADERS([readline/readline.h], [
382         RL_LDFLAGS="-L$with_readline/lib "
383         RL_CXXFLAGS="-I$with_readline/include "],
384         AC_MSG_ERROR([readline/readline.h header not found or not usable])
385        )
386    else
387        LDFLAGS="-L$with_readline $LDFLAGS"
388        CXXFLAGS="-I$with_readline $CXXFLAGS"
389        CPPFLAGS="-I$with_readline $CPPFLAGS"
390        AC_CHECK_HEADERS([readline/readline.h], [
391         RL_LDFLAGS="-L$with_readline"
392         RL_CXXFLAGS="-I$with_readline"],
393         AC_MSG_ERROR([readline/readline.h header not found or not usable])
394        )
395    fi
396
397    # readline requires termcap (or ncurses)
398    SAVE_LIBS=$LIBS
399    LIBS="$TERMCAP_LIBS $LIBS"
400    AC_CHECK_LIB([readline], [rl_save_prompt], [RL_LIBS="-lreadline"], [
401        AC_MSG_ERROR([readline library is required for the sample client.])])
402    LIBS=$SAVE_LIBS
403
404    #restore
405    LDFLAGS=$SAVE_LDFLAGS
406    CXXFLAGS=$SAVE_CXXFLAGS
407    CPPFLAGS=$SAVE_CPPFLAGS
408    ;;
409   esac
410  ],
411  [AC_MSG_RESULT([--with-readline not specified])
412    AC_CHECK_HEADERS([readline/readline.h],, [
413        AC_MSG_ERROR([readline/readline.h header not found or not usable])
414    ])
415    # readline requires termcap (or ncurses)
416    SAVE_LIBS=$LIBS
417    LIBS="$TERMCAP_LIBS $LIBS"
418    AC_CHECK_LIB([readline], [rl_save_prompt], [RL_LIBS="-lreadline"], [
419        AC_MSG_ERROR([readline library is required for the sample client.])])
420    LIBS=$SAVE_LIBS
421  ]
422  )
423AC_SUBST(RL_LDFLAGS)
424AC_SUBST(RL_LIBS)
425AC_SUBST(RL_CXXFLAGS)
426
427#PTHREAD
428
429# check for Pthreads
430AC_ARG_ENABLE(posix-threads,
431    AS_HELP_STRING(--disable-posix-threads, disable POSIX threads support),
432    [enable_posix_threads=no],
433    [enable_posix_threads=yes])
434if test x$enable_posix_threads != xno; then
435    AC_CHECK_LIB(pthread, pthread_create, [HAVE_PTHREAD=yes], [HAVE_PTHREAD=no])
436else
437    HAVE_PTHREAD=no
438fi
439if test "$HAVE_PTHREAD" = "yes"; then
440    AC_DEFINE(USE_PTHREAD, 1, [Defined if pthreads are available])
441        if test "$HAVE_PTHREAD" = "yes"; then
442        SAVE_LDFLAGS="-pthread $SAVE_LDFLAGS"
443        LDFLAGS="-pthread $LDFLAGS"
444    fi
445fi
446AM_CONDITIONAL([HAVE_PTHREAD], [test "${HAVE_PTHREAD}" = "yes"])
447
448#This will simulate compilation of sdk and extract the includes defined for it. Thus we can
449# take care of the configuration done for the subproject withtout having to recheck here
450AC_OUTPUT
451LMEGAINC=$(cd sdk; for i in $(make -n 2>/dev/null | grep "\-I" | grep megaapi ); do echo $i; done | grep "\-I" | sort | uniq | awk '{printf $0" "}')
452AC_SUBST(LMEGAINC)
453
454# Output
455#AC_CONFIG_FILES([
456# Makefile
457#])
458AC_OUTPUT
459#~ AC_OUTPUT([sdk/Makefile])
460
461
462AC_MSG_NOTICE([MEGAcmd configuration:
463
464  MEGACMD version:      megacmd_version
465  Install prefix:   $prefix
466  Compiler:         $CXX
467  CXXFLAGS:         $CXXFLAGS
468  CPPFLAGS:         $CPPFLAGS
469  LDFLAGS:          $LDFLAGS
470  debug:            $enable_debug
471  static:           $enable_static
472  posix threads:    $enable_posix_threads
473
474  Readline:         $RL_CXXFLAGS $RL_LDFLAGS $RL_LIBS
475  Termcap:          $TERMCAP_CXXFLAGS $TERMCAP_LDFLAGS $TERMCAP_LIBS
476  PCRE:             $PCRE_CXXFLAGS $PCRE_LDFLAGS $PCRE_LIBS
477  SDK includes:     $LMEGAINC
478])
479