/ auto.def
auto.def
  1  # vim:se syn=tcl:
  2  #
  3  
  4  use cc cc-lib cc-shared pkg-config
  5  
  6  set maj_ver 2
  7  set med_ver 6
  8  set min_ver 99
  9  set dev_ver 6
 10  define PKG_API [expr {$maj_ver * 1000000 + $med_ver * 1000 + $min_ver}]
 11  define VERSION $maj_ver.$med_ver.$min_ver[expr {$dev_ver ? ".$dev_ver" : ""}]
 12  
 13  # Add any user options here
 14  options {
 15  	pkgconfigdir:WHERE => "path to the directory where to install pc files"
 16  	with-ldns => "add support for libldns"
 17  	with-libarchive.pc => "build with libarchive via pkg-config"
 18  	with-coverage => "build with llvm coverage support"
 19  	with-asan => "build with Clang AddressSanitizer (non-prod use)"
 20  	with-lsan => "build with Clang LeakSanitizer (non-prod use)"
 21  	with-ubsan => "build with Clang UndefinedBehaviorSanitizer (non-prod use)"
 22  	with-tsan => "build with Clang ThreadSanitizer (non-prod use)"
 23  	with-openssl.pc => "build with openssl via pkg-config"
 24  	default-format:txz => "Default compression format: tzst (default), txz, tbz, tar"
 25  }
 26  
 27  if {[opt-str pkgconfigdir dir]} {
 28  	define pkgconfigdir $dir
 29  } else {
 30  	if {[string match *-freebsd* [get-define host]]} {
 31  		define pkgconfigdir [get-define prefix]/libdata/pkgconfig
 32  	} else {
 33  		define pkgconfigdir [get-define prefix]/lib/pkgconfig
 34  	}
 35  }
 36  
 37  # default compression format
 38  
 39  if {[opt-str default-format fmt]} {
 40  	switch $fmt \
 41  	    "tzst" { set comp TZS } \
 42  	    "txz" { set comp TXZ } \
 43  	    "tbz" { set comp TBZ } \
 44  	    "tgz" { set comp TGZ } \
 45  	    "tar" { set comp TAR } \
 46  	    "default" { user-error "Invalid format $fmt" }
 47  	define DEFAULT_COMPRESSION $comp
 48  } else {
 49  	define DEFAULT_COMPRESSION TZS
 50  }
 51  
 52  cc-check-tools ar ranlib strip
 53  
 54  define EXTRA_LIBS ""
 55  define LIBVERSION 4
 56  define LIBSOEXT [format [get-define SH_SOEXTVER] [get-define LIBVERSION]]
 57  
 58  define GITHASH ""
 59  if {[cc-check-progs git] && [file exists .git] &&
 60      ![catch {exec git rev-parse --short HEAD} gitrev] &&
 61      ![catch {exec git diff-index -m --name-only HEAD} gitdirty]} {
 62  	define GITHASH -$gitrev[expr {$gitdirty eq {} ? {} : {-dirty}}]
 63  }
 64  
 65  if {[string match *-linux* [get-define host]]} {
 66  	cc-with { -libs { -lbsd }} {
 67  		if {[cc-check-functions getprogname]} {
 68  			define-append EXTRA_LIBS -lbsd
 69  			define-feature libbsd
 70  		} else {
 71  			user-error "Unable to find libbsd"
 72  		}
 73  	}
 74  } else {
 75  	define-feature static
 76  }
 77  
 78  if {![opt-bool with-libarchive.pc]} {
 79  	cc-with { -libs { -larchive }} {
 80  		if {![cc-check-functions archive_read_open]} {
 81  			user-error "Unable to find libarchive"
 82  		}
 83  	}
 84  }
 85  
 86  cc-with { -libs { -lz }} {
 87  	if {![cc-check-functions zlibVersion]} {
 88  		user-error "Unable to find zlib"
 89  	}
 90  }
 91  
 92  cc-with { -libs { -lbz2 }} {
 93  	if {![cc-check-functions BZ2_bzReadOpen]} {
 94  		user-error "Unable to find bzip2"
 95  	}
 96  }
 97  
 98  cc-with { -libs { -llzma }} {
 99  	if {![cc-check-functions lzma_version_string]} {
100  		user-error "Unable to find liblzma"
101  	}
102  }
103  if {[string match *-freebsd* [get-define host]]} {
104  	define pkgos_freebsd
105  	cc-with { -libs { -lmd }} {
106  		if {![cc-check-functions SHA256_Data]} {
107  			user-error "Unable to find libmd"
108  		} else {
109  			define-feature libmd
110  		}
111  	}
112  }
113  
114  cc-with { -libs { -larchive }} {
115  	cc-check-functions archive_write_add_filter_zstd
116  }
117  
118  define ZSTDLIB ""
119  cc-with { -libs { -lprivatezstd }} {
120  	if {[cc-check-functions ZSTD_versionNumber]} {
121  		define ZSTDLIB "-lprivatezstd"
122  	}
123  }
124  
125  # Atomics
126  msg-checking "Checking for atomic builtins... "
127  if {[cctest -code {
128  	volatile unsigned long val = 1;
129  	__sync_synchronize();
130  	__sync_val_compare_and_swap(&val, 1, 0);
131  	__sync_add_and_fetch(&val, 1);
132  	__sync_sub_and_fetch(&val, 1);
133  	return 0;
134  	}
135  ]} {
136  	msg-result ok
137  	define-feature atomic_builtins
138  } else {
139  	msg-result no
140  	define-feature atomic_builtins 0
141  }
142  
143  msg-checking "Checking for /proc/self/fd support... "
144  if {[file exists /proc/self/fd]} {
145  	msg-result ok
146  	define-feature proc-pid
147  } else {
148  	msg-result no
149  	define-feature proc-pid 0
150  }
151  
152  foreach fct [list memmove usleep pread pwrite] {
153  	if {![cc-check-functions $fct]} {
154  		user-error "Cannot find $fct"
155  	}
156  }
157  
158  cc-with { -includes netinet/in.h } {
159  	cc-check-members "struct sockaddr_in.sin_len"
160  }
161  
162  cc-with { -includes sys/stat.h } {
163  	cc-check-members "struct stat.st_mtim" "struct stat.st_flags"
164  }
165  
166  # check for sqlite
167  cc-check-functions strchrnul
168  
169  # check for pkg itself
170  cc-check-functions arc4random arc4random_stir chflagsat \
171  	closefrom fopencookie funopen getprogname \
172  	strtofflags strtonum utimensat __res_setservers \
173  	fflagstostr strchrnul copy_file_range
174  
175  cc-with { -includes { libutil.h }} {
176  	if {[cc-check-decls humanize_number]} {
177  	}
178  }
179  
180  if {![cc-check-functions fts_open]} {
181  	cc-with { -libs { -lfts }} {
182  		if {[cc-check-functions fts_open]} {
183  			define-feature LIBFTS
184  		}
185  	}
186  }
187  
188  if {[get-define HAVE_DECL_HUMANIZE_NUMBER] eq 1} {
189  	cc-with { -libs { -lutil }} {
190  		if {[cc-check-functions humanize_number]} {
191  			define-feature LIBUTIL
192  		}
193  	}
194  }
195  
196  if {![cc-check-functions dlclose]} {
197  	cc-with { -libs { -ldl }} {
198  		if {[cc-check-functions dlclose]} {
199  			define-feature LIBDL
200  		}
201  	}
202  }
203  
204  #endian stuff
205  foreach fct [list be16dec be16enc be32dec be32enc be64dec be64enc le16dec le16enc le32dec le32enc le64dec le64enc] {
206  	foreach header [list endian.h sys/endian.h machine/endian.h] {
207  		if {[cc-check-includes $header]} {
208  			cc-with [list -includes $header] {
209  				if {[cc-check-decls $fct]} {
210  					break
211  				}
212  			}
213  		}
214  	}
215  }
216  
217  if {[string match *-darwin* [get-define host]]} {
218  	define pkgos_darwin
219  	define waflags ""
220  	define nowaflags ""
221  	define libelf-internal
222  } else {
223  # libelf
224  	define waflags "-Wl,-whole-archive"
225  	define nowaflags "-Wl,-no-whole-archive"
226  
227  	if {[cc-check-includes gelf.h libelf.h]} {
228  		cc-with { -libs { -lelf } } {
229  			if {[cc-check-functions gelf_getehdr]} {
230  				define-feature libelf
231  				define libelf-external
232  				define-append EXTRA_LIBS -lelf
233  				cc-check-types Elf_Note
234  			} else {
235  				define libelf-internal
236  				define-feature libelf 0
237  			}
238  		}
239  	} else {
240  		define-feature libelf 0
241  		define libelf-internal
242  	}
243  }
244  
245  cc-with { -libs { -ljail} } {
246  	if {[cc-check-functions jail_getid]} {
247  		define-feature libjail
248  		define-append EXTRA_LIBS -ljail
249  	}
250  }
251  
252  if {[opt-bool with-asan]} {
253  	define-append ASAN_CFLAGS -O0 -g -fsanitize=address
254  	define-append ASAN_LDFLAGS -fsanitize=address
255  	if {[string match *-linux* [get-define host]]} {
256  		# ASAN depends on libdl for dlsym when building with GCC
257  		define-append ASAN_LDFLAGS -ldl
258  	}
259  	define asan 1
260  	undefine HAVE_STATIC
261  }
262  if {[opt-bool with-lsan]} {
263  	define-append LSAN_CFLAGS -O0 -g -fsanitize=leak
264  	define-append LSAN_LDFLAGS -fsanitize=leak
265  	if {[string match *-linux* [get-define host]]} {
266  		# LSAN depends on libdl for dlsym when building with GCC
267  		define-append LSAN_LDFLAGS -ldl
268  	}
269  	define lsan 1
270  }
271  if {[opt-bool with-ubsan]} {
272  	# Avoid alignment error when building with UBSan inside _mum_hash_avx2
273  	define-append UBSAN_CFLAGS -D_MUM_UNALIGNED_ACCESS=0
274  	define-append UBSAN_CFLAGS -O0 -g -fsanitize=undefined
275  	define-append UBSAN_LDFLAGS -fsanitize=undefined
276  	define ubsan 1
277  }
278  if {[opt-bool with-tsan]} {
279  	define-append TSAN_CFLAGS -O1 -g -fsanitize=thread
280  	define-append TSAN_LDFLAGS -fsanitize=thread
281  	define tsan 1
282  }
283  if {[opt-bool with-coverage]} {
284  	define-append COVERAGE_CFLAGS -O0 -g -fprofile-arcs -ftest-coverage -fprofile-instr-generate -fcoverage-mapping -DCOVERAGE=1
285  	define-append COVERAGE_LDFLAGS -fprofile-instr-generate
286  	define coverage 1
287  }
288  
289  if {[pkg-config-init 0]} {
290  # atf
291  	if {[pkg-config atf-c] && [cc-check-progs kyua]} {
292  		define TESTS
293  		puts "test suite will be built"
294  	} else {
295  		puts "Skipping test suite"
296  	}
297  	if {[opt-bool with-ldns]} {
298  		if {![pkg-config libldns]} {
299  			user-error "Unable to find libldns"
300  		} else {
301  			define PKG_LIBDNS_LIBS_STATIC [exec pkg-config --static --libs-only-l libldns]
302  		}
303  	}
304  	if {[opt-bool with-libarchive.pc]} {
305  		if {![pkg-config libarchive]} {
306  			user-error "Unable to find libarchive"
307  		} else {
308  			define PKG_LIBARCHIVE_LIBS_STATIC [exec pkg-config --static --libs-only-l libarchive]
309  			cc-with { -libs { -larchive }} {
310  				cc-check-functions archive_write_add_filter_zstd
311  			}
312  		}
313  		user-notice LIBARCHIVE_CFLAGS=[get-define PKG_LIBARCHIVE_CFLAGS]
314  		user-notice LIBARCHIVE_LDLAGS=[get-define PKG_LIBARCHIVE_LDFLAGS]
315  		user-notice LIBARCHIVE_LIBS=[get-define PKG_LIBARCHIVE_LIBS]
316  	}
317  
318  	if {[opt-bool with-openssl.pc]} {
319  		if {![pkg-config openssl]} {
320  			user-error "Unable to find openssl"
321  		}
322  		user-notice OPENSSL_CFLAGS=[get-define PKG_OPENSSL_CFLAGS]
323  		user-notice OPENSSL_LDLAGS=[get-define PKG_OPENSSL_LDFLAGS]
324  		user-notice OPENSSL_LIBS=[get-define PKG_OPENSSL_LIBS]
325  	}
326  }
327  
328  foreach repo [list binary] {
329  	define-append REPOS $repo
330  	define-append REPOS_LDFLAGS -L\$(top_builddir)/libpkg/repo/${repo} -lrepo-${repo}_pic
331  	define-append REPOS_STATIC_LIBS \$(top_builddir)/libpkg/repo/${repo}/librepo-${repo}.a
332  }
333  
334  # ccache
335  if {[get-define CCACHE] ne {none}} {
336    define CC "[get-define CCACHE] [get-define CC]"
337  }
338  
339  make-config-header pkg_config.h -bare { DEFAULT_* }
340  make-config-header external/sqlite/sqlite_generated.h
341  foreach in [list mk/defs.mk.in libpkg/pkg.h.in libpkg/pkg.pc.in \
342  	tests/frontend/test_environment.sh.in ] {
343  	make-template $in
344  	define-append CONF_GEN_FILES $in
345  }
346  make-template Makefile.autosetup Makefile
347  define-append CONF_GEN_FILES Makefile.autosetup
348  
349  foreach dir [list external/blake2 external/picosat \
350  	external/linenoise external/sqlite external/libfetch \
351  	external compat libpkg libpkg/repo libpkg/repo/binary src \
352  	external/libucl external/libelf tests docs \
353  	external/liblua external/yxml scripts external/libder \
354  	external/libecc] {
355  	make-template $dir/Makefile.autosetup $dir/Makefile
356  	define-append CONF_GEN_FILES $dir/Makefile.autosetup
357  }
358  
359  set out [open "configure_call" w+]
360  puts $out "$autosetup(srcdir)/configure $argv"
361  close $out
362  
363  file mkdir external/libucl/src scripts/periodic scripts/completion tests/lib tests/frontend